InspectableObject.cs 11 KB

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