SceneWindow.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. UndoRedo.CloneSO(selectedObjects, message);
  288. EditorApplication.SetSceneDirty();
  289. }
  290. }
  291. /// <inheritdoc/>
  292. void IGlobalShortcuts.OnRenamePressed()
  293. {
  294. // Do nothing
  295. }
  296. /// <inheritdoc/>
  297. void IGlobalShortcuts.OnDuplicatePressed()
  298. {
  299. DuplicateSelection();
  300. }
  301. /// <inheritdoc/>
  302. void IGlobalShortcuts.OnDeletePressed()
  303. {
  304. DeleteSelection();
  305. }
  306. /// <inheritdoc/>
  307. void IGlobalShortcuts.OnCopyPressed()
  308. {
  309. // Do nothing
  310. }
  311. /// <inheritdoc/>
  312. void IGlobalShortcuts.OnCutPressed()
  313. {
  314. // Do nothing
  315. }
  316. /// <inheritdoc/>
  317. void IGlobalShortcuts.OnPastePressed()
  318. {
  319. // Do nothing
  320. }
  321. /// <summary>
  322. /// Orients the camera so it looks along the provided axis.
  323. /// </summary>
  324. /// <param name="axis">Axis to look along.</param>
  325. internal void LookAlong(Vector3 axis)
  326. {
  327. axis.Normalize();
  328. cameraController.LookAlong(axis);
  329. UpdateGridMode();
  330. }
  331. private void UpdateGridMode()
  332. {
  333. Vector3 forward = camera.SceneObject.Forward;
  334. if (camera.ProjectionType == ProjectionType.Perspective)
  335. sceneGrid.SetMode(GridMode.Perspective);
  336. else
  337. {
  338. float dotX = Vector3.Dot(forward, Vector3.XAxis);
  339. if (dotX >= 0.95f)
  340. sceneGrid.SetMode(GridMode.OrthoX);
  341. else if (dotX <= -0.95f)
  342. sceneGrid.SetMode(GridMode.OrthoNegX);
  343. else
  344. {
  345. float dotY = Vector3.Dot(forward, Vector3.YAxis);
  346. if (dotY >= 0.95f)
  347. sceneGrid.SetMode(GridMode.OrthoY);
  348. else if (dotY <= -0.95f)
  349. sceneGrid.SetMode(GridMode.OrthoNegY);
  350. else
  351. {
  352. float dotZ = Vector3.Dot(forward, Vector3.ZAxis);
  353. if (dotZ >= 0.95f)
  354. sceneGrid.SetMode(GridMode.OrthoZ);
  355. else if (dotZ <= -0.95f)
  356. sceneGrid.SetMode(GridMode.OrthoNegZ);
  357. else
  358. sceneGrid.SetMode(GridMode.Perspective);
  359. }
  360. }
  361. }
  362. }
  363. /// <summary>
  364. /// Converts screen coordinates into coordinates relative to the scene view render texture.
  365. /// </summary>
  366. /// <param name="screenPos">Coordinates relative to the screen.</param>
  367. /// <param name="scenePos">Output coordinates relative to the scene view texture.</param>
  368. /// <returns>True if the coordinates are within the scene view texture, false otherwise.</returns>
  369. private bool ScreenToScenePos(Vector2I screenPos, out Vector2I scenePos)
  370. {
  371. scenePos = screenPos;
  372. Vector2I windowPos = ScreenToWindowPos(screenPos);
  373. Rect2I bounds = GUIUtility.CalculateBounds(renderTextureGUI, GUI);
  374. if (bounds.Contains(windowPos))
  375. {
  376. scenePos.x = windowPos.x - bounds.x;
  377. scenePos.y = windowPos.y - bounds.y;
  378. return true;
  379. }
  380. return false;
  381. }
  382. private void OnEditorUpdate()
  383. {
  384. if (HasFocus)
  385. {
  386. if (!Input.IsPointerButtonHeld(PointerButton.Right))
  387. {
  388. if (VirtualInput.IsButtonDown(EditorApplication.DuplicateKey))
  389. DuplicateSelection();
  390. else if (VirtualInput.IsButtonDown(EditorApplication.DeleteKey))
  391. DeleteSelection();
  392. else if (VirtualInput.IsButtonDown(toggleProfilerOverlayKey))
  393. EditorSettings.SetBool(ProfilerOverlayActiveKey, !EditorSettings.GetBool(ProfilerOverlayActiveKey));
  394. else if(VirtualInput.IsButtonDown(viewToolKey))
  395. EditorApplication.ActiveSceneTool = SceneViewTool.View;
  396. else if(VirtualInput.IsButtonDown(moveToolKey))
  397. EditorApplication.ActiveSceneTool = SceneViewTool.Move;
  398. else if(VirtualInput.IsButtonDown(rotateToolKey))
  399. EditorApplication.ActiveSceneTool = SceneViewTool.Rotate;
  400. else if(VirtualInput.IsButtonDown(scaleToolKey))
  401. EditorApplication.ActiveSceneTool = SceneViewTool.Scale;
  402. }
  403. }
  404. // Refresh GUI buttons if needed (in case someones changes the values from script)
  405. if (editorSettingsHash != EditorSettings.Hash)
  406. {
  407. UpdateButtonStates();
  408. UpdateProfilerOverlay();
  409. editorSettingsHash = EditorSettings.Hash;
  410. }
  411. // Update scene view handles and selection
  412. sceneGizmos.Draw();
  413. sceneGrid.Draw();
  414. bool handleActive = sceneHandles.IsActive() || sceneAxesGUI.IsActive();
  415. Vector2I scenePos;
  416. bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);
  417. bool dragResult = false;
  418. if (Input.IsPointerButtonUp(PointerButton.Left))
  419. {
  420. dragResult = EndDragSelection();
  421. if (sceneHandles.IsActive())
  422. sceneHandles.ClearSelection();
  423. if (sceneAxesGUI.IsActive())
  424. sceneAxesGUI.ClearSelection();
  425. }
  426. else if (Input.IsPointerButtonDown(PointerButton.Left))
  427. {
  428. mouseDownPosition = scenePos;
  429. }
  430. bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;
  431. draggedOver &= IsPointerHovering && inBounds && DragDrop.Type == DragDropType.Resource;
  432. if (draggedOver)
  433. {
  434. if (DragDrop.DropInProgress)
  435. {
  436. dragActive = false;
  437. if (draggedSO != null)
  438. {
  439. Selection.SceneObject = draggedSO;
  440. EditorApplication.SetSceneDirty();
  441. }
  442. draggedSO = null;
  443. }
  444. else
  445. {
  446. if (!dragActive)
  447. {
  448. dragActive = true;
  449. ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;
  450. string[] draggedPaths = dragData.Paths;
  451. for (int i = 0; i < draggedPaths.Length; i++)
  452. {
  453. ResourceMeta meta = ProjectLibrary.GetMeta(draggedPaths[i]);
  454. if (meta != null)
  455. {
  456. if (meta.ResType == ResourceType.Mesh)
  457. {
  458. if (!string.IsNullOrEmpty(draggedPaths[i]))
  459. {
  460. string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]);
  461. draggedSO = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
  462. Mesh mesh = ProjectLibrary.Load<Mesh>(draggedPaths[i]);
  463. Renderable renderable = draggedSO.AddComponent<Renderable>();
  464. renderable.Mesh = mesh;
  465. if (mesh != null)
  466. draggedSOOffset = mesh.Bounds.Box.Center;
  467. else
  468. draggedSOOffset = Vector3.Zero;
  469. }
  470. break;
  471. }
  472. else if (meta.ResType == ResourceType.Prefab)
  473. {
  474. if (!string.IsNullOrEmpty(draggedPaths[i]))
  475. {
  476. Prefab prefab = ProjectLibrary.Load<Prefab>(draggedPaths[i]);
  477. draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);
  478. if (draggedSO != null)
  479. {
  480. AABox draggedObjBounds = EditorUtility.CalculateBounds(draggedSO);
  481. draggedSOOffset = draggedObjBounds.Center;
  482. }
  483. else
  484. draggedSOOffset = Vector3.Zero;
  485. }
  486. break;
  487. }
  488. }
  489. }
  490. }
  491. if (draggedSO != null)
  492. {
  493. if (Input.IsButtonHeld(ButtonCode.Space))
  494. {
  495. SnapData snapData;
  496. sceneSelection.Snap(scenePos, out snapData, new SceneObject[] {draggedSO});
  497. Quaternion q = Quaternion.FromToRotation(Vector3.YAxis, snapData.normal);
  498. draggedSO.Position = snapData.position;
  499. draggedSO.Rotation = q;
  500. }
  501. else
  502. {
  503. Ray worldRay = camera.ScreenPointToRay(scenePos);
  504. draggedSO.Position = worldRay * DefaultPlacementDepth - draggedSOOffset;
  505. }
  506. }
  507. }
  508. return;
  509. }
  510. else
  511. {
  512. if (dragActive)
  513. {
  514. dragActive = false;
  515. if (draggedSO != null)
  516. {
  517. draggedSO.Destroy();
  518. draggedSO = null;
  519. }
  520. }
  521. }
  522. if (HasContentFocus || IsPointerHovering)
  523. {
  524. cameraController.EnableInput(true);
  525. if (inBounds && HasContentFocus)
  526. {
  527. if (Input.IsPointerButtonDown(PointerButton.Left))
  528. {
  529. Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPaddingX,
  530. HandleAxesGUIPaddingY, HandleAxesGUISize, HandleAxesGUISize);
  531. if (sceneAxesGUIBounds.Contains(scenePos))
  532. sceneAxesGUI.TrySelect(scenePos);
  533. else
  534. sceneHandles.TrySelect(scenePos);
  535. }
  536. else if (Input.IsPointerButtonHeld(PointerButton.Left) && !handleActive && !dragActive &&
  537. draggedSO == null && scenePos != mouseDownPosition)
  538. {
  539. if (isDraggingSelection)
  540. UpdateDragSelection(scenePos);
  541. else
  542. StartDragSelection(scenePos);
  543. }
  544. else if (Input.IsPointerButtonUp(PointerButton.Left))
  545. {
  546. if (!handleActive && !dragActive && !dragResult)
  547. {
  548. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
  549. Input.IsButtonHeld(ButtonCode.RightControl);
  550. sceneSelection.PickObject(scenePos, ctrlHeld, new SceneObject[] {draggedSO});
  551. }
  552. }
  553. }
  554. }
  555. else
  556. cameraController.EnableInput(false);
  557. SceneHandles.BeginInput();
  558. sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
  559. sceneHandles.Draw();
  560. sceneAxesGUI.UpdateInput(scenePos);
  561. sceneAxesGUI.Draw();
  562. SceneHandles.EndInput();
  563. sceneSelection.Draw();
  564. UpdateGridMode();
  565. if (VirtualInput.IsButtonDown(frameKey))
  566. cameraController.FrameSelected();
  567. }
  568. /// <inheritdoc/>
  569. protected override void WindowResized(int width, int height)
  570. {
  571. UpdateRenderTexture(width, height - HeaderHeight);
  572. base.WindowResized(width, height);
  573. }
  574. /// <inheritdoc/>
  575. protected override void FocusChanged(bool inFocus)
  576. {
  577. if (!inFocus)
  578. {
  579. sceneHandles.ClearSelection();
  580. }
  581. }
  582. /// <summary>
  583. /// Triggered when one of the scene tool buttons is clicked, changing the active scene handle.
  584. /// </summary>
  585. /// <param name="tool">Clicked scene tool to activate.</param>
  586. private void OnSceneToolButtonClicked(SceneViewTool tool)
  587. {
  588. EditorApplication.ActiveSceneTool = tool;
  589. editorSettingsHash = EditorSettings.Hash;
  590. }
  591. /// <summary>
  592. /// Triggered when one of the coordinate mode buttons is clicked, changing the active coordinate mode.
  593. /// </summary>
  594. /// <param name="mode">Clicked coordinate mode to activate.</param>
  595. private void OnCoordinateModeButtonClicked(HandleCoordinateMode mode)
  596. {
  597. EditorApplication.ActiveCoordinateMode = mode;
  598. editorSettingsHash = EditorSettings.Hash;
  599. }
  600. /// <summary>
  601. /// Triggered when one of the pivot buttons is clicked, changing the active pivot mode.
  602. /// </summary>
  603. /// <param name="mode">Clicked pivot mode to activate.</param>
  604. private void OnPivotModeButtonClicked(HandlePivotMode mode)
  605. {
  606. EditorApplication.ActivePivotMode = mode;
  607. editorSettingsHash = EditorSettings.Hash;
  608. }
  609. /// <summary>
  610. /// Triggered when the move snap button is toggled.
  611. /// </summary>
  612. /// <param name="active">Determins should be move snap be activated or deactivated.</param>
  613. private void OnMoveSnapToggled(bool active)
  614. {
  615. Handles.MoveHandleSnapActive = active;
  616. editorSettingsHash = EditorSettings.Hash;
  617. }
  618. /// <summary>
  619. /// Triggered when the move snap increment value changes.
  620. /// </summary>
  621. /// <param name="value">Value that determines in what increments to perform move snapping.</param>
  622. private void OnMoveSnapValueChanged(float value)
  623. {
  624. Handles.MoveSnapAmount = MathEx.Clamp(value, 0.01f, 1000.0f);
  625. editorSettingsHash = EditorSettings.Hash;
  626. }
  627. /// <summary>
  628. /// Triggered when the rotate snap button is toggled.
  629. /// </summary>
  630. /// <param name="active">Determins should be rotate snap be activated or deactivated.</param>
  631. private void OnRotateSnapToggled(bool active)
  632. {
  633. Handles.RotateHandleSnapActive = active;
  634. editorSettingsHash = EditorSettings.Hash;
  635. }
  636. /// <summary>
  637. /// Triggered when the rotate snap increment value changes.
  638. /// </summary>
  639. /// <param name="value">Value that determines in what increments to perform rotate snapping.</param>
  640. private void OnRotateSnapValueChanged(float value)
  641. {
  642. Handles.RotateSnapAmount = (Degree)MathEx.Clamp(value, 0.01f, 360.0f);
  643. editorSettingsHash = EditorSettings.Hash;
  644. }
  645. /// <summary>
  646. /// Updates toggle button states according to current editor options. This is useful if tools, coordinate mode,
  647. /// pivot or other scene view options have been modified externally.
  648. /// </summary>
  649. private void UpdateButtonStates()
  650. {
  651. switch (EditorApplication.ActiveSceneTool)
  652. {
  653. case SceneViewTool.View:
  654. viewButton.Value = true;
  655. break;
  656. case SceneViewTool.Move:
  657. moveButton.Value = true;
  658. break;
  659. case SceneViewTool.Rotate:
  660. rotateButton.Value = true;
  661. break;
  662. case SceneViewTool.Scale:
  663. scaleButton.Value = true;
  664. break;
  665. }
  666. switch (EditorApplication.ActiveCoordinateMode)
  667. {
  668. case HandleCoordinateMode.Local:
  669. localCoordButton.Value = true;
  670. break;
  671. case HandleCoordinateMode.World:
  672. worldCoordButton.Value = true;
  673. break;
  674. }
  675. switch (EditorApplication.ActivePivotMode)
  676. {
  677. case HandlePivotMode.Center:
  678. centerButton.Value = true;
  679. break;
  680. case HandlePivotMode.Pivot:
  681. pivotButton.Value = true;
  682. break;
  683. }
  684. if (Handles.MoveHandleSnapActive)
  685. moveSnapButton.Value = true;
  686. else
  687. moveSnapButton.Value = false;
  688. moveSnapInput.Value = Handles.MoveSnapAmount;
  689. if (Handles.RotateHandleSnapActive)
  690. rotateSnapButton.Value = true;
  691. else
  692. rotateSnapButton.Value = false;
  693. moveSnapInput.Value = Handles.RotateSnapAmount.Degrees;
  694. }
  695. /// <summary>
  696. /// Activates or deactivates the profiler overlay according to current editor settings.
  697. /// </summary>
  698. private void UpdateProfilerOverlay()
  699. {
  700. if (EditorSettings.GetBool(ProfilerOverlayActiveKey))
  701. {
  702. if (activeProfilerOverlay == null)
  703. {
  704. SceneObject profilerSO = new SceneObject("EditorProfilerOverlay");
  705. profilerCamera = profilerSO.AddComponent<Camera>();
  706. profilerCamera.Viewport.Target = renderTexture;
  707. profilerCamera.Viewport.ClearFlags = ClearFlags.Empty;
  708. profilerCamera.Priority = 1;
  709. profilerCamera.Layers = 0;
  710. profilerCamera.RenderSettings.EnableHDR = false;
  711. activeProfilerOverlay = profilerSO.AddComponent<ProfilerOverlay>();
  712. }
  713. }
  714. else
  715. {
  716. if (activeProfilerOverlay != null)
  717. {
  718. activeProfilerOverlay.SceneObject.Destroy();
  719. activeProfilerOverlay = null;
  720. profilerCamera = null;
  721. }
  722. }
  723. }
  724. /// <summary>
  725. /// Creates the scene camera and updates the render texture. Should be called at least once before using the
  726. /// scene view. Should be called whenever the window is resized.
  727. /// </summary>
  728. /// <param name="width">Width of the scene render target, in pixels.</param>
  729. /// <param name="height">Height of the scene render target, in pixels.</param>
  730. private void UpdateRenderTexture(int width, int height)
  731. {
  732. width = MathEx.Max(20, width);
  733. height = MathEx.Max(20, height);
  734. // Note: Depth buffer and readable flags are required because ScenePicking uses it
  735. Texture colorTex = Texture.Create2D((uint)width, (uint)height, PixelFormat.RGBA8, TextureUsage.Render | TextureUsage.CPUReadable);
  736. Texture depthTex = Texture.Create2D((uint)width, (uint)height, PixelFormat.D32_S8X24, TextureUsage.DepthStencil | TextureUsage.CPUReadable);
  737. renderTexture = new RenderTexture(colorTex, depthTex);
  738. renderTexture.Priority = 1;
  739. if (camera == null)
  740. {
  741. SceneObject sceneCameraSO = new SceneObject("SceneCamera", true);
  742. camera = sceneCameraSO.AddComponent<Camera>();
  743. camera.Viewport.Target = renderTexture;
  744. camera.Viewport.Area = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  745. sceneCameraSO.Position = new Vector3(0, 0.5f, 1);
  746. sceneCameraSO.LookAt(new Vector3(0, 0.5f, 0));
  747. camera.Priority = 2;
  748. camera.NearClipPlane = 0.05f;
  749. camera.FarClipPlane = 2500.0f;
  750. camera.Viewport.ClearColor = ClearColor;
  751. camera.Layers = UInt64.MaxValue & ~SceneAxesHandle.LAYER; // Don't draw scene axes in this camera
  752. cameraController = sceneCameraSO.AddComponent<SceneCamera>();
  753. renderTextureGUI = new GUIRenderTexture(renderTexture);
  754. rtPanel.AddElement(renderTextureGUI);
  755. sceneGrid = new SceneGrid(camera);
  756. sceneSelection = new SceneSelection(camera);
  757. sceneGizmos = new SceneGizmos(camera);
  758. sceneHandles = new SceneHandles(this, camera);
  759. }
  760. else
  761. {
  762. camera.Viewport.Target = renderTexture;
  763. renderTextureGUI.RenderTexture = renderTexture;
  764. }
  765. Rect2I rtBounds = new Rect2I(0, 0, width, height);
  766. renderTextureGUI.Bounds = rtBounds;
  767. focusCatcher.Bounds = GUIUtility.CalculateBounds(rtPanel, GUI);
  768. sceneAxesGUI.SetPosition(width - HandleAxesGUISize - HandleAxesGUIPaddingX, HandleAxesGUIPaddingY);
  769. // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
  770. // render target destroy/create cycle for every single pixel.
  771. camera.AspectRatio = width / (float)height;
  772. if (profilerCamera != null)
  773. profilerCamera.Viewport.Target = renderTexture;
  774. }
  775. /// <summary>
  776. /// Parses an array of scene objects and removes elements that are children of elements that are also in the array.
  777. /// </summary>
  778. /// <param name="objects">Array containing duplicate objects as input, and array without duplicate objects as
  779. /// output.</param>
  780. private void CleanDuplicates(ref SceneObject[] objects)
  781. {
  782. List<SceneObject> cleanList = new List<SceneObject>();
  783. for (int i = 0; i < objects.Length; i++)
  784. {
  785. bool foundParent = false;
  786. for (int j = 0; j < objects.Length; j++)
  787. {
  788. SceneObject elem = objects[i];
  789. while (elem != null && elem != objects[j])
  790. elem = elem.Parent;
  791. bool isChildOf = elem == objects[j];
  792. if (i != j && isChildOf)
  793. {
  794. foundParent = true;
  795. break;
  796. }
  797. }
  798. if (!foundParent)
  799. cleanList.Add(objects[i]);
  800. }
  801. objects = cleanList.ToArray();
  802. }
  803. /// <summary>
  804. /// Starts a drag operation that displays a selection outline allowing the user to select multiple entries at once.
  805. /// </summary>
  806. /// <param name="scenePos">Coordinates relative to the scene where the drag originated.</param>
  807. private void StartDragSelection(Vector2I scenePos)
  808. {
  809. isDraggingSelection = true;
  810. dragSelectionStart = scenePos;
  811. dragSelectionEnd = dragSelectionStart;
  812. }
  813. /// <summary>
  814. /// Updates a selection outline drag operation by expanding the outline to the new location. Elements in the outline
  815. /// are selected.
  816. /// </summary>
  817. /// <param name="scenePos">Coordinates of the pointer relative to the scene.</param>
  818. /// <returns>True if the selection outline drag is valid and was updated, false otherwise.</returns>
  819. private bool UpdateDragSelection(Vector2I scenePos)
  820. {
  821. if (!isDraggingSelection)
  822. return false;
  823. if (dragSelection == null)
  824. {
  825. dragSelection = new GUITexture(null, true, EditorStylesInternal.SelectionArea);
  826. selectionPanel.AddElement(dragSelection);
  827. }
  828. dragSelectionEnd = scenePos;
  829. Rect2I selectionArea = new Rect2I();
  830. Vector2I min = new Vector2I(Math.Min(dragSelectionStart.x, dragSelectionEnd.x), Math.Min(dragSelectionStart.y, dragSelectionEnd.y));
  831. Vector2I max = new Vector2I(Math.Max(dragSelectionStart.x, dragSelectionEnd.x), Math.Max(dragSelectionStart.y, dragSelectionEnd.y));
  832. selectionArea.x = min.x;
  833. selectionArea.y = min.y;
  834. selectionArea.width = Math.Max(max.x - min.x, 1);
  835. selectionArea.height = Math.Max(max.y - min.y, 1);
  836. dragSelection.Bounds = selectionArea;
  837. return true;
  838. }
  839. /// <summary>
  840. /// Ends the selection outline drag operation. Elements in the outline are selected.
  841. /// </summary>
  842. /// <returns>True if the selection outline drag is valid and was ended, false otherwise.</returns>
  843. private bool EndDragSelection()
  844. {
  845. if (!isDraggingSelection)
  846. return false;
  847. if (dragSelection != null)
  848. {
  849. dragSelection.Destroy();
  850. dragSelection = null;
  851. }
  852. if ((dragSelectionEnd - dragSelectionStart).Length < 1)
  853. {
  854. isDraggingSelection = false;
  855. return false;
  856. }
  857. Vector2I min = new Vector2I(Math.Min(dragSelectionStart.x, dragSelectionEnd.x),
  858. Math.Min(dragSelectionStart.y, dragSelectionEnd.y));
  859. Vector2I max = new Vector2I(Math.Max(dragSelectionStart.x, dragSelectionEnd.x),
  860. Math.Max(dragSelectionStart.y, dragSelectionEnd.y));
  861. sceneSelection.PickObjects(min, max - min,
  862. Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl));
  863. isDraggingSelection = false;
  864. return true;
  865. }
  866. }
  867. /** @} */
  868. }