InspectorWindow.cs 35 KB

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