InspectableArray.cs 11 KB

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