SceneWindow.cs 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using bs;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. namespace bs.Editor
  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 string CameraPositionKey = "SceneCamera0_Position";
  24. private const string CameraRotationKey = "SceneCamera0_Rotation";
  25. private const int HeaderHeight = 20;
  26. private const float DefaultPlacementDepth = 5.0f;
  27. private static readonly Color ClearColor = new Color(0.0f, 0.3685f, 0.7969f);
  28. private const int HandleAxesGUISize = 50;
  29. private const int HandleAxesGUIPaddingX = 10;
  30. private const int HandleAxesGUIPaddingY = 5;
  31. private Camera camera;
  32. private SceneCamera sceneCamera;
  33. private RenderTexture renderTexture;
  34. private GUILayoutY mainLayout;
  35. private GUIPanel rtPanel;
  36. private GUIPanel sceneAxesPanel;
  37. private GUIButton focusCatcher;
  38. private GUIRenderTexture renderTextureGUI;
  39. private SceneGrid sceneGrid;
  40. private SceneSelection sceneSelection;
  41. private SceneGizmos sceneGizmos;
  42. private SceneHandles sceneHandles;
  43. private GUIToggle viewButton;
  44. private GUIToggle moveButton;
  45. private GUIToggle rotateButton;
  46. private GUIToggle scaleButton;
  47. private GUIToggle localCoordButton;
  48. private GUIToggle worldCoordButton;
  49. private GUIToggle pivotButton;
  50. private GUIToggle centerButton;
  51. private GUIToggle moveSnapButton;
  52. private GUIFloatField moveSnapInput;
  53. private GUIToggle rotateSnapButton;
  54. private GUIFloatField rotateSnapInput;
  55. private GUIButton cameraOptionsButton;
  56. private GUILayout progressLayout;
  57. private GUIProgressBar loadProgressBar;
  58. private GUILabel loadLabel;
  59. private SceneAxesGUI sceneAxesGUI;
  60. private ProjectionType sceneAxesLastProjectionType;
  61. private bool hasContentFocus = false;
  62. private bool HasContentFocus { get { return HasFocus && hasContentFocus; } }
  63. private bool loadingProgressShown = false;
  64. private bool AllowViewportInput { get { return !loadingProgressShown; } }
  65. private int editorSettingsHash = int.MaxValue;
  66. private VirtualButton frameKey;
  67. // Tool shortcuts
  68. private VirtualButton viewToolKey;
  69. private VirtualButton moveToolKey;
  70. private VirtualButton rotateToolKey;
  71. private VirtualButton scaleToolKey;
  72. // Drag & drop
  73. private bool dragActive;
  74. private SceneObject draggedSO;
  75. private Vector3 draggedSOOffset;
  76. private GUITexture dragSelection;
  77. private bool isDraggingSelection;
  78. private Vector2I dragSelectionStart;
  79. private Vector2I dragSelectionEnd;
  80. private Vector2I mouseDownPosition;
  81. private GUIPanel selectionPanel;
  82. // Camera previews
  83. private const int MaxCameraPreviews = 0; // Disabled until we resolve issues with adding temporary camera copies
  84. private List<CameraPreview> cameraPreviews = new List<CameraPreview>();
  85. private GUIPanel cameraPreviewsPanel;
  86. /// <summary>
  87. /// Returns the scene camera.
  88. /// </summary>
  89. public SceneCamera Camera => sceneCamera;
  90. /// <summary>
  91. /// Constructs a new scene window.
  92. /// </summary>
  93. internal SceneWindow()
  94. { }
  95. /// <summary>
  96. /// Opens a scene window if its not open already.
  97. /// </summary>
  98. [MenuItem("Windows/Scene", ButtonModifier.CtrlAlt, ButtonCode.S, 6000)]
  99. private static void OpenSceneWindow()
  100. {
  101. OpenWindow<SceneWindow>();
  102. }
  103. /// <summary>
  104. /// Focuses on the currently selected object.
  105. /// </summary>
  106. [MenuItem("Tools/Frame Selected", ButtonModifier.None, ButtonCode.F, 9275, true)]
  107. private static void OpenSettingsWindow()
  108. {
  109. SceneWindow window = GetWindow<SceneWindow>();
  110. if (window != null)
  111. window.sceneCamera.FrameSelected();
  112. }
  113. /// <summary>
  114. /// Switches the active tool to the view tool.
  115. /// </summary>
  116. [MenuItem("Tools/View", ButtonModifier.Ctrl, ButtonCode.Q, 9274, true)]
  117. private static void SetViewTool()
  118. {
  119. SceneWindow window = GetWindow<SceneWindow>();
  120. if (window != null)
  121. window.OnSceneToolButtonClicked(SceneViewTool.View);
  122. }
  123. /// <summary>
  124. /// Switches the active tool to the move tool.
  125. /// </summary>
  126. [MenuItem("Tools/Move", ButtonModifier.Ctrl, ButtonCode.W, 9273)]
  127. private static void SetMoveTool()
  128. {
  129. SceneWindow window = GetWindow<SceneWindow>();
  130. if (window != null)
  131. window.OnSceneToolButtonClicked(SceneViewTool.Move);
  132. }
  133. /// <summary>
  134. /// Switches the active tool to the rotate tool.
  135. /// </summary>
  136. [MenuItem("Tools/Rotate", ButtonModifier.Ctrl, ButtonCode.E, 9272)]
  137. private static void SetRotateTool()
  138. {
  139. SceneWindow window = GetWindow<SceneWindow>();
  140. if (window != null)
  141. window.OnSceneToolButtonClicked(SceneViewTool.Rotate);
  142. }
  143. /// <summary>
  144. /// Switches the active tool to the scale tool.
  145. /// </summary>
  146. [MenuItem("Tools/Scale", ButtonModifier.Ctrl, ButtonCode.R, 9271)]
  147. private static void SetScaleTool()
  148. {
  149. SceneWindow window = GetWindow<SceneWindow>();
  150. if (window != null)
  151. window.OnSceneToolButtonClicked(SceneViewTool.Scale);
  152. }
  153. /// <inheritdoc/>
  154. protected override LocString GetDisplayName()
  155. {
  156. return new LocEdString("Scene");
  157. }
  158. private void OnInitialize()
  159. {
  160. mainLayout = GUI.AddLayoutY();
  161. GUIContent viewIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.View),
  162. new LocEdString("View"));
  163. GUIContent moveIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Move),
  164. new LocEdString("Move"));
  165. GUIContent rotateIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Rotate),
  166. new LocEdString("Rotate"));
  167. GUIContent scaleIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Scale),
  168. new LocEdString("Scale"));
  169. GUIContent localIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Local),
  170. new LocEdString("Local"));
  171. GUIContent worldIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.World),
  172. new LocEdString("World"));
  173. GUIContent pivotIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Pivot),
  174. new LocEdString("Pivot"));
  175. GUIContent centerIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.Center),
  176. new LocEdString("Center"));
  177. GUIContent moveSnapIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.MoveSnap),
  178. new LocEdString("Move snap"));
  179. GUIContent rotateSnapIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.RotateSnap),
  180. new LocEdString("Rotate snap"));
  181. GUIToggleGroup handlesTG = new GUIToggleGroup();
  182. viewButton = new GUIToggle(viewIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  183. moveButton = new GUIToggle(moveIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  184. rotateButton = new GUIToggle(rotateIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  185. scaleButton = new GUIToggle(scaleIcon, handlesTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  186. GUIToggleGroup coordModeTG = new GUIToggleGroup();
  187. localCoordButton = new GUIToggle(localIcon, coordModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(75));
  188. worldCoordButton = new GUIToggle(worldIcon, coordModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(75));
  189. GUIToggleGroup pivotModeTG = new GUIToggleGroup();
  190. pivotButton = new GUIToggle(pivotIcon, pivotModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  191. centerButton = new GUIToggle(centerIcon, pivotModeTG, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  192. moveSnapButton = new GUIToggle(moveSnapIcon, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  193. moveSnapInput = new GUIFloatField("", GUIOption.FlexibleWidth(35));
  194. rotateSnapButton = new GUIToggle(rotateSnapIcon, EditorStyles.Button, GUIOption.FlexibleWidth(35));
  195. rotateSnapInput = new GUIFloatField("", GUIOption.FlexibleWidth(35));
  196. GUIContent cameraOptionsIcon = new GUIContent(EditorBuiltin.GetSceneWindowIcon(SceneWindowIcon.SceneCameraOptions), new LocEdString("Camera options"));
  197. cameraOptionsButton = new GUIButton(cameraOptionsIcon);
  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. cameraOptionsButton.OnClick += () => OnCameraOptionsClicked();
  211. GUILayout handlesLayout = mainLayout.AddLayoutX();
  212. handlesLayout.AddElement(viewButton);
  213. handlesLayout.AddElement(moveButton);
  214. handlesLayout.AddElement(rotateButton);
  215. handlesLayout.AddElement(scaleButton);
  216. handlesLayout.AddSpace(10);
  217. handlesLayout.AddElement(localCoordButton);
  218. handlesLayout.AddElement(worldCoordButton);
  219. handlesLayout.AddSpace(10);
  220. handlesLayout.AddElement(pivotButton);
  221. handlesLayout.AddElement(centerButton);
  222. handlesLayout.AddFlexibleSpace();
  223. handlesLayout.AddElement(moveSnapButton);
  224. handlesLayout.AddElement(moveSnapInput);
  225. handlesLayout.AddSpace(10);
  226. handlesLayout.AddElement(rotateSnapButton);
  227. handlesLayout.AddElement(rotateSnapInput);
  228. handlesLayout.AddSpace(10);
  229. handlesLayout.AddElement(cameraOptionsButton);
  230. handlesLayout.SetHeight(viewButton.Bounds.height);
  231. GUIPanel mainPanel = mainLayout.AddPanel();
  232. rtPanel = mainPanel.AddPanel();
  233. // Loading progress
  234. loadLabel = new GUILabel(new LocEdString("Loading scene..."));
  235. loadProgressBar = new GUIProgressBar("", GUIOption.FixedWidth(200));
  236. progressLayout = mainPanel.AddLayoutY();
  237. progressLayout.AddFlexibleSpace();
  238. GUILayout loadLabelLayout = progressLayout.AddLayoutX();
  239. loadLabelLayout.AddFlexibleSpace();
  240. loadLabelLayout.AddElement(loadLabel);
  241. loadLabelLayout.AddFlexibleSpace();
  242. GUILayout progressBarLayout = progressLayout.AddLayoutX();
  243. progressBarLayout.AddFlexibleSpace();
  244. progressBarLayout.AddElement(loadProgressBar);
  245. progressBarLayout.AddFlexibleSpace();
  246. progressLayout.AddFlexibleSpace();
  247. progressLayout.Active = false;
  248. selectionPanel = mainPanel.AddPanel(-1);
  249. sceneAxesPanel = mainPanel.AddPanel(-1);
  250. sceneAxesGUI = new SceneAxesGUI(this, sceneAxesPanel, HandleAxesGUISize, HandleAxesGUISize, ProjectionType.Perspective);
  251. sceneAxesLastProjectionType = ProjectionType.Perspective;
  252. focusCatcher = new GUIButton("", EditorStyles.Blank);
  253. focusCatcher.OnFocusGained += () => hasContentFocus = true;
  254. focusCatcher.OnFocusLost += () => hasContentFocus = false;
  255. GUIPanel focusPanel = GUI.AddPanel(-2);
  256. focusPanel.AddElement(focusCatcher);
  257. cameraPreviewsPanel = GUI.AddPanel(-3);
  258. viewToolKey = new VirtualButton(ViewToolBinding);
  259. moveToolKey = new VirtualButton(MoveToolBinding);
  260. rotateToolKey = new VirtualButton(RotateToolBinding);
  261. scaleToolKey = new VirtualButton(ScaleToolBinding);
  262. frameKey = new VirtualButton(FrameBinding);
  263. UpdateRenderTexture(Width, Height - HeaderHeight);
  264. UpdateLoadingProgress();
  265. UpdateCameraPreviews();
  266. Selection.OnSelectionChanged += OnSelectionChanged;
  267. }
  268. private void OnCameraOptionsClicked()
  269. {
  270. SceneCameraSettingsWindow.Open();
  271. }
  272. private void OnDestroy()
  273. {
  274. if (camera != null)
  275. {
  276. Vector3 pos = camera.SceneObject.Position;
  277. Quaternion rot = camera.SceneObject.Rotation;
  278. ProjectSettings.SetObject(CameraPositionKey, pos);
  279. ProjectSettings.SetObject(CameraRotationKey, rot);
  280. camera.SceneObject.Destroy(true);
  281. camera = null;
  282. }
  283. sceneAxesGUI.Destroy();
  284. sceneAxesGUI = null;
  285. Selection.OnSelectionChanged -= OnSelectionChanged;
  286. }
  287. /// <summary>
  288. /// Deletes all currently selected objects.
  289. /// </summary>
  290. private void DeleteSelection()
  291. {
  292. SceneObject[] selectedObjects = Selection.SceneObjects;
  293. CleanDuplicates(ref selectedObjects);
  294. if (selectedObjects.Length > 0)
  295. {
  296. foreach (var so in selectedObjects)
  297. {
  298. string message = "Deleted " + so.Name;
  299. UndoRedo.DeleteSO(so, message);
  300. }
  301. EditorApplication.SetSceneDirty();
  302. }
  303. }
  304. /// <summary>
  305. /// Duplicates all currently selected objects.
  306. /// </summary>
  307. private void DuplicateSelection()
  308. {
  309. SceneObject[] selectedObjects = Selection.SceneObjects;
  310. CleanDuplicates(ref selectedObjects);
  311. if (selectedObjects.Length > 0)
  312. {
  313. string message;
  314. if (selectedObjects.Length == 1)
  315. message = "Duplicated " + selectedObjects[0].Name;
  316. else
  317. message = "Duplicated " + selectedObjects.Length + " elements";
  318. Transform[] savedTransforms = new Transform[selectedObjects.Length];
  319. for (int i = 0; i < savedTransforms.Length; i++)
  320. savedTransforms[i] = new Transform(selectedObjects[i]);
  321. SceneObject[] clonedObjects = UndoRedo.CloneSO(selectedObjects, message);
  322. // Restore world positions
  323. for (int i = 0; i < savedTransforms.Length; i++)
  324. savedTransforms[i].Apply(clonedObjects[i]);
  325. EditorApplication.SetSceneDirty();
  326. }
  327. }
  328. /// <summary>
  329. /// Callback that triggers when a new set of scene objects or resources has been selected.
  330. /// </summary>
  331. /// <param name="objects">Selected scene objects.</param>
  332. /// <param name="resources">Selected resources.</param>
  333. private void OnSelectionChanged(SceneObject[] objects, string[] resources)
  334. {
  335. UpdateCameraPreviews();
  336. }
  337. /// <inheritdoc/>
  338. void IGlobalShortcuts.OnRenamePressed()
  339. {
  340. // Do nothing
  341. }
  342. /// <inheritdoc/>
  343. void IGlobalShortcuts.OnDuplicatePressed()
  344. {
  345. DuplicateSelection();
  346. }
  347. /// <inheritdoc/>
  348. void IGlobalShortcuts.OnDeletePressed()
  349. {
  350. DeleteSelection();
  351. }
  352. /// <inheritdoc/>
  353. void IGlobalShortcuts.OnCopyPressed()
  354. {
  355. // Do nothing
  356. }
  357. /// <inheritdoc/>
  358. void IGlobalShortcuts.OnCutPressed()
  359. {
  360. // Do nothing
  361. }
  362. /// <inheritdoc/>
  363. void IGlobalShortcuts.OnPastePressed()
  364. {
  365. // Do nothing
  366. }
  367. /// <summary>
  368. /// Orients the camera so it looks along the provided axis.
  369. /// </summary>
  370. /// <param name="axis">Axis to look along.</param>
  371. internal void LookAlong(Vector3 axis)
  372. {
  373. axis.Normalize();
  374. sceneCamera.LookAlong(axis);
  375. UpdateGridMode();
  376. }
  377. private void UpdateGridMode()
  378. {
  379. Vector3 forward = camera.SceneObject.Forward;
  380. if (camera.ProjectionType == ProjectionType.Perspective)
  381. sceneGrid.SetMode(GridMode.Perspective);
  382. else
  383. {
  384. float dotX = Vector3.Dot(forward, Vector3.XAxis);
  385. if (dotX >= 0.95f)
  386. sceneGrid.SetMode(GridMode.OrthoX);
  387. else if (dotX <= -0.95f)
  388. sceneGrid.SetMode(GridMode.OrthoNegX);
  389. else
  390. {
  391. float dotY = Vector3.Dot(forward, Vector3.YAxis);
  392. if (dotY >= 0.95f)
  393. sceneGrid.SetMode(GridMode.OrthoY);
  394. else if (dotY <= -0.95f)
  395. sceneGrid.SetMode(GridMode.OrthoNegY);
  396. else
  397. {
  398. float dotZ = Vector3.Dot(forward, Vector3.ZAxis);
  399. if (dotZ >= 0.95f)
  400. sceneGrid.SetMode(GridMode.OrthoZ);
  401. else if (dotZ <= -0.95f)
  402. sceneGrid.SetMode(GridMode.OrthoNegZ);
  403. else
  404. sceneGrid.SetMode(GridMode.Perspective);
  405. }
  406. }
  407. }
  408. }
  409. /// <summary>
  410. /// Converts screen coordinates into coordinates relative to the scene view render texture.
  411. /// </summary>
  412. /// <param name="screenPos">Coordinates relative to the screen.</param>
  413. /// <param name="scenePos">Output coordinates relative to the scene view texture.</param>
  414. /// <returns>True if the coordinates are within the scene view texture, false otherwise.</returns>
  415. private bool ScreenToScenePos(Vector2I screenPos, out Vector2I scenePos)
  416. {
  417. scenePos = screenPos;
  418. Vector2I windowPos = ScreenToWindowPos(screenPos);
  419. Rect2I bounds = GUIUtility.CalculateBounds(renderTextureGUI, GUI);
  420. if (bounds.Contains(windowPos))
  421. {
  422. scenePos.x = windowPos.x - bounds.x;
  423. scenePos.y = windowPos.y - bounds.y;
  424. return true;
  425. }
  426. return false;
  427. }
  428. private void OnEditorUpdate()
  429. {
  430. UpdateLoadingProgress();
  431. if (HasFocus)
  432. {
  433. if (!Input.IsPointerButtonHeld(PointerButton.Right))
  434. {
  435. if (VirtualInput.IsButtonDown(EditorApplication.DuplicateKey))
  436. DuplicateSelection();
  437. else if (VirtualInput.IsButtonDown(EditorApplication.DeleteKey))
  438. DeleteSelection();
  439. else if (VirtualInput.IsButtonDown(viewToolKey))
  440. EditorApplication.ActiveSceneTool = SceneViewTool.View;
  441. else if (VirtualInput.IsButtonDown(moveToolKey))
  442. EditorApplication.ActiveSceneTool = SceneViewTool.Move;
  443. else if (VirtualInput.IsButtonDown(rotateToolKey))
  444. EditorApplication.ActiveSceneTool = SceneViewTool.Rotate;
  445. else if (VirtualInput.IsButtonDown(scaleToolKey))
  446. EditorApplication.ActiveSceneTool = SceneViewTool.Scale;
  447. }
  448. }
  449. // Refresh GUI buttons if needed (in case someones changes the values from script)
  450. if (editorSettingsHash != EditorSettings.Hash)
  451. {
  452. UpdateButtonStates();
  453. editorSettingsHash = EditorSettings.Hash;
  454. }
  455. // Update scene view handles and selection
  456. sceneGrid.Draw();
  457. ProjectionType currentProjType = camera.ProjectionType;
  458. if (sceneAxesLastProjectionType != currentProjType)
  459. {
  460. sceneAxesGUI.ProjectionType = currentProjType;
  461. sceneAxesLastProjectionType = currentProjType;
  462. }
  463. bool handleActive = sceneHandles.IsActive() || sceneAxesGUI.IsActive();
  464. Vector2I scenePos;
  465. bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);
  466. bool clearSelection = false;
  467. if (AllowViewportInput)
  468. {
  469. if (Input.IsPointerButtonUp(PointerButton.Left))
  470. clearSelection = true;
  471. else if (Input.IsPointerButtonDown(PointerButton.Left))
  472. mouseDownPosition = scenePos;
  473. }
  474. else
  475. {
  476. clearSelection = true;
  477. inBounds = false;
  478. }
  479. bool dragResult = false;
  480. if (clearSelection)
  481. {
  482. dragResult = EndDragSelection();
  483. if (sceneHandles.IsActive())
  484. sceneHandles.ClearSelection();
  485. if (sceneAxesGUI.IsActive())
  486. sceneAxesGUI.ClearSelection();
  487. }
  488. bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;
  489. draggedOver &= IsPointerHovering && inBounds && DragDrop.Type == DragDropType.Resource;
  490. if (draggedOver)
  491. {
  492. if (DragDrop.DropInProgress)
  493. {
  494. dragActive = false;
  495. if (draggedSO != null)
  496. {
  497. Selection.SceneObject = draggedSO;
  498. EditorApplication.SetSceneDirty();
  499. }
  500. draggedSO = null;
  501. }
  502. else
  503. {
  504. if (!dragActive)
  505. {
  506. dragActive = true;
  507. ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;
  508. string[] draggedPaths = dragData.Paths;
  509. for (int i = 0; i < draggedPaths.Length; i++)
  510. {
  511. ResourceMeta meta = ProjectLibrary.GetMeta(draggedPaths[i]);
  512. if (meta != null)
  513. {
  514. if (meta.ResType == ResourceType.Mesh)
  515. {
  516. if (!string.IsNullOrEmpty(draggedPaths[i]))
  517. {
  518. Mesh mesh = ProjectLibrary.Load<Mesh>(draggedPaths[i]);
  519. string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]);
  520. draggedSO = new SceneObject(meshName);
  521. GameObjectUndo.RecordNewSceneObject(draggedSO);
  522. Renderable renderable = draggedSO.AddComponent<Renderable>();
  523. renderable.Mesh = mesh;
  524. GameObjectUndo.ResolveDiffs();
  525. if (mesh != null)
  526. draggedSOOffset = mesh.Bounds.Box.Center;
  527. else
  528. draggedSOOffset = Vector3.Zero;
  529. }
  530. break;
  531. }
  532. else if (meta.ResType == ResourceType.Prefab)
  533. {
  534. if (!string.IsNullOrEmpty(draggedPaths[i]))
  535. {
  536. Prefab prefab = ProjectLibrary.Load<Prefab>(draggedPaths[i]);
  537. draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);
  538. if (draggedSO != null)
  539. {
  540. AABox draggedObjBounds = EditorUtility.CalculateBounds(draggedSO);
  541. draggedSOOffset = draggedObjBounds.Center;
  542. }
  543. else
  544. draggedSOOffset = Vector3.Zero;
  545. }
  546. break;
  547. }
  548. }
  549. }
  550. }
  551. if (draggedSO != null)
  552. {
  553. if (Input.IsButtonHeld(ButtonCode.Space))
  554. {
  555. SnapData snapData;
  556. sceneSelection.Snap(scenePos, out snapData, new SceneObject[] { draggedSO });
  557. Quaternion q = Quaternion.FromToRotation(Vector3.YAxis, snapData.normal);
  558. draggedSO.Position = snapData.position;
  559. draggedSO.Rotation = q;
  560. }
  561. else
  562. {
  563. Ray worldRay = camera.ScreenPointToRay(scenePos);
  564. draggedSO.Position = worldRay * DefaultPlacementDepth - draggedSOOffset;
  565. }
  566. }
  567. }
  568. return;
  569. }
  570. else
  571. {
  572. if (dragActive)
  573. {
  574. dragActive = false;
  575. if (draggedSO != null)
  576. {
  577. draggedSO.Destroy();
  578. draggedSO = null;
  579. }
  580. }
  581. }
  582. if ((HasContentFocus || IsPointerHovering) && AllowViewportInput)
  583. {
  584. sceneCamera.EnableInput(true);
  585. if (inBounds && HasContentFocus)
  586. {
  587. if (Input.IsPointerButtonDown(PointerButton.Left))
  588. {
  589. Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPaddingX,
  590. HandleAxesGUIPaddingY, HandleAxesGUISize, HandleAxesGUISize);
  591. if (sceneAxesGUIBounds.Contains(scenePos))
  592. sceneAxesGUI.TrySelect(scenePos);
  593. else
  594. sceneHandles.TrySelect(scenePos);
  595. }
  596. else if (Input.IsPointerButtonHeld(PointerButton.Left) && !handleActive && !dragActive &&
  597. draggedSO == null && scenePos != mouseDownPosition)
  598. {
  599. if (isDraggingSelection)
  600. UpdateDragSelection(scenePos);
  601. else
  602. StartDragSelection(scenePos);
  603. }
  604. else if (Input.IsPointerButtonUp(PointerButton.Left))
  605. {
  606. if (!handleActive && !dragActive && !dragResult)
  607. {
  608. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
  609. Input.IsButtonHeld(ButtonCode.RightControl);
  610. sceneSelection.PickObject(scenePos, ctrlHeld, new SceneObject[] { draggedSO });
  611. }
  612. }
  613. }
  614. }
  615. else
  616. sceneCamera.EnableInput(false);
  617. if (AllowViewportInput)
  618. {
  619. SceneHandles.BeginInput();
  620. sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
  621. sceneAxesGUI.UpdateInput(scenePos);
  622. SceneHandles.EndInput();
  623. }
  624. sceneHandles.Draw();
  625. sceneAxesGUI.Draw();
  626. // Must be done after handle input is processed, in order to reflect most recent transform
  627. sceneGizmos.Draw();
  628. sceneSelection.Draw();
  629. UpdateGridMode();
  630. if (VirtualInput.IsButtonDown(frameKey))
  631. sceneCamera.FrameSelected();
  632. }
  633. /// <inheritdoc/>
  634. protected override void WindowResized(int width, int height)
  635. {
  636. UpdateRenderTexture(width, height - HeaderHeight);
  637. base.WindowResized(width, height);
  638. }
  639. /// <inheritdoc/>
  640. protected override void FocusChanged(bool inFocus)
  641. {
  642. if (!inFocus)
  643. {
  644. sceneHandles.ClearSelection();
  645. }
  646. }
  647. /// <summary>
  648. /// Triggered when one of the scene tool buttons is clicked, changing the active scene handle.
  649. /// </summary>
  650. /// <param name="tool">Clicked scene tool to activate.</param>
  651. private void OnSceneToolButtonClicked(SceneViewTool tool)
  652. {
  653. EditorApplication.ActiveSceneTool = tool;
  654. editorSettingsHash = EditorSettings.Hash;
  655. }
  656. /// <summary>
  657. /// Triggered when one of the coordinate mode buttons is clicked, changing the active coordinate mode.
  658. /// </summary>
  659. /// <param name="mode">Clicked coordinate mode to activate.</param>
  660. private void OnCoordinateModeButtonClicked(HandleCoordinateMode mode)
  661. {
  662. EditorApplication.ActiveCoordinateMode = mode;
  663. editorSettingsHash = EditorSettings.Hash;
  664. }
  665. /// <summary>
  666. /// Triggered when one of the pivot buttons is clicked, changing the active pivot mode.
  667. /// </summary>
  668. /// <param name="mode">Clicked pivot mode to activate.</param>
  669. private void OnPivotModeButtonClicked(HandlePivotMode mode)
  670. {
  671. EditorApplication.ActivePivotMode = mode;
  672. editorSettingsHash = EditorSettings.Hash;
  673. }
  674. /// <summary>
  675. /// Triggered when the move snap button is toggled.
  676. /// </summary>
  677. /// <param name="active">Determins should be move snap be activated or deactivated.</param>
  678. private void OnMoveSnapToggled(bool active)
  679. {
  680. Handles.MoveHandleSnapActive = active;
  681. editorSettingsHash = EditorSettings.Hash;
  682. }
  683. /// <summary>
  684. /// Triggered when the move snap increment value changes.
  685. /// </summary>
  686. /// <param name="value">Value that determines in what increments to perform move snapping.</param>
  687. private void OnMoveSnapValueChanged(float value)
  688. {
  689. Handles.MoveSnapAmount = MathEx.Clamp(value, 0.01f, 1000.0f);
  690. editorSettingsHash = EditorSettings.Hash;
  691. }
  692. /// <summary>
  693. /// Triggered when the rotate snap button is toggled.
  694. /// </summary>
  695. /// <param name="active">Determins should be rotate snap be activated or deactivated.</param>
  696. private void OnRotateSnapToggled(bool active)
  697. {
  698. Handles.RotateHandleSnapActive = active;
  699. editorSettingsHash = EditorSettings.Hash;
  700. }
  701. /// <summary>
  702. /// Triggered when the rotate snap increment value changes.
  703. /// </summary>
  704. /// <param name="value">Value that determines in what increments to perform rotate snapping.</param>
  705. private void OnRotateSnapValueChanged(float value)
  706. {
  707. Handles.RotateSnapAmount = (Degree)MathEx.Clamp(value, 0.01f, 360.0f);
  708. editorSettingsHash = EditorSettings.Hash;
  709. }
  710. /// <summary>
  711. /// Updates toggle button states according to current editor options. This is useful if tools, coordinate mode,
  712. /// pivot or other scene view options have been modified externally.
  713. /// </summary>
  714. private void UpdateButtonStates()
  715. {
  716. switch (EditorApplication.ActiveSceneTool)
  717. {
  718. case SceneViewTool.View:
  719. viewButton.Value = true;
  720. break;
  721. case SceneViewTool.Move:
  722. moveButton.Value = true;
  723. break;
  724. case SceneViewTool.Rotate:
  725. rotateButton.Value = true;
  726. break;
  727. case SceneViewTool.Scale:
  728. scaleButton.Value = true;
  729. break;
  730. }
  731. switch (EditorApplication.ActiveCoordinateMode)
  732. {
  733. case HandleCoordinateMode.Local:
  734. localCoordButton.Value = true;
  735. break;
  736. case HandleCoordinateMode.World:
  737. worldCoordButton.Value = true;
  738. break;
  739. }
  740. switch (EditorApplication.ActivePivotMode)
  741. {
  742. case HandlePivotMode.Center:
  743. centerButton.Value = true;
  744. break;
  745. case HandlePivotMode.Pivot:
  746. pivotButton.Value = true;
  747. break;
  748. }
  749. if (Handles.MoveHandleSnapActive)
  750. moveSnapButton.Value = true;
  751. else
  752. moveSnapButton.Value = false;
  753. moveSnapInput.Value = Handles.MoveSnapAmount;
  754. if (Handles.RotateHandleSnapActive)
  755. rotateSnapButton.Value = true;
  756. else
  757. rotateSnapButton.Value = false;
  758. moveSnapInput.Value = Handles.RotateSnapAmount.Degrees;
  759. }
  760. /// <summary>
  761. /// Creates the scene camera and updates the render texture. Should be called at least once before using the
  762. /// scene view. Should be called whenever the window is resized.
  763. /// </summary>
  764. /// <param name="width">Width of the scene render target, in pixels.</param>
  765. /// <param name="height">Height of the scene render target, in pixels.</param>
  766. private void UpdateRenderTexture(int width, int height)
  767. {
  768. width = MathEx.Max(20, width);
  769. height = MathEx.Max(20, height);
  770. // Note: Depth buffer and readable flags are required because ScenePicking uses it
  771. Texture colorTex = Texture.Create2D(width, height, PixelFormat.RGBA8, TextureUsage.Render | TextureUsage.CPUReadable);
  772. Texture depthTex = Texture.Create2D(width, height, PixelFormat.D32_S8X24, TextureUsage.DepthStencil | TextureUsage.CPUReadable);
  773. renderTexture = new RenderTexture(colorTex, depthTex);
  774. renderTexture.Priority = 1;
  775. if (camera == null)
  776. {
  777. SceneObject sceneCameraSO = new SceneObject("SceneCamera", true);
  778. camera = sceneCameraSO.AddComponent<Camera>();
  779. camera.Viewport.Target = renderTexture;
  780. camera.Viewport.Area = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  781. Vector3 camPosition = new Vector3(0.0f, 1.7f, 5.0f);
  782. object camPosObj = ProjectSettings.GetObject<object>(CameraPositionKey);
  783. if (camPosObj is Vector3)
  784. camPosition = (Vector3)camPosObj;
  785. Quaternion camRotation = Quaternion.Identity;
  786. object camRotObj = ProjectSettings.GetObject<object>(CameraRotationKey);
  787. if (camRotObj is Quaternion)
  788. camRotation = (Quaternion)camRotObj;
  789. sceneCameraSO.Position = camPosition;
  790. sceneCameraSO.Rotation = camRotation;
  791. camera.Priority = 2;
  792. camera.Viewport.ClearColor = ClearColor;
  793. camera.Layers = UInt64.MaxValue & ~SceneAxesHandle.LAYER; // Don't draw scene axes in this camera
  794. sceneCamera = sceneCameraSO.AddComponent<SceneCamera>();
  795. renderTextureGUI = new GUIRenderTexture(renderTexture);
  796. rtPanel.AddElement(renderTextureGUI);
  797. sceneGrid = new SceneGrid(camera);
  798. sceneSelection = new SceneSelection(camera);
  799. sceneGizmos = new SceneGizmos(camera);
  800. sceneHandles = new SceneHandles(this, camera);
  801. }
  802. else
  803. {
  804. camera.Viewport.Target = renderTexture;
  805. renderTextureGUI.RenderTexture = renderTexture;
  806. }
  807. Rect2I rtBounds = new Rect2I(0, 0, width, height);
  808. renderTextureGUI.Bounds = rtBounds;
  809. focusCatcher.Bounds = GUIUtility.CalculateBounds(rtPanel, GUI);
  810. sceneAxesGUI.SetPosition(width - HandleAxesGUISize - HandleAxesGUIPaddingX, HandleAxesGUIPaddingY);
  811. // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
  812. // render target destroy/create cycle for every single pixel.
  813. camera.AspectRatio = width / (float)height;
  814. }
  815. /// <summary>
  816. /// Set up preview boxes for the currently selected cameras.
  817. /// </summary>
  818. private void UpdateCameraPreviews()
  819. {
  820. SceneObject[] selectedObjects = Selection.SceneObjects;
  821. // Hide unpinned camera previews which are not selected
  822. for (int i = 0; i < cameraPreviews.Count; i++)
  823. {
  824. var cameraPreview = cameraPreviews[i];
  825. // Remove preview for destroyed cameras
  826. if (cameraPreview.Camera == null || cameraPreview.Camera.SceneObject == null
  827. || /*Temporary hack until we can render preview non-main cameras properly*/ !cameraPreview.Camera.Main)
  828. {
  829. HideCameraPreview(cameraPreview.Camera);
  830. continue;
  831. }
  832. if (cameraPreview.IsPinned)
  833. continue;
  834. bool cameraExists = Array.Exists(selectedObjects, x => x == cameraPreview.Camera.SceneObject);
  835. if (!cameraExists)
  836. HideCameraPreview(cameraPreview.Camera);
  837. }
  838. // Create preview for selected cameras
  839. for (int i = 0; i < selectedObjects.Length; i++)
  840. {
  841. if (cameraPreviews.Count >= MaxCameraPreviews)
  842. break;
  843. var selectedCamera = selectedObjects[i].GetComponent<Camera>();
  844. if (selectedCamera != null && /*Temporary hack until we can render preview non-main cameras properly*/ selectedCamera.Main)
  845. {
  846. var cameraPreview = cameraPreviews.Find(x => x.Camera == selectedCamera);
  847. if (cameraPreview == null)
  848. cameraPreviews.Add(new CameraPreview(selectedCamera, cameraPreviewsPanel));
  849. }
  850. }
  851. // Update preview box placement
  852. int previewsCount = cameraPreviews.Count;
  853. if (previewsCount > 0)
  854. {
  855. const float PreviewBoxAspect = 16.0f / 9.0f;
  856. const float PreviewBoxMinHeight = 80;
  857. const float PreviewBoxMinWidth = PreviewBoxMinHeight * PreviewBoxAspect;
  858. const float PreviewBoxMaxHeight = 150;
  859. const float PreviewBoxMaxWidth = PreviewBoxMaxHeight * PreviewBoxAspect;
  860. const float PreviewBoxViewSpaceMaxPercentage = 0.9f;
  861. const float PreviewBoxMarigin = 10;
  862. Rect2I rtBounds = renderTextureGUI.VisibleBounds;
  863. Vector2 rtSize = new Vector2(rtBounds.width, rtBounds.height);
  864. Vector2 usableSize = rtSize * PreviewBoxViewSpaceMaxPercentage - PreviewBoxMarigin;
  865. Vector2 previewSize = usableSize / previewsCount - previewsCount * PreviewBoxMarigin;
  866. float previewWidth = MathEx.Clamp(previewSize.x, PreviewBoxMinWidth, PreviewBoxMaxWidth);
  867. float previewHeight = previewWidth / PreviewBoxAspect;
  868. previewSize = new Vector2(previewWidth, previewHeight);
  869. int countPerRow = MathEx.FloorToInt(usableSize.x / previewSize.x);
  870. int countPerColumn = MathEx.FloorToInt(usableSize.y / previewSize.y);
  871. int index = 0;
  872. for (int y = 1; y <= countPerColumn; y++)
  873. {
  874. for (int x = 1; x <= countPerRow; x++)
  875. {
  876. if (index == previewsCount)
  877. break;
  878. var pos = rtSize - (previewSize + PreviewBoxMarigin) * new Vector2(x, y);
  879. var cameraPreview = cameraPreviews[index++];
  880. cameraPreview.ShowPreview(cameraPreview.Camera,
  881. new Rect2I((int)pos.x, (int)pos.y, (int)previewSize.x, (int)previewSize.y));
  882. }
  883. if (index == previewsCount)
  884. break;
  885. }
  886. }
  887. }
  888. /// <summary>
  889. /// Hides the preview box for the provided camera, if one exists.
  890. /// </summary>
  891. /// <param name="camera">Camera whose preview box to hide.</param>
  892. private void HideCameraPreview(Camera camera)
  893. {
  894. var cameraPreview = cameraPreviews.Find(x => x.Camera == camera);
  895. if (cameraPreview != null)
  896. {
  897. cameraPreviews.Remove(cameraPreview);
  898. cameraPreview.Destroy();
  899. }
  900. }
  901. /// <summary>
  902. /// Hides all the camera preview boxes.
  903. /// </summary>
  904. private void HideAllCameraPreviews()
  905. {
  906. foreach(var entry in cameraPreviews)
  907. entry.Destroy();
  908. cameraPreviews.Clear();
  909. }
  910. /// <summary>
  911. /// Parses an array of scene objects and removes elements that are children of elements that are also in the array.
  912. /// </summary>
  913. /// <param name="objects">Array containing duplicate objects as input, and array without duplicate objects as
  914. /// output.</param>
  915. private void CleanDuplicates(ref SceneObject[] objects)
  916. {
  917. List<SceneObject> cleanList = new List<SceneObject>();
  918. for (int i = 0; i < objects.Length; i++)
  919. {
  920. bool foundParent = false;
  921. for (int j = 0; j < objects.Length; j++)
  922. {
  923. SceneObject elem = objects[i];
  924. while (elem != null && elem != objects[j])
  925. elem = elem.Parent;
  926. bool isChildOf = elem == objects[j];
  927. if (i != j && isChildOf)
  928. {
  929. foundParent = true;
  930. break;
  931. }
  932. }
  933. if (!foundParent)
  934. cleanList.Add(objects[i]);
  935. }
  936. objects = cleanList.ToArray();
  937. }
  938. /// <summary>
  939. /// Checks if the load progress bar needs to be shown, shows/hides it and updates the progress accordingly.
  940. /// </summary>
  941. private void UpdateLoadingProgress()
  942. {
  943. bool needsProgress = EditorApplication.IsSceneLoading;
  944. if (needsProgress && !loadingProgressShown)
  945. {
  946. progressLayout.Active = true;
  947. rtPanel.Active = false;
  948. sceneAxesPanel.Active = false;
  949. loadingProgressShown = true;
  950. }
  951. else if (!needsProgress && loadingProgressShown)
  952. {
  953. progressLayout.Active = false;
  954. rtPanel.Active = true;
  955. sceneAxesPanel.Active = true;
  956. loadingProgressShown = false;
  957. }
  958. if (needsProgress)
  959. loadProgressBar.Percent = EditorApplication.SceneLoadProgress;
  960. }
  961. /// <summary>
  962. /// Starts a drag operation that displays a selection outline allowing the user to select multiple entries at once.
  963. /// </summary>
  964. /// <param name="scenePos">Coordinates relative to the scene where the drag originated.</param>
  965. private void StartDragSelection(Vector2I scenePos)
  966. {
  967. isDraggingSelection = true;
  968. dragSelectionStart = scenePos;
  969. dragSelectionEnd = dragSelectionStart;
  970. }
  971. /// <summary>
  972. /// Updates a selection outline drag operation by expanding the outline to the new location. Elements in the outline
  973. /// are selected.
  974. /// </summary>
  975. /// <param name="scenePos">Coordinates of the pointer relative to the scene.</param>
  976. /// <returns>True if the selection outline drag is valid and was updated, false otherwise.</returns>
  977. private bool UpdateDragSelection(Vector2I scenePos)
  978. {
  979. if (!isDraggingSelection)
  980. return false;
  981. if (dragSelection == null)
  982. {
  983. dragSelection = new GUITexture(null, true, EditorStylesInternal.SelectionArea);
  984. selectionPanel.AddElement(dragSelection);
  985. }
  986. dragSelectionEnd = scenePos;
  987. Rect2I selectionArea = new Rect2I();
  988. Vector2I min = new Vector2I(Math.Min(dragSelectionStart.x, dragSelectionEnd.x), Math.Min(dragSelectionStart.y, dragSelectionEnd.y));
  989. Vector2I max = new Vector2I(Math.Max(dragSelectionStart.x, dragSelectionEnd.x), Math.Max(dragSelectionStart.y, dragSelectionEnd.y));
  990. selectionArea.x = min.x;
  991. selectionArea.y = min.y;
  992. selectionArea.width = Math.Max(max.x - min.x, 1);
  993. selectionArea.height = Math.Max(max.y - min.y, 1);
  994. dragSelection.Bounds = selectionArea;
  995. return true;
  996. }
  997. /// <summary>
  998. /// Ends the selection outline drag operation. Elements in the outline are selected.
  999. /// </summary>
  1000. /// <returns>True if the selection outline drag is valid and was ended, false otherwise.</returns>
  1001. private bool EndDragSelection()
  1002. {
  1003. if (!isDraggingSelection)
  1004. return false;
  1005. if (dragSelection != null)
  1006. {
  1007. dragSelection.Destroy();
  1008. dragSelection = null;
  1009. }
  1010. if ((dragSelectionEnd - dragSelectionStart).Length < 1)
  1011. {
  1012. isDraggingSelection = false;
  1013. return false;
  1014. }
  1015. Vector2I min = new Vector2I(Math.Min(dragSelectionStart.x, dragSelectionEnd.x),
  1016. Math.Min(dragSelectionStart.y, dragSelectionEnd.y));
  1017. Vector2I max = new Vector2I(Math.Max(dragSelectionStart.x, dragSelectionEnd.x),
  1018. Math.Max(dragSelectionStart.y, dragSelectionEnd.y));
  1019. sceneSelection.PickObjects(min, max - min,
  1020. Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl));
  1021. isDraggingSelection = false;
  1022. return true;
  1023. }
  1024. /// <summary>
  1025. /// Contains information about world transform of a single scene object.
  1026. /// </summary>
  1027. struct Transform
  1028. {
  1029. public Transform(SceneObject so)
  1030. {
  1031. position = so.Position;
  1032. rotation = so.Rotation;
  1033. scale = so.Scale;
  1034. }
  1035. /// <summary>
  1036. /// Applies the saved transform to the specified scene object. The transform is assumed to be in world space.
  1037. /// </summary>
  1038. /// <param name="so">Scene object to apply the transform to.</param>
  1039. public void Apply(SceneObject so)
  1040. {
  1041. so.Position = position;
  1042. so.Rotation = rotation;
  1043. so.LocalScale = scale;
  1044. }
  1045. public Vector3 position;
  1046. public Quaternion rotation;
  1047. public Vector3 scale;
  1048. }
  1049. }
  1050. /** @} */
  1051. }