InspectorWindow.cs 35 KB

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