InspectableList.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. /// <summary>
  10. /// Displays GUI for a serializable property containing a list. List contents are displayed as rows of entries
  11. /// that can be shown, hidden or manipulated.
  12. /// </summary>
  13. public class InspectableList : InspectableField
  14. {
  15. private InspectableListGUI listGUIField;
  16. /// <summary>
  17. /// Creates a new inspectable list GUI for the specified property.
  18. /// </summary>
  19. /// <param name="parent">Parent Inspector this field belongs to.</param>
  20. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  21. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  22. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  23. /// contain other fields, in which case you should increase this value by one.</param>
  24. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  25. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  26. public InspectableList(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
  27. SerializableProperty property)
  28. : base(parent, title, path, SerializableProperty.FieldType.List, depth, layout, property)
  29. {
  30. }
  31. /// <inheritdoc/>
  32. public override GUILayoutX GetTitleLayout()
  33. {
  34. return listGUIField.GetTitleLayout();
  35. }
  36. /// <inheritdoc/>
  37. public override InspectableState Refresh(int layoutIndex)
  38. {
  39. return listGUIField.Refresh();
  40. }
  41. /// <inheritdoc/>
  42. protected internal override void Initialize(int layoutIndex)
  43. {
  44. GUILayout arrayLayout = layout.AddLayoutY(layoutIndex);
  45. listGUIField = InspectableListGUI.Create(parent, title, path, property, arrayLayout, depth);
  46. listGUIField.IsExpanded = parent.Persistent.GetBool(path + "_Expanded");
  47. listGUIField.OnExpand += x => parent.Persistent.SetBool(path + "_Expanded", x);
  48. }
  49. /// <summary>
  50. /// Handles creation of GUI elements for a GUI list field that displays a <see cref="SerializableList"/> object.
  51. /// </summary>
  52. private class InspectableListGUI : GUIListFieldBase
  53. {
  54. private IList list;
  55. private int numElements;
  56. private Inspector parent;
  57. private SerializableProperty property;
  58. private string path;
  59. /// <summary>
  60. /// Returns the parent inspector the array GUI belongs to.
  61. /// </summary>
  62. public Inspector Inspector
  63. {
  64. get { return parent; }
  65. }
  66. /// <summary>
  67. /// Returns a property path to the array field (name of the array field and all parent object fields).
  68. /// </summary>
  69. public string Path
  70. {
  71. get { return path; }
  72. }
  73. /// <summary>
  74. /// Constructs a new empty inspectable list GUI.
  75. /// </summary>
  76. /// <param name="parent">Parent Inspector this field belongs to.</param>
  77. /// <param name="title">Label to display on the list GUI title.</param>
  78. /// <param name="path">Full path to this property (includes name of this property and all parent properties).
  79. /// </param>
  80. /// <param name="property">Serializable property referencing a list.</param>
  81. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  82. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  83. /// nested containers whose backgrounds are overlaping. Also determines background style,
  84. /// depths divisible by two will use an alternate style.</param>
  85. public InspectableListGUI(Inspector parent, LocString title, string path, SerializableProperty property,
  86. GUILayout layout, int depth)
  87. : base(title, layout, depth)
  88. {
  89. this.property = property;
  90. this.parent = parent;
  91. this.path = path;
  92. list = property.GetValue<IList>();
  93. if (list != null)
  94. numElements = list.Count;
  95. }
  96. /// <summary>
  97. /// Creates a new inspectable list GUI object that displays the contents of the provided serializable property.
  98. /// </summary>
  99. /// <param name="parent">Parent Inspector this field belongs to.</param>
  100. /// <param name="title">Label to display on the list GUI title.</param>
  101. /// <param name="path">Full path to this property (includes name of this property and all parent properties).
  102. /// </param>
  103. /// <param name="property">Serializable property referencing a list.</param>
  104. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  105. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  106. /// nested containers whose backgrounds are overlaping. Also determines background style,
  107. /// depths divisible by two will use an alternate style.</param>
  108. public static InspectableListGUI Create(Inspector parent, LocString title, string path,
  109. SerializableProperty property, GUILayout layout, int depth)
  110. {
  111. InspectableListGUI listGUI = new InspectableListGUI(parent, title, path, property, layout, depth);
  112. listGUI.BuildGUI();
  113. return listGUI;
  114. }
  115. /// <inheritdoc/>
  116. public override InspectableState Refresh()
  117. {
  118. // Check if any modifications to the array were made outside the inspector
  119. IList newList = property.GetValue<IList>();
  120. if (list == null && newList != null)
  121. {
  122. list = newList;
  123. numElements = list.Count;
  124. BuildGUI();
  125. }
  126. else if (newList == null && list != null)
  127. {
  128. list = null;
  129. numElements = 0;
  130. BuildGUI();
  131. }
  132. else
  133. {
  134. if (list != null)
  135. {
  136. if (numElements != list.Count)
  137. {
  138. numElements = list.Count;
  139. BuildGUI();
  140. }
  141. }
  142. }
  143. return base.Refresh();
  144. }
  145. /// <inheritdoc/>
  146. protected override GUIListFieldRow CreateRow()
  147. {
  148. return new InspectableListGUIRow();
  149. }
  150. /// <inheritdoc/>
  151. protected override bool IsNull()
  152. {
  153. return list == null;
  154. }
  155. /// <inheritdoc/>
  156. protected override int GetNumRows()
  157. {
  158. if (list != null)
  159. return list.Count;
  160. return 0;
  161. }
  162. /// <inheritdoc/>
  163. protected internal override object GetValue(int seqIndex)
  164. {
  165. SerializableList list = property.GetList();
  166. return list.GetProperty(seqIndex);
  167. }
  168. /// <inheritdoc/>
  169. protected internal override void SetValue(int seqIndex, object value)
  170. {
  171. // Setting the value should be done through the property
  172. throw new InvalidOperationException();
  173. }
  174. /// <inheritdoc/>
  175. protected override void CreateList()
  176. {
  177. list = property.CreateListInstance(0);
  178. property.SetValue(list);
  179. numElements = 0;
  180. }
  181. /// <inheritdoc/>
  182. protected override void ResizeList()
  183. {
  184. int size = guiSizeField.Value;
  185. IList newList = property.CreateListInstance(size);
  186. int maxSize = MathEx.Min(size, list.Count);
  187. for (int i = 0; i < maxSize; i++)
  188. newList[i] = list[i];
  189. property.SetValue(newList);
  190. list = newList;
  191. numElements = list.Count;
  192. }
  193. /// <inheritdoc/>
  194. protected override void ClearList()
  195. {
  196. property.SetValue<object>(null);
  197. list = null;
  198. numElements = 0;
  199. }
  200. /// <inheritdoc/>
  201. protected internal override void DeleteElement(int index)
  202. {
  203. if (index >= 0 && index < list.Count)
  204. list.RemoveAt(index);
  205. numElements = list.Count;
  206. }
  207. /// <inheritdoc/>
  208. protected internal override void CloneElement(int index)
  209. {
  210. SerializableList serializableList = property.GetList();
  211. if (index >= 0 && index < list.Count)
  212. list.Add(SerializableUtility.Clone(serializableList.GetProperty(index).GetValue<object>()));
  213. numElements = list.Count;
  214. }
  215. /// <inheritdoc/>
  216. protected internal override void MoveUpElement(int index)
  217. {
  218. if ((index - 1) >= 0)
  219. {
  220. object previousEntry = list[index - 1];
  221. list[index - 1] = list[index];
  222. list[index] = previousEntry;
  223. }
  224. }
  225. /// <inheritdoc/>
  226. protected internal override void MoveDownElement(int index)
  227. {
  228. if ((index + 1) < list.Count)
  229. {
  230. object nextEntry = list[index + 1];
  231. list[index + 1] = list[index];
  232. list[index] = nextEntry;
  233. }
  234. }
  235. }
  236. /// <summary>
  237. /// Contains GUI elements for a single entry in the list.
  238. /// </summary>
  239. private class InspectableListGUIRow : GUIListFieldRow
  240. {
  241. private InspectableField field;
  242. /// <inheritdoc/>
  243. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  244. {
  245. InspectableListGUI listParent = (InspectableListGUI)parent;
  246. SerializableProperty property = GetValue<SerializableProperty>();
  247. string entryPath = listParent.Path + "[" + SeqIndex + "]";
  248. field = CreateInspectable(listParent.Inspector, SeqIndex + ".", entryPath, 0, Depth + 1,
  249. new InspectableFieldLayout(layout), property);
  250. return field.GetTitleLayout();
  251. }
  252. /// <inheritdoc/>
  253. protected internal override InspectableState Refresh()
  254. {
  255. field.Property = GetValue<SerializableProperty>();
  256. return field.Refresh(0);
  257. }
  258. }
  259. }
  260. }