InspectorWindow.cs 35 KB

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