InspectableObject.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. new LocEdString("Create"));
  84. GUIButton createBtn = new GUIButton(createIcon, GUIOption.FixedWidth(30));
  85. createBtn.OnClick += OnCreateButtonClicked;
  86. guiInternalTitleLayout.AddElement(createBtn);
  87. }
  88. };
  89. Action BuildFilledGUI = () =>
  90. {
  91. guiInternalTitleLayout = guiTitleLayout.InsertLayoutX(0);
  92. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  93. guiFoldout.Value = isExpanded;
  94. guiFoldout.OnToggled += OnFoldoutToggled;
  95. guiInternalTitleLayout.AddElement(guiFoldout);
  96. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clear),
  97. new LocEdString("Clear"));
  98. GUIButton clearBtn = new GUIButton(clearIcon, GUIOption.FixedWidth(20));
  99. clearBtn.OnClick += OnClearButtonClicked;
  100. guiInternalTitleLayout.AddElement(clearBtn);
  101. if (isExpanded)
  102. {
  103. SerializableObject serializableObject = property.GetObject();
  104. SerializableField[] fields = serializableObject.Fields;
  105. if (fields.Length > 0)
  106. {
  107. guiChildLayout = guiLayout.AddLayoutX();
  108. guiChildLayout.AddSpace(IndentAmount);
  109. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  110. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  111. guiIndentLayoutX.AddSpace(IndentAmount);
  112. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  113. guiIndentLayoutY.AddSpace(IndentAmount);
  114. GUILayoutY guiContentLayout = guiIndentLayoutY.AddLayoutY();
  115. guiIndentLayoutY.AddSpace(IndentAmount);
  116. guiIndentLayoutX.AddSpace(IndentAmount);
  117. guiChildLayout.AddSpace(IndentAmount);
  118. short backgroundDepth = (short) (Inspector.START_BACKGROUND_DEPTH - depth - 1);
  119. string bgPanelStyle = depth%2 == 0
  120. ? EditorStyles.InspectorContentBgAlternate
  121. : EditorStyles.InspectorContentBg;
  122. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  123. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  124. backgroundPanel.AddElement(inspectorContentBg);
  125. int currentIndex = 0;
  126. foreach (var field in fields)
  127. {
  128. if (!field.Inspectable)
  129. continue;
  130. InspectableField inspectable = CreateInspectable(field.Name, currentIndex, depth + 1,
  131. new InspectableFieldLayout(guiContentLayout), field.GetProperty());
  132. children.Add(inspectable);
  133. currentIndex += inspectable.GetNumLayoutElements();
  134. }
  135. }
  136. }
  137. else
  138. guiChildLayout = null;
  139. };
  140. if (state == State.None)
  141. {
  142. if (propertyValue != null)
  143. {
  144. BuildFilledGUI();
  145. state = State.Filled;
  146. }
  147. else
  148. {
  149. BuildEmptyGUI();
  150. state = State.Empty;
  151. }
  152. }
  153. else if (state == State.Empty)
  154. {
  155. if (propertyValue != null)
  156. {
  157. guiInternalTitleLayout.Destroy();
  158. BuildFilledGUI();
  159. state = State.Filled;
  160. }
  161. }
  162. else if (state == State.Filled)
  163. {
  164. foreach (var child in children)
  165. child.Destroy();
  166. children.Clear();
  167. guiInternalTitleLayout.Destroy();
  168. if (guiChildLayout != null)
  169. {
  170. guiChildLayout.Destroy();
  171. guiChildLayout = null;
  172. }
  173. if (propertyValue == null)
  174. {
  175. BuildEmptyGUI();
  176. state = State.Empty;
  177. }
  178. else
  179. {
  180. BuildFilledGUI();
  181. }
  182. }
  183. }
  184. /// <inheritdoc/>
  185. protected internal override void Initialize(int index)
  186. {
  187. guiLayout = layout.AddLayoutY(index);
  188. guiTitleLayout = guiLayout.AddLayoutX();
  189. propertyValue = property.GetValue<object>();
  190. BuildGUI(index);
  191. }
  192. /// <summary>
  193. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  194. /// </summary>
  195. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  196. private void OnFoldoutToggled(bool expanded)
  197. {
  198. isExpanded = expanded;
  199. forceUpdate = true;
  200. }
  201. /// <summary>
  202. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new object with default
  203. /// values in the place of the current array.
  204. /// </summary>
  205. private void OnCreateButtonClicked()
  206. {
  207. property.SetValue(property.CreateObjectInstance<object>());
  208. }
  209. /// <summary>
  210. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current object and sets
  211. /// the reference to the object in the parent object to null. This is only relevant for objects of reference types.
  212. /// </summary>
  213. private void OnClearButtonClicked()
  214. {
  215. property.SetValue<object>(null);
  216. }
  217. /// <summary>
  218. /// Possible states object GUI can be in.
  219. /// </summary>
  220. private enum State
  221. {
  222. None,
  223. Empty,
  224. Filled
  225. }
  226. }
  227. }