InspectableField.cs 10 KB

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