InspectableArray.cs 13 KB

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