InspectorWindow.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 bs;
  7. namespace bs.Editor
  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 UUID uuid;
  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 mainPanel;
  47. public GUIPanel previewPanel;
  48. public Inspector inspector;
  49. }
  50. private static readonly Color HIGHLIGHT_COLOR = new Color(1.0f, 1.0f, 1.0f, 0.5f);
  51. private const int RESOURCE_TITLE_HEIGHT = 30;
  52. private const int COMPONENT_SPACING = 10;
  53. private const int PADDING = 5;
  54. private List<InspectorComponent> inspectorComponents = new List<InspectorComponent>();
  55. private InspectorPersistentData persistentData;
  56. private InspectorResource inspectorResource;
  57. private GUIScrollArea inspectorScrollArea;
  58. private GUILayout inspectorLayout;
  59. private GUIPanel highlightPanel;
  60. private GUITexture scrollAreaHighlight;
  61. private SceneObject activeSO;
  62. private InspectableState modifyState;
  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. private bool resourceInspectorInitialized;
  76. /// <summary>
  77. /// Opens the inspector window from the menu bar.
  78. /// </summary>
  79. [MenuItem("Windows/Inspector", ButtonModifier.CtrlAlt, ButtonCode.I, 6000)]
  80. private static void OpenInspectorWindow()
  81. {
  82. OpenWindow<InspectorWindow>();
  83. }
  84. /// <summary>
  85. /// Name of the inspector window to display on the window title.
  86. /// </summary>
  87. /// <returns>Name of the inspector window to display on the window title.</returns>
  88. protected override LocString GetDisplayName()
  89. {
  90. return new LocEdString("Inspector");
  91. }
  92. /// <summary>
  93. /// Sets a resource whose GUI is to be displayed in the inspector. Clears any previous contents of the window.
  94. /// </summary>
  95. /// <param name="resourcePath">Resource path relative to the project of the resource to inspect.</param>
  96. private void SetObjectToInspect(string resourcePath)
  97. {
  98. activeResourcePath = resourcePath;
  99. resourceInspectorInitialized = false;
  100. if (!ProjectLibrary.Exists(resourcePath))
  101. return;
  102. ResourceMeta meta = ProjectLibrary.GetMeta(resourcePath);
  103. if (meta == null)
  104. return;
  105. Type resourceType = meta.Type;
  106. currentType = InspectorType.Resource;
  107. resourceInspectorInitialized = true;
  108. inspectorScrollArea = new GUIScrollArea(ScrollBarType.ShowIfDoesntFit, ScrollBarType.NeverShow);
  109. GUI.AddElement(inspectorScrollArea);
  110. inspectorLayout = inspectorScrollArea.Layout;
  111. GUIPanel titlePanel = inspectorLayout.AddPanel();
  112. titlePanel.SetHeight(RESOURCE_TITLE_HEIGHT);
  113. GUILayoutY titleLayout = titlePanel.AddLayoutY();
  114. titleLayout.SetPosition(PADDING, PADDING);
  115. string name = Path.GetFileNameWithoutExtension(resourcePath);
  116. string type = resourceType.Name;
  117. LocString title = new LocEdString(name + " (" + type + ")");
  118. GUILabel titleLabel = new GUILabel(title);
  119. titleLayout.AddFlexibleSpace();
  120. GUILayoutX titleLabelLayout = titleLayout.AddLayoutX();
  121. titleLabelLayout.AddElement(titleLabel);
  122. titleLayout.AddFlexibleSpace();
  123. GUIPanel titleBgPanel = titlePanel.AddPanel(1);
  124. GUITexture titleBg = new GUITexture(null, EditorStylesInternal.InspectorTitleBg);
  125. titleBgPanel.AddElement(titleBg);
  126. inspectorLayout.AddSpace(COMPONENT_SPACING);
  127. inspectorResource = new InspectorResource();
  128. inspectorResource.mainPanel = inspectorLayout.AddPanel();
  129. inspectorLayout.AddFlexibleSpace();
  130. inspectorResource.previewPanel = inspectorLayout.AddPanel();
  131. var persistentProperties = persistentData.GetProperties(meta.UUID.ToString());
  132. inspectorResource.inspector = InspectorUtility.GetInspector(resourceType);
  133. inspectorResource.inspector.Initialize(inspectorResource.mainPanel, inspectorResource.previewPanel,
  134. activeResourcePath, persistentProperties);
  135. }
  136. /// <summary>
  137. /// Sets a scene object whose GUI is to be displayed in the inspector. Clears any previous contents of the window.
  138. /// </summary>
  139. /// <param name="so">Scene object to inspect.</param>
  140. private void SetObjectToInspect(SceneObject so)
  141. {
  142. if (so == null)
  143. return;
  144. currentType = InspectorType.SceneObject;
  145. activeSO = so;
  146. inspectorScrollArea = new GUIScrollArea(ScrollBarType.ShowIfDoesntFit, ScrollBarType.NeverShow);
  147. scrollAreaHighlight = new GUITexture(Builtin.WhiteTexture);
  148. scrollAreaHighlight.SetTint(HIGHLIGHT_COLOR);
  149. scrollAreaHighlight.Active = false;
  150. GUI.AddElement(inspectorScrollArea);
  151. GUIPanel inspectorPanel = inspectorScrollArea.Layout.AddPanel();
  152. inspectorLayout = inspectorPanel.AddLayoutY();
  153. highlightPanel = inspectorPanel.AddPanel(-1);
  154. highlightPanel.AddElement(scrollAreaHighlight);
  155. // SceneObject fields
  156. CreateSceneObjectFields();
  157. RefreshSceneObjectFields(true);
  158. // Components
  159. Component[] allComponents = so.GetComponents();
  160. for (int i = 0; i < allComponents.Length; i++)
  161. {
  162. inspectorLayout.AddSpace(COMPONENT_SPACING);
  163. InspectorComponent data = new InspectorComponent();
  164. data.uuid = allComponents[i].UUID;
  165. data.folded = false;
  166. data.foldout = new GUIToggle(allComponents[i].GetType().Name, EditorStyles.Foldout);
  167. data.foldout.AcceptsKeyFocus = false;
  168. SpriteTexture xBtnIcon = EditorBuiltin.GetEditorIcon(EditorIcon.X);
  169. data.removeBtn = new GUIButton(new GUIContent(xBtnIcon), GUIOption.FixedWidth(30));
  170. data.title = inspectorLayout.AddLayoutX();
  171. data.title.AddElement(data.foldout);
  172. data.title.AddElement(data.removeBtn);
  173. data.panel = inspectorLayout.AddPanel();
  174. var persistentProperties = persistentData.GetProperties(allComponents[i].InstanceId);
  175. data.inspector = InspectorUtility.GetInspector(allComponents[i].GetType());
  176. data.inspector.Initialize(data.panel, allComponents[i], persistentProperties);
  177. bool isExpanded = data.inspector.Persistent.GetBool(data.uuid + "_Expanded", true);
  178. data.foldout.Value = isExpanded;
  179. if (!isExpanded)
  180. data.inspector.SetVisible(false);
  181. Type curComponentType = allComponents[i].GetType();
  182. data.foldout.OnToggled += (bool expanded) => OnComponentFoldoutToggled(data, expanded);
  183. data.removeBtn.OnClick += () => OnComponentRemoveClicked(curComponentType);
  184. inspectorComponents.Add(data);
  185. }
  186. inspectorLayout.AddFlexibleSpace();
  187. UpdateDropAreas();
  188. }
  189. /// <summary>
  190. /// Creates GUI elements required for displaying <see cref="SceneObject"/> fields like name, prefab data and
  191. /// transform (position, rotation, scale). Assumes that necessary inspector scroll area layout has already been
  192. /// created.
  193. /// </summary>
  194. private void CreateSceneObjectFields()
  195. {
  196. GUIPanel sceneObjectPanel = inspectorLayout.AddPanel();
  197. sceneObjectPanel.SetHeight(GetTitleBounds().height);
  198. GUILayoutY sceneObjectLayout = sceneObjectPanel.AddLayoutY();
  199. sceneObjectLayout.SetPosition(PADDING, PADDING);
  200. GUIPanel sceneObjectBgPanel = sceneObjectPanel.AddPanel(1);
  201. GUILayoutX nameLayout = sceneObjectLayout.AddLayoutX();
  202. soActiveToggle = new GUIToggle("");
  203. soActiveToggle.OnToggled += OnSceneObjectActiveStateToggled;
  204. GUILabel nameLbl = new GUILabel(new LocEdString("Name"), GUIOption.FixedWidth(50));
  205. soNameInput = new GUITextBox(false, GUIOption.FlexibleWidth(180));
  206. soNameInput.Text = activeSO.Name;
  207. soNameInput.OnChanged += OnSceneObjectRename;
  208. soNameInput.OnConfirmed += () =>
  209. {
  210. OnModifyConfirm();
  211. StartUndo("name");
  212. };
  213. soNameInput.OnFocusGained += () => StartUndo("name");
  214. soNameInput.OnFocusLost += OnModifyConfirm;
  215. nameLayout.AddElement(soActiveToggle);
  216. nameLayout.AddSpace(3);
  217. nameLayout.AddElement(nameLbl);
  218. nameLayout.AddElement(soNameInput);
  219. nameLayout.AddFlexibleSpace();
  220. GUILayoutX mobilityLayout = sceneObjectLayout.AddLayoutX();
  221. GUILabel mobilityLbl = new GUILabel(new LocEdString("Mobility"), GUIOption.FixedWidth(50));
  222. soMobility = new GUIEnumField(typeof(ObjectMobility), "", 0, GUIOption.FixedWidth(85));
  223. soMobility.Value = (ulong)activeSO.Mobility;
  224. soMobility.OnSelectionChanged += value => activeSO.Mobility = (ObjectMobility) value;
  225. mobilityLayout.AddElement(mobilityLbl);
  226. mobilityLayout.AddElement(soMobility);
  227. soPrefabLayout = sceneObjectLayout.AddLayoutX();
  228. soPos = new GUIVector3Field(new LocEdString("Position"), 50);
  229. sceneObjectLayout.AddElement(soPos);
  230. soPos.OnComponentChanged += OnPositionChanged;
  231. soPos.OnConfirm += x =>
  232. {
  233. OnModifyConfirm();
  234. StartUndo("position." + x.ToString());
  235. };
  236. soPos.OnComponentFocusChanged += (focus, comp) =>
  237. {
  238. if (focus)
  239. StartUndo("position." + comp.ToString());
  240. else
  241. OnModifyConfirm();
  242. };
  243. soRot = new GUIVector3Field(new LocEdString("Rotation"), 50);
  244. sceneObjectLayout.AddElement(soRot);
  245. soRot.OnComponentChanged += OnRotationChanged;
  246. soRot.OnConfirm += x =>
  247. {
  248. OnModifyConfirm();
  249. StartUndo("rotation." + x.ToString());
  250. };
  251. soRot.OnComponentFocusChanged += (focus, comp) =>
  252. {
  253. if (focus)
  254. StartUndo("rotation." + comp.ToString());
  255. else
  256. OnModifyConfirm();
  257. };
  258. soScale = new GUIVector3Field(new LocEdString("Scale"), 50);
  259. sceneObjectLayout.AddElement(soScale);
  260. soScale.OnComponentChanged += OnScaleChanged;
  261. soScale.OnConfirm += x =>
  262. {
  263. OnModifyConfirm();
  264. StartUndo("scale." + x.ToString());
  265. };
  266. soScale.OnComponentFocusChanged += (focus, comp) =>
  267. {
  268. if (focus)
  269. StartUndo("scale." + comp.ToString());
  270. else
  271. OnModifyConfirm();
  272. };
  273. sceneObjectLayout.AddFlexibleSpace();
  274. GUITexture titleBg = new GUITexture(null, EditorStylesInternal.InspectorTitleBg);
  275. sceneObjectBgPanel.AddElement(titleBg);
  276. }
  277. /// <summary>
  278. /// Updates contents of the scene object specific fields (name, position, rotation, etc.)
  279. /// </summary>
  280. /// <param name="forceUpdate">If true, the GUI elements will be updated regardless of whether a change was
  281. /// detected or not.</param>
  282. internal void RefreshSceneObjectFields(bool forceUpdate)
  283. {
  284. if (activeSO == null)
  285. return;
  286. soNameInput.Text = activeSO.Name;
  287. soActiveToggle.Value = activeSO.Active;
  288. soMobility.Value = (ulong) activeSO.Mobility;
  289. SceneObject prefabParent = PrefabUtility.GetPrefabParent(activeSO);
  290. // Ignore prefab parent if scene root, we only care for non-root prefab instances
  291. bool hasPrefab = prefabParent != null && prefabParent.Parent != null;
  292. if (soHasPrefab != hasPrefab || forceUpdate)
  293. {
  294. int numChildren = soPrefabLayout.ChildCount;
  295. for (int i = 0; i < numChildren; i++)
  296. soPrefabLayout.GetChild(0).Destroy();
  297. GUILabel prefabLabel =new GUILabel(new LocEdString("Prefab"), GUIOption.FixedWidth(50));
  298. soPrefabLayout.AddElement(prefabLabel);
  299. if (hasPrefab)
  300. {
  301. GUIButton btnApplyPrefab = new GUIButton(new LocEdString("Apply"), GUIOption.FixedWidth(60));
  302. GUIButton btnRevertPrefab = new GUIButton(new LocEdString("Revert"), GUIOption.FixedWidth(60));
  303. GUIButton btnBreakPrefab = new GUIButton(new LocEdString("Break"), GUIOption.FixedWidth(60));
  304. btnApplyPrefab.OnClick += () =>
  305. {
  306. PrefabUtility.ApplyPrefab(activeSO);
  307. };
  308. btnRevertPrefab.OnClick += () =>
  309. {
  310. GameObjectUndo.RecordSceneObject(activeSO, true, "Reverting \"" + activeSO.Name + "\" to prefab.");
  311. PrefabUtility.RevertPrefab(activeSO);
  312. GameObjectUndo.ResolveDiffs();
  313. EditorApplication.SetSceneDirty();
  314. };
  315. btnBreakPrefab.OnClick += () =>
  316. {
  317. UndoRedo.BreakPrefab(activeSO, "Breaking prefab link for " + activeSO.Name);
  318. EditorApplication.SetSceneDirty();
  319. };
  320. soPrefabLayout.AddElement(btnApplyPrefab);
  321. soPrefabLayout.AddElement(btnRevertPrefab);
  322. soPrefabLayout.AddElement(btnBreakPrefab);
  323. }
  324. else
  325. {
  326. GUILabel noPrefabLabel = new GUILabel("None");
  327. soPrefabLayout.AddElement(noPrefabLabel);
  328. }
  329. soHasPrefab = hasPrefab;
  330. }
  331. Vector3 position;
  332. Quaternion rotation;
  333. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  334. {
  335. position = activeSO.Position;
  336. rotation = activeSO.Rotation;
  337. }
  338. else
  339. {
  340. position = activeSO.LocalPosition;
  341. rotation = activeSO.LocalRotation;
  342. }
  343. Vector3 scale = activeSO.LocalScale;
  344. if (!soPos.HasInputFocus || forceUpdate)
  345. soPos.Value = position;
  346. // Avoid updating the rotation unless actually changed externally, since switching back and forth between
  347. // quaternion and euler angles can cause weird behavior
  348. if ((!soRot.HasInputFocus && rotation != lastRotation) || forceUpdate)
  349. {
  350. soRot.Value = rotation.ToEuler();
  351. lastRotation = rotation;
  352. }
  353. if (!soScale.HasInputFocus || forceUpdate)
  354. soScale.Value = scale;
  355. }
  356. /// <summary>
  357. /// Forces all the GUI fields for the specified component to update their values from the current component state.
  358. /// </summary>
  359. /// <param name="component">Component for whose GUI elements to perform the refresh on.</param>
  360. internal void RefreshComponentFields(Component component)
  361. {
  362. if (component == null)
  363. return;
  364. foreach (var entry in inspectorComponents)
  365. {
  366. if (entry.uuid == component.UUID)
  367. entry.inspector.Refresh(true);
  368. }
  369. }
  370. private void OnInitialize()
  371. {
  372. Selection.OnSelectionChanged += OnSelectionChanged;
  373. ProjectLibrary.OnEntryImported += OnResourceImported;
  374. const string soName = "InspectorPersistentData";
  375. SceneObject so = Scene.Root.FindChild(soName);
  376. if (so == null)
  377. so = new SceneObject(soName, true);
  378. persistentData = so.GetComponent<InspectorPersistentData>();
  379. if (persistentData == null)
  380. persistentData = so.AddComponent<InspectorPersistentData>();
  381. OnSelectionChanged(new SceneObject[0], new string[0]);
  382. }
  383. private void OnDestroy()
  384. {
  385. Selection.OnSelectionChanged -= OnSelectionChanged;
  386. ProjectLibrary.OnEntryImported -= OnResourceImported;
  387. }
  388. private void OnEditorUpdate()
  389. {
  390. if (currentType == InspectorType.SceneObject)
  391. {
  392. Component[] allComponents = activeSO.GetComponents();
  393. bool requiresRebuild = allComponents.Length != inspectorComponents.Count;
  394. if (!requiresRebuild)
  395. {
  396. for (int i = 0; i < inspectorComponents.Count; i++)
  397. {
  398. if (inspectorComponents[i].uuid != allComponents[i].UUID)
  399. {
  400. requiresRebuild = true;
  401. break;
  402. }
  403. }
  404. }
  405. if (requiresRebuild)
  406. {
  407. SceneObject so = activeSO;
  408. Clear();
  409. SetObjectToInspect(so);
  410. }
  411. else
  412. {
  413. RefreshSceneObjectFields(false);
  414. InspectableState componentModifyState = InspectableState.NotModified;
  415. for (int i = 0; i < inspectorComponents.Count; i++)
  416. componentModifyState |= inspectorComponents[i].inspector.Refresh();
  417. if (componentModifyState.HasFlag(InspectableState.ModifyInProgress))
  418. EditorApplication.SetSceneDirty();
  419. modifyState |= componentModifyState;
  420. }
  421. }
  422. else if (currentType == InspectorType.Resource)
  423. {
  424. inspectorResource.inspector.Refresh();
  425. }
  426. // Detect drag and drop
  427. bool isValidDrag = false;
  428. if (activeSO != null)
  429. {
  430. if ((DragDrop.DragInProgress || DragDrop.DropInProgress) && DragDrop.Type == DragDropType.Resource)
  431. {
  432. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  433. Vector2I scrollPos = windowPos;
  434. Rect2I contentBounds = inspectorLayout.Bounds;
  435. scrollPos.x -= contentBounds.x;
  436. scrollPos.y -= contentBounds.y;
  437. bool isInBounds = false;
  438. Rect2I dropArea = new Rect2I();
  439. foreach (var bounds in dropAreas)
  440. {
  441. if (bounds.Contains(scrollPos))
  442. {
  443. isInBounds = true;
  444. dropArea = bounds;
  445. break;
  446. }
  447. }
  448. Type draggedComponentType = null;
  449. if (isInBounds)
  450. {
  451. ResourceDragDropData dragData = DragDrop.Data as ResourceDragDropData;
  452. if (dragData != null)
  453. {
  454. foreach (var resPath in dragData.Paths)
  455. {
  456. ResourceMeta meta = ProjectLibrary.GetMeta(resPath);
  457. if (meta != null)
  458. {
  459. if (meta.ResType == ResourceType.ScriptCode)
  460. {
  461. ScriptCode scriptFile = ProjectLibrary.Load<ScriptCode>(resPath);
  462. if (scriptFile != null)
  463. {
  464. Type[] scriptTypes = scriptFile.Types;
  465. foreach (var type in scriptTypes)
  466. {
  467. if (type.IsSubclassOf(typeof (Component)))
  468. {
  469. draggedComponentType = type;
  470. isValidDrag = true;
  471. break;
  472. }
  473. }
  474. if (draggedComponentType != null)
  475. break;
  476. }
  477. }
  478. }
  479. }
  480. }
  481. }
  482. if (isValidDrag)
  483. {
  484. scrollAreaHighlight.Bounds = dropArea;
  485. if (DragDrop.DropInProgress)
  486. {
  487. GameObjectUndo.RecordSceneObject(activeSO, false, $"Added component \"{draggedComponentType.Name}\" to \"{activeSO.Name}\"");
  488. activeSO.AddComponent(draggedComponentType);
  489. modifyState = InspectableState.Modified;
  490. EditorApplication.SetSceneDirty();
  491. }
  492. }
  493. }
  494. }
  495. if (scrollAreaHighlight != null)
  496. scrollAreaHighlight.Active = isValidDrag;
  497. }
  498. /// <summary>
  499. /// Triggered when the user selects a new resource or a scene object, or deselects everything.
  500. /// </summary>
  501. /// <param name="objects">A set of new scene objects that were selected.</param>
  502. /// <param name="paths">A set of absolute resource paths that were selected.</param>
  503. private void OnSelectionChanged(SceneObject[] objects, string[] paths)
  504. {
  505. Clear();
  506. modifyState = InspectableState.NotModified;
  507. if (objects.Length == 0 && paths.Length == 0)
  508. {
  509. currentType = InspectorType.None;
  510. inspectorScrollArea = new GUIScrollArea(ScrollBarType.ShowIfDoesntFit, ScrollBarType.NeverShow);
  511. GUI.AddElement(inspectorScrollArea);
  512. inspectorLayout = inspectorScrollArea.Layout;
  513. inspectorLayout.AddFlexibleSpace();
  514. GUILayoutX layoutMsg = inspectorLayout.AddLayoutX();
  515. layoutMsg.AddFlexibleSpace();
  516. layoutMsg.AddElement(new GUILabel(new LocEdString("No object selected")));
  517. layoutMsg.AddFlexibleSpace();
  518. inspectorLayout.AddFlexibleSpace();
  519. }
  520. else if ((objects.Length + paths.Length) > 1)
  521. {
  522. currentType = InspectorType.None;
  523. inspectorScrollArea = new GUIScrollArea(ScrollBarType.ShowIfDoesntFit, ScrollBarType.NeverShow);
  524. GUI.AddElement(inspectorScrollArea);
  525. inspectorLayout = inspectorScrollArea.Layout;
  526. inspectorLayout.AddFlexibleSpace();
  527. GUILayoutX layoutMsg = inspectorLayout.AddLayoutX();
  528. layoutMsg.AddFlexibleSpace();
  529. layoutMsg.AddElement(new GUILabel(new LocEdString("Multiple objects selected")));
  530. layoutMsg.AddFlexibleSpace();
  531. inspectorLayout.AddFlexibleSpace();
  532. }
  533. else if (objects.Length == 1)
  534. {
  535. if (objects[0] != null)
  536. SetObjectToInspect(objects[0]);
  537. }
  538. else if (paths.Length == 1)
  539. {
  540. SetObjectToInspect(paths[0]);
  541. }
  542. }
  543. /// <summary>
  544. /// Updates the inspector if the inspected resource just got reimported.
  545. /// </summary>
  546. /// <param name="path">Path to the resource that got imported.</param>
  547. private void OnResourceImported(string path)
  548. {
  549. if (resourceInspectorInitialized || string.IsNullOrEmpty(activeResourcePath))
  550. return;
  551. if(path == activeResourcePath)
  552. SetObjectToInspect(path);
  553. }
  554. /// <summary>
  555. /// Triggered when the user closes or expands a component foldout, making the component fields visible or hidden.
  556. /// </summary>
  557. /// <param name="inspectorData">Contains GUI data for the component that was toggled.</param>
  558. /// <param name="expanded">Determines whether to display or hide component contents.</param>
  559. private void OnComponentFoldoutToggled(InspectorComponent inspectorData, bool expanded)
  560. {
  561. inspectorData.inspector.Persistent.SetBool(inspectorData.uuid + "_Expanded", expanded);
  562. inspectorData.inspector.SetVisible(expanded);
  563. inspectorData.folded = !expanded;
  564. UpdateDropAreas();
  565. }
  566. /// <summary>
  567. /// Triggered when the user clicks the component remove button. Removes that component from the active scene object.
  568. /// </summary>
  569. /// <param name="componentType">Type of the component to remove.</param>
  570. private void OnComponentRemoveClicked(Type componentType)
  571. {
  572. if (activeSO != null)
  573. {
  574. GameObjectUndo.RecordSceneObject(activeSO, false, $"Removed component \"{componentType.Name}\" from \"{activeSO.Name}\"");
  575. activeSO.RemoveComponent(componentType);
  576. modifyState = InspectableState.Modified;
  577. EditorApplication.SetSceneDirty();
  578. GameObjectUndo.ResolveDiffs();
  579. }
  580. }
  581. /// <summary>
  582. /// Destroys all inspector GUI elements.
  583. /// </summary>
  584. internal void Clear()
  585. {
  586. for (int i = 0; i < inspectorComponents.Count; i++)
  587. {
  588. inspectorComponents[i].foldout.Destroy();
  589. inspectorComponents[i].removeBtn.Destroy();
  590. inspectorComponents[i].inspector.Destroy();
  591. }
  592. inspectorComponents.Clear();
  593. if (inspectorResource != null)
  594. {
  595. inspectorResource.inspector.Destroy();
  596. inspectorResource = null;
  597. }
  598. if (inspectorScrollArea != null)
  599. {
  600. inspectorScrollArea.Destroy();
  601. inspectorScrollArea = null;
  602. }
  603. if (scrollAreaHighlight != null)
  604. {
  605. scrollAreaHighlight.Destroy();
  606. scrollAreaHighlight = null;
  607. }
  608. if (highlightPanel != null)
  609. {
  610. highlightPanel.Destroy();
  611. highlightPanel = null;
  612. }
  613. activeSO = null;
  614. soNameInput = null;
  615. soActiveToggle = null;
  616. soMobility = null;
  617. soPrefabLayout = null;
  618. soHasPrefab = false;
  619. soPos = null;
  620. soRot = null;
  621. soScale = null;
  622. dropAreas = new Rect2I[0];
  623. activeResourcePath = null;
  624. currentType = InspectorType.None;
  625. }
  626. /// <summary>
  627. /// Changes keyboard focus to a specific field on the component with the provided UUID.
  628. /// </summary>
  629. /// <param name="uuid">UUID of the component on which to select the field.</param>
  630. /// <param name="path">Path to the field on the object being inspected.</param>
  631. internal void FocusOnField(UUID uuid, string path)
  632. {
  633. if (activeSO == null)
  634. return;
  635. if (activeSO.UUID == uuid)
  636. {
  637. if(path == "position.X")
  638. soPos.SetInputFocus(VectorComponent.X, true);
  639. else if(path == "position.Y")
  640. soPos.SetInputFocus(VectorComponent.Y, true);
  641. else if(path == "position.Z")
  642. soPos.SetInputFocus(VectorComponent.Z, true);
  643. else if(path == "rotation.X")
  644. soRot.SetInputFocus(VectorComponent.X, true);
  645. else if(path == "rotation.Y")
  646. soRot.SetInputFocus(VectorComponent.Y, true);
  647. else if(path == "rotation.Z")
  648. soRot.SetInputFocus(VectorComponent.Z, true);
  649. else if(path == "scale.X")
  650. soScale.SetInputFocus(VectorComponent.X, true);
  651. else if(path == "scale.Y")
  652. soScale.SetInputFocus(VectorComponent.Y, true);
  653. else if(path == "scale.Z")
  654. soScale.SetInputFocus(VectorComponent.Z, true);
  655. else if (path == "name")
  656. soNameInput.Focus = true;
  657. }
  658. else
  659. {
  660. foreach (var entry in inspectorComponents)
  661. {
  662. if (entry.uuid != uuid)
  663. continue;
  664. entry.inspector.FocusOnField(path);
  665. }
  666. }
  667. }
  668. /// <summary>
  669. /// Returns the size of the title bar area that is displayed for <see cref="SceneObject"/> specific fields.
  670. /// </summary>
  671. /// <returns>Area of the title bar, relative to the window.</returns>
  672. private Rect2I GetTitleBounds()
  673. {
  674. return new Rect2I(0, 0, Width, 135);
  675. }
  676. /// <summary>
  677. /// Triggered when the user changes the name of the currently active scene object.
  678. /// </summary>
  679. private void OnSceneObjectRename(string name)
  680. {
  681. if (activeSO != null)
  682. {
  683. activeSO.Name = name;
  684. modifyState |= InspectableState.ModifyInProgress;
  685. EditorApplication.SetSceneDirty();
  686. }
  687. }
  688. /// <summary>
  689. /// Triggered when the user changes the active state of the scene object.
  690. /// </summary>
  691. /// <param name="active">True if the object is active, false otherwise.</param>
  692. private void OnSceneObjectActiveStateToggled(bool active)
  693. {
  694. if (activeSO != null)
  695. {
  696. StartUndo("active");
  697. activeSO.Active = active;
  698. EndUndo();
  699. }
  700. }
  701. /// <summary>
  702. /// Triggered when the scene object modification is confirmed by the user.
  703. /// </summary>
  704. private void OnModifyConfirm()
  705. {
  706. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  707. modifyState = InspectableState.Modified;
  708. EndUndo();
  709. }
  710. /// <summary>
  711. /// Triggered when the position value in the currently active <see cref="SceneObject"/> changes. Updates the
  712. /// necessary GUI elements.
  713. /// </summary>
  714. /// <param name="value">New value of the component that changed.</param>
  715. /// <param name="component">Identifier of the component that changed.</param>
  716. private void OnPositionChanged(float value, VectorComponent component)
  717. {
  718. if (activeSO == null)
  719. return;
  720. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  721. activeSO.Position = soPos.Value;
  722. else
  723. activeSO.LocalPosition = soPos.Value;
  724. modifyState = InspectableState.ModifyInProgress;
  725. EditorApplication.SetSceneDirty();
  726. }
  727. /// <summary>
  728. /// Triggered when the rotation value in the currently active <see cref="SceneObject"/> changes. Updates the
  729. /// necessary GUI elements.
  730. /// </summary>
  731. /// <param name="value">New value of the component that changed.</param>
  732. /// <param name="component">Identifier of the component that changed.</param>
  733. private void OnRotationChanged(float value, VectorComponent component)
  734. {
  735. if (activeSO == null)
  736. return;
  737. Quaternion rotation = Quaternion.FromEuler(soRot.Value);
  738. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  739. activeSO.Rotation = rotation;
  740. else
  741. activeSO.LocalRotation = rotation;
  742. lastRotation = rotation;
  743. modifyState = InspectableState.ModifyInProgress;
  744. EditorApplication.SetSceneDirty();
  745. }
  746. /// <summary>
  747. /// Triggered when the scale value in the currently active <see cref="SceneObject"/> changes. Updates the
  748. /// necessary GUI elements.
  749. /// </summary>
  750. /// <param name="value">New value of the component that changed.</param>
  751. /// <param name="component">Identifier of the component that changed.</param>
  752. private void OnScaleChanged(float value, VectorComponent component)
  753. {
  754. if (activeSO == null)
  755. return;
  756. activeSO.LocalScale = soScale.Value;
  757. modifyState = InspectableState.ModifyInProgress;
  758. EditorApplication.SetSceneDirty();
  759. }
  760. /// <inheritdoc/>
  761. protected override void WindowResized(int width, int height)
  762. {
  763. base.WindowResized(width, height);
  764. UpdateDropAreas();
  765. }
  766. /// <summary>
  767. /// Updates drop areas used for dragging and dropping components on the inspector.
  768. /// </summary>
  769. private void UpdateDropAreas()
  770. {
  771. if (activeSO == null)
  772. return;
  773. Rect2I contentBounds = inspectorLayout.Bounds;
  774. dropAreas = new Rect2I[inspectorComponents.Count + 1];
  775. int yOffset = GetTitleBounds().height;
  776. for (int i = 0; i < inspectorComponents.Count; i++)
  777. {
  778. dropAreas[i] = new Rect2I(0, yOffset, contentBounds.width, COMPONENT_SPACING);
  779. yOffset += inspectorComponents[i].title.Bounds.height + COMPONENT_SPACING;
  780. if (!inspectorComponents[i].folded)
  781. yOffset += inspectorComponents[i].panel.Bounds.height;
  782. }
  783. dropAreas[dropAreas.Length - 1] = new Rect2I(0, yOffset, contentBounds.width, contentBounds.height - yOffset);
  784. }
  785. /// <summary>
  786. /// Notifies the system to start recording a new undo command. Any changes to scene object fields after this is
  787. /// called will be recorded in the command. User must call <see cref="EndUndo"/> after the field is done being
  788. /// changed.
  789. /// </summary>
  790. /// <param name="name">Name of the field being changed.</param>
  791. private void StartUndo(string name)
  792. {
  793. if (activeSO != null)
  794. GameObjectUndo.RecordSceneObjectHeader(activeSO, name);
  795. }
  796. /// <summary>
  797. /// Finishes recording an undo command started via <see cref="StartUndo(string)"/>. If any changes are detected on
  798. /// the field an undo command is recorded onto the undo-redo stack, otherwise nothing is done.
  799. /// </summary>
  800. private void EndUndo()
  801. {
  802. GameObjectUndo.ResolveDiffs();
  803. }
  804. }
  805. /** @} */
  806. }