InspectorWindow.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. internal sealed class InspectorWindow : EditorWindow
  7. {
  8. private class InspectorData
  9. {
  10. public GUIFoldout foldout;
  11. public GUIFixedSpace space;
  12. public Inspector inspector;
  13. }
  14. private List<InspectorData> inspectorData = new List<InspectorData>();
  15. private GUILayout inspectorLayout;
  16. internal void SetObjectToInspect(SceneObject so)
  17. {
  18. Clear();
  19. // TODO - Create SceneObject gui elements (name + transform)
  20. inspectorLayout = GUI.layout.AddLayoutY();
  21. Component[] allComponents = so.GetComponents();
  22. for (int i = 0; i < allComponents.Length; i++)
  23. {
  24. InspectorData data = new InspectorData();
  25. data.foldout = new GUIFoldout(allComponents[i].GetType().Name);
  26. inspectorLayout.AddElement(data.foldout);
  27. data.space = inspectorLayout.AddSpace(0);
  28. data.inspector = GetInspector(allComponents[i].GetType());
  29. data.inspector.Initialize(CreatePanel(0, 0, 0, 0), allComponents[i]);
  30. data.foldout.OnToggled += (bool expanded) => Foldout_OnToggled(data.inspector, expanded);
  31. inspectorData.Add(data);
  32. }
  33. inspectorLayout.AddFlexibleSpace();
  34. RepositionInspectors();
  35. }
  36. void Foldout_OnToggled(Inspector inspector, bool expanded)
  37. {
  38. inspector.SetVisible(expanded);
  39. }
  40. internal void Refresh()
  41. {
  42. for (int i = 0; i < inspectorData.Count; i++)
  43. inspectorData[i].inspector.Refresh();
  44. }
  45. internal void Destroy()
  46. {
  47. // TODO - Destroy SceneObject GUI elements
  48. Clear();
  49. }
  50. internal void Clear()
  51. {
  52. for (int i = 0; i < inspectorData.Count; i++)
  53. {
  54. inspectorData[i].foldout.Destroy();
  55. inspectorData[i].inspector.Destroy();
  56. }
  57. inspectorData.Clear();
  58. if (inspectorLayout != null)
  59. {
  60. inspectorLayout.Destroy();
  61. inspectorLayout = null;
  62. }
  63. }
  64. protected override void WindowResized(int width, int height)
  65. {
  66. base.WindowResized(width, height);
  67. RepositionInspectors();
  68. }
  69. private void RepositionInspectors()
  70. {
  71. int curPosition = 0;
  72. for (int i = 0; i < inspectorData.Count; i++)
  73. {
  74. int inspectorHeight = inspectorData[i].inspector.GetOptimalHeight();
  75. inspectorData[i].inspector.SetArea(0, curPosition, width, inspectorHeight);
  76. inspectorData[i].space.SetSize(inspectorHeight);
  77. curPosition += inspectorHeight;
  78. }
  79. }
  80. private Inspector GetInspector(Type type)
  81. {
  82. // TODO - Check if type has a custom inspector
  83. // and return the custom inspector, otherwise create a generic one
  84. return new GenericInspector();
  85. }
  86. }
  87. }