InspectableArray.cs 10 KB

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