InspectableField.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. /// <summary>
  6. /// Inspectable field displays GUI elements for a single <see cref="SerializableProperty"/>. This is a base class that
  7. /// should be specialized for all supported types contained by <see cref="SerializableProperty"/>. Inspectable fields
  8. /// can and should be created recursively - normally complex types like objects and arrays will contain fields of their
  9. /// own, while primitive types like integer or boolean will consist of only a GUI element.
  10. /// </summary>
  11. public abstract class InspectableField
  12. {
  13. protected InspectableFieldLayout layout;
  14. protected SerializableProperty property;
  15. protected string title;
  16. protected int depth;
  17. /// <summary>
  18. /// Creates a new inspectable field GUI for the specified property.
  19. /// </summary>
  20. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  21. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  22. /// contain other fields, in which case you should increase this value by one.</param>
  23. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  24. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  25. public InspectableField(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  26. {
  27. this.layout = layout;
  28. this.title = title;
  29. this.property = property;
  30. this.depth = depth;
  31. }
  32. /// <summary>
  33. /// Checks if contents of the field have been modified, and updates them if needed.
  34. /// </summary>
  35. /// <param name="layoutIndex">Index in the parent's layout at which to insert the GUI elements for this field.
  36. /// </param>
  37. /// <returns>True if there were any modifications in this field, or any child fields.</returns>
  38. public virtual bool Refresh(int layoutIndex)
  39. {
  40. bool anythingModified = false;
  41. if (IsModified())
  42. {
  43. Update(layoutIndex);
  44. anythingModified = true;
  45. }
  46. return anythingModified;
  47. }
  48. /// <summary>
  49. /// Returns the total number of GUI elements in the field's layout.
  50. /// </summary>
  51. /// <returns>Number of GUI elements in the field's layout.</returns>
  52. public int GetNumLayoutElements()
  53. {
  54. return layout.NumElements;
  55. }
  56. /// <summary>
  57. /// Returns an optional title layout. Certain fields may contain separate title and content layouts. Parent fields
  58. /// may use the separate title layout instead of the content layout to append elements. Having a separate title
  59. /// layout is purely cosmetical.
  60. /// </summary>
  61. /// <returns>Title layout if the field has one, null otherwise.</returns>
  62. public virtual GUILayoutX GetTitleLayout()
  63. {
  64. return null;
  65. }
  66. /// <summary>
  67. /// Checks have the values in the referenced serializable property have been changed compare to the value currently
  68. /// displayed in the field.
  69. /// </summary>
  70. /// <returns>True if the value has been modified and needs updating.</returns>
  71. public virtual bool IsModified()
  72. {
  73. return false;
  74. }
  75. /// <summary>
  76. /// Checks does the field GUI has to be rebuilt if the field is marked as modified.
  77. /// </summary>
  78. /// <returns>True if field GUI has to be rebuilt if the field is marked as modified.</returns>
  79. public virtual bool ShouldRebuildOnModify()
  80. {
  81. return false;
  82. }
  83. /// <summary>
  84. /// Reconstructs the GUI by using the most up to date values from the referenced serializable property.
  85. /// </summary>
  86. /// <param name="layoutIndex">Index in the parent's layout at which to insert the GUI elements for this field.</param>
  87. protected internal abstract void Update(int layoutIndex);
  88. /// <summary>
  89. /// Initializes the GUI elements for the field.
  90. /// </summary>
  91. /// <param name="layoutIndex">Index at which to insert the GUI elements.</param>
  92. protected internal abstract void BuildGUI(int layoutIndex);
  93. /// <summary>
  94. /// Destroys all GUI elements in the inspectable field.
  95. /// </summary>
  96. public virtual void Destroy()
  97. {
  98. layout.DestroyElements();
  99. }
  100. /// <summary>
  101. /// Creates a new inspectable field, automatically detecting the most appropriate implementation for the type
  102. /// contained in the provided serializable property. This may be one of the built-in inspectable field implemetations
  103. /// (like ones for primitives like int or bool), or a user defined implementation defined with a
  104. /// <see cref="CustomInspector"/> attribute.
  105. /// </summary>
  106. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  107. /// <param name="layoutIndex">Index into the parent layout at which to insert the GUI elements for the field .</param>
  108. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  109. /// contain other fields, in which case you should increase this value by one.</param>
  110. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  111. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  112. /// <returns>Inspectable field implementation that can be used for displaying the GUI for a serializable property
  113. /// of the provided type.</returns>
  114. public static InspectableField CreateInspectable(string title, int layoutIndex, int depth,
  115. InspectableFieldLayout layout, SerializableProperty property)
  116. {
  117. InspectableField field = null;
  118. Type customInspectable = InspectorUtility.GetCustomInspectable(property.InternalType);
  119. if (customInspectable != null)
  120. {
  121. field = (InspectableField) Activator.CreateInstance(customInspectable, depth, title, property);
  122. }
  123. else
  124. {
  125. switch (property.Type)
  126. {
  127. case SerializableProperty.FieldType.Int:
  128. field = new InspectableInt(title, depth, layout, property);
  129. break;
  130. case SerializableProperty.FieldType.Float:
  131. field = new InspectableFloat(title, depth, layout, property);
  132. break;
  133. case SerializableProperty.FieldType.Bool:
  134. field = new InspectableBool(title, depth, layout, property);
  135. break;
  136. case SerializableProperty.FieldType.Color:
  137. field = new InspectableColor(title, depth, layout, property);
  138. break;
  139. case SerializableProperty.FieldType.String:
  140. field = new InspectableString(title, depth, layout, property);
  141. break;
  142. case SerializableProperty.FieldType.Vector2:
  143. field = new InspectableVector2(title, depth, layout, property);
  144. break;
  145. case SerializableProperty.FieldType.Vector3:
  146. field = new InspectableVector3(title, depth, layout, property);
  147. break;
  148. case SerializableProperty.FieldType.Vector4:
  149. field = new InspectableVector4(title, depth, layout, property);
  150. break;
  151. case SerializableProperty.FieldType.ResourceRef:
  152. field = new InspectableResourceRef(title, depth, layout, property);
  153. break;
  154. case SerializableProperty.FieldType.GameObjectRef:
  155. field = new InspectableGameObjectRef(title, depth, layout, property);
  156. break;
  157. case SerializableProperty.FieldType.Object:
  158. field = new InspectableObject(title, depth, layout, property);
  159. break;
  160. case SerializableProperty.FieldType.Array:
  161. field = new InspectableArray(title, depth, layout, property);
  162. break;
  163. case SerializableProperty.FieldType.List:
  164. field = new InspectableList(title, depth, layout, property);
  165. break;
  166. }
  167. }
  168. if (field == null)
  169. throw new Exception("No inspector exists for the provided field type.");
  170. field.BuildGUI(layoutIndex);
  171. field.Refresh(layoutIndex);
  172. return field;
  173. }
  174. }
  175. }