InspectableArray.cs 10 KB

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