SceneWindow.cs 40 KB

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