SceneWindow.cs 15 KB

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