SceneWindow.cs 15 KB

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