InspectableArray.cs 13 KB

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