InspectorWindow.cs 31 KB

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