InspectableArray.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 anythingModified = false;
  48. if (IsModified())
  49. {
  50. Update(layoutIndex);
  51. anythingModified = true;
  52. }
  53. anythingModified |= arrayGUIField.Refresh();
  54. return anythingModified;
  55. }
  56. /// <inheritdoc/>
  57. public override bool ShouldRebuildOnModify()
  58. {
  59. return true;
  60. }
  61. /// <inheritdoc/>
  62. protected internal override void BuildGUI(int layoutIndex)
  63. {
  64. GUILayout arrayLayout = layout.AddLayoutY(layoutIndex);
  65. arrayGUIField = InspectableArrayGUI.Create(title, property, arrayLayout);
  66. }
  67. /// <inheritdoc/>
  68. protected internal override void Update(int layoutIndex)
  69. {
  70. propertyValue = property.GetValue<object>();
  71. if (propertyValue != null)
  72. {
  73. SerializableArray array = property.GetArray();
  74. numArrayElements = array.GetLength();
  75. }
  76. else
  77. numArrayElements = 0;
  78. layout.DestroyElements();
  79. BuildGUI(layoutIndex);
  80. }
  81. /// <summary>
  82. /// Handles creation of GUI elements for a GUI list field that displays a <see cref="SerializableArray"/> object.
  83. /// </summary>
  84. private class InspectableArrayGUI : GUIListFieldBase
  85. {
  86. private SerializableProperty property;
  87. /// <summary>
  88. /// Constructs an inspectable array GUI.
  89. /// </summary>
  90. /// <param name="property">Serializable property referencing a single-dimensional array.</param>
  91. private InspectableArrayGUI(SerializableProperty property)
  92. {
  93. this.property = property;
  94. }
  95. /// <summary>
  96. /// Creates a new inspectable GUI array.
  97. /// </summary>
  98. /// <param name="title">Label to display on the list GUI title.</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. /// <returns>Newly created inspectable GUI array object.</returns>
  102. public static InspectableArrayGUI Create(LocString title, SerializableProperty property, GUILayout layout)
  103. {
  104. InspectableArrayGUI newArrayField = new InspectableArrayGUI(property);
  105. object propertyValue = property.GetValue<object>();
  106. if (propertyValue != null)
  107. {
  108. SerializableArray array = property.GetArray();
  109. newArrayField.Construct<InspectableArrayGUIRow>(title, false, array.GetLength(), layout);
  110. }
  111. else
  112. newArrayField.Construct<InspectableArrayGUIRow>(title, true, 0, layout);
  113. return newArrayField;
  114. }
  115. /// <inheritdoc/>
  116. protected internal override object GetValue(int seqIndex)
  117. {
  118. SerializableArray array = property.GetArray();
  119. return array.GetProperty(seqIndex);
  120. }
  121. /// <inheritdoc/>
  122. protected internal override void SetValue(int seqIndex, object value)
  123. {
  124. // Setting the value should be done through the property
  125. throw new InvalidOperationException();
  126. }
  127. /// <inheritdoc/>
  128. protected override void OnCreateButtonClicked()
  129. {
  130. property.SetValue(property.CreateArrayInstance(new int[1] { 0 }));
  131. }
  132. /// <inheritdoc/>
  133. protected override void OnResizeButtonClicked()
  134. {
  135. int size = guiSizeField.Value; // TODO - Support multi-rank arrays
  136. Array newArray = property.CreateArrayInstance(new int[] { size });
  137. Array array = property.GetValue<Array>();
  138. int maxSize = MathEx.Min(size, array.Length);
  139. for (int i = 0; i < maxSize; i++)
  140. newArray.SetValue(array.GetValue(i), i);
  141. property.SetValue(newArray);
  142. }
  143. /// <inheritdoc/>
  144. protected override void OnClearButtonClicked()
  145. {
  146. property.SetValue<object>(null);
  147. }
  148. /// <inheritdoc/>
  149. protected internal override void OnDeleteButtonClicked(int index)
  150. {
  151. Array array = property.GetValue<Array>();
  152. int size = MathEx.Max(0, array.Length - 1);
  153. Array newArray = property.CreateArrayInstance(new int[] { size });
  154. int destIdx = 0;
  155. for (int i = 0; i < array.Length; i++)
  156. {
  157. if (i == index)
  158. continue;
  159. newArray.SetValue(array.GetValue(i), destIdx);
  160. destIdx++;
  161. }
  162. property.SetValue(newArray);
  163. }
  164. /// <inheritdoc/>
  165. protected internal override void OnCloneButtonClicked(int index)
  166. {
  167. SerializableArray array = property.GetArray();
  168. int size = array.GetLength() + 1;
  169. Array newArray = property.CreateArrayInstance(new int[] { size });
  170. object clonedEntry = null;
  171. for (int i = 0; i < array.GetLength(); i++)
  172. {
  173. object value = array.GetProperty(i).GetValue<object>();
  174. newArray.SetValue(value, i);
  175. if (i == index)
  176. {
  177. clonedEntry = array.GetProperty(i).GetValueCopy<object>();
  178. }
  179. }
  180. newArray.SetValue(clonedEntry, size - 1);
  181. property.SetValue(newArray);
  182. }
  183. /// <inheritdoc/>
  184. protected internal override void OnMoveUpButtonClicked(int index)
  185. {
  186. Array array = property.GetValue<Array>();
  187. if ((index - 1) >= 0)
  188. {
  189. object previousEntry = array.GetValue(index - 1);
  190. array.SetValue(array.GetValue(index), index - 1);
  191. array.SetValue(previousEntry, index);
  192. }
  193. }
  194. /// <inheritdoc/>
  195. protected internal override void OnMoveDownButtonClicked(int index)
  196. {
  197. Array array = property.GetValue<Array>();
  198. if ((index + 1) < array.Length)
  199. {
  200. object nextEntry = array.GetValue(index + 1);
  201. array.SetValue(array.GetValue(index), index + 1);
  202. array.SetValue(nextEntry, index);
  203. }
  204. }
  205. }
  206. /// <summary>
  207. /// Contains GUI elements for a single entry in the array.
  208. /// </summary>
  209. private class InspectableArrayGUIRow : GUIListFieldRow
  210. {
  211. private InspectableField field;
  212. /// <inheritdoc/>
  213. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  214. {
  215. if (field == null)
  216. {
  217. SerializableProperty property = GetValue<SerializableProperty>();
  218. field = CreateInspectable(seqIndex + ".", 0, 0,
  219. new InspectableFieldLayout(layout), property);
  220. }
  221. return field.GetTitleLayout();
  222. }
  223. /// <inheritdoc/>
  224. protected internal override bool Refresh(out bool rebuildGUI)
  225. {
  226. if (field.IsModified())
  227. {
  228. rebuildGUI = field.ShouldRebuildOnModify();
  229. return field.Refresh(0);
  230. }
  231. rebuildGUI = false;
  232. return field.Refresh(0);
  233. }
  234. }
  235. }
  236. }