InspectableObject.cs 11 KB

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