InspectableArray.cs 9.8 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;
  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 = IsModified();
  48. anythingModified |= arrayGUIField.Refresh();
  49. return anythingModified;
  50. }
  51. /// <inheritdoc/>
  52. public override bool GetRebuildOnModify()
  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);
  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. /// Creates a new inspectable GUI array.
  84. /// </summary>
  85. /// <param name="title">Label to display on the list GUI title.</param>
  86. /// <param name="property">Serializable property referencing a single-dimensional array.</param>
  87. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  88. /// <returns>Newly created inspectable GUI array object.</returns>
  89. public static InspectableArrayGUI Create(LocString title, SerializableProperty property, GUILayout layout)
  90. {
  91. InspectableArrayGUI newArrayField = new InspectableArrayGUI();
  92. object propertyValue = property.GetValue<object>();
  93. if (propertyValue != null)
  94. {
  95. SerializableArray array = property.GetArray();
  96. newArrayField.Construct<InspectableArrayGUIRow>(title, false, array.GetLength(), layout);
  97. }
  98. else
  99. newArrayField.Construct<InspectableArrayGUIRow>(title, true, 0, layout);
  100. newArrayField.property = property;
  101. return newArrayField;
  102. }
  103. /// <inheritdoc/>
  104. protected internal override object GetValue(int seqIndex)
  105. {
  106. SerializableArray array = property.GetArray();
  107. return array.GetProperty(seqIndex);
  108. }
  109. /// <inheritdoc/>
  110. protected internal override void SetValue(int seqIndex, object value)
  111. {
  112. // Setting the value should be done through the property
  113. throw new InvalidOperationException();
  114. }
  115. /// <inheritdoc/>
  116. protected override void OnCreateButtonClicked()
  117. {
  118. property.SetValue(property.CreateArrayInstance(new int[1] { 0 }));
  119. }
  120. /// <inheritdoc/>
  121. protected override void OnResizeButtonClicked()
  122. {
  123. int size = guiSizeField.Value; // TODO - Support multi-rank arrays
  124. Array newArray = property.CreateArrayInstance(new int[] { size });
  125. Array array = property.GetValue<Array>();
  126. int maxSize = MathEx.Min(size, array.Length);
  127. for (int i = 0; i < maxSize; i++)
  128. newArray.SetValue(array.GetValue(i), i);
  129. property.SetValue(newArray);
  130. }
  131. /// <inheritdoc/>
  132. protected override void OnClearButtonClicked()
  133. {
  134. property.SetValue<object>(null);
  135. }
  136. /// <inheritdoc/>
  137. protected internal override void OnDeleteButtonClicked(int index)
  138. {
  139. Array array = property.GetValue<Array>();
  140. int size = MathEx.Max(0, array.Length - 1);
  141. Array newArray = property.CreateArrayInstance(new int[] { size });
  142. int destIdx = 0;
  143. for (int i = 0; i < array.Length; i++)
  144. {
  145. if (i == index)
  146. continue;
  147. newArray.SetValue(array.GetValue(i), destIdx);
  148. destIdx++;
  149. }
  150. property.SetValue(newArray);
  151. }
  152. /// <inheritdoc/>
  153. protected internal override void OnCloneButtonClicked(int index)
  154. {
  155. SerializableArray array = property.GetArray();
  156. int size = array.GetLength() + 1;
  157. Array newArray = property.CreateArrayInstance(new int[] { size });
  158. object clonedEntry = null;
  159. for (int i = 0; i < array.GetLength(); i++)
  160. {
  161. object value = array.GetProperty(i).GetValue<object>();
  162. newArray.SetValue(value, i);
  163. if (i == index)
  164. {
  165. clonedEntry = array.GetProperty(i).GetValueCopy<object>();
  166. }
  167. }
  168. newArray.SetValue(clonedEntry, size - 1);
  169. property.SetValue(newArray);
  170. }
  171. /// <inheritdoc/>
  172. protected internal override void OnMoveUpButtonClicked(int index)
  173. {
  174. Array array = property.GetValue<Array>();
  175. if ((index - 1) >= 0)
  176. {
  177. object previousEntry = array.GetValue(index - 1);
  178. array.SetValue(array.GetValue(index), index - 1);
  179. array.SetValue(previousEntry, index);
  180. }
  181. }
  182. /// <inheritdoc/>
  183. protected internal override void OnMoveDownButtonClicked(int index)
  184. {
  185. Array array = property.GetValue<Array>();
  186. if ((index + 1) < array.Length)
  187. {
  188. object nextEntry = array.GetValue(index + 1);
  189. array.SetValue(array.GetValue(index), index + 1);
  190. array.SetValue(nextEntry, index);
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// Contains GUI elements for a single entry in the array.
  196. /// </summary>
  197. private class InspectableArrayGUIRow : GUIListFieldRow
  198. {
  199. private InspectableField field;
  200. /// <inheritdoc/>
  201. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  202. {
  203. SerializableProperty property = GetValue<SerializableProperty>();
  204. field = CreateInspectable(seqIndex + ".", 0, 0,
  205. new InspectableFieldLayout(layout), property);
  206. return field.GetTitleLayout();
  207. }
  208. /// <inheritdoc/>
  209. protected internal override bool Refresh(out bool rebuildGUI)
  210. {
  211. if (field.IsModified())
  212. {
  213. // If rebuild GUI is set to true, we will just rebuild the entire inspectable field, so no need to
  214. // call Update on the existing one.
  215. if (!field.GetRebuildOnModify())
  216. {
  217. rebuildGUI = false;
  218. return field.Refresh(0);
  219. }
  220. rebuildGUI = true;
  221. return true;
  222. }
  223. rebuildGUI = false;
  224. return field.Refresh(0);
  225. }
  226. }
  227. }
  228. }