SceneWindow.cs 20 KB

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