SceneWindow.cs 20 KB

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