SceneWindow.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 new LocEdString("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. if (HasFocus)
  133. {
  134. if (VirtualInput.IsButtonUp(toggleProfilerOverlayKey))
  135. EditorSettings.SetBool(ProfilerOverlayActiveKey, !EditorSettings.GetBool(ProfilerOverlayActiveKey));
  136. }
  137. // Refresh GUI buttons if needed (in case someones changes the values from script)
  138. if (editorSettingsHash != EditorSettings.Hash)
  139. {
  140. UpdateButtonStates();
  141. UpdateProfilerOverlay();
  142. editorSettingsHash = EditorSettings.Hash;
  143. }
  144. // Update scene view handles and selection
  145. sceneViewHandler.Update();
  146. bool handleActive = false;
  147. if (Input.IsPointerButtonUp(PointerButton.Left))
  148. {
  149. if (sceneViewHandler.IsHandleActive())
  150. {
  151. sceneViewHandler.ClearHandleSelection();
  152. handleActive = true;
  153. }
  154. }
  155. Vector2I scenePos;
  156. bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);
  157. bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;
  158. draggedOver &= inBounds && DragDrop.Type == DragDropType.Resource;
  159. if (draggedOver)
  160. {
  161. if (DragDrop.DropInProgress)
  162. {
  163. dragActive = false;
  164. draggedSO = null;
  165. }
  166. else
  167. {
  168. if (!dragActive)
  169. {
  170. dragActive = true;
  171. ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;
  172. string draggedMeshPath = "";
  173. string[] draggedPaths = dragData.Paths;
  174. for (int i = 0; i < draggedPaths.Length; i++)
  175. {
  176. LibraryEntry entry = ProjectLibrary.GetEntry(draggedPaths[i]);
  177. if (entry != null && entry.Type == LibraryEntryType.File)
  178. {
  179. FileEntry fileEntry = (FileEntry) entry;
  180. if (fileEntry.ResType == ResourceType.Mesh)
  181. {
  182. draggedMeshPath = draggedPaths[i];
  183. break;
  184. }
  185. }
  186. }
  187. if (!string.IsNullOrEmpty(draggedMeshPath))
  188. {
  189. string meshName = Path.GetFileName(draggedMeshPath);
  190. draggedSO = new SceneObject(meshName);
  191. Mesh mesh = ProjectLibrary.Load<Mesh>(draggedMeshPath);
  192. Material material = new Material(Builtin.DiffuseShader);
  193. Renderable renderable = draggedSO.AddComponent<Renderable>();
  194. renderable.Mesh = mesh;
  195. renderable.SetMaterial(material);
  196. }
  197. }
  198. if (draggedSO != null)
  199. {
  200. Ray worldRay = camera.ScreenToWorldRay(scenePos);
  201. draggedSO.Position = worldRay*DefaultPlacementDepth;
  202. }
  203. }
  204. return;
  205. }
  206. else
  207. {
  208. if (dragActive)
  209. {
  210. dragActive = false;
  211. if (draggedSO != null)
  212. {
  213. draggedSO.Destroy();
  214. draggedSO = null;
  215. }
  216. }
  217. }
  218. if (HasFocus)
  219. {
  220. cameraController.SceneObject.Active = true;
  221. if (inBounds)
  222. {
  223. if (Input.IsPointerButtonDown(PointerButton.Left))
  224. {
  225. sceneViewHandler.TrySelectHandle(scenePos);
  226. }
  227. else if (Input.IsPointerButtonUp(PointerButton.Left))
  228. {
  229. if (!handleActive)
  230. {
  231. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
  232. Input.IsButtonHeld(ButtonCode.RightControl);
  233. sceneViewHandler.PickObject(scenePos, ctrlHeld);
  234. }
  235. }
  236. }
  237. }
  238. else
  239. cameraController.SceneObject.Active = false;
  240. sceneViewHandler.UpdateHandle(scenePos, Input.PointerDelta);
  241. sceneViewHandler.UpdateSelection();
  242. }
  243. protected override void WindowResized(int width, int height)
  244. {
  245. UpdateRenderTexture(width, height - HeaderHeight);
  246. base.WindowResized(width, height);
  247. }
  248. protected override void FocusChanged(bool inFocus)
  249. {
  250. if (!inFocus)
  251. {
  252. sceneViewHandler.ClearHandleSelection();
  253. }
  254. }
  255. private void OnSceneToolButtonClicked(SceneViewTool tool)
  256. {
  257. EditorApplication.ActiveSceneTool = tool;
  258. editorSettingsHash = EditorSettings.Hash;
  259. }
  260. private void OnCoordinateModeButtonClicked(HandleCoordinateMode mode)
  261. {
  262. EditorApplication.ActiveCoordinateMode = mode;
  263. editorSettingsHash = EditorSettings.Hash;
  264. }
  265. private void OnPivotModeButtonClicked(HandlePivotMode mode)
  266. {
  267. EditorApplication.ActivePivotMode = mode;
  268. editorSettingsHash = EditorSettings.Hash;
  269. }
  270. private void OnMoveSnapToggled(bool active)
  271. {
  272. Handles.MoveHandleSnapActive = active;
  273. editorSettingsHash = EditorSettings.Hash;
  274. }
  275. private void OnMoveSnapValueChanged(float value)
  276. {
  277. Handles.MoveSnapAmount = MathEx.Clamp(value, 0.01f, 1000.0f);
  278. editorSettingsHash = EditorSettings.Hash;
  279. }
  280. private void OnRotateSnapToggled(bool active)
  281. {
  282. Handles.RotateHandleSnapActive = active;
  283. editorSettingsHash = EditorSettings.Hash;
  284. }
  285. private void OnRotateSnapValueChanged(float value)
  286. {
  287. Handles.RotateSnapAmount = MathEx.Clamp(value, 0.01f, 360.0f);
  288. editorSettingsHash = EditorSettings.Hash;
  289. }
  290. private void UpdateButtonStates()
  291. {
  292. switch (EditorApplication.ActiveSceneTool)
  293. {
  294. case SceneViewTool.View:
  295. viewButton.ToggleOn();
  296. break;
  297. case SceneViewTool.Move:
  298. moveButton.ToggleOn();
  299. break;
  300. case SceneViewTool.Rotate:
  301. rotateButton.ToggleOn();
  302. break;
  303. case SceneViewTool.Scale:
  304. scaleButton.ToggleOn();
  305. break;
  306. }
  307. switch (EditorApplication.ActiveCoordinateMode)
  308. {
  309. case HandleCoordinateMode.Local:
  310. localCoordButton.ToggleOn();
  311. break;
  312. case HandleCoordinateMode.World:
  313. worldCoordButton.ToggleOn();
  314. break;
  315. }
  316. switch (EditorApplication.ActivePivotMode)
  317. {
  318. case HandlePivotMode.Center:
  319. centerButton.ToggleOn();
  320. break;
  321. case HandlePivotMode.Pivot:
  322. pivotButton.ToggleOn();
  323. break;
  324. }
  325. if (Handles.MoveHandleSnapActive)
  326. moveSnapButton.ToggleOn();
  327. else
  328. moveSnapButton.ToggleOff();
  329. moveSnapInput.Value = Handles.MoveSnapAmount;
  330. if (Handles.RotateHandleSnapActive)
  331. rotateSnapButton.ToggleOn();
  332. else
  333. rotateSnapButton.ToggleOff();
  334. moveSnapInput.Value = Handles.RotateSnapAmount.Degrees;
  335. }
  336. private void UpdateProfilerOverlay()
  337. {
  338. if (EditorSettings.GetBool(ProfilerOverlayActiveKey))
  339. {
  340. if (activeProfilerOverlay == null)
  341. {
  342. SceneObject profilerSO = new SceneObject("EditorProfilerOverlay");
  343. profilerCamera = profilerSO.AddComponent<Camera>();
  344. profilerCamera.Target = renderTexture;
  345. profilerCamera.ClearFlags = ClearFlags.None;
  346. profilerCamera.Priority = 1;
  347. profilerCamera.Layers = 0;
  348. activeProfilerOverlay = profilerSO.AddComponent<ProfilerOverlay>();
  349. }
  350. }
  351. else
  352. {
  353. if (activeProfilerOverlay != null)
  354. {
  355. activeProfilerOverlay.SceneObject.Destroy();
  356. activeProfilerOverlay = null;
  357. profilerCamera = null;
  358. }
  359. }
  360. }
  361. private void UpdateRenderTexture(int width, int height)
  362. {
  363. width = MathEx.Max(20, width);
  364. height = MathEx.Max(20, height);
  365. renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height);
  366. renderTexture.Priority = 1;
  367. if (camera == null)
  368. {
  369. SceneObject sceneCameraSO = new SceneObject("SceneCamera", true);
  370. camera = sceneCameraSO.AddComponent<Camera>();
  371. camera.Target = renderTexture;
  372. camera.ViewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  373. sceneCameraSO.Position = new Vector3(0, 0.5f, 1);
  374. sceneCameraSO.LookAt(new Vector3(0, 0, 0));
  375. camera.Priority = 2;
  376. camera.NearClipPlane = 0.005f;
  377. camera.FarClipPlane = 1000.0f;
  378. camera.ClearColor = ClearColor;
  379. cameraController = sceneCameraSO.AddComponent<SceneCamera>();
  380. renderTextureGUI = new GUIRenderTexture(renderTexture);
  381. mainLayout.AddElement(renderTextureGUI);
  382. sceneViewHandler = new SceneViewHandler(this, camera);
  383. }
  384. else
  385. {
  386. camera.Target = renderTexture;
  387. renderTextureGUI.RenderTexture = renderTexture;
  388. }
  389. // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
  390. // render target destroy/create cycle for every single pixel.
  391. camera.AspectRatio = width / (float)height;
  392. if (profilerCamera != null)
  393. profilerCamera.Target = renderTexture;
  394. }
  395. }
  396. }