InspectorWindow.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. private void OnInitialize()
  354. {
  355. Selection.OnSelectionChanged += OnSelectionChanged;
  356. const string soName = "InspectorPersistentData";
  357. SceneObject so = Scene.Root.FindChild(soName);
  358. if (so == null)
  359. so = new SceneObject(soName, true);
  360. persistentData = so.GetComponent<InspectorPersistentData>();
  361. if (persistentData == null)
  362. persistentData = so.AddComponent<InspectorPersistentData>();
  363. OnSelectionChanged(new SceneObject[0], new string[0]);
  364. }
  365. private void OnDestroy()
  366. {
  367. Selection.OnSelectionChanged -= OnSelectionChanged;
  368. }
  369. private void OnEditorUpdate()
  370. {
  371. if (currentType == InspectorType.SceneObject)
  372. {
  373. Component[] allComponents = activeSO.GetComponents();
  374. bool requiresRebuild = allComponents.Length != inspectorComponents.Count;
  375. if (!requiresRebuild)
  376. {
  377. for (int i = 0; i < inspectorComponents.Count; i++)
  378. {
  379. if (inspectorComponents[i].uuid != allComponents[i].UUID)
  380. {
  381. requiresRebuild = true;
  382. break;
  383. }
  384. }
  385. }
  386. if (requiresRebuild)
  387. {
  388. SceneObject so = activeSO;
  389. Clear();
  390. SetObjectToInspect(so);
  391. }
  392. else
  393. {
  394. RefreshSceneObjectFields(false);
  395. InspectableState componentModifyState = InspectableState.NotModified;
  396. for (int i = 0; i < inspectorComponents.Count; i++)
  397. componentModifyState |= inspectorComponents[i].inspector.Refresh();
  398. if (componentModifyState.HasFlag(InspectableState.ModifyInProgress))
  399. EditorApplication.SetSceneDirty();
  400. modifyState |= componentModifyState;
  401. }
  402. }
  403. else if (currentType == InspectorType.Resource)
  404. {
  405. inspectorResource.inspector.Refresh();
  406. }
  407. // Detect drag and drop
  408. bool isValidDrag = false;
  409. if (activeSO != null)
  410. {
  411. if ((DragDrop.DragInProgress || DragDrop.DropInProgress) && DragDrop.Type == DragDropType.Resource)
  412. {
  413. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  414. Vector2I scrollPos = windowPos;
  415. Rect2I contentBounds = inspectorLayout.Bounds;
  416. scrollPos.x -= contentBounds.x;
  417. scrollPos.y -= contentBounds.y;
  418. bool isInBounds = false;
  419. Rect2I dropArea = new Rect2I();
  420. foreach (var bounds in dropAreas)
  421. {
  422. if (bounds.Contains(scrollPos))
  423. {
  424. isInBounds = true;
  425. dropArea = bounds;
  426. break;
  427. }
  428. }
  429. Type draggedComponentType = null;
  430. if (isInBounds)
  431. {
  432. ResourceDragDropData dragData = DragDrop.Data as ResourceDragDropData;
  433. if (dragData != null)
  434. {
  435. foreach (var resPath in dragData.Paths)
  436. {
  437. ResourceMeta meta = ProjectLibrary.GetMeta(resPath);
  438. if (meta != null)
  439. {
  440. if (meta.ResType == ResourceType.ScriptCode)
  441. {
  442. ScriptCode scriptFile = ProjectLibrary.Load<ScriptCode>(resPath);
  443. if (scriptFile != null)
  444. {
  445. Type[] scriptTypes = scriptFile.Types;
  446. foreach (var type in scriptTypes)
  447. {
  448. if (type.IsSubclassOf(typeof (Component)))
  449. {
  450. draggedComponentType = type;
  451. isValidDrag = true;
  452. break;
  453. }
  454. }
  455. if (draggedComponentType != null)
  456. break;
  457. }
  458. }
  459. }
  460. }
  461. }
  462. }
  463. if (isValidDrag)
  464. {
  465. scrollAreaHighlight.Bounds = dropArea;
  466. if (DragDrop.DropInProgress)
  467. {
  468. GameObjectUndo.RecordSceneObject(activeSO, false, $"Added component \"{draggedComponentType.Name}\" to \"{activeSO.Name}\"");
  469. activeSO.AddComponent(draggedComponentType);
  470. modifyState = InspectableState.Modified;
  471. EditorApplication.SetSceneDirty();
  472. }
  473. }
  474. }
  475. }
  476. if (scrollAreaHighlight != null)
  477. scrollAreaHighlight.Active = isValidDrag;
  478. }
  479. /// <summary>
  480. /// Triggered when the user selects a new resource or a scene object, or deselects everything.
  481. /// </summary>
  482. /// <param name="objects">A set of new scene objects that were selected.</param>
  483. /// <param name="paths">A set of absolute resource paths that were selected.</param>
  484. private void OnSelectionChanged(SceneObject[] objects, string[] paths)
  485. {
  486. Clear();
  487. modifyState = InspectableState.NotModified;
  488. if (objects.Length == 0 && paths.Length == 0)
  489. {
  490. currentType = InspectorType.None;
  491. inspectorScrollArea = new GUIScrollArea(ScrollBarType.ShowIfDoesntFit, ScrollBarType.NeverShow);
  492. GUI.AddElement(inspectorScrollArea);
  493. inspectorLayout = inspectorScrollArea.Layout;
  494. inspectorLayout.AddFlexibleSpace();
  495. GUILayoutX layoutMsg = inspectorLayout.AddLayoutX();
  496. layoutMsg.AddFlexibleSpace();
  497. layoutMsg.AddElement(new GUILabel(new LocEdString("No object selected")));
  498. layoutMsg.AddFlexibleSpace();
  499. inspectorLayout.AddFlexibleSpace();
  500. }
  501. else if ((objects.Length + paths.Length) > 1)
  502. {
  503. currentType = InspectorType.None;
  504. inspectorScrollArea = new GUIScrollArea(ScrollBarType.ShowIfDoesntFit, ScrollBarType.NeverShow);
  505. GUI.AddElement(inspectorScrollArea);
  506. inspectorLayout = inspectorScrollArea.Layout;
  507. inspectorLayout.AddFlexibleSpace();
  508. GUILayoutX layoutMsg = inspectorLayout.AddLayoutX();
  509. layoutMsg.AddFlexibleSpace();
  510. layoutMsg.AddElement(new GUILabel(new LocEdString("Multiple objects selected")));
  511. layoutMsg.AddFlexibleSpace();
  512. inspectorLayout.AddFlexibleSpace();
  513. }
  514. else if (objects.Length == 1)
  515. {
  516. if (objects[0] != null)
  517. SetObjectToInspect(objects[0]);
  518. }
  519. else if (paths.Length == 1)
  520. {
  521. SetObjectToInspect(paths[0]);
  522. }
  523. }
  524. /// <summary>
  525. /// Triggered when the user closes or expands a component foldout, making the component fields visible or hidden.
  526. /// </summary>
  527. /// <param name="inspectorData">Contains GUI data for the component that was toggled.</param>
  528. /// <param name="expanded">Determines whether to display or hide component contents.</param>
  529. private void OnComponentFoldoutToggled(InspectorComponent inspectorData, bool expanded)
  530. {
  531. inspectorData.inspector.Persistent.SetBool(inspectorData.uuid + "_Expanded", expanded);
  532. inspectorData.inspector.SetVisible(expanded);
  533. inspectorData.folded = !expanded;
  534. UpdateDropAreas();
  535. }
  536. /// <summary>
  537. /// Triggered when the user clicks the component remove button. Removes that component from the active scene object.
  538. /// </summary>
  539. /// <param name="componentType">Type of the component to remove.</param>
  540. private void OnComponentRemoveClicked(Type componentType)
  541. {
  542. if (activeSO != null)
  543. {
  544. GameObjectUndo.RecordSceneObject(activeSO, false, $"Removed component \"{componentType.Name}\" from \"{activeSO.Name}\"");
  545. activeSO.RemoveComponent(componentType);
  546. modifyState = InspectableState.Modified;
  547. EditorApplication.SetSceneDirty();
  548. GameObjectUndo.ResolveDiffs();
  549. }
  550. }
  551. /// <summary>
  552. /// Destroys all inspector GUI elements.
  553. /// </summary>
  554. internal void Clear()
  555. {
  556. for (int i = 0; i < inspectorComponents.Count; i++)
  557. {
  558. inspectorComponents[i].foldout.Destroy();
  559. inspectorComponents[i].removeBtn.Destroy();
  560. inspectorComponents[i].inspector.Destroy();
  561. }
  562. inspectorComponents.Clear();
  563. if (inspectorResource != null)
  564. {
  565. inspectorResource.inspector.Destroy();
  566. inspectorResource = null;
  567. }
  568. if (inspectorScrollArea != null)
  569. {
  570. inspectorScrollArea.Destroy();
  571. inspectorScrollArea = null;
  572. }
  573. if (scrollAreaHighlight != null)
  574. {
  575. scrollAreaHighlight.Destroy();
  576. scrollAreaHighlight = null;
  577. }
  578. if (highlightPanel != null)
  579. {
  580. highlightPanel.Destroy();
  581. highlightPanel = null;
  582. }
  583. activeSO = null;
  584. soNameInput = null;
  585. soActiveToggle = null;
  586. soMobility = null;
  587. soPrefabLayout = null;
  588. soHasPrefab = false;
  589. soPos = null;
  590. soRot = null;
  591. soScale = null;
  592. dropAreas = new Rect2I[0];
  593. activeResourcePath = null;
  594. currentType = InspectorType.None;
  595. }
  596. /// <summary>
  597. /// Changes keyboard focus to a specific field on the component with the provided UUID.
  598. /// </summary>
  599. /// <param name="uuid">UUID of the component on which to select the field.</param>
  600. /// <param name="path">Path to the field on the object being inspected.</param>
  601. internal void FocusOnField(UUID uuid, string path)
  602. {
  603. if (activeSO == null)
  604. return;
  605. if (activeSO.UUID == uuid)
  606. {
  607. if(path == "position.X")
  608. soPos.SetInputFocus(VectorComponent.X, true);
  609. else if(path == "position.Y")
  610. soPos.SetInputFocus(VectorComponent.Y, true);
  611. else if(path == "position.Z")
  612. soPos.SetInputFocus(VectorComponent.Z, true);
  613. else if(path == "rotation.X")
  614. soRot.SetInputFocus(VectorComponent.X, true);
  615. else if(path == "rotation.Y")
  616. soRot.SetInputFocus(VectorComponent.Y, true);
  617. else if(path == "rotation.Z")
  618. soRot.SetInputFocus(VectorComponent.Z, true);
  619. else if(path == "scale.X")
  620. soScale.SetInputFocus(VectorComponent.X, true);
  621. else if(path == "scale.Y")
  622. soScale.SetInputFocus(VectorComponent.Y, true);
  623. else if(path == "scale.Z")
  624. soScale.SetInputFocus(VectorComponent.Z, true);
  625. else if (path == "name")
  626. soNameInput.Focus = true;
  627. }
  628. else
  629. {
  630. foreach (var entry in inspectorComponents)
  631. {
  632. if (entry.uuid != uuid)
  633. continue;
  634. entry.inspector.FocusOnField(path);
  635. }
  636. }
  637. }
  638. /// <summary>
  639. /// Returns the size of the title bar area that is displayed for <see cref="SceneObject"/> specific fields.
  640. /// </summary>
  641. /// <returns>Area of the title bar, relative to the window.</returns>
  642. private Rect2I GetTitleBounds()
  643. {
  644. return new Rect2I(0, 0, Width, 135);
  645. }
  646. /// <summary>
  647. /// Triggered when the user changes the name of the currently active scene object.
  648. /// </summary>
  649. private void OnSceneObjectRename(string name)
  650. {
  651. if (activeSO != null)
  652. {
  653. activeSO.Name = name;
  654. modifyState |= InspectableState.ModifyInProgress;
  655. EditorApplication.SetSceneDirty();
  656. }
  657. }
  658. /// <summary>
  659. /// Triggered when the user changes the active state of the scene object.
  660. /// </summary>
  661. /// <param name="active">True if the object is active, false otherwise.</param>
  662. private void OnSceneObjectActiveStateToggled(bool active)
  663. {
  664. if (activeSO != null)
  665. {
  666. StartUndo("active");
  667. activeSO.Active = active;
  668. EndUndo();
  669. }
  670. }
  671. /// <summary>
  672. /// Triggered when the scene object modification is confirmed by the user.
  673. /// </summary>
  674. private void OnModifyConfirm()
  675. {
  676. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  677. modifyState = InspectableState.Modified;
  678. EndUndo();
  679. }
  680. /// <summary>
  681. /// Triggered when the position value in the currently active <see cref="SceneObject"/> changes. Updates the
  682. /// necessary GUI elements.
  683. /// </summary>
  684. /// <param name="value">New value of the component that changed.</param>
  685. /// <param name="component">Identifier of the component that changed.</param>
  686. private void OnPositionChanged(float value, VectorComponent component)
  687. {
  688. if (activeSO == null)
  689. return;
  690. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  691. activeSO.Position = soPos.Value;
  692. else
  693. activeSO.LocalPosition = soPos.Value;
  694. modifyState = InspectableState.ModifyInProgress;
  695. EditorApplication.SetSceneDirty();
  696. }
  697. /// <summary>
  698. /// Triggered when the rotation value in the currently active <see cref="SceneObject"/> changes. Updates the
  699. /// necessary GUI elements.
  700. /// </summary>
  701. /// <param name="value">New value of the component that changed.</param>
  702. /// <param name="component">Identifier of the component that changed.</param>
  703. private void OnRotationChanged(float value, VectorComponent component)
  704. {
  705. if (activeSO == null)
  706. return;
  707. Quaternion rotation = Quaternion.FromEuler(soRot.Value);
  708. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  709. activeSO.Rotation = rotation;
  710. else
  711. activeSO.LocalRotation = rotation;
  712. lastRotation = rotation;
  713. modifyState = InspectableState.ModifyInProgress;
  714. EditorApplication.SetSceneDirty();
  715. }
  716. /// <summary>
  717. /// Triggered when the scale value in the currently active <see cref="SceneObject"/> changes. Updates the
  718. /// necessary GUI elements.
  719. /// </summary>
  720. /// <param name="value">New value of the component that changed.</param>
  721. /// <param name="component">Identifier of the component that changed.</param>
  722. private void OnScaleChanged(float value, VectorComponent component)
  723. {
  724. if (activeSO == null)
  725. return;
  726. activeSO.LocalScale = soScale.Value;
  727. modifyState = InspectableState.ModifyInProgress;
  728. EditorApplication.SetSceneDirty();
  729. }
  730. /// <inheritdoc/>
  731. protected override void WindowResized(int width, int height)
  732. {
  733. base.WindowResized(width, height);
  734. UpdateDropAreas();
  735. }
  736. /// <summary>
  737. /// Updates drop areas used for dragging and dropping components on the inspector.
  738. /// </summary>
  739. private void UpdateDropAreas()
  740. {
  741. if (activeSO == null)
  742. return;
  743. Rect2I contentBounds = inspectorLayout.Bounds;
  744. dropAreas = new Rect2I[inspectorComponents.Count + 1];
  745. int yOffset = GetTitleBounds().height;
  746. for (int i = 0; i < inspectorComponents.Count; i++)
  747. {
  748. dropAreas[i] = new Rect2I(0, yOffset, contentBounds.width, COMPONENT_SPACING);
  749. yOffset += inspectorComponents[i].title.Bounds.height + COMPONENT_SPACING;
  750. if (!inspectorComponents[i].folded)
  751. yOffset += inspectorComponents[i].panel.Bounds.height;
  752. }
  753. dropAreas[dropAreas.Length - 1] = new Rect2I(0, yOffset, contentBounds.width, contentBounds.height - yOffset);
  754. }
  755. /// <summary>
  756. /// Notifies the system to start recording a new undo command. Any changes to scene object fields after this is
  757. /// called will be recorded in the command. User must call <see cref="EndUndo"/> after the field is done being
  758. /// changed.
  759. /// </summary>
  760. /// <param name="name">Name of the field being changed.</param>
  761. private void StartUndo(string name)
  762. {
  763. if (activeSO != null)
  764. GameObjectUndo.RecordSceneObjectHeader(activeSO, name);
  765. }
  766. /// <summary>
  767. /// Finishes recording an undo command started via <see cref="StartUndo(string)"/>. If any changes are detected on
  768. /// the field an undo command is recorded onto the undo-redo stack, otherwise nothing is done.
  769. /// </summary>
  770. private void EndUndo()
  771. {
  772. GameObjectUndo.ResolveDiffs();
  773. }
  774. }
  775. /** @} */
  776. }