InspectorWindow.cs 34 KB

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