SceneWindow.cs 35 KB

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