InspectorWindow.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Common;
  4. using System.IO;
  5. using BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. internal sealed class InspectorWindow : EditorWindow
  9. {
  10. private enum InspectorType
  11. {
  12. SceneObject,
  13. Resource,
  14. Multiple,
  15. None
  16. }
  17. private class InspectorComponent
  18. {
  19. public GUIComponentFoldout foldout;
  20. public GUIPanel panel;
  21. public Inspector inspector;
  22. public bool expanded = true;
  23. public UInt64 instanceId;
  24. }
  25. private class InspectorResource
  26. {
  27. public GUIPanel panel;
  28. public Inspector inspector;
  29. }
  30. private static readonly Color HIGHLIGHT_COLOR = new Color(1.0f, 1.0f, 1.0f, 0.5f);
  31. private List<InspectorComponent> inspectorComponents = new List<InspectorComponent>();
  32. private InspectorResource inspectorResource;
  33. private GUIScrollArea inspectorScrollArea;
  34. private GUILayout inspectorLayout;
  35. private GUIPanel highlightPanel;
  36. private GUITexture scrollAreaHighlight;
  37. private SceneObject activeSO;
  38. private GUITextBox soNameInput;
  39. private GUILayout soPrefabLayout;
  40. private bool soHasPrefab;
  41. private GUIFloatField soPosX;
  42. private GUIFloatField soPosY;
  43. private GUIFloatField soPosZ;
  44. private GUIFloatField soRotX;
  45. private GUIFloatField soRotY;
  46. private GUIFloatField soRotZ;
  47. private GUIFloatField soScaleX;
  48. private GUIFloatField soScaleY;
  49. private GUIFloatField soScaleZ;
  50. private Rect2I dropBounds;
  51. private InspectorType currentType = InspectorType.None;
  52. private Resource activeResource;
  53. [MenuItem("Windows/Inspector", ButtonModifier.CtrlAlt, ButtonCode.I)]
  54. private static void OpenInspectorWindow()
  55. {
  56. OpenWindow<InspectorWindow>();
  57. }
  58. protected override LocString GetDisplayName()
  59. {
  60. return new LocEdString("Inspector");
  61. }
  62. private void SetObjectToInspect(String resourcePath)
  63. {
  64. activeResource = ProjectLibrary.Load<Resource>(resourcePath);
  65. if (activeResource == null)
  66. return;
  67. currentType = InspectorType.Resource;
  68. inspectorScrollArea = new GUIScrollArea();
  69. GUI.AddElement(inspectorScrollArea);
  70. inspectorLayout = inspectorScrollArea.Layout;
  71. inspectorResource = new InspectorResource();
  72. inspectorResource.panel = inspectorLayout.AddPanel();
  73. inspectorResource.inspector = GetInspector(activeResource.GetType());
  74. inspectorResource.inspector.Initialize(this, inspectorResource.panel, activeResource);
  75. inspectorResource.inspector.Refresh();
  76. inspectorLayout.AddFlexibleSpace();
  77. }
  78. private void SetObjectToInspect(SceneObject so)
  79. {
  80. if (so == null)
  81. return;
  82. currentType = InspectorType.SceneObject;
  83. activeSO = so;
  84. inspectorScrollArea = new GUIScrollArea();
  85. scrollAreaHighlight = new GUITexture(Builtin.WhiteTexture);
  86. scrollAreaHighlight.SetTint(HIGHLIGHT_COLOR);
  87. GUI.AddElement(inspectorScrollArea);
  88. highlightPanel = GUI.AddPanel(-1);
  89. highlightPanel.AddElement(scrollAreaHighlight);
  90. inspectorLayout = inspectorScrollArea.Layout;
  91. // SceneObject fields
  92. CreateSceneObjectFields();
  93. RefreshSceneObjectFields(true);
  94. // Components
  95. Component[] allComponents = so.GetComponents();
  96. for (int i = 0; i < allComponents.Length; i++)
  97. {
  98. InspectorComponent data = new InspectorComponent();
  99. data.instanceId = allComponents[i].InstanceId;
  100. data.foldout = new GUIComponentFoldout(allComponents[i].GetType().Name);
  101. inspectorLayout.AddElement(data.foldout);
  102. data.panel = inspectorLayout.AddPanel();
  103. data.inspector = GetInspector(allComponents[i].GetType());
  104. data.inspector.Initialize(this, data.panel, allComponents[i]);
  105. data.foldout.SetExpanded(true);
  106. Type curComponentType = allComponents[i].GetType();
  107. data.foldout.OnToggled += (bool expanded) => OnComponentFoldoutToggled(data, expanded);
  108. data.foldout.OnRemoveClicked += () => OnComponentRemoveClicked(curComponentType);
  109. inspectorComponents.Add(data);
  110. inspectorComponents[i].inspector.Refresh();
  111. }
  112. inspectorLayout.AddFlexibleSpace();
  113. dropBounds = inspectorScrollArea.Bounds;
  114. scrollAreaHighlight.Bounds = dropBounds;
  115. }
  116. private void OnComponentFoldoutToggled(InspectorComponent inspectorData, bool expanded)
  117. {
  118. inspectorData.expanded = expanded;
  119. inspectorData.inspector.SetVisible(expanded);
  120. }
  121. private void CreateSceneObjectFields()
  122. {
  123. GUIPanel sceneObjectPanel = inspectorLayout.AddPanel();
  124. GUILayoutY sceneObjectLayout = sceneObjectPanel.AddLayoutY();
  125. GUILayoutX nameLayout = sceneObjectLayout.AddLayoutX();
  126. GUILabel nameLbl = new GUILabel(new LocEdString("Name"), GUIOption.FixedWidth(50));
  127. soNameInput = new GUITextBox(false, GUIOption.FlexibleWidth(180));
  128. soNameInput.Text = activeSO.Name;
  129. soNameInput.OnChanged += (x) => { if (activeSO != null) activeSO.Name = x; };
  130. nameLayout.AddElement(nameLbl);
  131. nameLayout.AddElement(soNameInput);
  132. nameLayout.AddFlexibleSpace();
  133. soPrefabLayout = sceneObjectLayout.AddLayoutX();
  134. GUILayoutX positionLayout = sceneObjectLayout.AddLayoutX();
  135. GUILabel positionLbl = new GUILabel(new LocEdString("Position"), GUIOption.FixedWidth(50));
  136. soPosX = new GUIFloatField(new LocEdString("X"), 10, "", GUIOption.FixedWidth(60));
  137. soPosY = new GUIFloatField(new LocEdString("Y"), 10, "", GUIOption.FixedWidth(60));
  138. soPosZ = new GUIFloatField(new LocEdString("Z"), 10, "", GUIOption.FixedWidth(60));
  139. soPosX.OnChanged += (x) => OnPositionChanged(0, x);
  140. soPosY.OnChanged += (y) => OnPositionChanged(1, y);
  141. soPosZ.OnChanged += (z) => OnPositionChanged(2, z);
  142. positionLayout.AddElement(positionLbl);
  143. positionLayout.AddElement(soPosX);
  144. positionLayout.AddSpace(10);
  145. positionLayout.AddFlexibleSpace();
  146. positionLayout.AddElement(soPosY);
  147. positionLayout.AddSpace(10);
  148. positionLayout.AddFlexibleSpace();
  149. positionLayout.AddElement(soPosZ);
  150. positionLayout.AddFlexibleSpace();
  151. GUILayoutX rotationLayout = sceneObjectLayout.AddLayoutX();
  152. GUILabel rotationLbl = new GUILabel(new LocEdString("Rotation"), GUIOption.FixedWidth(50));
  153. soRotX = new GUIFloatField(new LocEdString("X"), 10, "", GUIOption.FixedWidth(60));
  154. soRotY = new GUIFloatField(new LocEdString("Y"), 10, "", GUIOption.FixedWidth(60));
  155. soRotZ = new GUIFloatField(new LocEdString("Z"), 10, "", GUIOption.FixedWidth(60));
  156. soRotX.OnChanged += (x) => OnRotationChanged(0, x);
  157. soRotY.OnChanged += (y) => OnRotationChanged(1, y);
  158. soRotZ.OnChanged += (z) => OnRotationChanged(2, z);
  159. rotationLayout.AddElement(rotationLbl);
  160. rotationLayout.AddElement(soRotX);
  161. rotationLayout.AddSpace(10);
  162. rotationLayout.AddFlexibleSpace();
  163. rotationLayout.AddElement(soRotY);
  164. rotationLayout.AddSpace(10);
  165. rotationLayout.AddFlexibleSpace();
  166. rotationLayout.AddElement(soRotZ);
  167. rotationLayout.AddFlexibleSpace();
  168. GUILayoutX scaleLayout = sceneObjectLayout.AddLayoutX();
  169. GUILabel scaleLbl = new GUILabel(new LocEdString("Scale"), GUIOption.FixedWidth(50));
  170. soScaleX = new GUIFloatField(new LocEdString("X"), 10, "", GUIOption.FixedWidth(60));
  171. soScaleY = new GUIFloatField(new LocEdString("Y"), 10, "", GUIOption.FixedWidth(60));
  172. soScaleZ = new GUIFloatField(new LocEdString("Z"), 10, "", GUIOption.FixedWidth(60));
  173. soScaleX.OnChanged += (x) => OnScaleChanged(0, x);
  174. soScaleY.OnChanged += (y) => OnScaleChanged(1, y);
  175. soScaleX.OnChanged += (z) => OnScaleChanged(2, z);
  176. scaleLayout.AddElement(scaleLbl);
  177. scaleLayout.AddElement(soScaleX);
  178. scaleLayout.AddSpace(10);
  179. scaleLayout.AddFlexibleSpace();
  180. scaleLayout.AddElement(soScaleY);
  181. scaleLayout.AddSpace(10);
  182. scaleLayout.AddFlexibleSpace();
  183. scaleLayout.AddElement(soScaleZ);
  184. scaleLayout.AddFlexibleSpace();
  185. inspectorLayout.AddSpace(15);
  186. }
  187. private void RefreshSceneObjectFields(bool forceUpdate)
  188. {
  189. if (activeSO == null)
  190. return;
  191. soNameInput.Text = activeSO.Name;
  192. bool hasPrefab = PrefabUtility.HasPrefabLink(activeSO);
  193. if (soHasPrefab != hasPrefab || forceUpdate)
  194. {
  195. int numChildren = soPrefabLayout.GetNumChildren();
  196. for (int i = 0; i < numChildren; i++)
  197. soPrefabLayout.GetChild(0).Destroy();
  198. GUILabel prefabLabel =new GUILabel(new LocEdString("Prefab"), GUIOption.FixedWidth(50));
  199. soPrefabLayout.AddElement(prefabLabel);
  200. //if (hasPrefab) // TODO - Disabled check for preview purposes
  201. {
  202. GUIButton btnApplyPrefab = new GUIButton(new LocEdString("Apply"), GUIOption.FixedWidth(60));
  203. GUIButton btnRevertPrefab = new GUIButton(new LocEdString("Revert"), GUIOption.FixedWidth(60));
  204. GUIButton btnBreakPrefab = new GUIButton(new LocEdString("Break"), GUIOption.FixedWidth(60));
  205. btnApplyPrefab.OnClick += () => PrefabUtility.ApplyPrefab(activeSO);
  206. btnRevertPrefab.OnClick += () => PrefabUtility.RevertPrefab(activeSO);
  207. btnBreakPrefab.OnClick += () => PrefabUtility.BreakPrefab(activeSO);
  208. soPrefabLayout.AddElement(btnApplyPrefab);
  209. soPrefabLayout.AddElement(btnRevertPrefab);
  210. soPrefabLayout.AddElement(btnBreakPrefab);
  211. }
  212. //else
  213. //{
  214. // GUILabel noPrefabLabel = new GUILabel("None");
  215. // soPrefabLayout.AddElement(noPrefabLabel);
  216. //}
  217. soHasPrefab = hasPrefab;
  218. }
  219. Vector3 position;
  220. Vector3 angles;
  221. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  222. {
  223. position = activeSO.Position;
  224. angles = activeSO.Rotation.ToEuler();
  225. }
  226. else
  227. {
  228. position = activeSO.LocalPosition;
  229. angles = activeSO.LocalRotation.ToEuler();
  230. }
  231. Vector3 scale = activeSO.LocalScale;
  232. soPosX.Value = position.x;
  233. soPosY.Value = position.y;
  234. soPosZ.Value = position.z;
  235. soRotX.Value = angles.x;
  236. soRotY.Value = angles.y;
  237. soRotZ.Value = angles.z;
  238. soScaleX.Value = scale.x;
  239. soScaleY.Value = scale.y;
  240. soScaleZ.Value = scale.z;
  241. }
  242. private void OnInitialize()
  243. {
  244. Selection.OnSelectionChanged += OnSelectionChanged;
  245. OnSelectionChanged(new SceneObject[0], new string[0]);
  246. }
  247. private void OnDestroy()
  248. {
  249. Selection.OnSelectionChanged -= OnSelectionChanged;
  250. }
  251. private void OnEditorUpdate()
  252. {
  253. if (currentType == InspectorType.SceneObject)
  254. {
  255. Component[] allComponents = activeSO.GetComponents();
  256. bool requiresRebuild = allComponents.Length != inspectorComponents.Count;
  257. if (!requiresRebuild)
  258. {
  259. for (int i = 0; i < inspectorComponents.Count; i++)
  260. {
  261. if (inspectorComponents[i].instanceId != allComponents[i].InstanceId)
  262. {
  263. requiresRebuild = true;
  264. break;
  265. }
  266. }
  267. }
  268. if (requiresRebuild)
  269. {
  270. SceneObject so = activeSO;
  271. Clear();
  272. SetObjectToInspect(so);
  273. }
  274. else
  275. {
  276. RefreshSceneObjectFields(false);
  277. for (int i = 0; i < inspectorComponents.Count; i++)
  278. {
  279. inspectorComponents[i].inspector.Refresh();
  280. }
  281. }
  282. }
  283. else if (currentType == InspectorType.Resource)
  284. {
  285. inspectorResource.inspector.Refresh();
  286. }
  287. // Detect drag and drop
  288. bool isDraggingOver = false;
  289. if (activeSO != null && inspectorScrollArea != null)
  290. {
  291. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  292. ScriptCode droppedCodeFile = null;
  293. string droppedCodeFileName = "";
  294. if (DragDrop.DragInProgress && DragDrop.Type == DragDropType.Resource)
  295. {
  296. isDraggingOver = dropBounds.Contains(windowPos);
  297. }
  298. else if (DragDrop.DropInProgress && DragDrop.Type == DragDropType.Resource)
  299. {
  300. ResourceDragDropData dragData = DragDrop.Data as ResourceDragDropData;
  301. if (dragData != null)
  302. {
  303. foreach (var resPath in dragData.Paths)
  304. {
  305. LibraryEntry entry = ProjectLibrary.GetEntry(resPath);
  306. FileEntry fileEntry = entry as FileEntry;
  307. if (fileEntry != null)
  308. {
  309. if (fileEntry.ResType == ResourceType.ScriptCode)
  310. {
  311. droppedCodeFile = ProjectLibrary.Load<ScriptCode>(resPath);
  312. droppedCodeFileName = fileEntry.Name;
  313. break;
  314. }
  315. }
  316. }
  317. }
  318. }
  319. if (droppedCodeFile != null)
  320. {
  321. Type droppedComponentType = null;
  322. Type[] droppedTypes = droppedCodeFile.Types;
  323. foreach (var type in droppedTypes)
  324. {
  325. if (type.IsSubclassOf(typeof(Component)))
  326. {
  327. droppedComponentType = type;
  328. break;
  329. }
  330. }
  331. if (droppedComponentType != null)
  332. {
  333. UndoRedo.RecordSO(activeSO, "Added component " + droppedComponentType.Name);
  334. activeSO.AddComponent(droppedComponentType);
  335. }
  336. else
  337. Debug.LogWarning("Cannot find a Component in " + droppedCodeFileName);
  338. }
  339. }
  340. if (scrollAreaHighlight != null)
  341. scrollAreaHighlight.Visible = isDraggingOver;
  342. }
  343. private void OnSelectionChanged(SceneObject[] objects, string[] paths)
  344. {
  345. Clear();
  346. if (objects.Length == 0 && paths.Length == 0)
  347. {
  348. currentType = InspectorType.None;
  349. inspectorScrollArea = new GUIScrollArea();
  350. GUI.AddElement(inspectorScrollArea);
  351. inspectorLayout = inspectorScrollArea.Layout;
  352. inspectorLayout.AddFlexibleSpace();
  353. GUILayoutX layoutMsg = inspectorLayout.AddLayoutX();
  354. layoutMsg.AddFlexibleSpace();
  355. layoutMsg.AddElement(new GUILabel(new LocEdString("No object selected")));
  356. layoutMsg.AddFlexibleSpace();
  357. inspectorLayout.AddFlexibleSpace();
  358. }
  359. else if ((objects.Length + paths.Length) > 1)
  360. {
  361. currentType = InspectorType.None;
  362. inspectorScrollArea = new GUIScrollArea();
  363. GUI.AddElement(inspectorScrollArea);
  364. inspectorLayout = inspectorScrollArea.Layout;
  365. inspectorLayout.AddFlexibleSpace();
  366. GUILayoutX layoutMsg = inspectorLayout.AddLayoutX();
  367. layoutMsg.AddFlexibleSpace();
  368. layoutMsg.AddElement(new GUILabel(new LocEdString("Multiple objects selected")));
  369. layoutMsg.AddFlexibleSpace();
  370. inspectorLayout.AddFlexibleSpace();
  371. }
  372. else if (objects.Length == 1)
  373. {
  374. SetObjectToInspect(objects[0]);
  375. }
  376. else if (paths.Length == 1)
  377. {
  378. SetObjectToInspect(paths[0]);
  379. }
  380. }
  381. private void OnComponentRemoveClicked(Type componentType)
  382. {
  383. if (activeSO != null)
  384. {
  385. UndoRedo.RecordSO(activeSO, "Removed component " + componentType.Name);
  386. activeSO.RemoveComponent(componentType);
  387. }
  388. }
  389. internal void Destroy()
  390. {
  391. Clear();
  392. }
  393. internal void Clear()
  394. {
  395. for (int i = 0; i < inspectorComponents.Count; i++)
  396. {
  397. inspectorComponents[i].foldout.Destroy();
  398. inspectorComponents[i].inspector.Destroy();
  399. }
  400. inspectorComponents.Clear();
  401. if (inspectorResource != null)
  402. {
  403. inspectorResource.inspector.Destroy();
  404. inspectorResource = null;
  405. }
  406. if (inspectorScrollArea != null)
  407. {
  408. inspectorScrollArea.Destroy();
  409. inspectorScrollArea = null;
  410. }
  411. if (scrollAreaHighlight != null)
  412. {
  413. scrollAreaHighlight.Destroy();
  414. scrollAreaHighlight = null;
  415. }
  416. if (highlightPanel != null)
  417. {
  418. highlightPanel.Destroy();
  419. highlightPanel = null;
  420. }
  421. activeSO = null;
  422. soNameInput = null;
  423. soPrefabLayout = null;
  424. soHasPrefab = false;
  425. soPosX = null;
  426. soPosY = null;
  427. soPosZ = null;
  428. soRotX = null;
  429. soRotY = null;
  430. soRotZ = null;
  431. soScaleX = null;
  432. soScaleY = null;
  433. soScaleZ = null;
  434. dropBounds = new Rect2I();
  435. activeResource = null;
  436. currentType = InspectorType.None;
  437. }
  438. private void OnPositionChanged(int idx, float value)
  439. {
  440. if (activeSO == null)
  441. return;
  442. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  443. {
  444. Vector3 position = activeSO.Position;
  445. position[idx] = value;
  446. activeSO.Position = position;
  447. }
  448. else
  449. {
  450. Vector3 position = activeSO.LocalPosition;
  451. position[idx] = value;
  452. activeSO.LocalPosition = position;
  453. }
  454. }
  455. private void OnRotationChanged(int idx, float value)
  456. {
  457. if (activeSO == null)
  458. return;
  459. if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
  460. {
  461. Vector3 angles = activeSO.Rotation.ToEuler();
  462. angles[idx] = value;
  463. activeSO.Rotation = Quaternion.FromEuler(angles);
  464. }
  465. else
  466. {
  467. Vector3 angles = activeSO.LocalRotation.ToEuler();
  468. angles[idx] = value;
  469. activeSO.LocalRotation = Quaternion.FromEuler(angles);
  470. }
  471. }
  472. private void OnScaleChanged(int idx, float value)
  473. {
  474. if (activeSO == null)
  475. return;
  476. Vector3 scale = activeSO.LocalScale;
  477. scale[idx] = value;
  478. activeSO.LocalScale = scale;
  479. }
  480. protected override void WindowResized(int width, int height)
  481. {
  482. base.WindowResized(width, height);
  483. if(inspectorScrollArea != null)
  484. dropBounds = inspectorScrollArea.Bounds;
  485. if (scrollAreaHighlight != null)
  486. scrollAreaHighlight.Bounds = dropBounds;
  487. }
  488. private Inspector GetInspector(Type type)
  489. {
  490. // TODO - Check if type has a custom inspector
  491. // and return the custom inspector, otherwise create a generic one
  492. return new GenericInspector();
  493. }
  494. }
  495. }