InspectorWindow.cs 35 KB

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