InspectorWindow.cs 34 KB

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