Inspector.cs 4.1 KB

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