SceneWindow.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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(0.0f, 0.3685f, 0.7969f);
  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 RenderTexture 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. private Vector3 draggedSOOffset;
  71. private GUITexture dragSelection;
  72. private bool isDraggingSelection;
  73. private Vector2I dragSelectionStart;
  74. private Vector2I dragSelectionEnd;
  75. private Vector2I mouseDownPosition;
  76. private GUIPanel selectionPanel;
  77. /// <summary>
  78. /// Returns the scene camera.
  79. /// </summary>
  80. public Camera Camera
  81. {
  82. get { return camera; }
  83. }
  84. /// <summary>
  85. /// Determines scene camera's projection type.
  86. /// </summary>
  87. internal ProjectionType ProjectionType
  88. {
  89. get { return cameraController.ProjectionType; }
  90. set { cameraController.ProjectionType = value; sceneAxesGUI.ProjectionType = value; }
  91. }
  92. /// <summary>
  93. /// Constructs a new scene window.
  94. /// </summary>
  95. internal SceneWindow()
  96. { }
  97. /// <summary>
  98. /// Opens a scene window if its not open already.
  99. /// </summary>
  100. [MenuItem("Windows/Scene", ButtonModifier.CtrlAlt, ButtonCode.S, 6000)]
  101. private static void OpenSceneWindow()
  102. {
  103. OpenWindow<SceneWindow>();
  104. }
  105. /// <summary>
  106. /// Focuses on the currently selected object.
  107. /// </summary>
  108. [MenuItem("Tools/Frame Selected", ButtonModifier.None, ButtonCode.F, 9275, true)]
  109. private static void OpenSettingsWindow()
  110. {
  111. SceneWindow window = GetWindow<SceneWindow>();
  112. if (window != null)
  113. window.cameraController.FrameSelected();
  114. }
  115. /// <summary>
  116. /// Switches the active tool to the view tool.
  117. /// </summary>
  118. [MenuItem("Tools/View", ButtonModifier.Ctrl, ButtonCode.Q, 9274, true)]
  119. private static void SetViewTool()
  120. {
  121. SceneWindow window = GetWindow<SceneWindow>();
  122. if (window != null)
  123. window.OnSceneToolButtonClicked(SceneViewTool.View);
  124. }
  125. /// <summary>
  126. /// Switches the active tool to the move tool.
  127. /// </summary>
  128. [MenuItem("Tools/Move", ButtonModifier.Ctrl, ButtonCode.W, 9273)]
  129. private static void SetMoveTool()
  130. {
  131. SceneWindow window = GetWindow<SceneWindow>();
  132. if (window != null)
  133. window.OnSceneToolButtonClicked(SceneViewTool.Move);
  134. }
  135. /// <summary>
  136. /// Switches the active tool to the rotate tool.
  137. /// </summary>
  138. [MenuItem("Tools/Rotate", ButtonModifier.Ctrl, ButtonCode.E, 9272)]
  139. private static void SetRotateTool()
  140. {
  141. SceneWindow window = GetWindow<SceneWindow>();
  142. if (window != null)
  143. window.OnSceneToolButtonClicked(SceneViewTool.Rotate);
  144. }
  145. /// <summary>
  146. /// Switches the active tool to the scale tool.
  147. /// </summary>
  148. [MenuItem("Tools/Scale", ButtonModifier.Ctrl, ButtonCode.R, 9271)]
  149. private static void SetScaleTool()
  150. {
  151. SceneWindow window = GetWindow<SceneWindow>();
  152. if (window != null)
  153. window.OnSceneToolButtonClicked(SceneViewTool.Scale);
  154. }
  155. /// <inheritdoc/>
  156. protected override LocString GetDisplayName()
  157. {
  158. return new LocEdString("Scene");
  159. }
  160. private void OnInitialize()
  161. {
  162. mainLayout = GUI.AddLayoutY();
  163. GUIContent viewIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.View),
  164. new LocEdString("View"));
  165. GUIContent moveIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Move),
  166. new LocEdString("Move"));
  167. GUIContent rotateIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Rotate),
  168. new LocEdString("Rotate"));
  169. GUIContent scaleIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Scale),
  170. new LocEdString("Scale"));
  171. GUIContent localIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Local),
  172. new LocEdString("Local"));
  173. GUIContent worldIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.World),
  174. new LocEdString("World"));
  175. GUIContent pivotIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Pivot),
  176. new LocEdString("Pivot"));
  177. GUIContent centerIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Center),
  178. new LocEdString("Center"));
  179. GUIContent moveSnapIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.MoveSnap),
  180. new LocEdString("Move snap"));
  181. GUIContent rotateSnapIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.RotateSnap),
  182. new LocEdString("Rotate snap"));
  183. GUIToggleGroup handlesTG = new GUIToggleGroup();
  184. viewButton = new GUIToggle(viewIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  185. moveButton = new GUIToggle(moveIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  186. rotateButton = new GUIToggle(rotateIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  187. scaleButton = new GUIToggle(scaleIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  188. GUIToggleGroup coordModeTG = new GUIToggleGroup();
  189. localCoordButton = new GUIToggle(localIcon, coordModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(75));
  190. worldCoordButton = new GUIToggle(worldIcon, coordModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(75));
  191. GUIToggleGroup pivotModeTG = new GUIToggleGroup();
  192. pivotButton = new GUIToggle(pivotIcon, pivotModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  193. centerButton = new GUIToggle(centerIcon, pivotModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  194. moveSnapButton = new GUIToggle(moveSnapIcon, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  195. moveSnapInput = new GUIFloatField("", GUIOption.FlexibleWidth(35));
  196. rotateSnapButton = new GUIToggle(rotateSnapIcon, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  197. rotateSnapInput = new GUIFloatField("", GUIOption.FlexibleWidth(35));
  198. viewButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.View);
  199. moveButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Move);
  200. rotateButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Rotate);
  201. scaleButton.OnClick += () => OnSceneToolButtonClicked(SceneViewTool.Scale);
  202. localCoordButton.OnClick += () => OnCoordinateModeButtonClicked(HandleCoordinateMode.Local);
  203. worldCoordButton.OnClick += () => OnCoordinateModeButtonClicked(HandleCoordinateMode.World);
  204. pivotButton.OnClick += () => OnPivotModeButtonClicked(HandlePivotMode.Pivot);
  205. centerButton.OnClick += () => OnPivotModeButtonClicked(HandlePivotMode.Center);
  206. moveSnapButton.OnToggled += (bool active) => OnMoveSnapToggled(active);
  207. moveSnapInput.OnChanged += (float value) => OnMoveSnapValueChanged(value);
  208. rotateSnapButton.OnToggled += (bool active) => OnRotateSnapToggled(active);
  209. rotateSnapInput.OnChanged += (float value) => OnRotateSnapValueChanged(value);
  210. GUILayout handlesLayout = mainLayout.AddLayoutX();
  211. handlesLayout.AddElement(viewButton);
  212. handlesLayout.AddElement(moveButton);
  213. handlesLayout.AddElement(rotateButton);
  214. handlesLayout.AddElement(scaleButton);
  215. handlesLayout.AddSpace(10);
  216. handlesLayout.AddElement(localCoordButton);
  217. handlesLayout.AddElement(worldCoordButton);
  218. handlesLayout.AddSpace(10);
  219. handlesLayout.AddElement(pivotButton);
  220. handlesLayout.AddElement(centerButton);
  221. handlesLayout.AddFlexibleSpace();
  222. handlesLayout.AddElement(moveSnapButton);
  223. handlesLayout.AddElement(moveSnapInput);
  224. handlesLayout.AddSpace(10);
  225. handlesLayout.AddElement(rotateSnapButton);
  226. handlesLayout.AddElement(rotateSnapInput);
  227. GUIPanel mainPanel = mainLayout.AddPanel();
  228. rtPanel = mainPanel.AddPanel();
  229. selectionPanel = mainPanel.AddPanel(-1);
  230. GUIPanel sceneAxesPanel = mainPanel.AddPanel(-1);
  231. sceneAxesGUI = new SceneAxesGUI(this, sceneAxesPanel, HandleAxesGUISize, HandleAxesGUISize, ProjectionType.Perspective);
  232. focusCatcher = new GUIButton("", EditorStyles.Blank);
  233. focusCatcher.OnFocusGained += () => hasContentFocus = true;
  234. focusCatcher.OnFocusLost += () => hasContentFocus = false;
  235. GUIPanel focusPanel = GUI.AddPanel(-2);
  236. focusPanel.AddElement(focusCatcher);
  237. toggleProfilerOverlayKey = new VirtualButton(ToggleProfilerOverlayBinding);
  238. viewToolKey = new VirtualButton(ViewToolBinding);
  239. moveToolKey = new VirtualButton(MoveToolBinding);
  240. rotateToolKey = new VirtualButton(RotateToolBinding);
  241. scaleToolKey = new VirtualButton(ScaleToolBinding);
  242. frameKey = new VirtualButton(FrameBinding);
  243. UpdateRenderTexture(Width, Height - HeaderHeight);
  244. UpdateProfilerOverlay();
  245. }
  246. private void OnDestroy()
  247. {
  248. if (camera != null)
  249. {
  250. camera.SceneObject.Destroy(true);
  251. camera = null;
  252. }
  253. sceneAxesGUI.Destroy();
  254. sceneAxesGUI = null;
  255. }
  256. /// <summary>
  257. /// Deletes all currently selected objects.
  258. /// </summary>
  259. private void DeleteSelection()
  260. {
  261. SceneObject[] selectedObjects = Selection.SceneObjects;
  262. CleanDuplicates(ref selectedObjects);
  263. if (selectedObjects.Length > 0)
  264. {
  265. foreach (var so in selectedObjects)
  266. {
  267. string message = "Deleted " + so.Name;
  268. UndoRedo.DeleteSO(so, message);
  269. }
  270. EditorApplication.SetSceneDirty();
  271. }
  272. }
  273. /// <summary>
  274. /// Duplicates all currently selected objects.
  275. /// </summary>
  276. private void DuplicateSelection()
  277. {
  278. SceneObject[] selectedObjects = Selection.SceneObjects;
  279. CleanDuplicates(ref selectedObjects);
  280. if (selectedObjects.Length > 0)
  281. {
  282. string message;
  283. if (selectedObjects.Length == 1)
  284. message = "Duplicated " + selectedObjects[0].Name;
  285. else
  286. message = "Duplicated " + selectedObjects.Length + " elements";
  287. Transform[] savedTransforms = new Transform[selectedObjects.Length];
  288. for (int i = 0; i < savedTransforms.Length; i++)
  289. savedTransforms[i] = new Transform(selectedObjects[i]);
  290. SceneObject[] clonedObjects = UndoRedo.CloneSO(selectedObjects, message);
  291. // Restore world positions
  292. for (int i = 0; i < savedTransforms.Length; i++)
  293. savedTransforms[i].Apply(clonedObjects[i]);
  294. EditorApplication.SetSceneDirty();
  295. }
  296. }
  297. /// <inheritdoc/>
  298. void IGlobalShortcuts.OnRenamePressed()
  299. {
  300. // Do nothing
  301. }
  302. /// <inheritdoc/>
  303. void IGlobalShortcuts.OnDuplicatePressed()
  304. {
  305. DuplicateSelection();
  306. }
  307. /// <inheritdoc/>
  308. void IGlobalShortcuts.OnDeletePressed()
  309. {
  310. DeleteSelection();
  311. }
  312. /// <inheritdoc/>
  313. void IGlobalShortcuts.OnCopyPressed()
  314. {
  315. // Do nothing
  316. }
  317. /// <inheritdoc/>
  318. void IGlobalShortcuts.OnCutPressed()
  319. {
  320. // Do nothing
  321. }
  322. /// <inheritdoc/>
  323. void IGlobalShortcuts.OnPastePressed()
  324. {
  325. // Do nothing
  326. }
  327. /// <summary>
  328. /// Orients the camera so it looks along the provided axis.
  329. /// </summary>
  330. /// <param name="axis">Axis to look along.</param>
  331. internal void LookAlong(Vector3 axis)
  332. {
  333. axis.Normalize();
  334. cameraController.LookAlong(axis);
  335. UpdateGridMode();
  336. }
  337. private void UpdateGridMode()
  338. {
  339. Vector3 forward = camera.SceneObject.Forward;
  340. if (camera.ProjectionType == ProjectionType.Perspective)
  341. sceneGrid.SetMode(GridMode.Perspective);
  342. else
  343. {
  344. float dotX = Vector3.Dot(forward, Vector3.XAxis);
  345. if (dotX >= 0.95f)
  346. sceneGrid.SetMode(GridMode.OrthoX);
  347. else if (dotX <= -0.95f)
  348. sceneGrid.SetMode(GridMode.OrthoNegX);
  349. else
  350. {
  351. float dotY = Vector3.Dot(forward, Vector3.YAxis);
  352. if (dotY >= 0.95f)
  353. sceneGrid.SetMode(GridMode.OrthoY);
  354. else if (dotY <= -0.95f)
  355. sceneGrid.SetMode(GridMode.OrthoNegY);
  356. else
  357. {
  358. float dotZ = Vector3.Dot(forward, Vector3.ZAxis);
  359. if (dotZ >= 0.95f)
  360. sceneGrid.SetMode(GridMode.OrthoZ);
  361. else if (dotZ <= -0.95f)
  362. sceneGrid.SetMode(GridMode.OrthoNegZ);
  363. else
  364. sceneGrid.SetMode(GridMode.Perspective);
  365. }
  366. }
  367. }
  368. }
  369. /// <summary>
  370. /// Converts screen coordinates into coordinates relative to the scene view render texture.
  371. /// </summary>
  372. /// <param name="screenPos">Coordinates relative to the screen.</param>
  373. /// <param name="scenePos">Output coordinates relative to the scene view texture.</param>
  374. /// <returns>True if the coordinates are within the scene view texture, false otherwise.</returns>
  375. private bool ScreenToScenePos(Vector2I screenPos, out Vector2I scenePos)
  376. {
  377. scenePos = screenPos;
  378. Vector2I windowPos = ScreenToWindowPos(screenPos);
  379. Rect2I bounds = GUIUtility.CalculateBounds(renderTextureGUI, GUI);
  380. if (bounds.Contains(windowPos))
  381. {
  382. scenePos.x = windowPos.x - bounds.x;
  383. scenePos.y = windowPos.y - bounds.y;
  384. return true;
  385. }
  386. return false;
  387. }
  388. private void OnEditorUpdate()
  389. {
  390. if (HasFocus)
  391. {
  392. if (!Input.IsPointerButtonHeld(PointerButton.Right))
  393. {
  394. if (VirtualInput.IsButtonDown(EditorApplication.DuplicateKey))
  395. DuplicateSelection();
  396. else if (VirtualInput.IsButtonDown(EditorApplication.DeleteKey))
  397. DeleteSelection();
  398. else if (VirtualInput.IsButtonDown(toggleProfilerOverlayKey))
  399. EditorSettings.SetBool(ProfilerOverlayActiveKey, !EditorSettings.GetBool(ProfilerOverlayActiveKey));
  400. else if(VirtualInput.IsButtonDown(viewToolKey))
  401. EditorApplication.ActiveSceneTool = SceneViewTool.View;
  402. else if(VirtualInput.IsButtonDown(moveToolKey))
  403. EditorApplication.ActiveSceneTool = SceneViewTool.Move;
  404. else if(VirtualInput.IsButtonDown(rotateToolKey))
  405. EditorApplication.ActiveSceneTool = SceneViewTool.Rotate;
  406. else if(VirtualInput.IsButtonDown(scaleToolKey))
  407. EditorApplication.ActiveSceneTool = SceneViewTool.Scale;
  408. }
  409. }
  410. // Refresh GUI buttons if needed (in case someones changes the values from script)
  411. if (editorSettingsHash != EditorSettings.Hash)
  412. {
  413. UpdateButtonStates();
  414. UpdateProfilerOverlay();
  415. editorSettingsHash = EditorSettings.Hash;
  416. }
  417. // Update scene view handles and selection
  418. sceneGrid.Draw();
  419. bool handleActive = sceneHandles.IsActive() || sceneAxesGUI.IsActive();
  420. Vector2I scenePos;
  421. bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);
  422. bool dragResult = false;
  423. if (Input.IsPointerButtonUp(PointerButton.Left))
  424. {
  425. dragResult = EndDragSelection();
  426. if (sceneHandles.IsActive())
  427. sceneHandles.ClearSelection();
  428. if (sceneAxesGUI.IsActive())
  429. sceneAxesGUI.ClearSelection();
  430. }
  431. else if (Input.IsPointerButtonDown(PointerButton.Left))
  432. {
  433. mouseDownPosition = scenePos;
  434. }
  435. bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;
  436. draggedOver &= IsPointerHovering && inBounds && DragDrop.Type == DragDropType.Resource;
  437. if (draggedOver)
  438. {
  439. if (DragDrop.DropInProgress)
  440. {
  441. dragActive = false;
  442. if (draggedSO != null)
  443. {
  444. Selection.SceneObject = draggedSO;
  445. EditorApplication.SetSceneDirty();
  446. }
  447. draggedSO = null;
  448. }
  449. else
  450. {
  451. if (!dragActive)
  452. {
  453. dragActive = true;
  454. ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;
  455. string[] draggedPaths = dragData.Paths;
  456. for (int i = 0; i < draggedPaths.Length; i++)
  457. {
  458. ResourceMeta meta = ProjectLibrary.GetMeta(draggedPaths[i]);
  459. if (meta != null)
  460. {
  461. if (meta.ResType == ResourceType.Mesh)
  462. {
  463. if (!string.IsNullOrEmpty(draggedPaths[i]))
  464. {
  465. string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]);
  466. draggedSO = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
  467. Mesh mesh = ProjectLibrary.Load<Mesh>(draggedPaths[i]);
  468. Renderable renderable = draggedSO.AddComponent<Renderable>();
  469. renderable.Mesh = mesh;
  470. if (mesh != null)
  471. draggedSOOffset = mesh.Bounds.Box.Center;
  472. else
  473. draggedSOOffset = Vector3.Zero;
  474. }
  475. break;
  476. }
  477. else if (meta.ResType == ResourceType.Prefab)
  478. {
  479. if (!string.IsNullOrEmpty(draggedPaths[i]))
  480. {
  481. Prefab prefab = ProjectLibrary.Load<Prefab>(draggedPaths[i]);
  482. draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);
  483. if (draggedSO != null)
  484. {
  485. AABox draggedObjBounds = EditorUtility.CalculateBounds(draggedSO);
  486. draggedSOOffset = draggedObjBounds.Center;
  487. }
  488. else
  489. draggedSOOffset = Vector3.Zero;
  490. }
  491. break;
  492. }
  493. }
  494. }
  495. }
  496. if (draggedSO != null)
  497. {
  498. if (Input.IsButtonHeld(ButtonCode.Space))
  499. {
  500. SnapData snapData;
  501. sceneSelection.Snap(scenePos, out snapData, new SceneObject[] {draggedSO});
  502. Quaternion q = Quaternion.FromToRotation(Vector3.YAxis, snapData.normal);
  503. draggedSO.Position = snapData.position;
  504. draggedSO.Rotation = q;
  505. }
  506. else
  507. {
  508. Ray worldRay = camera.ScreenPointToRay(scenePos);
  509. draggedSO.Position = worldRay * DefaultPlacementDepth - draggedSOOffset;
  510. }
  511. }
  512. }
  513. return;
  514. }
  515. else
  516. {
  517. if (dragActive)
  518. {
  519. dragActive = false;
  520. if (draggedSO != null)
  521. {
  522. draggedSO.Destroy();
  523. draggedSO = null;
  524. }
  525. }
  526. }
  527. if (HasContentFocus || IsPointerHovering)
  528. {
  529. cameraController.EnableInput(true);
  530. if (inBounds && HasContentFocus)
  531. {
  532. if (Input.IsPointerButtonDown(PointerButton.Left))
  533. {
  534. Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPaddingX,
  535. HandleAxesGUIPaddingY, HandleAxesGUISize, HandleAxesGUISize);
  536. if (sceneAxesGUIBounds.Contains(scenePos))
  537. sceneAxesGUI.TrySelect(scenePos);
  538. else
  539. sceneHandles.TrySelect(scenePos);
  540. }
  541. else if (Input.IsPointerButtonHeld(PointerButton.Left) && !handleActive && !dragActive &&
  542. draggedSO == null && scenePos != mouseDownPosition)
  543. {
  544. if (isDraggingSelection)
  545. UpdateDragSelection(scenePos);
  546. else
  547. StartDragSelection(scenePos);
  548. }
  549. else if (Input.IsPointerButtonUp(PointerButton.Left))
  550. {
  551. if (!handleActive && !dragActive && !dragResult)
  552. {
  553. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
  554. Input.IsButtonHeld(ButtonCode.RightControl);
  555. sceneSelection.PickObject(scenePos, ctrlHeld, new SceneObject[] {draggedSO});
  556. }
  557. }
  558. }
  559. }
  560. else
  561. cameraController.EnableInput(false);
  562. SceneHandles.BeginInput();
  563. sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
  564. sceneAxesGUI.UpdateInput(scenePos);
  565. SceneHandles.EndInput();
  566. sceneHandles.Draw();
  567. sceneAxesGUI.Draw();
  568. // Must be done after handle input is processed, in order to reflect most recent transform
  569. sceneGizmos.Draw();
  570. sceneSelection.Draw();
  571. UpdateGridMode();
  572. if (VirtualInput.IsButtonDown(frameKey))
  573. cameraController.FrameSelected();
  574. }
  575. /// <inheritdoc/>
  576. protected override void WindowResized(int width, int height)
  577. {
  578. UpdateRenderTexture(width, height - HeaderHeight);
  579. base.WindowResized(width, height);
  580. }
  581. /// <inheritdoc/>
  582. protected override void FocusChanged(bool inFocus)
  583. {
  584. if (!inFocus)
  585. {
  586. sceneHandles.ClearSelection();
  587. }
  588. }
  589. /// <summary>
  590. /// Triggered when one of the scene tool buttons is clicked, changing the active scene handle.
  591. /// </summary>
  592. /// <param name="tool">Clicked scene tool to activate.</param>
  593. private void OnSceneToolButtonClicked(SceneViewTool tool)
  594. {
  595. EditorApplication.ActiveSceneTool = tool;
  596. editorSettingsHash = EditorSettings.Hash;
  597. }
  598. /// <summary>
  599. /// Triggered when one of the coordinate mode buttons is clicked, changing the active coordinate mode.
  600. /// </summary>
  601. /// <param name="mode">Clicked coordinate mode to activate.</param>
  602. private void OnCoordinateModeButtonClicked(HandleCoordinateMode mode)
  603. {
  604. EditorApplication.ActiveCoordinateMode = mode;
  605. editorSettingsHash = EditorSettings.Hash;
  606. }
  607. /// <summary>
  608. /// Triggered when one of the pivot buttons is clicked, changing the active pivot mode.
  609. /// </summary>
  610. /// <param name="mode">Clicked pivot mode to activate.</param>
  611. private void OnPivotModeButtonClicked(HandlePivotMode mode)
  612. {
  613. EditorApplication.ActivePivotMode = mode;
  614. editorSettingsHash = EditorSettings.Hash;
  615. }
  616. /// <summary>
  617. /// Triggered when the move snap button is toggled.
  618. /// </summary>
  619. /// <param name="active">Determins should be move snap be activated or deactivated.</param>
  620. private void OnMoveSnapToggled(bool active)
  621. {
  622. Handles.MoveHandleSnapActive = active;
  623. editorSettingsHash = EditorSettings.Hash;
  624. }
  625. /// <summary>
  626. /// Triggered when the move snap increment value changes.
  627. /// </summary>
  628. /// <param name="value">Value that determines in what increments to perform move snapping.</param>
  629. private void OnMoveSnapValueChanged(float value)
  630. {
  631. Handles.MoveSnapAmount = MathEx.Clamp(value, 0.01f, 1000.0f);
  632. editorSettingsHash = EditorSettings.Hash;
  633. }
  634. /// <summary>
  635. /// Triggered when the rotate snap button is toggled.
  636. /// </summary>
  637. /// <param name="active">Determins should be rotate snap be activated or deactivated.</param>
  638. private void OnRotateSnapToggled(bool active)
  639. {
  640. Handles.RotateHandleSnapActive = active;
  641. editorSettingsHash = EditorSettings.Hash;
  642. }
  643. /// <summary>
  644. /// Triggered when the rotate snap increment value changes.
  645. /// </summary>
  646. /// <param name="value">Value that determines in what increments to perform rotate snapping.</param>
  647. private void OnRotateSnapValueChanged(float value)
  648. {
  649. Handles.RotateSnapAmount = (Degree)MathEx.Clamp(value, 0.01f, 360.0f);
  650. editorSettingsHash = EditorSettings.Hash;
  651. }
  652. /// <summary>
  653. /// Updates toggle button states according to current editor options. This is useful if tools, coordinate mode,
  654. /// pivot or other scene view options have been modified externally.
  655. /// </summary>
  656. private void UpdateButtonStates()
  657. {
  658. switch (EditorApplication.ActiveSceneTool)
  659. {
  660. case SceneViewTool.View:
  661. viewButton.Value = true;
  662. break;
  663. case SceneViewTool.Move:
  664. moveButton.Value = true;
  665. break;
  666. case SceneViewTool.Rotate:
  667. rotateButton.Value = true;
  668. break;
  669. case SceneViewTool.Scale:
  670. scaleButton.Value = true;
  671. break;
  672. }
  673. switch (EditorApplication.ActiveCoordinateMode)
  674. {
  675. case HandleCoordinateMode.Local:
  676. localCoordButton.Value = true;
  677. break;
  678. case HandleCoordinateMode.World:
  679. worldCoordButton.Value = true;
  680. break;
  681. }
  682. switch (EditorApplication.ActivePivotMode)
  683. {
  684. case HandlePivotMode.Center:
  685. centerButton.Value = true;
  686. break;
  687. case HandlePivotMode.Pivot:
  688. pivotButton.Value = true;
  689. break;
  690. }
  691. if (Handles.MoveHandleSnapActive)
  692. moveSnapButton.Value = true;
  693. else
  694. moveSnapButton.Value = false;
  695. moveSnapInput.Value = Handles.MoveSnapAmount;
  696. if (Handles.RotateHandleSnapActive)
  697. rotateSnapButton.Value = true;
  698. else
  699. rotateSnapButton.Value = false;
  700. moveSnapInput.Value = Handles.RotateSnapAmount.Degrees;
  701. }
  702. /// <summary>
  703. /// Activates or deactivates the profiler overlay according to current editor settings.
  704. /// </summary>
  705. private void UpdateProfilerOverlay()
  706. {
  707. if (EditorSettings.GetBool(ProfilerOverlayActiveKey))
  708. {
  709. if (activeProfilerOverlay == null)
  710. {
  711. SceneObject profilerSO = new SceneObject("EditorProfilerOverlay");
  712. profilerCamera = profilerSO.AddComponent<Camera>();
  713. profilerCamera.Viewport.Target = renderTexture;
  714. profilerCamera.Viewport.ClearFlags = ClearFlags.Empty;
  715. profilerCamera.Priority = 1;
  716. profilerCamera.Layers = 0;
  717. profilerCamera.RenderSettings.EnableHDR = false;
  718. activeProfilerOverlay = profilerSO.AddComponent<ProfilerOverlay>();
  719. }
  720. }
  721. else
  722. {
  723. if (activeProfilerOverlay != null)
  724. {
  725. activeProfilerOverlay.SceneObject.Destroy();
  726. activeProfilerOverlay = null;
  727. profilerCamera = null;
  728. }
  729. }
  730. }
  731. /// <summary>
  732. /// Creates the scene camera and updates the render texture. Should be called at least once before using the
  733. /// scene view. Should be called whenever the window is resized.
  734. /// </summary>
  735. /// <param name="width">Width of the scene render target, in pixels.</param>
  736. /// <param name="height">Height of the scene render target, in pixels.</param>
  737. private void UpdateRenderTexture(int width, int height)
  738. {
  739. width = MathEx.Max(20, width);
  740. height = MathEx.Max(20, height);
  741. // Note: Depth buffer and readable flags are required because ScenePicking uses it
  742. Texture colorTex = Texture.Create2D((uint)width, (uint)height, PixelFormat.RGBA8, TextureUsage.Render | TextureUsage.CPUReadable);
  743. Texture depthTex = Texture.Create2D((uint)width, (uint)height, PixelFormat.D32_S8X24, TextureUsage.DepthStencil | TextureUsage.CPUReadable);
  744. renderTexture = new RenderTexture(colorTex, depthTex);
  745. renderTexture.Priority = 1;
  746. if (camera == null)
  747. {
  748. SceneObject sceneCameraSO = new SceneObject("SceneCamera", true);
  749. camera = sceneCameraSO.AddComponent<Camera>();
  750. camera.Viewport.Target = renderTexture;
  751. camera.Viewport.Area = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  752. sceneCameraSO.Position = new Vector3(0, 0.5f, 1);
  753. sceneCameraSO.LookAt(new Vector3(0, 0.5f, 0));
  754. camera.Priority = 2;
  755. camera.NearClipPlane = 0.05f;
  756. camera.FarClipPlane = 2500.0f;
  757. camera.Viewport.ClearColor = ClearColor;
  758. camera.Layers = UInt64.MaxValue & ~SceneAxesHandle.LAYER; // Don't draw scene axes in this camera
  759. cameraController = sceneCameraSO.AddComponent<SceneCamera>();
  760. renderTextureGUI = new GUIRenderTexture(renderTexture);
  761. rtPanel.AddElement(renderTextureGUI);
  762. sceneGrid = new SceneGrid(camera);
  763. sceneSelection = new SceneSelection(camera);
  764. sceneGizmos = new SceneGizmos(camera);
  765. sceneHandles = new SceneHandles(this, camera);
  766. }
  767. else
  768. {
  769. camera.Viewport.Target = renderTexture;
  770. renderTextureGUI.RenderTexture = renderTexture;
  771. }
  772. Rect2I rtBounds = new Rect2I(0, 0, width, height);
  773. renderTextureGUI.Bounds = rtBounds;
  774. focusCatcher.Bounds = GUIUtility.CalculateBounds(rtPanel, GUI);
  775. sceneAxesGUI.SetPosition(width - HandleAxesGUISize - HandleAxesGUIPaddingX, HandleAxesGUIPaddingY);
  776. // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
  777. // render target destroy/create cycle for every single pixel.
  778. camera.AspectRatio = width / (float)height;
  779. if (profilerCamera != null)
  780. profilerCamera.Viewport.Target = renderTexture;
  781. }
  782. /// <summary>
  783. /// Parses an array of scene objects and removes elements that are children of elements that are also in the array.
  784. /// </summary>
  785. /// <param name="objects">Array containing duplicate objects as input, and array without duplicate objects as
  786. /// output.</param>
  787. private void CleanDuplicates(ref SceneObject[] objects)
  788. {
  789. List<SceneObject> cleanList = new List<SceneObject>();
  790. for (int i = 0; i < objects.Length; i++)
  791. {
  792. bool foundParent = false;
  793. for (int j = 0; j < objects.Length; j++)
  794. {
  795. SceneObject elem = objects[i];
  796. while (elem != null && elem != objects[j])
  797. elem = elem.Parent;
  798. bool isChildOf = elem == objects[j];
  799. if (i != j && isChildOf)
  800. {
  801. foundParent = true;
  802. break;
  803. }
  804. }
  805. if (!foundParent)
  806. cleanList.Add(objects[i]);
  807. }
  808. objects = cleanList.ToArray();
  809. }
  810. /// <summary>
  811. /// Starts a drag operation that displays a selection outline allowing the user to select multiple entries at once.
  812. /// </summary>
  813. /// <param name="scenePos">Coordinates relative to the scene where the drag originated.</param>
  814. private void StartDragSelection(Vector2I scenePos)
  815. {
  816. isDraggingSelection = true;
  817. dragSelectionStart = scenePos;
  818. dragSelectionEnd = dragSelectionStart;
  819. }
  820. /// <summary>
  821. /// Updates a selection outline drag operation by expanding the outline to the new location. Elements in the outline
  822. /// are selected.
  823. /// </summary>
  824. /// <param name="scenePos">Coordinates of the pointer relative to the scene.</param>
  825. /// <returns>True if the selection outline drag is valid and was updated, false otherwise.</returns>
  826. private bool UpdateDragSelection(Vector2I scenePos)
  827. {
  828. if (!isDraggingSelection)
  829. return false;
  830. if (dragSelection == null)
  831. {
  832. dragSelection = new GUITexture(null, true, EditorStylesInternal.SelectionArea);
  833. selectionPanel.AddElement(dragSelection);
  834. }
  835. dragSelectionEnd = scenePos;
  836. Rect2I selectionArea = new Rect2I();
  837. Vector2I min = new Vector2I(Math.Min(dragSelectionStart.x, dragSelectionEnd.x), Math.Min(dragSelectionStart.y, dragSelectionEnd.y));
  838. Vector2I max = new Vector2I(Math.Max(dragSelectionStart.x, dragSelectionEnd.x), Math.Max(dragSelectionStart.y, dragSelectionEnd.y));
  839. selectionArea.x = min.x;
  840. selectionArea.y = min.y;
  841. selectionArea.width = Math.Max(max.x - min.x, 1);
  842. selectionArea.height = Math.Max(max.y - min.y, 1);
  843. dragSelection.Bounds = selectionArea;
  844. return true;
  845. }
  846. /// <summary>
  847. /// Ends the selection outline drag operation. Elements in the outline are selected.
  848. /// </summary>
  849. /// <returns>True if the selection outline drag is valid and was ended, false otherwise.</returns>
  850. private bool EndDragSelection()
  851. {
  852. if (!isDraggingSelection)
  853. return false;
  854. if (dragSelection != null)
  855. {
  856. dragSelection.Destroy();
  857. dragSelection = null;
  858. }
  859. if ((dragSelectionEnd - dragSelectionStart).Length < 1)
  860. {
  861. isDraggingSelection = false;
  862. return false;
  863. }
  864. Vector2I min = new Vector2I(Math.Min(dragSelectionStart.x, dragSelectionEnd.x),
  865. Math.Min(dragSelectionStart.y, dragSelectionEnd.y));
  866. Vector2I max = new Vector2I(Math.Max(dragSelectionStart.x, dragSelectionEnd.x),
  867. Math.Max(dragSelectionStart.y, dragSelectionEnd.y));
  868. sceneSelection.PickObjects(min, max - min,
  869. Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl));
  870. isDraggingSelection = false;
  871. return true;
  872. }
  873. /// <summary>
  874. /// Contains information about world transform of a single scene object.
  875. /// </summary>
  876. struct Transform
  877. {
  878. public Transform(SceneObject so)
  879. {
  880. position = so.Position;
  881. rotation = so.Rotation;
  882. scale = so.Scale;
  883. }
  884. /// <summary>
  885. /// Applies the saved transform to the specified scene object. The transform is assumed to be in world space.
  886. /// </summary>
  887. /// <param name="so">Scene object to apply the transform to.</param>
  888. public void Apply(SceneObject so)
  889. {
  890. so.Position = position;
  891. so.Rotation = rotation;
  892. so.LocalScale = scale;
  893. }
  894. public Vector3 position;
  895. public Quaternion rotation;
  896. public Vector3 scale;
  897. }
  898. }
  899. /** @} */
  900. }