InspectableObject.cs 10 KB

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