InspectorWindow.cs 36 KB

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