InspectableObject.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System.Collections.Generic;
  2. using System;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Displays GUI for a serializable property containing a generic object. Inspectable object fields are displayed
  8. /// in separate rows.
  9. /// </summary>
  10. public class InspectableObject : InspectableField
  11. {
  12. private const int IndentAmount = 5;
  13. private object propertyValue;
  14. private List<InspectableField> children = new List<InspectableField>();
  15. private GUILayoutY guiLayout;
  16. private GUILayoutX guiChildLayout;
  17. private GUILayoutX guiTitleLayout;
  18. private GUILayoutX guiInternalTitleLayout;
  19. private bool isExpanded;
  20. private bool forceUpdate = true;
  21. private State state;
  22. /// <summary>
  23. /// Creates a new inspectable array GUI for the specified property.
  24. /// </summary>
  25. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  26. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field.Some fields may
  27. /// contain other fields, in which case you should increase this value by one.</param>
  28. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  29. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  30. public InspectableObject(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  31. : base(title, SerializableProperty.FieldType.Object, depth, layout, property)
  32. {
  33. }
  34. /// <inheritdoc/>
  35. public override GUILayoutX GetTitleLayout()
  36. {
  37. return guiTitleLayout;
  38. }
  39. /// <inheritdoc/>
  40. public override InspectableState Refresh(int layoutIndex)
  41. {
  42. // Check if modified internally and rebuild if needed
  43. object newPropertyValue = property.GetValue<object>();
  44. if (forceUpdate)
  45. {
  46. propertyValue = newPropertyValue;
  47. BuildGUI(layoutIndex);
  48. forceUpdate = false;
  49. }
  50. else if (propertyValue == null && newPropertyValue != null)
  51. {
  52. propertyValue = newPropertyValue;
  53. BuildGUI(layoutIndex);
  54. }
  55. else if (newPropertyValue == null && propertyValue != null)
  56. {
  57. propertyValue = null;
  58. BuildGUI(layoutIndex);
  59. }
  60. InspectableState state = InspectableState.NotModified;
  61. int currentIndex = 0;
  62. for (int i = 0; i < children.Count; i++)
  63. {
  64. state |= children[i].Refresh(currentIndex);
  65. currentIndex += children[i].GetNumLayoutElements();
  66. }
  67. return state;
  68. }
  69. /// <summary>
  70. /// Rebuilds the GUI object header if needed.
  71. /// </summary>
  72. /// <param name="layoutIndex">Index at which to insert the GUI elements.</param>
  73. protected void BuildGUI(int layoutIndex)
  74. {
  75. Action BuildEmptyGUI = () =>
  76. {
  77. guiInternalTitleLayout = guiTitleLayout.InsertLayoutX(0);
  78. guiInternalTitleLayout.AddElement(new GUILabel(title));
  79. guiInternalTitleLayout.AddElement(new GUILabel("Empty", GUIOption.FixedWidth(100)));
  80. if (!property.IsValueType)
  81. {
  82. GUIContent createIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Create));
  83. GUIButton createBtn = new GUIButton(createIcon, GUIOption.FixedWidth(30));
  84. createBtn.OnClick += OnCreateButtonClicked;
  85. guiInternalTitleLayout.AddElement(createBtn);
  86. }
  87. };
  88. Action BuildFilledGUI = () =>
  89. {
  90. guiInternalTitleLayout = guiTitleLayout.InsertLayoutX(0);
  91. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  92. guiFoldout.Value = isExpanded;
  93. guiFoldout.OnToggled += OnFoldoutToggled;
  94. guiInternalTitleLayout.AddElement(guiFoldout);
  95. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clear));
  96. GUIButton clearBtn = new GUIButton(clearIcon, GUIOption.FixedWidth(20));
  97. clearBtn.OnClick += OnClearButtonClicked;
  98. guiInternalTitleLayout.AddElement(clearBtn);
  99. if (isExpanded)
  100. {
  101. SerializableObject serializableObject = property.GetObject();
  102. SerializableField[] fields = serializableObject.Fields;
  103. if (fields.Length > 0)
  104. {
  105. guiChildLayout = guiLayout.AddLayoutX();
  106. guiChildLayout.AddSpace(IndentAmount);
  107. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  108. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  109. guiIndentLayoutX.AddSpace(IndentAmount);
  110. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  111. guiIndentLayoutY.AddSpace(IndentAmount);
  112. GUILayoutY guiContentLayout = guiIndentLayoutY.AddLayoutY();
  113. guiIndentLayoutY.AddSpace(IndentAmount);
  114. guiIndentLayoutX.AddSpace(IndentAmount);
  115. guiChildLayout.AddSpace(IndentAmount);
  116. short backgroundDepth = (short) (Inspector.START_BACKGROUND_DEPTH - depth - 1);
  117. string bgPanelStyle = depth%2 == 0
  118. ? EditorStyles.InspectorContentBgAlternate
  119. : EditorStyles.InspectorContentBg;
  120. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  121. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  122. backgroundPanel.AddElement(inspectorContentBg);
  123. int currentIndex = 0;
  124. foreach (var field in fields)
  125. {
  126. if (!field.Inspectable)
  127. continue;
  128. InspectableField inspectable = CreateInspectable(field.Name, currentIndex, depth + 1,
  129. new InspectableFieldLayout(guiContentLayout), field.GetProperty());
  130. children.Add(inspectable);
  131. currentIndex += inspectable.GetNumLayoutElements();
  132. }
  133. }
  134. }
  135. else
  136. guiChildLayout = null;
  137. };
  138. if (state == State.None)
  139. {
  140. if (propertyValue != null)
  141. {
  142. BuildFilledGUI();
  143. state = State.Filled;
  144. }
  145. else
  146. {
  147. BuildEmptyGUI();
  148. state = State.Empty;
  149. }
  150. }
  151. else if (state == State.Empty)
  152. {
  153. if (propertyValue != null)
  154. {
  155. guiInternalTitleLayout.Destroy();
  156. BuildFilledGUI();
  157. state = State.Filled;
  158. }
  159. }
  160. else if (state == State.Filled)
  161. {
  162. foreach (var child in children)
  163. child.Destroy();
  164. children.Clear();
  165. guiInternalTitleLayout.Destroy();
  166. if (guiChildLayout != null)
  167. {
  168. guiChildLayout.Destroy();
  169. guiChildLayout = null;
  170. }
  171. if (propertyValue == null)
  172. {
  173. BuildEmptyGUI();
  174. state = State.Empty;
  175. }
  176. else
  177. {
  178. BuildFilledGUI();
  179. }
  180. }
  181. }
  182. /// <inheritdoc/>
  183. protected internal override void Initialize(int index)
  184. {
  185. guiLayout = layout.AddLayoutY(index);
  186. guiTitleLayout = guiLayout.AddLayoutX();
  187. propertyValue = property.GetValue<object>();
  188. BuildGUI(index);
  189. }
  190. /// <summary>
  191. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  192. /// </summary>
  193. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  194. private void OnFoldoutToggled(bool expanded)
  195. {
  196. isExpanded = expanded;
  197. forceUpdate = true;
  198. }
  199. /// <summary>
  200. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new object with default
  201. /// values in the place of the current array.
  202. /// </summary>
  203. private void OnCreateButtonClicked()
  204. {
  205. property.SetValue(property.CreateObjectInstance<object>());
  206. }
  207. /// <summary>
  208. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current object and sets
  209. /// the reference to the object in the parent object to null. This is only relevant for objects of reference types.
  210. /// </summary>
  211. private void OnClearButtonClicked()
  212. {
  213. property.SetValue<object>(null);
  214. }
  215. /// <summary>
  216. /// Possible states object GUI can be in.
  217. /// </summary>
  218. private enum State
  219. {
  220. None,
  221. Empty,
  222. Filled
  223. }
  224. }
  225. }