InspectorWindow.cs 35 KB

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