InspectorWindow.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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 += () =>
  308. {
  309. PrefabUtility.ApplyPrefab(activeSO);
  310. };
  311. btnRevertPrefab.OnClick += () =>
  312. {
  313. UndoRedo.RecordSO(activeSO, true, "Reverting \"" + activeSO.Name + "\" to prefab.");
  314. PrefabUtility.RevertPrefab(activeSO);
  315. EditorApplication.SetSceneDirty();
  316. };
  317. btnBreakPrefab.OnClick += () =>
  318. {
  319. UndoRedo.BreakPrefab(activeSO, "Breaking prefab link for " + activeSO.Name);
  320. EditorApplication.SetSceneDirty();
  321. };
  322. soPrefabLayout.AddElement(btnApplyPrefab);
  323. soPrefabLayout.AddElement(btnRevertPrefab);
  324. soPrefabLayout.AddElement(btnBreakPrefab);
  325. }
  326. else
  327. {
  328. GUILabel noPrefabLabel = new GUILabel("None");
  329. soPrefabLayout.AddElement(noPrefabLabel);
  330. }
  331. soHasPrefab = hasPrefab;
  332. }
  333. Vector3 position;
  334. Vector3 angles;
  335. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  336. {
  337. position = activeSO.Position;
  338. angles = activeSO.Rotation.ToEuler();
  339. }
  340. else
  341. {
  342. position = activeSO.LocalPosition;
  343. angles = activeSO.LocalRotation.ToEuler();
  344. }
  345. Vector3 scale = activeSO.LocalScale;
  346. if(!soPosX.HasInputFocus)
  347. soPosX.Value = position.x;
  348. if (!soPosY.HasInputFocus)
  349. soPosY.Value = position.y;
  350. if (!soPosZ.HasInputFocus)
  351. soPosZ.Value = position.z;
  352. if (!soRotX.HasInputFocus)
  353. soRotX.Value = angles.x;
  354. if (!soRotY.HasInputFocus)
  355. soRotY.Value = angles.y;
  356. if (!soRotZ.HasInputFocus)
  357. soRotZ.Value = angles.z;
  358. if (!soScaleX.HasInputFocus)
  359. soScaleX.Value = scale.x;
  360. if (!soScaleY.HasInputFocus)
  361. soScaleY.Value = scale.y;
  362. if (!soScaleZ.HasInputFocus)
  363. soScaleZ.Value = scale.z;
  364. }
  365. private void OnInitialize()
  366. {
  367. Selection.OnSelectionChanged += OnSelectionChanged;
  368. const string soName = "InspectorPersistentData";
  369. SceneObject so = Scene.Root.FindChild(soName);
  370. if (so == null)
  371. so = new SceneObject(soName, true);
  372. persistentData = so.GetComponent<InspectorPersistentData>();
  373. if (persistentData == null)
  374. persistentData = so.AddComponent<InspectorPersistentData>();
  375. OnSelectionChanged(new SceneObject[0], new string[0]);
  376. }
  377. private void OnDestroy()
  378. {
  379. Selection.OnSelectionChanged -= OnSelectionChanged;
  380. }
  381. private void OnEditorUpdate()
  382. {
  383. if (currentType == InspectorType.SceneObject)
  384. {
  385. Component[] allComponents = activeSO.GetComponents();
  386. bool requiresRebuild = allComponents.Length != inspectorComponents.Count;
  387. if (!requiresRebuild)
  388. {
  389. for (int i = 0; i < inspectorComponents.Count; i++)
  390. {
  391. if (inspectorComponents[i].instanceId != allComponents[i].InstanceId)
  392. {
  393. requiresRebuild = true;
  394. break;
  395. }
  396. }
  397. }
  398. if (requiresRebuild)
  399. {
  400. SceneObject so = activeSO;
  401. Clear();
  402. SetObjectToInspect(so);
  403. }
  404. else
  405. {
  406. RefreshSceneObjectFields(false);
  407. InspectableState componentModifyState = InspectableState.NotModified;
  408. for (int i = 0; i < inspectorComponents.Count; i++)
  409. componentModifyState |= inspectorComponents[i].inspector.Refresh();
  410. if (componentModifyState.HasFlag(InspectableState.ModifyInProgress))
  411. EditorApplication.SetSceneDirty();
  412. modifyState |= componentModifyState;
  413. }
  414. }
  415. else if (currentType == InspectorType.Resource)
  416. {
  417. inspectorResource.inspector.Refresh();
  418. }
  419. // Detect drag and drop
  420. bool isValidDrag = false;
  421. if (activeSO != null)
  422. {
  423. if ((DragDrop.DragInProgress || DragDrop.DropInProgress) && DragDrop.Type == DragDropType.Resource)
  424. {
  425. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  426. Vector2I scrollPos = windowPos;
  427. Rect2I contentBounds = inspectorLayout.Bounds;
  428. scrollPos.x -= contentBounds.x;
  429. scrollPos.y -= contentBounds.y;
  430. bool isInBounds = false;
  431. Rect2I dropArea = new Rect2I();
  432. foreach (var bounds in dropAreas)
  433. {
  434. if (bounds.Contains(scrollPos))
  435. {
  436. isInBounds = true;
  437. dropArea = bounds;
  438. break;
  439. }
  440. }
  441. Type draggedComponentType = null;
  442. if (isInBounds)
  443. {
  444. ResourceDragDropData dragData = DragDrop.Data as ResourceDragDropData;
  445. if (dragData != null)
  446. {
  447. foreach (var resPath in dragData.Paths)
  448. {
  449. ResourceMeta meta = ProjectLibrary.GetMeta(resPath);
  450. if (meta != null)
  451. {
  452. if (meta.ResType == ResourceType.ScriptCode)
  453. {
  454. ScriptCode scriptFile = ProjectLibrary.Load<ScriptCode>(resPath);
  455. if (scriptFile != null)
  456. {
  457. Type[] scriptTypes = scriptFile.Types;
  458. foreach (var type in scriptTypes)
  459. {
  460. if (type.IsSubclassOf(typeof (Component)))
  461. {
  462. draggedComponentType = type;
  463. isValidDrag = true;
  464. break;
  465. }
  466. }
  467. if (draggedComponentType != null)
  468. break;
  469. }
  470. }
  471. }
  472. }
  473. }
  474. }
  475. if (isValidDrag)
  476. {
  477. scrollAreaHighlight.Bounds = dropArea;
  478. if (DragDrop.DropInProgress)
  479. {
  480. activeSO.AddComponent(draggedComponentType);
  481. modifyState = InspectableState.Modified;
  482. EditorApplication.SetSceneDirty();
  483. }
  484. }
  485. }
  486. }
  487. if (scrollAreaHighlight != null)
  488. scrollAreaHighlight.Active = isValidDrag;
  489. }
  490. /// <summary>
  491. /// Triggered when the user selects a new resource or a scene object, or deselects everything.
  492. /// </summary>
  493. /// <param name="objects">A set of new scene objects that were selected.</param>
  494. /// <param name="paths">A set of absolute resource paths that were selected.</param>
  495. private void OnSelectionChanged(SceneObject[] objects, string[] paths)
  496. {
  497. if (currentType == InspectorType.SceneObject && modifyState == InspectableState.NotModified)
  498. UndoRedo.PopCommand(undoCommandIdx);
  499. Clear();
  500. modifyState = InspectableState.NotModified;
  501. if (objects.Length == 0 && paths.Length == 0)
  502. {
  503. currentType = InspectorType.None;
  504. inspectorScrollArea = new GUIScrollArea();
  505. GUI.AddElement(inspectorScrollArea);
  506. inspectorLayout = inspectorScrollArea.Layout;
  507. inspectorLayout.AddFlexibleSpace();
  508. GUILayoutX layoutMsg = inspectorLayout.AddLayoutX();
  509. layoutMsg.AddFlexibleSpace();
  510. layoutMsg.AddElement(new GUILabel(new LocEdString("No object selected")));
  511. layoutMsg.AddFlexibleSpace();
  512. inspectorLayout.AddFlexibleSpace();
  513. }
  514. else if ((objects.Length + paths.Length) > 1)
  515. {
  516. currentType = InspectorType.None;
  517. inspectorScrollArea = new GUIScrollArea();
  518. GUI.AddElement(inspectorScrollArea);
  519. inspectorLayout = inspectorScrollArea.Layout;
  520. inspectorLayout.AddFlexibleSpace();
  521. GUILayoutX layoutMsg = inspectorLayout.AddLayoutX();
  522. layoutMsg.AddFlexibleSpace();
  523. layoutMsg.AddElement(new GUILabel(new LocEdString("Multiple objects selected")));
  524. layoutMsg.AddFlexibleSpace();
  525. inspectorLayout.AddFlexibleSpace();
  526. }
  527. else if (objects.Length == 1)
  528. {
  529. if (objects[0] != null)
  530. {
  531. UndoRedo.RecordSO(objects[0]);
  532. undoCommandIdx = UndoRedo.TopCommandId;
  533. SetObjectToInspect(objects[0]);
  534. }
  535. }
  536. else if (paths.Length == 1)
  537. {
  538. SetObjectToInspect(paths[0]);
  539. }
  540. }
  541. /// <summary>
  542. /// Triggered when the user closes or expands a component foldout, making the component fields visible or hidden.
  543. /// </summary>
  544. /// <param name="inspectorData">Contains GUI data for the component that was toggled.</param>
  545. /// <param name="expanded">Determines whether to display or hide component contents.</param>
  546. private void OnComponentFoldoutToggled(InspectorComponent inspectorData, bool expanded)
  547. {
  548. inspectorData.inspector.Persistent.SetBool(inspectorData.instanceId + "_Expanded", expanded);
  549. inspectorData.inspector.SetVisible(expanded);
  550. }
  551. /// <summary>
  552. /// Triggered when the user clicks the component remove button. Removes that component from the active scene object.
  553. /// </summary>
  554. /// <param name="componentType">Type of the component to remove.</param>
  555. private void OnComponentRemoveClicked(Type componentType)
  556. {
  557. if (activeSO != null)
  558. {
  559. activeSO.RemoveComponent(componentType);
  560. modifyState = InspectableState.Modified;
  561. EditorApplication.SetSceneDirty();
  562. }
  563. }
  564. /// <summary>
  565. /// Destroys all inspector GUI elements.
  566. /// </summary>
  567. internal void Clear()
  568. {
  569. for (int i = 0; i < inspectorComponents.Count; i++)
  570. {
  571. inspectorComponents[i].foldout.Destroy();
  572. inspectorComponents[i].removeBtn.Destroy();
  573. inspectorComponents[i].inspector.Destroy();
  574. }
  575. inspectorComponents.Clear();
  576. if (inspectorResource != null)
  577. {
  578. inspectorResource.inspector.Destroy();
  579. inspectorResource = null;
  580. }
  581. if (inspectorScrollArea != null)
  582. {
  583. inspectorScrollArea.Destroy();
  584. inspectorScrollArea = null;
  585. }
  586. if (scrollAreaHighlight != null)
  587. {
  588. scrollAreaHighlight.Destroy();
  589. scrollAreaHighlight = null;
  590. }
  591. if (highlightPanel != null)
  592. {
  593. highlightPanel.Destroy();
  594. highlightPanel = null;
  595. }
  596. activeSO = null;
  597. soNameInput = null;
  598. soActiveToggle = null;
  599. soPrefabLayout = null;
  600. soHasPrefab = false;
  601. soPosX = null;
  602. soPosY = null;
  603. soPosZ = null;
  604. soRotX = null;
  605. soRotY = null;
  606. soRotZ = null;
  607. soScaleX = null;
  608. soScaleY = null;
  609. soScaleZ = null;
  610. dropAreas = new Rect2I[0];
  611. activeResource = null;
  612. currentType = InspectorType.None;
  613. }
  614. /// <summary>
  615. /// Returns the size of the title bar area that is displayed for <see cref="SceneObject"/> specific fields.
  616. /// </summary>
  617. /// <returns>Area of the title bar, relative to the window.</returns>
  618. private Rect2I GetTitleBounds()
  619. {
  620. return new Rect2I(0, 0, Width, 115);
  621. }
  622. /// <summary>
  623. /// Triggered when the user changes the name of the currently active scene object.
  624. /// </summary>
  625. private void OnSceneObjectRename(string name)
  626. {
  627. if (activeSO != null)
  628. {
  629. activeSO.Name = name;
  630. modifyState |= InspectableState.ModifyInProgress;
  631. EditorApplication.SetSceneDirty();
  632. }
  633. }
  634. /// <summary>
  635. /// Triggered when the user changes the active state of the scene object.
  636. /// </summary>
  637. /// <param name="active">True if the object is active, false otherwise.</param>
  638. private void OnSceneObjectActiveStateToggled(bool active)
  639. {
  640. if (activeSO != null)
  641. activeSO.Active = active;
  642. }
  643. /// <summary>
  644. /// Triggered when the scene object modification is confirmed by the user.
  645. /// </summary>
  646. private void OnModifyConfirm()
  647. {
  648. if (modifyState.HasFlag(InspectableState.ModifyInProgress))
  649. modifyState = InspectableState.Modified;
  650. }
  651. /// <summary>
  652. /// Triggered when the position value in the currently active <see cref="SceneObject"/> changes. Updates the
  653. /// necessary GUI elements.
  654. /// </summary>
  655. /// <param name="idx">Index of the coordinate that was changed.</param>
  656. /// <param name="value">New value of the field.</param>
  657. private void OnPositionChanged(int idx, float value)
  658. {
  659. if (activeSO == null)
  660. return;
  661. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  662. {
  663. Vector3 position = activeSO.Position;
  664. position[idx] = value;
  665. activeSO.Position = position;
  666. }
  667. else
  668. {
  669. Vector3 position = activeSO.LocalPosition;
  670. position[idx] = value;
  671. activeSO.LocalPosition = position;
  672. }
  673. modifyState = InspectableState.ModifyInProgress;
  674. EditorApplication.SetSceneDirty();
  675. }
  676. /// <summary>
  677. /// Triggered when the rotation value in the currently active <see cref="SceneObject"/> changes. Updates the
  678. /// necessary GUI elements.
  679. /// </summary>
  680. /// <param name="idx">Index of the euler angle that was changed (0 - X, 1 - Y, 2 - Z).</param>
  681. /// <param name="value">New value of the field.</param>
  682. private void OnRotationChanged(int idx, float value)
  683. {
  684. if (activeSO == null)
  685. return;
  686. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  687. {
  688. Vector3 angles = activeSO.Rotation.ToEuler();
  689. angles[idx] = value;
  690. activeSO.Rotation = Quaternion.FromEuler(angles);
  691. }
  692. else
  693. {
  694. Vector3 angles = activeSO.LocalRotation.ToEuler();
  695. angles[idx] = value;
  696. activeSO.LocalRotation = Quaternion.FromEuler(angles);
  697. }
  698. modifyState = InspectableState.ModifyInProgress;
  699. EditorApplication.SetSceneDirty();
  700. }
  701. /// <summary>
  702. /// Triggered when the scale value in the currently active <see cref="SceneObject"/> changes. Updates the
  703. /// necessary GUI elements.
  704. /// </summary>
  705. /// <param name="idx">Index of the coordinate that was changed.</param>
  706. /// <param name="value">New value of the field.</param>
  707. private void OnScaleChanged(int idx, float value)
  708. {
  709. if (activeSO == null)
  710. return;
  711. Vector3 scale = activeSO.LocalScale;
  712. scale[idx] = value;
  713. activeSO.LocalScale = scale;
  714. modifyState = InspectableState.ModifyInProgress;
  715. EditorApplication.SetSceneDirty();
  716. }
  717. /// <inheritdoc/>
  718. protected override void WindowResized(int width, int height)
  719. {
  720. base.WindowResized(width, height);
  721. UpdateDropAreas();
  722. }
  723. /// <summary>
  724. /// Updates drop areas used for dragging and dropping components on the inspector.
  725. /// </summary>
  726. private void UpdateDropAreas()
  727. {
  728. if (activeSO == null)
  729. return;
  730. Rect2I contentBounds = inspectorLayout.Bounds;
  731. dropAreas = new Rect2I[inspectorComponents.Count + 1];
  732. int yOffset = GetTitleBounds().height;
  733. for (int i = 0; i < inspectorComponents.Count; i++)
  734. {
  735. dropAreas[i] = new Rect2I(0, yOffset, contentBounds.width, COMPONENT_SPACING);
  736. yOffset += inspectorComponents[i].title.Bounds.height + inspectorComponents[i].panel.Bounds.height + COMPONENT_SPACING;
  737. }
  738. dropAreas[dropAreas.Length - 1] = new Rect2I(0, yOffset, contentBounds.width, contentBounds.height - yOffset);
  739. }
  740. }
  741. /** @} */
  742. }