SceneWindow.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. /// <summary>
  11. /// Displays the scene view camera and various scene controls.
  12. /// </summary>
  13. internal sealed class SceneWindow : EditorWindow
  14. {
  15. internal const string ToggleProfilerOverlayBinding = "ToggleProfilerOverlay";
  16. internal const string ViewToolBinding = "ViewTool";
  17. internal const string MoveToolBinding = "MoveTool";
  18. internal const string RotateToolBinding = "RotateTool";
  19. internal const string ScaleToolBinding = "ScaleTool";
  20. internal const string DuplicateBinding = "Duplicate";
  21. internal const string DeleteBinding = "Delete";
  22. internal const string FrameBinding = "SceneFrame";
  23. private const int HeaderHeight = 20;
  24. private const float DefaultPlacementDepth = 5.0f;
  25. private static readonly Color ClearColor = new Color(83.0f/255.0f, 83.0f/255.0f, 83.0f/255.0f);
  26. private const string ProfilerOverlayActiveKey = "_Internal_ProfilerOverlayActive";
  27. private const int HandleAxesGUISize = 50;
  28. private const int HandleAxesGUIPadding = 5;
  29. private Camera camera;
  30. private SceneCamera cameraController;
  31. private RenderTexture2D renderTexture;
  32. private GUILayoutY mainLayout;
  33. private GUIPanel rtPanel;
  34. private GUIRenderTexture renderTextureGUI;
  35. private SceneGrid sceneGrid;
  36. private SceneSelection sceneSelection;
  37. private SceneGizmos sceneGizmos;
  38. private SceneHandles sceneHandles;
  39. private GUIToggle viewButton;
  40. private GUIToggle moveButton;
  41. private GUIToggle rotateButton;
  42. private GUIToggle scaleButton;
  43. private GUIToggle localCoordButton;
  44. private GUIToggle worldCoordButton;
  45. private GUIToggle pivotButton;
  46. private GUIToggle centerButton;
  47. private GUIToggle moveSnapButton;
  48. private GUIFloatField moveSnapInput;
  49. private GUIToggle rotateSnapButton;
  50. private GUIFloatField rotateSnapInput;
  51. private SceneAxesGUI sceneAxesGUI;
  52. private int editorSettingsHash = int.MaxValue;
  53. private VirtualButton duplicateKey;
  54. private VirtualButton deleteKey;
  55. private VirtualButton frameKey;
  56. // Tool shortcuts
  57. private VirtualButton viewToolKey;
  58. private VirtualButton moveToolKey;
  59. private VirtualButton rotateToolKey;
  60. private VirtualButton scaleToolKey;
  61. // Profiler overlay
  62. private ProfilerOverlay activeProfilerOverlay;
  63. private Camera profilerCamera;
  64. private VirtualButton toggleProfilerOverlayKey;
  65. // Drag & drop
  66. private bool dragActive;
  67. private SceneObject draggedSO;
  68. /// <summary>
  69. /// Returns the scene camera.
  70. /// </summary>
  71. public Camera Camera
  72. {
  73. get { return camera; }
  74. }
  75. /// <summary>
  76. /// Constructs a new scene window.
  77. /// </summary>
  78. internal SceneWindow()
  79. { }
  80. /// <summary>
  81. /// Opens a scene window if its not open already.
  82. /// </summary>
  83. [MenuItem("Windows/Scene", ButtonModifier.CtrlAlt, ButtonCode.S, 6000)]
  84. private static void OpenSceneWindow()
  85. {
  86. OpenWindow<SceneWindow>();
  87. }
  88. /// <summary>
  89. /// Focuses on the currently selected object.
  90. /// </summary>
  91. [MenuItem("Tools/Frame Selected", ButtonModifier.None, ButtonCode.F, 9275, true)]
  92. private static void OpenSettingsWindow()
  93. {
  94. SceneWindow window = GetWindow<SceneWindow>();
  95. if (window != null)
  96. window.cameraController.FrameSelected();
  97. }
  98. /// <summary>
  99. /// Switches the active tool to the view tool.
  100. /// </summary>
  101. [MenuItem("Tools/View", ButtonModifier.Ctrl, ButtonCode.Q, 9274, true)]
  102. private static void SetViewTool()
  103. {
  104. SceneWindow window = GetWindow<SceneWindow>();
  105. if (window != null)
  106. window.OnSceneToolButtonClicked(SceneViewTool.View);
  107. }
  108. /// <summary>
  109. /// Switches the active tool to the move tool.
  110. /// </summary>
  111. [MenuItem("Tools/Move", ButtonModifier.Ctrl, ButtonCode.W, 9273)]
  112. private static void SetMoveTool()
  113. {
  114. SceneWindow window = GetWindow<SceneWindow>();
  115. if (window != null)
  116. window.OnSceneToolButtonClicked(SceneViewTool.Move);
  117. }
  118. /// <summary>
  119. /// Switches the active tool to the rotate tool.
  120. /// </summary>
  121. [MenuItem("Tools/Rotate", ButtonModifier.Ctrl, ButtonCode.E, 9272)]
  122. private static void SetRotateTool()
  123. {
  124. SceneWindow window = GetWindow<SceneWindow>();
  125. if (window != null)
  126. window.OnSceneToolButtonClicked(SceneViewTool.Rotate);
  127. }
  128. /// <summary>
  129. /// Switches the active tool to the scale tool.
  130. /// </summary>
  131. [MenuItem("Tools/Scale", ButtonModifier.Ctrl, ButtonCode.R, 9271)]
  132. private static void SetScaleTool()
  133. {
  134. SceneWindow window = GetWindow<SceneWindow>();
  135. if (window != null)
  136. window.OnSceneToolButtonClicked(SceneViewTool.Scale);
  137. }
  138. /// <inheritdoc/>
  139. protected override LocString GetDisplayName()
  140. {
  141. return new LocEdString("Scene");
  142. }
  143. private void OnInitialize()
  144. {
  145. mainLayout = GUI.AddLayoutY();
  146. GUIContent viewIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.View),
  147. new LocEdString("View"));
  148. GUIContent moveIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Move),
  149. new LocEdString("Move"));
  150. GUIContent rotateIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Rotate),
  151. new LocEdString("Rotate"));
  152. GUIContent scaleIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Scale),
  153. new LocEdString("Scale"));
  154. GUIContent localIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Local),
  155. new LocEdString("Local"));
  156. GUIContent worldIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.World),
  157. new LocEdString("World"));
  158. GUIContent pivotIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Pivot),
  159. new LocEdString("Pivot"));
  160. GUIContent centerIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Center),
  161. new LocEdString("Center"));
  162. GUIContent moveSnapIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.MoveSnap),
  163. new LocEdString("Move snap"));
  164. GUIContent rotateSnapIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.RotateSnap),
  165. new LocEdString("Rotate snap"));
  166. GUIToggleGroup handlesTG = new GUIToggleGroup();
  167. viewButton = new GUIToggle(viewIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  168. moveButton = new GUIToggle(moveIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  169. rotateButton = new GUIToggle(rotateIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  170. scaleButton = new GUIToggle(scaleIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  171. GUIToggleGroup coordModeTG = new GUIToggleGroup();
  172. localCoordButton = new GUIToggle(localIcon, coordModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(75));
  173. worldCoordButton = new GUIToggle(worldIcon, coordModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(75));
  174. GUIToggleGroup pivotModeTG = new GUIToggleGroup();
  175. pivotButton = new GUIToggle(pivotIcon, pivotModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  176. centerButton = new GUIToggle(centerIcon, pivotModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  177. moveSnapButton = new GUIToggle(moveSnapIcon, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  178. moveSnapInput = new GUIFloatField("", GUIOption.FlexibleWidth(35));
  179. rotateSnapButton = new GUIToggle(rotateSnapIcon, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  180. rotateSnapInput = new GUIFloatField("", GUIOption.FlexibleWidth(35));
  181. viewButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.View);
  182. moveButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Move);
  183. rotateButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Rotate);
  184. scaleButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Scale);
  185. localCoordButton.OnClick += () => OnCoordinateModeButtonClicked(HandleCoordinateMode.Local);
  186. worldCoordButton.OnClick += () => OnCoordinateModeButtonClicked(HandleCoordinateMode.World);
  187. pivotButton.OnClick += () => OnPivotModeButtonClicked(HandlePivotMode.Pivot);
  188. centerButton.OnClick += () => OnPivotModeButtonClicked(HandlePivotMode.Center);
  189. moveSnapButton.OnToggled += (bool active) => OnMoveSnapToggled(active);
  190. moveSnapInput.OnChanged += (float value) => OnMoveSnapValueChanged(value);
  191. rotateSnapButton.OnToggled += (bool active) => OnRotateSnapToggled(active);
  192. rotateSnapInput.OnChanged += (float value) => OnRotateSnapValueChanged(value);
  193. GUILayout handlesLayout = mainLayout.AddLayoutX();
  194. handlesLayout.AddElement(viewButton);
  195. handlesLayout.AddElement(moveButton);
  196. handlesLayout.AddElement(rotateButton);
  197. handlesLayout.AddElement(scaleButton);
  198. handlesLayout.AddSpace(10);
  199. handlesLayout.AddElement(localCoordButton);
  200. handlesLayout.AddElement(worldCoordButton);
  201. handlesLayout.AddSpace(10);
  202. handlesLayout.AddElement(pivotButton);
  203. handlesLayout.AddElement(centerButton);
  204. handlesLayout.AddFlexibleSpace();
  205. handlesLayout.AddElement(moveSnapButton);
  206. handlesLayout.AddElement(moveSnapInput);
  207. handlesLayout.AddSpace(10);
  208. handlesLayout.AddElement(rotateSnapButton);
  209. handlesLayout.AddElement(rotateSnapInput);
  210. GUIPanel mainPanel = mainLayout.AddPanel();
  211. rtPanel = mainPanel.AddPanel();
  212. GUIPanel sceneAxesPanel = mainPanel.AddPanel(-1);
  213. sceneAxesGUI = new SceneAxesGUI(this, sceneAxesPanel, HandleAxesGUISize, HandleAxesGUISize);
  214. toggleProfilerOverlayKey = new VirtualButton(ToggleProfilerOverlayBinding);
  215. viewToolKey = new VirtualButton(ViewToolBinding);
  216. moveToolKey = new VirtualButton(MoveToolBinding);
  217. rotateToolKey = new VirtualButton(RotateToolBinding);
  218. scaleToolKey = new VirtualButton(ScaleToolBinding);
  219. duplicateKey = new VirtualButton(DuplicateBinding);
  220. deleteKey = new VirtualButton(DeleteBinding);
  221. frameKey = new VirtualButton(FrameBinding);
  222. UpdateRenderTexture(Width, Height - HeaderHeight);
  223. UpdateProfilerOverlay();
  224. }
  225. private void OnDestroy()
  226. {
  227. if (camera != null)
  228. {
  229. camera.SceneObject.Destroy();
  230. camera = null;
  231. }
  232. }
  233. /// <summary>
  234. /// Converts screen coordinates into coordinates relative to the scene view render texture.
  235. /// </summary>
  236. /// <param name="screenPos">Coordinates relative to the screen.</param>
  237. /// <param name="scenePos">Output coordinates relative to the scene view texture.</param>
  238. /// <returns>True if the coordinates are within the scene view texture, false otherwise.</returns>
  239. private bool ScreenToScenePos(Vector2I screenPos, out Vector2I scenePos)
  240. {
  241. scenePos = screenPos;
  242. Vector2I windowPos = ScreenToWindowPos(screenPos);
  243. Rect2I bounds = GUILayoutUtility.CalculateBounds(renderTextureGUI, GUI);
  244. if (bounds.Contains(windowPos))
  245. {
  246. scenePos.x = windowPos.x - bounds.x;
  247. scenePos.y = windowPos.y - bounds.y;
  248. return true;
  249. }
  250. return false;
  251. }
  252. private void OnEditorUpdate()
  253. {
  254. if (HasFocus)
  255. {
  256. if (!Input.IsPointerButtonHeld(PointerButton.Right))
  257. {
  258. if (VirtualInput.IsButtonUp(toggleProfilerOverlayKey))
  259. EditorSettings.SetBool(ProfilerOverlayActiveKey, !EditorSettings.GetBool(ProfilerOverlayActiveKey));
  260. if (VirtualInput.IsButtonUp(viewToolKey))
  261. EditorApplication.ActiveSceneTool = SceneViewTool.View;
  262. if (VirtualInput.IsButtonUp(moveToolKey))
  263. EditorApplication.ActiveSceneTool = SceneViewTool.Move;
  264. if (VirtualInput.IsButtonUp(rotateToolKey))
  265. EditorApplication.ActiveSceneTool = SceneViewTool.Rotate;
  266. if (VirtualInput.IsButtonUp(scaleToolKey))
  267. EditorApplication.ActiveSceneTool = SceneViewTool.Scale;
  268. if (VirtualInput.IsButtonUp(duplicateKey))
  269. {
  270. SceneObject[] selectedObjects = Selection.SceneObjects;
  271. CleanDuplicates(ref selectedObjects);
  272. if (selectedObjects.Length > 0)
  273. {
  274. String message;
  275. if (selectedObjects.Length == 1)
  276. message = "Duplicated " + selectedObjects[0].Name;
  277. else
  278. message = "Duplicated " + selectedObjects.Length + " elements";
  279. UndoRedo.CloneSO(selectedObjects, message);
  280. EditorApplication.SetSceneDirty();
  281. }
  282. }
  283. if (VirtualInput.IsButtonUp(deleteKey))
  284. {
  285. SceneObject[] selectedObjects = Selection.SceneObjects;
  286. CleanDuplicates(ref selectedObjects);
  287. if (selectedObjects.Length > 0)
  288. {
  289. foreach (var so in selectedObjects)
  290. {
  291. string message = "Deleted " + so.Name;
  292. UndoRedo.DeleteSO(so, message);
  293. }
  294. EditorApplication.SetSceneDirty();
  295. }
  296. }
  297. }
  298. }
  299. // Refresh GUI buttons if needed (in case someones changes the values from script)
  300. if (editorSettingsHash != EditorSettings.Hash)
  301. {
  302. UpdateButtonStates();
  303. UpdateProfilerOverlay();
  304. editorSettingsHash = EditorSettings.Hash;
  305. }
  306. // Update scene view handles and selection
  307. sceneGizmos.Draw();
  308. sceneGrid.Draw();
  309. bool handleActive = false;
  310. if (Input.IsPointerButtonUp(PointerButton.Left))
  311. {
  312. if (sceneHandles.IsActive())
  313. {
  314. sceneHandles.ClearSelection();
  315. handleActive = true;
  316. }
  317. if (sceneAxesGUI.IsActive())
  318. {
  319. sceneAxesGUI.ClearSelection();
  320. handleActive = true;
  321. }
  322. }
  323. Vector2I scenePos;
  324. bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);
  325. bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;
  326. draggedOver &= inBounds && DragDrop.Type == DragDropType.Resource;
  327. if (draggedOver)
  328. {
  329. if (DragDrop.DropInProgress)
  330. {
  331. dragActive = false;
  332. if (draggedSO != null)
  333. Selection.SceneObject = draggedSO;
  334. draggedSO = null;
  335. }
  336. else
  337. {
  338. if (!dragActive)
  339. {
  340. dragActive = true;
  341. ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;
  342. string[] draggedPaths = dragData.Paths;
  343. for (int i = 0; i < draggedPaths.Length; i++)
  344. {
  345. LibraryEntry entry = ProjectLibrary.GetEntry(draggedPaths[i]);
  346. if (entry != null && entry.Type == LibraryEntryType.File)
  347. {
  348. FileEntry fileEntry = (FileEntry) entry;
  349. if (fileEntry.ResType == ResourceType.Mesh)
  350. {
  351. if (!string.IsNullOrEmpty(draggedPaths[i]))
  352. {
  353. string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]);
  354. draggedSO = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
  355. Mesh mesh = ProjectLibrary.Load<Mesh>(draggedPaths[i]);
  356. Renderable renderable = draggedSO.AddComponent<Renderable>();
  357. renderable.Mesh = mesh;
  358. EditorApplication.SetSceneDirty();
  359. }
  360. break;
  361. }
  362. else if (fileEntry.ResType == ResourceType.Prefab)
  363. {
  364. if (!string.IsNullOrEmpty(draggedPaths[i]))
  365. {
  366. Prefab prefab = ProjectLibrary.Load<Prefab>(draggedPaths[i]);
  367. draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);
  368. EditorApplication.SetSceneDirty();
  369. }
  370. break;
  371. }
  372. }
  373. }
  374. }
  375. if (draggedSO != null)
  376. {
  377. Ray worldRay = camera.ScreenToWorldRay(scenePos);
  378. draggedSO.Position = worldRay*DefaultPlacementDepth;
  379. }
  380. }
  381. return;
  382. }
  383. else
  384. {
  385. if (dragActive)
  386. {
  387. dragActive = false;
  388. if (draggedSO != null)
  389. {
  390. draggedSO.Destroy();
  391. draggedSO = null;
  392. }
  393. }
  394. }
  395. if (HasFocus)
  396. {
  397. cameraController.EnableInput(true);
  398. if (inBounds)
  399. {
  400. if (Input.IsPointerButtonDown(PointerButton.Left))
  401. {
  402. Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPadding,
  403. HandleAxesGUIPadding, HandleAxesGUISize, HandleAxesGUISize);
  404. if (sceneAxesGUIBounds.Contains(scenePos))
  405. sceneAxesGUI.TrySelect(scenePos);
  406. else
  407. sceneHandles.TrySelect(scenePos);
  408. }
  409. else if (Input.IsPointerButtonUp(PointerButton.Left))
  410. {
  411. if (!handleActive)
  412. {
  413. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
  414. Input.IsButtonHeld(ButtonCode.RightControl);
  415. sceneSelection.PickObject(scenePos, ctrlHeld);
  416. }
  417. }
  418. }
  419. }
  420. else
  421. cameraController.EnableInput(false);
  422. SceneHandles.BeginInput();
  423. sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
  424. sceneHandles.Draw();
  425. sceneAxesGUI.UpdateInput(scenePos);
  426. sceneAxesGUI.Draw();
  427. SceneHandles.EndInput();
  428. sceneSelection.Draw();
  429. if (VirtualInput.IsButtonDown(frameKey))
  430. {
  431. cameraController.FrameSelected();
  432. }
  433. }
  434. /// <inheritdoc/>
  435. protected override void WindowResized(int width, int height)
  436. {
  437. UpdateRenderTexture(width, height - HeaderHeight);
  438. base.WindowResized(width, height);
  439. }
  440. /// <inheritdoc/>
  441. protected override void FocusChanged(bool inFocus)
  442. {
  443. if (!inFocus)
  444. {
  445. sceneHandles.ClearSelection();
  446. }
  447. }
  448. /// <summary>
  449. /// Triggered when one of the scene tool buttons is clicked, changing the active scene handle.
  450. /// </summary>
  451. /// <param name="tool">Clicked scene tool to activate.</param>
  452. private void OnSceneToolButtonClicked(SceneViewTool tool)
  453. {
  454. EditorApplication.ActiveSceneTool = tool;
  455. editorSettingsHash = EditorSettings.Hash;
  456. }
  457. /// <summary>
  458. /// Triggered when one of the coordinate mode buttons is clicked, changing the active coordinate mode.
  459. /// </summary>
  460. /// <param name="mode">Clicked coordinate mode to activate.</param>
  461. private void OnCoordinateModeButtonClicked(HandleCoordinateMode mode)
  462. {
  463. EditorApplication.ActiveCoordinateMode = mode;
  464. editorSettingsHash = EditorSettings.Hash;
  465. }
  466. /// <summary>
  467. /// Triggered when one of the pivot buttons is clicked, changing the active pivot mode.
  468. /// </summary>
  469. /// <param name="mode">Clicked pivot mode to activate.</param>
  470. private void OnPivotModeButtonClicked(HandlePivotMode mode)
  471. {
  472. EditorApplication.ActivePivotMode = mode;
  473. editorSettingsHash = EditorSettings.Hash;
  474. }
  475. /// <summary>
  476. /// Triggered when the move snap button is toggled.
  477. /// </summary>
  478. /// <param name="active">Determins should be move snap be activated or deactivated.</param>
  479. private void OnMoveSnapToggled(bool active)
  480. {
  481. Handles.MoveHandleSnapActive = active;
  482. editorSettingsHash = EditorSettings.Hash;
  483. }
  484. /// <summary>
  485. /// Triggered when the move snap increment value changes.
  486. /// </summary>
  487. /// <param name="value">Value that determines in what increments to perform move snapping.</param>
  488. private void OnMoveSnapValueChanged(float value)
  489. {
  490. Handles.MoveSnapAmount = MathEx.Clamp(value, 0.01f, 1000.0f);
  491. editorSettingsHash = EditorSettings.Hash;
  492. }
  493. /// <summary>
  494. /// Triggered when the rotate snap button is toggled.
  495. /// </summary>
  496. /// <param name="active">Determins should be rotate snap be activated or deactivated.</param>
  497. private void OnRotateSnapToggled(bool active)
  498. {
  499. Handles.RotateHandleSnapActive = active;
  500. editorSettingsHash = EditorSettings.Hash;
  501. }
  502. /// <summary>
  503. /// Triggered when the rotate snap increment value changes.
  504. /// </summary>
  505. /// <param name="value">Value that determines in what increments to perform rotate snapping.</param>
  506. private void OnRotateSnapValueChanged(float value)
  507. {
  508. Handles.RotateSnapAmount = MathEx.Clamp(value, 0.01f, 360.0f);
  509. editorSettingsHash = EditorSettings.Hash;
  510. }
  511. /// <summary>
  512. /// Updates toggle button states according to current editor options. This is useful if tools, coordinate mode,
  513. /// pivot or other scene view options have been modified externally.
  514. /// </summary>
  515. private void UpdateButtonStates()
  516. {
  517. switch (EditorApplication.ActiveSceneTool)
  518. {
  519. case SceneViewTool.View:
  520. viewButton.Value = true;
  521. break;
  522. case SceneViewTool.Move:
  523. moveButton.Value = true;
  524. break;
  525. case SceneViewTool.Rotate:
  526. rotateButton.Value = true;
  527. break;
  528. case SceneViewTool.Scale:
  529. scaleButton.Value = true;
  530. break;
  531. }
  532. switch (EditorApplication.ActiveCoordinateMode)
  533. {
  534. case HandleCoordinateMode.Local:
  535. localCoordButton.Value = true;
  536. break;
  537. case HandleCoordinateMode.World:
  538. worldCoordButton.Value = true;
  539. break;
  540. }
  541. switch (EditorApplication.ActivePivotMode)
  542. {
  543. case HandlePivotMode.Center:
  544. centerButton.Value = true;
  545. break;
  546. case HandlePivotMode.Pivot:
  547. pivotButton.Value = true;
  548. break;
  549. }
  550. if (Handles.MoveHandleSnapActive)
  551. moveSnapButton.Value = true;
  552. else
  553. moveSnapButton.Value = false;
  554. moveSnapInput.Value = Handles.MoveSnapAmount;
  555. if (Handles.RotateHandleSnapActive)
  556. rotateSnapButton.Value = true;
  557. else
  558. rotateSnapButton.Value = false;
  559. moveSnapInput.Value = Handles.RotateSnapAmount.Degrees;
  560. }
  561. /// <summary>
  562. /// Activates or deactivates the profiler overlay according to current editor settings.
  563. /// </summary>
  564. private void UpdateProfilerOverlay()
  565. {
  566. if (EditorSettings.GetBool(ProfilerOverlayActiveKey))
  567. {
  568. if (activeProfilerOverlay == null)
  569. {
  570. SceneObject profilerSO = new SceneObject("EditorProfilerOverlay");
  571. profilerCamera = profilerSO.AddComponent<Camera>();
  572. profilerCamera.Target = renderTexture;
  573. profilerCamera.ClearFlags = ClearFlags.None;
  574. profilerCamera.Priority = 1;
  575. profilerCamera.Layers = 0;
  576. activeProfilerOverlay = profilerSO.AddComponent<ProfilerOverlay>();
  577. }
  578. }
  579. else
  580. {
  581. if (activeProfilerOverlay != null)
  582. {
  583. activeProfilerOverlay.SceneObject.Destroy();
  584. activeProfilerOverlay = null;
  585. profilerCamera = null;
  586. }
  587. }
  588. }
  589. /// <summary>
  590. /// Creates the scene camera and updates the render texture. Should be called at least once before using the
  591. /// scene view. Should be called whenever the window is resized.
  592. /// </summary>
  593. /// <param name="width">Width of the scene render target, in pixels.</param>
  594. /// <param name="height">Height of the scene render target, in pixels.</param>
  595. private void UpdateRenderTexture(int width, int height)
  596. {
  597. width = MathEx.Max(20, width);
  598. height = MathEx.Max(20, height);
  599. renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height);
  600. renderTexture.Priority = 1;
  601. if (camera == null)
  602. {
  603. SceneObject sceneCameraSO = new SceneObject("SceneCamera", true);
  604. camera = sceneCameraSO.AddComponent<Camera>();
  605. camera.Target = renderTexture;
  606. camera.ViewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  607. sceneCameraSO.Position = new Vector3(0, 0.5f, 1);
  608. sceneCameraSO.LookAt(new Vector3(0, 0.5f, 0));
  609. camera.Priority = 2;
  610. camera.NearClipPlane = 0.05f;
  611. camera.FarClipPlane = 2500.0f;
  612. camera.ClearColor = ClearColor;
  613. camera.Layers = UInt64.MaxValue & ~SceneAxesHandle.LAYER; // Don't draw scene axes in this camera
  614. cameraController = sceneCameraSO.AddComponent<SceneCamera>();
  615. renderTextureGUI = new GUIRenderTexture(renderTexture);
  616. rtPanel.AddElement(renderTextureGUI);
  617. sceneGrid = new SceneGrid(camera);
  618. sceneSelection = new SceneSelection(camera);
  619. sceneGizmos = new SceneGizmos(camera);
  620. sceneHandles = new SceneHandles(this, camera);
  621. }
  622. else
  623. {
  624. camera.Target = renderTexture;
  625. renderTextureGUI.RenderTexture = renderTexture;
  626. }
  627. Rect2I rtBounds = new Rect2I(0, 0, width, height);
  628. renderTextureGUI.Bounds = rtBounds;
  629. sceneAxesGUI.SetPosition(width - HandleAxesGUISize - HandleAxesGUIPadding, HandleAxesGUIPadding);
  630. // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
  631. // render target destroy/create cycle for every single pixel.
  632. camera.AspectRatio = width / (float)height;
  633. if (profilerCamera != null)
  634. profilerCamera.Target = renderTexture;
  635. }
  636. /// <summary>
  637. /// Parses an array of scene objects and removes elements that are children of elements that are also in the array.
  638. /// </summary>
  639. /// <param name="objects">Array containing duplicate objects as input, and array without duplicate objects as
  640. /// output.</param>
  641. private void CleanDuplicates(ref SceneObject[] objects)
  642. {
  643. List<SceneObject> cleanList = new List<SceneObject>();
  644. for (int i = 0; i < objects.Length; i++)
  645. {
  646. bool foundParent = false;
  647. for (int j = 0; j < objects.Length; j++)
  648. {
  649. SceneObject elem = objects[i];
  650. while (elem != null && elem != objects[j])
  651. elem = elem.Parent;
  652. bool isChildOf = elem == objects[j];
  653. if (i != j && isChildOf)
  654. {
  655. foundParent = true;
  656. break;
  657. }
  658. }
  659. if (!foundParent)
  660. cleanList.Add(objects[i]);
  661. }
  662. objects = cleanList.ToArray();
  663. }
  664. }
  665. }