InspectorWindow.cs 3.0 KB

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