SceneWindow.cs 34 KB

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