Inspector.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /// <summary>
  9. /// Displays GUI elements for all the inspectable fields in an object.
  10. /// </summary>
  11. public abstract class Inspector
  12. {
  13. public const short START_BACKGROUND_DEPTH = 50;
  14. /// <summary>
  15. /// Returns the main GUI layout for the inspector.
  16. /// </summary>
  17. protected GUILayoutY Layout
  18. {
  19. get { return layout; }
  20. }
  21. /// <summary>
  22. /// Returns the main GUI panel for the inspector. <see cref="Layout"/> is a child of this panel.
  23. /// </summary>
  24. protected GUIPanel GUI
  25. {
  26. get { return mainPanel; }
  27. }
  28. /// <summary>
  29. /// Returns the object the inspector is currently displaying.
  30. /// </summary>
  31. protected object InspectedObject
  32. {
  33. get { return inspectedObject; }
  34. }
  35. private GUIPanel rootGUI;
  36. private GUIPanel mainPanel;
  37. private GUILayoutY layout;
  38. private object inspectedObject;
  39. /// <summary>
  40. /// Initializes the inspector. Must be called after construction.
  41. /// </summary>
  42. /// <param name="gui">GUI panel to add the GUI elements to.</param>
  43. /// <param name="instance">Instance of the object whose fields to display GUI for.</param>
  44. internal virtual void Initialize(GUIPanel gui, object instance)
  45. {
  46. rootGUI = gui;
  47. GUILayout contentLayoutX = gui.AddLayoutX();
  48. contentLayoutX.AddSpace(5);
  49. GUILayout contentLayoutY = contentLayoutX.AddLayoutY();
  50. contentLayoutY.AddSpace(5);
  51. GUIPanel contentPanel = contentLayoutY.AddPanel();
  52. contentLayoutY.AddSpace(5);
  53. contentLayoutX.AddSpace(5);
  54. GUIPanel backgroundPanel = gui.AddPanel(START_BACKGROUND_DEPTH);
  55. GUITexture inspectorContentBg = new GUITexture(null, EditorStyles.InspectorContentBg);
  56. backgroundPanel.AddElement(inspectorContentBg);
  57. mainPanel = contentPanel;
  58. layout = GUI.AddLayoutY();
  59. inspectedObject = instance;
  60. Initialize();
  61. Refresh();
  62. }
  63. /// <summary>
  64. /// Hides or shows the inspector GUI elements.
  65. /// </summary>
  66. /// <param name="visible">True to make the GUI elements visible.</param>
  67. internal virtual void SetVisible(bool visible)
  68. {
  69. rootGUI.Active = visible;
  70. }
  71. /// <summary>
  72. /// Destroys all inspector GUI elements.
  73. /// </summary>
  74. internal void Destroy()
  75. {
  76. Layout.Destroy();
  77. GUI.Destroy();
  78. }
  79. /// <summary>
  80. /// Called when the inspector is first created.
  81. /// </summary>
  82. protected internal abstract void Initialize();
  83. /// <summary>
  84. /// Checks if contents of the inspector have been modified, and updates them if needed.
  85. /// </summary>
  86. /// <returns>State representing was anything modified between two last calls to <see cref="Refresh"/>.</returns>
  87. protected internal abstract InspectableState Refresh();
  88. }
  89. }