InspectorWindow.cs 35 KB

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