InspectableArray.cs 10 KB

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