SceneWindow.cs 15 KB

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