2
0

InspectorWindow.cs 33 KB

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