SceneWindow.cs 34 KB

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