InspectableArray.cs 13 KB

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