SceneWindow.cs 17 KB

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