InspectorWindow.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Common;
  4. using System.IO;
  5. using BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /// <summary>
  9. /// Displays GUI for a <see cref="SceneObject"/> or for a <see cref="Resource"/>. Scene object's transform values
  10. /// are displayed, along with all their components and their fields.
  11. /// </summary>
  12. internal sealed class InspectorWindow : EditorWindow
  13. {
  14. /// <summary>
  15. /// Type of objects displayed in the window.
  16. /// </summary>
  17. private enum InspectorType
  18. {
  19. SceneObject,
  20. Resource,
  21. Multiple,
  22. None
  23. }
  24. /// <summary>
  25. /// Inspector GUI elements for a single <see cref="Component"/> in a <see cref="SceneObject"/>.
  26. /// </summary>
  27. private class InspectorComponent
  28. {
  29. public GUIToggle foldout;
  30. public GUIButton removeBtn;
  31. public GUILayout title;
  32. public GUIPanel panel;
  33. public Inspector inspector;
  34. public bool expanded = true;
  35. public UInt64 instanceId;
  36. }
  37. /// <summary>
  38. /// Inspector GUI elements for a <see cref="Resource"/>
  39. /// </summary>
  40. private class InspectorResource
  41. {
  42. public GUIPanel panel;
  43. public Inspector inspector;
  44. }
  45. private static readonly Color HIGHLIGHT_COLOR = new Color(1.0f, 1.0f, 1.0f, 0.5f);
  46. private const int RESOURCE_TITLE_HEIGHT = 30;
  47. private const int COMPONENT_SPACING = 10;
  48. private const int PADDING = 5;
  49. private List<InspectorComponent> inspectorComponents = new List<InspectorComponent>();
  50. private InspectorResource inspectorResource;
  51. private GUIScrollArea inspectorScrollArea;
  52. private GUILayout inspectorLayout;
  53. private GUIPanel highlightPanel;
  54. private GUITexture scrollAreaHighlight;
  55. private SceneObject activeSO;
  56. private GUITextBox soNameInput;
  57. private GUILayout soPrefabLayout;
  58. private bool soHasPrefab;
  59. private GUIFloatField soPosX;
  60. private GUIFloatField soPosY;
  61. private GUIFloatField soPosZ;
  62. private GUIFloatField soRotX;
  63. private GUIFloatField soRotY;
  64. private GUIFloatField soRotZ;
  65. private GUIFloatField soScaleX;
  66. private GUIFloatField soScaleY;
  67. private GUIFloatField soScaleZ;
  68. private Rect2I[] dropAreas = new Rect2I[0];
  69. private InspectorType currentType = InspectorType.None;
  70. private Resource activeResource;
  71. /// <summary>
  72. /// Opens the inspector window from the menu bar.
  73. /// </summary>
  74. [MenuItem("Windows/Inspector", ButtonModifier.CtrlAlt, ButtonCode.I, 6000)]
  75. private static void OpenInspectorWindow()
  76. {
  77. OpenWindow<InspectorWindow>();
  78. }
  79. /// <summary>
  80. /// Name of the inspector window to display on the window title.
  81. /// </summary>
  82. /// <returns>Name of the inspector window to display on the window title.</returns>
  83. protected override LocString GetDisplayName()
  84. {
  85. return new LocEdString("Inspector");
  86. }
  87. /// <summary>
  88. /// Sets a resource whose GUI is to be displayed in the inspector. Clears any previous contents of the window.
  89. /// </summary>
  90. /// <param name="resourcePath">Resource path relative to the project of the resource to inspect.</param>
  91. private void SetObjectToInspect(String resourcePath)
  92. {
  93. activeResource = ProjectLibrary.Load<Resource>(resourcePath);
  94. if (activeResource == null)
  95. return;
  96. currentType = InspectorType.Resource;
  97. inspectorScrollArea = new GUIScrollArea();
  98. GUI.AddElement(inspectorScrollArea);
  99. inspectorLayout = inspectorScrollArea.Layout;
  100. GUIPanel titlePanel = inspectorLayout.AddPanel();
  101. titlePanel.SetHeight(RESOURCE_TITLE_HEIGHT);
  102. GUILayoutY titleLayout = titlePanel.AddLayoutY();
  103. titleLayout.SetPosition(PADDING, PADDING);
  104. string name = Path.GetFileNameWithoutExtension(resourcePath);
  105. string type = activeResource.GetType().Name;
  106. LocString title = new LocEdString(name + " (" + type + ")");
  107. GUILabel titleLabel = new GUILabel(title);
  108. titleLayout.AddFlexibleSpace();
  109. GUILayoutX titleLabelLayout = titleLayout.AddLayoutX();
  110. titleLabelLayout.AddElement(titleLabel);
  111. titleLayout.AddFlexibleSpace();
  112. GUIPanel titleBgPanel = titlePanel.AddPanel(1);
  113. GUITexture titleBg = new GUITexture(null, EditorStyles.InspectorTitleBg);
  114. titleBgPanel.AddElement(titleBg);
  115. inspectorLayout.AddSpace(COMPONENT_SPACING);
  116. inspectorResource = new InspectorResource();
  117. inspectorResource.panel = inspectorLayout.AddPanel();
  118. inspectorResource.inspector = InspectorUtility.GetInspector(activeResource.GetType());
  119. inspectorResource.inspector.Initialize(inspectorResource.panel, activeResource);
  120. inspectorLayout.AddFlexibleSpace();
  121. }
  122. /// <summary>
  123. /// Sets a scene object whose GUI is to be displayed in the inspector. Clears any previous contents of the window.
  124. /// </summary>
  125. /// <param name="so">Scene object to inspect.</param>
  126. private void SetObjectToInspect(SceneObject so)
  127. {
  128. if (so == null)
  129. return;
  130. currentType = InspectorType.SceneObject;
  131. activeSO = so;
  132. inspectorScrollArea = new GUIScrollArea();
  133. scrollAreaHighlight = new GUITexture(Builtin.WhiteTexture);
  134. scrollAreaHighlight.SetTint(HIGHLIGHT_COLOR);
  135. scrollAreaHighlight.Active = false;
  136. GUI.AddElement(inspectorScrollArea);
  137. GUIPanel inspectorPanel = inspectorScrollArea.Layout.AddPanel();
  138. inspectorLayout = inspectorPanel.AddLayoutY();
  139. highlightPanel = inspectorPanel.AddPanel(-1);
  140. highlightPanel.AddElement(scrollAreaHighlight);
  141. // SceneObject fields
  142. CreateSceneObjectFields();
  143. RefreshSceneObjectFields(true);
  144. // Components
  145. Component[] allComponents = so.GetComponents();
  146. for (int i = 0; i < allComponents.Length; i++)
  147. {
  148. inspectorLayout.AddSpace(COMPONENT_SPACING);
  149. InspectorComponent data = new InspectorComponent();
  150. data.instanceId = allComponents[i].InstanceId;
  151. data.foldout = new GUIToggle(allComponents[i].GetType().Name, EditorStyles.Foldout);
  152. data.removeBtn = new GUIButton(new GUIContent(EditorBuiltin.XBtnIcon), GUIOption.FixedWidth(30));
  153. data.title = inspectorLayout.AddLayoutX();
  154. data.title.AddElement(data.foldout);
  155. data.title.AddElement(data.removeBtn);
  156. data.panel = inspectorLayout.AddPanel();
  157. data.inspector = InspectorUtility.GetInspector(allComponents[i].GetType());
  158. data.inspector.Initialize(data.panel, allComponents[i]);
  159. data.foldout.Value = true;
  160. Type curComponentType = allComponents[i].GetType();
  161. data.foldout.OnToggled += (bool expanded) => OnComponentFoldoutToggled(data, expanded);
  162. data.removeBtn.OnClick += () => OnComponentRemoveClicked(curComponentType);
  163. inspectorComponents.Add(data);
  164. }
  165. inspectorLayout.AddFlexibleSpace();
  166. UpdateDropAreas();
  167. }
  168. /// <summary>
  169. /// Creates GUI elements required for displaying <see cref="SceneObject"/> fields like name, prefab data and
  170. /// transform (position, rotation, scale). Assumes that necessary inspector scroll area layout has already been
  171. /// created.
  172. /// </summary>
  173. private void CreateSceneObjectFields()
  174. {
  175. GUIPanel sceneObjectPanel = inspectorLayout.AddPanel();
  176. sceneObjectPanel.SetHeight(GetTitleBounds().height);
  177. GUILayoutY sceneObjectLayout = sceneObjectPanel.AddLayoutY();
  178. sceneObjectLayout.SetPosition(PADDING, PADDING);
  179. GUIPanel sceneObjectBgPanel = sceneObjectPanel.AddPanel(1);
  180. GUILayoutX nameLayout = sceneObjectLayout.AddLayoutX();
  181. GUILabel nameLbl = new GUILabel(new LocEdString("Name"), GUIOption.FixedWidth(50));
  182. soNameInput = new GUITextBox(false, GUIOption.FlexibleWidth(180));
  183. soNameInput.Text = activeSO.Name;
  184. soNameInput.OnChanged += (x) => { if (activeSO != null) activeSO.Name = x; };
  185. nameLayout.AddElement(nameLbl);
  186. nameLayout.AddElement(soNameInput);
  187. nameLayout.AddFlexibleSpace();
  188. soPrefabLayout = sceneObjectLayout.AddLayoutX();
  189. GUILayoutX positionLayout = sceneObjectLayout.AddLayoutX();
  190. GUILabel positionLbl = new GUILabel(new LocEdString("Position"), GUIOption.FixedWidth(50));
  191. soPosX = new GUIFloatField(new LocEdString("X"), 10, "", GUIOption.FixedWidth(60));
  192. soPosY = new GUIFloatField(new LocEdString("Y"), 10, "", GUIOption.FixedWidth(60));
  193. soPosZ = new GUIFloatField(new LocEdString("Z"), 10, "", GUIOption.FixedWidth(60));
  194. soPosX.OnChanged += (x) => OnPositionChanged(0, x);
  195. soPosY.OnChanged += (y) => OnPositionChanged(1, y);
  196. soPosZ.OnChanged += (z) => OnPositionChanged(2, z);
  197. positionLayout.AddElement(positionLbl);
  198. positionLayout.AddElement(soPosX);
  199. positionLayout.AddSpace(10);
  200. positionLayout.AddFlexibleSpace();
  201. positionLayout.AddElement(soPosY);
  202. positionLayout.AddSpace(10);
  203. positionLayout.AddFlexibleSpace();
  204. positionLayout.AddElement(soPosZ);
  205. positionLayout.AddFlexibleSpace();
  206. GUILayoutX rotationLayout = sceneObjectLayout.AddLayoutX();
  207. GUILabel rotationLbl = new GUILabel(new LocEdString("Rotation"), GUIOption.FixedWidth(50));
  208. soRotX = new GUIFloatField(new LocEdString("X"), 10, "", GUIOption.FixedWidth(60));
  209. soRotY = new GUIFloatField(new LocEdString("Y"), 10, "", GUIOption.FixedWidth(60));
  210. soRotZ = new GUIFloatField(new LocEdString("Z"), 10, "", GUIOption.FixedWidth(60));
  211. soRotX.OnChanged += (x) => OnRotationChanged(0, x);
  212. soRotY.OnChanged += (y) => OnRotationChanged(1, y);
  213. soRotZ.OnChanged += (z) => OnRotationChanged(2, z);
  214. rotationLayout.AddElement(rotationLbl);
  215. rotationLayout.AddElement(soRotX);
  216. rotationLayout.AddSpace(10);
  217. rotationLayout.AddFlexibleSpace();
  218. rotationLayout.AddElement(soRotY);
  219. rotationLayout.AddSpace(10);
  220. rotationLayout.AddFlexibleSpace();
  221. rotationLayout.AddElement(soRotZ);
  222. rotationLayout.AddFlexibleSpace();
  223. GUILayoutX scaleLayout = sceneObjectLayout.AddLayoutX();
  224. GUILabel scaleLbl = new GUILabel(new LocEdString("Scale"), GUIOption.FixedWidth(50));
  225. soScaleX = new GUIFloatField(new LocEdString("X"), 10, "", GUIOption.FixedWidth(60));
  226. soScaleY = new GUIFloatField(new LocEdString("Y"), 10, "", GUIOption.FixedWidth(60));
  227. soScaleZ = new GUIFloatField(new LocEdString("Z"), 10, "", GUIOption.FixedWidth(60));
  228. soScaleX.OnChanged += (x) => OnScaleChanged(0, x);
  229. soScaleY.OnChanged += (y) => OnScaleChanged(1, y);
  230. soScaleX.OnChanged += (z) => OnScaleChanged(2, z);
  231. scaleLayout.AddElement(scaleLbl);
  232. scaleLayout.AddElement(soScaleX);
  233. scaleLayout.AddSpace(10);
  234. scaleLayout.AddFlexibleSpace();
  235. scaleLayout.AddElement(soScaleY);
  236. scaleLayout.AddSpace(10);
  237. scaleLayout.AddFlexibleSpace();
  238. scaleLayout.AddElement(soScaleZ);
  239. scaleLayout.AddFlexibleSpace();
  240. sceneObjectLayout.AddFlexibleSpace();
  241. GUITexture titleBg = new GUITexture(null, EditorStyles.InspectorTitleBg);
  242. sceneObjectBgPanel.AddElement(titleBg);
  243. }
  244. /// <summary>
  245. /// Updates contents of the scene object specific fields (name, position, rotation, etc.)
  246. /// </summary>
  247. /// <param name="forceUpdate">If true, the GUI elements will be updated regardless of whether a change was
  248. /// detected or not.</param>
  249. private void RefreshSceneObjectFields(bool forceUpdate)
  250. {
  251. if (activeSO == null)
  252. return;
  253. soNameInput.Text = activeSO.Name;
  254. bool hasPrefab = PrefabUtility.IsPrefabInstance(activeSO);
  255. if (soHasPrefab != hasPrefab || forceUpdate)
  256. {
  257. int numChildren = soPrefabLayout.ChildCount;
  258. for (int i = 0; i < numChildren; i++)
  259. soPrefabLayout.GetChild(0).Destroy();
  260. GUILabel prefabLabel =new GUILabel(new LocEdString("Prefab"), GUIOption.FixedWidth(50));
  261. soPrefabLayout.AddElement(prefabLabel);
  262. if (hasPrefab)
  263. {
  264. GUIButton btnApplyPrefab = new GUIButton(new LocEdString("Apply"), GUIOption.FixedWidth(60));
  265. GUIButton btnRevertPrefab = new GUIButton(new LocEdString("Revert"), GUIOption.FixedWidth(60));
  266. GUIButton btnBreakPrefab = new GUIButton(new LocEdString("Break"), GUIOption.FixedWidth(60));
  267. btnApplyPrefab.OnClick += () => PrefabUtility.ApplyPrefab(activeSO);
  268. btnRevertPrefab.OnClick += () => PrefabUtility.RevertPrefab(activeSO);
  269. btnBreakPrefab.OnClick += () => PrefabUtility.BreakPrefab(activeSO);
  270. soPrefabLayout.AddElement(btnApplyPrefab);
  271. soPrefabLayout.AddElement(btnRevertPrefab);
  272. soPrefabLayout.AddElement(btnBreakPrefab);
  273. }
  274. else
  275. {
  276. GUILabel noPrefabLabel = new GUILabel("None");
  277. soPrefabLayout.AddElement(noPrefabLabel);
  278. }
  279. soHasPrefab = hasPrefab;
  280. }
  281. Vector3 position;
  282. Vector3 angles;
  283. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  284. {
  285. position = activeSO.Position;
  286. angles = activeSO.Rotation.ToEuler();
  287. }
  288. else
  289. {
  290. position = activeSO.LocalPosition;
  291. angles = activeSO.LocalRotation.ToEuler();
  292. }
  293. Vector3 scale = activeSO.LocalScale;
  294. soPosX.Value = position.x;
  295. soPosY.Value = position.y;
  296. soPosZ.Value = position.z;
  297. soRotX.Value = angles.x;
  298. soRotY.Value = angles.y;
  299. soRotZ.Value = angles.z;
  300. soScaleX.Value = scale.x;
  301. soScaleY.Value = scale.y;
  302. soScaleZ.Value = scale.z;
  303. }
  304. private void OnInitialize()
  305. {
  306. Selection.OnSelectionChanged += OnSelectionChanged;
  307. OnSelectionChanged(new SceneObject[0], new string[0]);
  308. }
  309. private void OnDestroy()
  310. {
  311. Selection.OnSelectionChanged -= OnSelectionChanged;
  312. }
  313. private void OnEditorUpdate()
  314. {
  315. if (currentType == InspectorType.SceneObject)
  316. {
  317. Component[] allComponents = activeSO.GetComponents();
  318. bool requiresRebuild = allComponents.Length != inspectorComponents.Count;
  319. if (!requiresRebuild)
  320. {
  321. for (int i = 0; i < inspectorComponents.Count; i++)
  322. {
  323. if (inspectorComponents[i].instanceId != allComponents[i].InstanceId)
  324. {
  325. requiresRebuild = true;
  326. break;
  327. }
  328. }
  329. }
  330. if (requiresRebuild)
  331. {
  332. SceneObject so = activeSO;
  333. Clear();
  334. SetObjectToInspect(so);
  335. }
  336. else
  337. {
  338. RefreshSceneObjectFields(false);
  339. for (int i = 0; i < inspectorComponents.Count; i++)
  340. inspectorComponents[i].inspector.Refresh();
  341. }
  342. }
  343. else if (currentType == InspectorType.Resource)
  344. {
  345. inspectorResource.inspector.Refresh();
  346. }
  347. // Detect drag and drop
  348. bool isValidDrag = false;
  349. if (activeSO != null)
  350. {
  351. if ((DragDrop.DragInProgress || DragDrop.DropInProgress) && DragDrop.Type == DragDropType.Resource)
  352. {
  353. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  354. Vector2I scrollPos = windowPos;
  355. Rect2I contentBounds = inspectorLayout.Bounds;
  356. scrollPos.x -= contentBounds.x;
  357. scrollPos.y -= contentBounds.y;
  358. bool isInBounds = false;
  359. Rect2I dropArea = new Rect2I();
  360. foreach (var bounds in dropAreas)
  361. {
  362. if (bounds.Contains(scrollPos))
  363. {
  364. isInBounds = true;
  365. dropArea = bounds;
  366. break;
  367. }
  368. }
  369. Type draggedComponentType = null;
  370. if (isInBounds)
  371. {
  372. ResourceDragDropData dragData = DragDrop.Data as ResourceDragDropData;
  373. if (dragData != null)
  374. {
  375. foreach (var resPath in dragData.Paths)
  376. {
  377. LibraryEntry entry = ProjectLibrary.GetEntry(resPath);
  378. FileEntry fileEntry = entry as FileEntry;
  379. if (fileEntry != null)
  380. {
  381. if (fileEntry.ResType == ResourceType.ScriptCode)
  382. {
  383. ScriptCode scriptFile = ProjectLibrary.Load<ScriptCode>(resPath);
  384. if (scriptFile != null)
  385. {
  386. Type[] scriptTypes = scriptFile.Types;
  387. foreach (var type in scriptTypes)
  388. {
  389. if (type.IsSubclassOf(typeof (Component)))
  390. {
  391. draggedComponentType = type;
  392. isValidDrag = true;
  393. break;
  394. }
  395. }
  396. if (draggedComponentType != null)
  397. break;
  398. }
  399. }
  400. }
  401. }
  402. }
  403. }
  404. if (isValidDrag)
  405. {
  406. scrollAreaHighlight.Bounds = dropArea;
  407. if (DragDrop.DropInProgress)
  408. {
  409. UndoRedo.RecordSO(activeSO, "Added component " + draggedComponentType.Name);
  410. activeSO.AddComponent(draggedComponentType);
  411. }
  412. }
  413. }
  414. }
  415. if (scrollAreaHighlight != null)
  416. scrollAreaHighlight.Active = isValidDrag;
  417. }
  418. /// <summary>
  419. /// Triggered when the user selects a new resource or a scene object, or deselects everything.
  420. /// </summary>
  421. /// <param name="objects">A set of new scene objects that were selected.</param>
  422. /// <param name="paths">A set of absolute resource paths that were selected.</param>
  423. private void OnSelectionChanged(SceneObject[] objects, string[] paths)
  424. {
  425. Clear();
  426. if (objects.Length == 0 && paths.Length == 0)
  427. {
  428. currentType = InspectorType.None;
  429. inspectorScrollArea = new GUIScrollArea();
  430. GUI.AddElement(inspectorScrollArea);
  431. inspectorLayout = inspectorScrollArea.Layout;
  432. inspectorLayout.AddFlexibleSpace();
  433. GUILayoutX layoutMsg = inspectorLayout.AddLayoutX();
  434. layoutMsg.AddFlexibleSpace();
  435. layoutMsg.AddElement(new GUILabel(new LocEdString("No object selected")));
  436. layoutMsg.AddFlexibleSpace();
  437. inspectorLayout.AddFlexibleSpace();
  438. }
  439. else if ((objects.Length + paths.Length) > 1)
  440. {
  441. currentType = InspectorType.None;
  442. inspectorScrollArea = new GUIScrollArea();
  443. GUI.AddElement(inspectorScrollArea);
  444. inspectorLayout = inspectorScrollArea.Layout;
  445. inspectorLayout.AddFlexibleSpace();
  446. GUILayoutX layoutMsg = inspectorLayout.AddLayoutX();
  447. layoutMsg.AddFlexibleSpace();
  448. layoutMsg.AddElement(new GUILabel(new LocEdString("Multiple objects selected")));
  449. layoutMsg.AddFlexibleSpace();
  450. inspectorLayout.AddFlexibleSpace();
  451. }
  452. else if (objects.Length == 1)
  453. {
  454. SetObjectToInspect(objects[0]);
  455. }
  456. else if (paths.Length == 1)
  457. {
  458. SetObjectToInspect(paths[0]);
  459. }
  460. }
  461. /// <summary>
  462. /// Triggered when the user closes or expands a component foldout, making the component fields visible or hidden.
  463. /// </summary>
  464. /// <param name="inspectorData">Contains GUI data for the component that was toggled.</param>
  465. /// <param name="expanded">Determines whether to display or hide component contents.</param>
  466. private void OnComponentFoldoutToggled(InspectorComponent inspectorData, bool expanded)
  467. {
  468. inspectorData.expanded = expanded;
  469. inspectorData.inspector.SetVisible(expanded);
  470. }
  471. /// <summary>
  472. /// Triggered when the user clicks the component remove button. Removes that component from the active scene object.
  473. /// </summary>
  474. /// <param name="componentType">Type of the component to remove.</param>
  475. private void OnComponentRemoveClicked(Type componentType)
  476. {
  477. if (activeSO != null)
  478. {
  479. UndoRedo.RecordSO(activeSO, "Removed component " + componentType.Name);
  480. activeSO.RemoveComponent(componentType);
  481. }
  482. }
  483. /// <summary>
  484. /// Destroys all inspector GUI elements.
  485. /// </summary>
  486. internal void Clear()
  487. {
  488. for (int i = 0; i < inspectorComponents.Count; i++)
  489. {
  490. inspectorComponents[i].foldout.Destroy();
  491. inspectorComponents[i].removeBtn.Destroy();
  492. inspectorComponents[i].inspector.Destroy();
  493. }
  494. inspectorComponents.Clear();
  495. if (inspectorResource != null)
  496. {
  497. inspectorResource.inspector.Destroy();
  498. inspectorResource = null;
  499. }
  500. if (inspectorScrollArea != null)
  501. {
  502. inspectorScrollArea.Destroy();
  503. inspectorScrollArea = null;
  504. }
  505. if (scrollAreaHighlight != null)
  506. {
  507. scrollAreaHighlight.Destroy();
  508. scrollAreaHighlight = null;
  509. }
  510. if (highlightPanel != null)
  511. {
  512. highlightPanel.Destroy();
  513. highlightPanel = null;
  514. }
  515. activeSO = null;
  516. soNameInput = null;
  517. soPrefabLayout = null;
  518. soHasPrefab = false;
  519. soPosX = null;
  520. soPosY = null;
  521. soPosZ = null;
  522. soRotX = null;
  523. soRotY = null;
  524. soRotZ = null;
  525. soScaleX = null;
  526. soScaleY = null;
  527. soScaleZ = null;
  528. dropAreas = new Rect2I[0];
  529. activeResource = null;
  530. currentType = InspectorType.None;
  531. }
  532. /// <summary>
  533. /// Returns the size of the title bar area that is displayed for <see cref="SceneObject"/> specific fields.
  534. /// </summary>
  535. /// <returns>Area of the title bar, relative to the window.</returns>
  536. private Rect2I GetTitleBounds()
  537. {
  538. return new Rect2I(0, 0, Width, 115);
  539. }
  540. /// <summary>
  541. /// Triggered when the position value in the currently active <see cref="SceneObject"/> changes. Updates the
  542. /// necessary GUI elements.
  543. /// </summary>
  544. /// <param name="idx">Index of the coordinate that was changed.</param>
  545. /// <param name="value">New value of the field.</param>
  546. private void OnPositionChanged(int idx, float value)
  547. {
  548. if (activeSO == null)
  549. return;
  550. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  551. {
  552. Vector3 position = activeSO.Position;
  553. position[idx] = value;
  554. activeSO.Position = position;
  555. }
  556. else
  557. {
  558. Vector3 position = activeSO.LocalPosition;
  559. position[idx] = value;
  560. activeSO.LocalPosition = position;
  561. }
  562. }
  563. /// <summary>
  564. /// Triggered when the rotation value in the currently active <see cref="SceneObject"/> changes. Updates the
  565. /// necessary GUI elements.
  566. /// </summary>
  567. /// <param name="idx">Index of the euler angle that was changed (0 - X, 1 - Y, 2 - Z).</param>
  568. /// <param name="value">New value of the field.</param>
  569. private void OnRotationChanged(int idx, float value)
  570. {
  571. if (activeSO == null)
  572. return;
  573. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  574. {
  575. Vector3 angles = activeSO.Rotation.ToEuler();
  576. angles[idx] = value;
  577. activeSO.Rotation = Quaternion.FromEuler(angles);
  578. }
  579. else
  580. {
  581. Vector3 angles = activeSO.LocalRotation.ToEuler();
  582. angles[idx] = value;
  583. activeSO.LocalRotation = Quaternion.FromEuler(angles);
  584. }
  585. }
  586. /// <summary>
  587. /// Triggered when the scale value in the currently active <see cref="SceneObject"/> changes. Updates the
  588. /// necessary GUI elements.
  589. /// </summary>
  590. /// <param name="idx">Index of the coordinate that was changed.</param>
  591. /// <param name="value">New value of the field.</param>
  592. private void OnScaleChanged(int idx, float value)
  593. {
  594. if (activeSO == null)
  595. return;
  596. Vector3 scale = activeSO.LocalScale;
  597. scale[idx] = value;
  598. activeSO.LocalScale = scale;
  599. }
  600. /// <inheritdoc/>
  601. protected override void WindowResized(int width, int height)
  602. {
  603. base.WindowResized(width, height);
  604. UpdateDropAreas();
  605. }
  606. /// <summary>
  607. /// Updates drop areas used for dragging and dropping components on the inspector.
  608. /// </summary>
  609. private void UpdateDropAreas()
  610. {
  611. if (activeSO == null)
  612. return;
  613. Rect2I contentBounds = inspectorLayout.Bounds;
  614. dropAreas = new Rect2I[inspectorComponents.Count + 1];
  615. int yOffset = GetTitleBounds().height;
  616. for (int i = 0; i < inspectorComponents.Count; i++)
  617. {
  618. dropAreas[i] = new Rect2I(0, yOffset, contentBounds.width, COMPONENT_SPACING);
  619. yOffset += inspectorComponents[i].title.Bounds.height + inspectorComponents[i].panel.Bounds.height + COMPONENT_SPACING;
  620. }
  621. dropAreas[dropAreas.Length - 1] = new Rect2I(0, yOffset, contentBounds.width, contentBounds.height - yOffset);
  622. }
  623. }
  624. }