Inspector.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Collections.Generic;
  4. using bs;
  5. namespace bs.Editor
  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 secondary GUI panel. Located at the bottom of the inspector window and unlike <see cref="GUI"/> has
  32. /// no padding or styling applied. Only available when inspecting resources.
  33. /// </summary>
  34. protected GUIPanel PreviewGUI
  35. {
  36. get { return previewPanel; }
  37. }
  38. /// <summary>
  39. /// Returns the object the inspector is currently displaying. If the current object is a resource use
  40. /// <see cref="InspectedResourcePath"/> instead;
  41. /// </summary>
  42. protected object InspectedObject
  43. {
  44. get { return inspectedObject; }
  45. }
  46. /// <summary>
  47. /// Returns the path to the resource the inspector is currently displaying.
  48. /// </summary>
  49. protected string InspectedResourcePath
  50. {
  51. get { return inspectedResourcePath; }
  52. }
  53. /// <summary>
  54. /// A set of properties that the inspector can read/write. They will be persisted even after the inspector is closed
  55. /// and restored when it is re-opened.
  56. /// </summary>
  57. protected internal SerializableProperties Persistent
  58. {
  59. get { return persistent; }
  60. }
  61. private GUIPanel rootGUI;
  62. private GUIPanel mainPanel;
  63. private GUIPanel previewPanel;
  64. private GUILayoutY layout;
  65. private object inspectedObject;
  66. private string inspectedResourcePath;
  67. private SerializableProperties persistent;
  68. /// <summary>
  69. /// Common code called by both Initialize() overloads.
  70. /// </summary>
  71. /// <param name="mainGui">Primary GUI panel to add the GUI elements to.</param>
  72. /// <param name="previewGui">Secondary GUI panel located at the bottom of the inspector window, aimed primarily for
  73. /// resource previews, but can be used for any purpose.</param>
  74. /// <param name="persistent">A set of properties that the inspector can read/write. They will be persisted even
  75. /// after the inspector is closed and restored when it is re-opened.</param>
  76. private void InitializeBase(GUIPanel mainGui, GUIPanel previewGui, SerializableProperties persistent)
  77. {
  78. rootGUI = mainGui;
  79. this.persistent = persistent;
  80. GUILayout contentLayoutX = mainGui.AddLayoutX();
  81. contentLayoutX.AddSpace(5);
  82. GUILayout contentLayoutY = contentLayoutX.AddLayoutY();
  83. contentLayoutY.AddSpace(5);
  84. GUIPanel contentPanel = contentLayoutY.AddPanel();
  85. contentLayoutY.AddSpace(5);
  86. contentLayoutX.AddSpace(5);
  87. GUIPanel backgroundPanel = mainGui.AddPanel(START_BACKGROUND_DEPTH);
  88. GUITexture inspectorContentBg = new GUITexture(null, EditorStylesInternal.InspectorContentBg);
  89. backgroundPanel.AddElement(inspectorContentBg);
  90. mainPanel = contentPanel;
  91. previewPanel = previewGui;
  92. layout = GUI.AddLayoutY();
  93. }
  94. /// <summary>
  95. /// Initializes the inspector using an object instance. Must be called after construction.
  96. /// </summary>
  97. /// <param name="gui">GUI panel to add the GUI elements to.</param>
  98. /// <param name="instance">Instance of the object whose fields to display GUI for.</param>
  99. /// <param name="persistent">A set of properties that the inspector can read/write. They will be persisted even
  100. /// after the inspector is closed and restored when it is re-opened.</param>
  101. internal virtual void Initialize(GUIPanel gui, object instance, SerializableProperties persistent)
  102. {
  103. InitializeBase(gui, null, persistent);
  104. inspectedObject = instance;
  105. Initialize();
  106. Refresh();
  107. }
  108. /// <summary>
  109. /// Initializes the inspector using a resource path. Must be called after construction.
  110. /// </summary>
  111. /// <param name="mainGui">Primary GUI panel to add the GUI elements to.</param>
  112. /// <param name="previewGui">Secondary GUI panel located at the bottom of the inspector window, aimed primarily for
  113. /// resource previews, but can be used for any purpose.</param>
  114. /// <param name="path">Path to the resource for which to display GUI for.</param>
  115. /// <param name="persistent">A set of properties that the inspector can read/write. They will be persisted even
  116. /// after the inspector is closed and restored when it is re-opened.</param>
  117. internal virtual void Initialize(GUIPanel mainGui, GUIPanel previewGui, string path,
  118. SerializableProperties persistent)
  119. {
  120. InitializeBase(mainGui, previewGui, persistent);
  121. inspectedResourcePath = path;
  122. Initialize();
  123. Refresh();
  124. }
  125. /// <summary>
  126. /// Changes keyboard focus to the provided field.
  127. /// </summary>
  128. /// <param name="path">Path to the field on the object being inspected.</param>
  129. internal virtual void FocusOnField(string path) { }
  130. /// <summary>
  131. /// Loads the currently inspected resource into the <see cref="InspectedObject"/> field. By default resources
  132. /// are not loaded and you can only retrieve their path through <see cref="InspectedResourcePath"/>.
  133. /// </summary>
  134. protected void LoadResource()
  135. {
  136. if(!string.IsNullOrEmpty(inspectedResourcePath))
  137. inspectedObject = ProjectLibrary.Load<Resource>(inspectedResourcePath);
  138. }
  139. /// <summary>
  140. /// Hides or shows the inspector GUI elements.
  141. /// </summary>
  142. /// <param name="visible">True to make the GUI elements visible.</param>
  143. internal virtual void SetVisible(bool visible)
  144. {
  145. rootGUI.Active = visible;
  146. }
  147. /// <summary>
  148. /// Destroys all inspector GUI elements.
  149. /// </summary>
  150. internal void Destroy()
  151. {
  152. Layout.Destroy();
  153. GUI.Destroy();
  154. }
  155. /// <summary>
  156. /// Called when the inspector is first created.
  157. /// </summary>
  158. protected internal abstract void Initialize();
  159. /// <summary>
  160. /// Checks if contents of the inspector have been modified, and updates them if needed.
  161. /// </summary>
  162. /// <returns>State representing was anything modified between two last calls to <see cref="Refresh"/>.</returns>
  163. protected internal abstract InspectableState Refresh();
  164. }
  165. /** @} */
  166. }