Inspector.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. /** @addtogroup Inspector
  8. * @{
  9. */
  10. /// <summary>
  11. /// Displays GUI elements for all the inspectable fields in an object.
  12. /// </summary>
  13. public abstract class Inspector
  14. {
  15. public const short START_BACKGROUND_DEPTH = 50;
  16. /// <summary>
  17. /// Returns the main GUI layout for the inspector.
  18. /// </summary>
  19. protected GUILayoutY Layout
  20. {
  21. get { return layout; }
  22. }
  23. /// <summary>
  24. /// Returns the main GUI panel for the inspector. <see cref="Layout"/> is a child of this panel.
  25. /// </summary>
  26. protected GUIPanel GUI
  27. {
  28. get { return mainPanel; }
  29. }
  30. /// <summary>
  31. /// Returns the object the inspector is currently displaying. If the current object is a resource use
  32. /// <see cref="InspectedResourcePath"/> instead;
  33. /// </summary>
  34. protected object InspectedObject
  35. {
  36. get { return inspectedObject; }
  37. }
  38. /// <summary>
  39. /// Returns the path to the resource the inspector is currently displaying.
  40. /// </summary>
  41. protected string InspectedResourcePath
  42. {
  43. get { return inspectedResourcePath; }
  44. }
  45. /// <summary>
  46. /// A set of properties that the inspector can read/write. They will be persisted even after the inspector is closed
  47. /// and restored when it is re-opened.
  48. /// </summary>
  49. protected internal SerializableProperties Persistent
  50. {
  51. get { return persistent; }
  52. }
  53. private GUIPanel rootGUI;
  54. private GUIPanel mainPanel;
  55. private GUILayoutY layout;
  56. private object inspectedObject;
  57. private string inspectedResourcePath;
  58. private SerializableProperties persistent;
  59. /// <summary>
  60. /// Common code called by both Initialize() overloads.
  61. /// </summary>
  62. /// <param name="gui">GUI panel to add the GUI elements to.</param>
  63. /// <param name="persistent">A set of properties that the inspector can read/write. They will be persisted even
  64. /// after the inspector is closed and restored when it is re-opened.</param>
  65. private void InitializeBase(GUIPanel gui, SerializableProperties persistent)
  66. {
  67. rootGUI = gui;
  68. this.persistent = persistent;
  69. GUILayout contentLayoutX = gui.AddLayoutX();
  70. contentLayoutX.AddSpace(5);
  71. GUILayout contentLayoutY = contentLayoutX.AddLayoutY();
  72. contentLayoutY.AddSpace(5);
  73. GUIPanel contentPanel = contentLayoutY.AddPanel();
  74. contentLayoutY.AddSpace(5);
  75. contentLayoutX.AddSpace(5);
  76. GUIPanel backgroundPanel = gui.AddPanel(START_BACKGROUND_DEPTH);
  77. GUITexture inspectorContentBg = new GUITexture(null, EditorStylesInternal.InspectorContentBg);
  78. backgroundPanel.AddElement(inspectorContentBg);
  79. mainPanel = contentPanel;
  80. layout = GUI.AddLayoutY();
  81. }
  82. /// <summary>
  83. /// Initializes the inspector using an object instance. Must be called after construction.
  84. /// </summary>
  85. /// <param name="gui">GUI panel to add the GUI elements to.</param>
  86. /// <param name="instance">Instance of the object whose fields to display GUI for.</param>
  87. /// <param name="persistent">A set of properties that the inspector can read/write. They will be persisted even
  88. /// after the inspector is closed and restored when it is re-opened.</param>
  89. internal virtual void Initialize(GUIPanel gui, object instance, SerializableProperties persistent)
  90. {
  91. InitializeBase(gui, persistent);
  92. inspectedObject = instance;
  93. Initialize();
  94. Refresh();
  95. }
  96. /// <summary>
  97. /// Initializes the inspector using a resource path. Must be called after construction.
  98. /// </summary>
  99. /// <param name="gui">GUI panel to add the GUI elements to.</param>
  100. /// <param name="path">Path to the resource for which to display GUI for.</param>
  101. /// <param name="persistent">A set of properties that the inspector can read/write. They will be persisted even
  102. /// after the inspector is closed and restored when it is re-opened.</param>
  103. internal virtual void Initialize(GUIPanel gui, string path, SerializableProperties persistent)
  104. {
  105. InitializeBase(gui, persistent);
  106. inspectedResourcePath = path;
  107. Initialize();
  108. Refresh();
  109. }
  110. /// <summary>
  111. /// Loads the currently inspected resource into the <see cref="InspectedObject"/> field. By default resources
  112. /// are not loaded and you can only retrieve their path through <see cref="InspectedResourcePath"/>.
  113. /// </summary>
  114. protected void LoadResource()
  115. {
  116. if(!string.IsNullOrEmpty(inspectedResourcePath))
  117. inspectedObject = ProjectLibrary.Load<Resource>(inspectedResourcePath);
  118. }
  119. /// <summary>
  120. /// Hides or shows the inspector GUI elements.
  121. /// </summary>
  122. /// <param name="visible">True to make the GUI elements visible.</param>
  123. internal virtual void SetVisible(bool visible)
  124. {
  125. rootGUI.Active = visible;
  126. }
  127. /// <summary>
  128. /// Destroys all inspector GUI elements.
  129. /// </summary>
  130. internal void Destroy()
  131. {
  132. Layout.Destroy();
  133. GUI.Destroy();
  134. }
  135. /// <summary>
  136. /// Called when the inspector is first created.
  137. /// </summary>
  138. protected internal abstract void Initialize();
  139. /// <summary>
  140. /// Checks if contents of the inspector have been modified, and updates them if needed.
  141. /// </summary>
  142. /// <returns>State representing was anything modified between two last calls to <see cref="Refresh"/>.</returns>
  143. protected internal abstract InspectableState Refresh();
  144. }
  145. /** @} */
  146. }