InspectorWindow.cs 32 KB

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