SceneWindow.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using BansheeEngine;
  8. namespace BansheeEditor
  9. {
  10. internal sealed class SceneWindow : EditorWindow
  11. {
  12. private const int HeaderHeight = 20;
  13. private const float DefaultPlacementDepth = 5.0f;
  14. private Camera camera;
  15. private SceneCamera cameraController;
  16. private RenderTexture2D renderTexture;
  17. private GUILayoutY mainLayout;
  18. private GUIRenderTexture renderTextureGUI;
  19. private SceneViewHandler sceneViewHandler;
  20. private GUIToggle viewButton;
  21. private GUIToggle moveButton;
  22. private GUIToggle rotateButton;
  23. private GUIToggle scaleButton;
  24. private GUIToggle localCoordButton;
  25. private GUIToggle worldCoordButton;
  26. private GUIToggle pivotButton;
  27. private GUIToggle centerButton;
  28. private GUIToggle moveSnapButton;
  29. private GUIFloatField moveSnapInput;
  30. private GUIToggle rotateSnapButton;
  31. private GUIFloatField rotateSnapInput;
  32. private int editorSettingsHash = int.MaxValue;
  33. // Drag & drop
  34. private bool dragActive;
  35. private SceneObject draggedSO;
  36. public Camera GetCamera()
  37. {
  38. return camera;
  39. }
  40. internal SceneWindow()
  41. { }
  42. private void OnInitialize()
  43. {
  44. mainLayout = GUI.AddLayoutY();
  45. GUIToggleGroup handlesTG = new GUIToggleGroup();
  46. viewButton = new GUIToggle("V", handlesTG, EditorStyles.Button);
  47. moveButton = new GUIToggle("M", handlesTG, EditorStyles.Button);
  48. rotateButton = new GUIToggle("R", handlesTG, EditorStyles.Button);
  49. scaleButton = new GUIToggle("S", handlesTG, EditorStyles.Button);
  50. GUIToggleGroup coordModeTG = new GUIToggleGroup();
  51. localCoordButton = new GUIToggle("L", coordModeTG, EditorStyles.Button);
  52. worldCoordButton = new GUIToggle("W", coordModeTG, EditorStyles.Button);
  53. GUIToggleGroup pivotModeTG = new GUIToggleGroup();
  54. pivotButton = new GUIToggle("P", pivotModeTG, EditorStyles.Button);
  55. centerButton = new GUIToggle("C", pivotModeTG, EditorStyles.Button);
  56. moveSnapButton = new GUIToggle("MS", EditorStyles.Button);
  57. moveSnapInput = new GUIFloatField();
  58. rotateSnapButton = new GUIToggle("RS", EditorStyles.Button);
  59. rotateSnapInput = new GUIFloatField();
  60. viewButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.View);
  61. moveButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Move);
  62. rotateButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Rotate);
  63. scaleButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Scale);
  64. localCoordButton.OnClick += () => OnCoordinateModeButtonClicked(HandleCoordinateMode.Local);
  65. worldCoordButton.OnClick += () => OnCoordinateModeButtonClicked(HandleCoordinateMode.World);
  66. pivotButton.OnClick += () => OnPivotModeButtonClicked(HandlePivotMode.Pivot);
  67. centerButton.OnClick += () => OnPivotModeButtonClicked(HandlePivotMode.Center);
  68. moveSnapButton.OnToggled += (bool active) => OnMoveSnapToggled(active);
  69. moveSnapInput.OnChanged += (float value) => OnMoveSnapValueChanged(value);
  70. rotateSnapButton.OnToggled += (bool active) => OnRotateSnapToggled(active);
  71. rotateSnapInput.OnChanged += (float value) => OnRotateSnapValueChanged(value);
  72. GUILayout handlesLayout = mainLayout.AddLayoutX();
  73. handlesLayout.AddElement(viewButton);
  74. handlesLayout.AddElement(moveButton);
  75. handlesLayout.AddElement(rotateButton);
  76. handlesLayout.AddElement(scaleButton);
  77. handlesLayout.AddSpace(10);
  78. handlesLayout.AddElement(localCoordButton);
  79. handlesLayout.AddElement(worldCoordButton);
  80. handlesLayout.AddSpace(10);
  81. handlesLayout.AddElement(pivotButton);
  82. handlesLayout.AddElement(centerButton);
  83. handlesLayout.AddFlexibleSpace();
  84. handlesLayout.AddElement(moveSnapButton);
  85. handlesLayout.AddElement(moveSnapInput);
  86. handlesLayout.AddSpace(10);
  87. handlesLayout.AddElement(rotateSnapButton);
  88. handlesLayout.AddElement(rotateSnapInput);
  89. UpdateRenderTexture(Width, Height - HeaderHeight);
  90. }
  91. private void OnDestroy()
  92. {
  93. if (camera != null)
  94. {
  95. camera.SceneObject.Destroy();
  96. }
  97. }
  98. private bool ScreenToScenePos(Vector2I screenPos, out Vector2I scenePos)
  99. {
  100. scenePos = screenPos;
  101. Vector2I windowPos = ScreenToWindowPos(screenPos);
  102. Rect2I bounds = GUILayoutUtility.CalculateBounds(renderTextureGUI);
  103. if (bounds.Contains(windowPos))
  104. {
  105. scenePos.x = windowPos.x - bounds.x;
  106. scenePos.y = windowPos.y - bounds.y;
  107. return true;
  108. }
  109. return false;
  110. }
  111. private void OnEditorUpdate()
  112. {
  113. // Refresh GUI buttons if needed (in case someones changes the values from script)
  114. if (editorSettingsHash != EditorSettings.Hash)
  115. {
  116. UpdateButtonStates();
  117. editorSettingsHash = EditorSettings.Hash;
  118. }
  119. // Update scene view handles and selection
  120. sceneViewHandler.Update();
  121. bool handleActive = false;
  122. if (Input.IsPointerButtonUp(PointerButton.Left))
  123. {
  124. if (sceneViewHandler.IsHandleActive())
  125. {
  126. sceneViewHandler.ClearHandleSelection();
  127. handleActive = true;
  128. }
  129. }
  130. Vector2I scenePos;
  131. bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);
  132. bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;
  133. draggedOver &= inBounds && DragDrop.Type == DragDropType.Resource;
  134. if (draggedOver)
  135. {
  136. if (DragDrop.DropInProgress)
  137. {
  138. dragActive = false;
  139. draggedSO = null;
  140. }
  141. else
  142. {
  143. if (!dragActive)
  144. {
  145. dragActive = true;
  146. ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;
  147. string draggedMeshPath = "";
  148. string[] draggedPaths = dragData.Paths;
  149. for (int i = 0; i < draggedPaths.Length; i++)
  150. {
  151. LibraryEntry entry = ProjectLibrary.GetEntry(draggedPaths[i]);
  152. if (entry != null && entry.Type == LibraryEntryType.File)
  153. {
  154. FileEntry fileEntry = (FileEntry) entry;
  155. if (fileEntry.ResType == ResourceType.Mesh)
  156. {
  157. draggedMeshPath = draggedPaths[i];
  158. break;
  159. }
  160. }
  161. }
  162. if (!string.IsNullOrEmpty(draggedMeshPath))
  163. {
  164. string meshName = Path.GetFileName(draggedMeshPath);
  165. draggedSO = new SceneObject(meshName);
  166. Mesh mesh = ProjectLibrary.Load<Mesh>(draggedMeshPath);
  167. Material material = new Material(Builtin.DiffuseShader);
  168. Renderable renderable = draggedSO.AddComponent<Renderable>();
  169. renderable.Mesh = mesh;
  170. renderable.SetMaterial(material);
  171. }
  172. }
  173. if (draggedSO != null)
  174. {
  175. Ray worldRay = camera.ScreenToWorldRay(scenePos);
  176. draggedSO.Position = worldRay*DefaultPlacementDepth;
  177. }
  178. }
  179. return;
  180. }
  181. else
  182. {
  183. if (dragActive)
  184. {
  185. dragActive = false;
  186. if (draggedSO != null)
  187. {
  188. draggedSO.Destroy();
  189. draggedSO = null;
  190. }
  191. }
  192. }
  193. if (!HasFocus)
  194. return;
  195. if (inBounds)
  196. {
  197. if (Input.IsPointerButtonDown(PointerButton.Left))
  198. {
  199. sceneViewHandler.TrySelectHandle(scenePos);
  200. }
  201. else if (Input.IsPointerButtonUp(PointerButton.Left))
  202. {
  203. if (!handleActive)
  204. {
  205. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
  206. Input.IsButtonHeld(ButtonCode.RightControl);
  207. sceneViewHandler.PickObject(scenePos, ctrlHeld);
  208. }
  209. }
  210. }
  211. sceneViewHandler.UpdateHandle(scenePos, Input.PointerDelta);
  212. }
  213. protected override void WindowResized(int width, int height)
  214. {
  215. UpdateRenderTexture(width, height - HeaderHeight);
  216. base.WindowResized(width, height);
  217. }
  218. protected override void FocusChanged(bool inFocus)
  219. {
  220. if (!inFocus)
  221. {
  222. sceneViewHandler.ClearHandleSelection();
  223. }
  224. }
  225. private void OnSceneToolButtonClicked(SceneViewTool tool)
  226. {
  227. EditorApplication.ActiveSceneTool = tool;
  228. editorSettingsHash = EditorSettings.Hash;
  229. }
  230. private void OnCoordinateModeButtonClicked(HandleCoordinateMode mode)
  231. {
  232. EditorApplication.ActiveCoordinateMode = mode;
  233. editorSettingsHash = EditorSettings.Hash;
  234. }
  235. private void OnPivotModeButtonClicked(HandlePivotMode mode)
  236. {
  237. EditorApplication.ActivePivotMode = mode;
  238. editorSettingsHash = EditorSettings.Hash;
  239. }
  240. private void OnMoveSnapToggled(bool active)
  241. {
  242. Handles.MoveHandleSnapActive = active;
  243. editorSettingsHash = EditorSettings.Hash;
  244. }
  245. private void OnMoveSnapValueChanged(float value)
  246. {
  247. Handles.MoveSnapAmount = MathEx.Clamp(value, 0.01f, 1000.0f);
  248. editorSettingsHash = EditorSettings.Hash;
  249. }
  250. private void OnRotateSnapToggled(bool active)
  251. {
  252. Handles.RotateHandleSnapActive = active;
  253. editorSettingsHash = EditorSettings.Hash;
  254. }
  255. private void OnRotateSnapValueChanged(float value)
  256. {
  257. Handles.RotateSnapAmount = MathEx.Clamp(value, 0.01f, 360.0f);
  258. editorSettingsHash = EditorSettings.Hash;
  259. }
  260. private void UpdateButtonStates()
  261. {
  262. switch (EditorApplication.ActiveSceneTool)
  263. {
  264. case SceneViewTool.View:
  265. viewButton.ToggleOn();
  266. break;
  267. case SceneViewTool.Move:
  268. moveButton.ToggleOn();
  269. break;
  270. case SceneViewTool.Rotate:
  271. rotateButton.ToggleOn();
  272. break;
  273. case SceneViewTool.Scale:
  274. scaleButton.ToggleOn();
  275. break;
  276. }
  277. switch (EditorApplication.ActiveCoordinateMode)
  278. {
  279. case HandleCoordinateMode.Local:
  280. localCoordButton.ToggleOn();
  281. break;
  282. case HandleCoordinateMode.World:
  283. worldCoordButton.ToggleOn();
  284. break;
  285. }
  286. switch (EditorApplication.ActivePivotMode)
  287. {
  288. case HandlePivotMode.Center:
  289. centerButton.ToggleOn();
  290. break;
  291. case HandlePivotMode.Pivot:
  292. pivotButton.ToggleOn();
  293. break;
  294. }
  295. if (Handles.MoveHandleSnapActive)
  296. moveSnapButton.ToggleOn();
  297. else
  298. moveSnapButton.ToggleOff();
  299. moveSnapInput.Value = Handles.MoveSnapAmount;
  300. if (Handles.RotateHandleSnapActive)
  301. rotateSnapButton.ToggleOn();
  302. else
  303. rotateSnapButton.ToggleOff();
  304. moveSnapInput.Value = Handles.RotateSnapAmount.Degrees;
  305. }
  306. private void UpdateRenderTexture(int width, int height)
  307. {
  308. width = MathEx.Max(20, width);
  309. height = MathEx.Max(20, height);
  310. renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height);
  311. renderTexture.Priority = 1;
  312. if (camera == null)
  313. {
  314. SceneObject sceneCameraSO = new SceneObject("SceneCamera");
  315. camera = sceneCameraSO.AddComponent<Camera>();
  316. camera.Target = renderTexture;
  317. camera.ViewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  318. sceneCameraSO.Position = new Vector3(0, 0.5f, 1);
  319. sceneCameraSO.LookAt(new Vector3(0, 0, 0));
  320. camera.Priority = 1;
  321. camera.NearClipPlane = 0.005f;
  322. camera.FarClipPlane = 1000.0f;
  323. cameraController = sceneCameraSO.AddComponent<SceneCamera>();
  324. renderTextureGUI = new GUIRenderTexture(renderTexture);
  325. mainLayout.AddElement(renderTextureGUI);
  326. sceneViewHandler = new SceneViewHandler(this, camera);
  327. }
  328. else
  329. {
  330. camera.Target = renderTexture;
  331. renderTextureGUI.RenderTexture = renderTexture;
  332. }
  333. // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
  334. // render target destroy/create cycle for every single pixel.
  335. camera.AspectRatio = width / (float)height;
  336. }
  337. }
  338. }