InspectableArray.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 = new InspectableArrayGUI();
  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.Update(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 empty inspectable array GUI.
  84. /// </summary>
  85. public InspectableArrayGUI()
  86. { }
  87. /// <summary>
  88. /// Updates the contents of the inspectable GUI array. Must be called at least once in order for the contents
  89. /// to be populated.
  90. /// </summary>
  91. /// <param name="title">Label to display on the list GUI title.</param>
  92. /// <param name="property">Serializable property referencing a single-dimensional array.</param>
  93. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  94. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  95. /// nested containers whose backgrounds are overlaping. Also determines background style,
  96. /// depths divisible by two will use an alternate style.</param>
  97. public void Update(LocString title, SerializableProperty property, GUILayout layout, int depth)
  98. {
  99. this.property = property;
  100. object propertyValue = property.GetValue<object>();
  101. if (propertyValue != null)
  102. {
  103. SerializableArray array = property.GetArray();
  104. base.Update<InspectableArrayGUIRow>(title, false, array.GetLength(), layout, depth);
  105. }
  106. else
  107. base.Update<InspectableArrayGUIRow>(title, true, 0, layout, depth);
  108. }
  109. /// <inheritdoc/>
  110. protected internal override object GetValue(int seqIndex)
  111. {
  112. SerializableArray array = property.GetArray();
  113. return array.GetProperty(seqIndex);
  114. }
  115. /// <inheritdoc/>
  116. protected internal override void SetValue(int seqIndex, object value)
  117. {
  118. // Setting the value should be done through the property
  119. throw new InvalidOperationException();
  120. }
  121. /// <inheritdoc/>
  122. protected override void OnCreateButtonClicked()
  123. {
  124. property.SetValue(property.CreateArrayInstance(new int[1] { 0 }));
  125. }
  126. /// <inheritdoc/>
  127. protected override void OnResizeButtonClicked()
  128. {
  129. int size = guiSizeField.Value; // TODO - Support multi-rank arrays
  130. Array newArray = property.CreateArrayInstance(new int[] { size });
  131. Array array = property.GetValue<Array>();
  132. int maxSize = MathEx.Min(size, array.Length);
  133. for (int i = 0; i < maxSize; i++)
  134. newArray.SetValue(array.GetValue(i), i);
  135. property.SetValue(newArray);
  136. }
  137. /// <inheritdoc/>
  138. protected override void OnClearButtonClicked()
  139. {
  140. property.SetValue<object>(null);
  141. }
  142. /// <inheritdoc/>
  143. protected internal override void OnDeleteButtonClicked(int index)
  144. {
  145. Array array = property.GetValue<Array>();
  146. int size = MathEx.Max(0, array.Length - 1);
  147. Array newArray = property.CreateArrayInstance(new int[] { size });
  148. int destIdx = 0;
  149. for (int i = 0; i < array.Length; i++)
  150. {
  151. if (i == index)
  152. continue;
  153. newArray.SetValue(array.GetValue(i), destIdx);
  154. destIdx++;
  155. }
  156. property.SetValue(newArray);
  157. }
  158. /// <inheritdoc/>
  159. protected internal override void OnCloneButtonClicked(int index)
  160. {
  161. SerializableArray array = property.GetArray();
  162. int size = array.GetLength() + 1;
  163. Array newArray = property.CreateArrayInstance(new int[] { size });
  164. object clonedEntry = null;
  165. for (int i = 0; i < array.GetLength(); i++)
  166. {
  167. object value = array.GetProperty(i).GetValue<object>();
  168. newArray.SetValue(value, i);
  169. if (i == index)
  170. {
  171. clonedEntry = array.GetProperty(i).GetValueCopy<object>();
  172. }
  173. }
  174. newArray.SetValue(clonedEntry, size - 1);
  175. property.SetValue(newArray);
  176. }
  177. /// <inheritdoc/>
  178. protected internal override void OnMoveUpButtonClicked(int index)
  179. {
  180. Array array = property.GetValue<Array>();
  181. if ((index - 1) >= 0)
  182. {
  183. object previousEntry = array.GetValue(index - 1);
  184. array.SetValue(array.GetValue(index), index - 1);
  185. array.SetValue(previousEntry, index);
  186. }
  187. }
  188. /// <inheritdoc/>
  189. protected internal override void OnMoveDownButtonClicked(int index)
  190. {
  191. Array array = property.GetValue<Array>();
  192. if ((index + 1) < array.Length)
  193. {
  194. object nextEntry = array.GetValue(index + 1);
  195. array.SetValue(array.GetValue(index), index + 1);
  196. array.SetValue(nextEntry, index);
  197. }
  198. }
  199. }
  200. /// <summary>
  201. /// Contains GUI elements for a single entry in the array.
  202. /// </summary>
  203. private class InspectableArrayGUIRow : GUIListFieldRow
  204. {
  205. private InspectableField field;
  206. /// <inheritdoc/>
  207. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  208. {
  209. if (field == null)
  210. {
  211. SerializableProperty property = GetValue<SerializableProperty>();
  212. field = CreateInspectable(seqIndex + ".", 0, depth + 1,
  213. new InspectableFieldLayout(layout), property);
  214. }
  215. return field.GetTitleLayout();
  216. }
  217. /// <inheritdoc/>
  218. protected internal override bool Refresh()
  219. {
  220. if (field.IsModified())
  221. {
  222. field.Refresh(0);
  223. return field.ShouldRebuildOnModify();
  224. }
  225. field.Refresh(0);
  226. return false;
  227. }
  228. }
  229. }
  230. }