InspectableArray.cs 13 KB

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