InspectableObject.cs 10 KB

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