InspectableObject.cs 11 KB

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