InspectorWindow.cs 29 KB

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