InspectableArray.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /** @addtogroup Inspector
  8. * @{
  9. */
  10. /// <summary>
  11. /// Displays GUI for a serializable property containing an array. Array contents are displayed as rows of entries
  12. /// that can be shown, hidden or manipulated.
  13. /// </summary>
  14. public class InspectableArray : InspectableField
  15. {
  16. private InspectableArrayGUI arrayGUIField;
  17. private InspectableFieldStyleInfo style;
  18. /// <summary>
  19. /// Style applied to the elements of the array and the array itself.
  20. /// </summary>
  21. internal InspectableFieldStyleInfo Style
  22. {
  23. get { return style; }
  24. }
  25. /// <summary>
  26. /// Creates a new inspectable array GUI for the specified property.
  27. /// </summary>
  28. /// <param name="parent">Parent Inspector this field belongs to.</param>
  29. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  30. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  31. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  32. /// contain other fields, in which case you should increase this value by one.</param>
  33. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  34. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  35. /// <param name="style">Information that can be used for customizing field rendering and behaviour.</param>
  36. public InspectableArray(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
  37. SerializableProperty property, InspectableFieldStyleInfo style)
  38. : base(parent, title, path, SerializableProperty.FieldType.Array, depth, layout, property)
  39. {
  40. this.style = style;
  41. }
  42. /// <inheritdoc/>
  43. public override GUILayoutX GetTitleLayout()
  44. {
  45. return arrayGUIField.GetTitleLayout();
  46. }
  47. /// <inheritdoc/>
  48. public override InspectableState Refresh(int layoutIndex)
  49. {
  50. return arrayGUIField.Refresh();
  51. }
  52. /// <inheritdoc/>
  53. protected internal override void Initialize(int layoutIndex)
  54. {
  55. GUILayout arrayLayout = layout.AddLayoutY(layoutIndex);
  56. arrayGUIField = InspectableArrayGUI.Create(parent, title, path, property, arrayLayout, depth, style);
  57. arrayGUIField.IsExpanded = parent.Persistent.GetBool(path + "_Expanded");
  58. arrayGUIField.OnExpand += x => parent.Persistent.SetBool(path + "_Expanded", x);
  59. }
  60. /// <summary>
  61. /// Handles creation of GUI elements for a GUI list field that displays a <see cref="SerializableArray"/> object.
  62. /// </summary>
  63. private class InspectableArrayGUI : GUIListFieldBase
  64. {
  65. private Array array;
  66. private int numElements;
  67. private Inspector parent;
  68. private SerializableProperty property;
  69. private string path;
  70. private InspectableFieldStyleInfo style;
  71. /// <summary>
  72. /// Returns the parent inspector the array GUI belongs to.
  73. /// </summary>
  74. public Inspector Inspector
  75. {
  76. get { return parent; }
  77. }
  78. /// <summary>
  79. /// Returns a property path to the array field (name of the array field and all parent object fields).
  80. /// </summary>
  81. public string Path
  82. {
  83. get { return path; }
  84. }
  85. /// <summary>
  86. /// Style applied to the elements of the array and the array itself.
  87. /// </summary>
  88. public InspectableFieldStyleInfo Style
  89. {
  90. get { return style; }
  91. }
  92. /// <summary>
  93. /// Constructs a new inspectable array GUI.
  94. /// </summary>
  95. /// <param name="parent">Parent Inspector this field belongs to.</param>
  96. /// <param name="title">Label to display on the list GUI title.</param>
  97. /// <param name="path">Full path to this property (includes name of this property and all parent properties).
  98. /// </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. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  102. /// nested containers whose backgrounds are overlaping. Also determines background style,
  103. /// depths divisible by two will use an alternate style.</param>
  104. /// <param name="style">Information that can be used for customizing field rendering and behaviour.</param>
  105. public InspectableArrayGUI(Inspector parent, LocString title, string path, SerializableProperty property,
  106. GUILayout layout, int depth, InspectableFieldStyleInfo style)
  107. : base(title, layout, depth)
  108. {
  109. this.property = property;
  110. this.parent = parent;
  111. this.path = path;
  112. this.style = style;
  113. array = property.GetValue<Array>();
  114. if (array != null)
  115. numElements = array.Length;
  116. }
  117. /// <summary>
  118. /// Creates a new inspectable array GUI object that displays the contents of the provided serializable property.
  119. /// </summary>
  120. /// <param name="parent">Parent Inspector this field belongs to.</param>
  121. /// <param name="title">Label to display on the list GUI title.</param>
  122. /// <param name="path">Full path to this property (includes name of this property and all parent properties).
  123. /// </param>
  124. /// <param name="property">Serializable property referencing a single-dimensional array.</param>
  125. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  126. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  127. /// nested containers whose backgrounds are overlaping. Also determines background style,
  128. /// depths divisible by two will use an alternate style.</param>
  129. /// <param name="style">Information that can be used for customizing field rendering and behaviour.</param>
  130. public static InspectableArrayGUI Create(Inspector parent, LocString title, string path,
  131. SerializableProperty property, GUILayout layout, int depth, InspectableFieldStyleInfo style)
  132. {
  133. InspectableArrayGUI guiArray = new InspectableArrayGUI(parent, title, path, property, layout, depth, style);
  134. guiArray.BuildGUI();
  135. return guiArray;
  136. }
  137. /// <inheritdoc/>
  138. public override InspectableState Refresh()
  139. {
  140. // Check if any modifications to the array were made outside the inspector
  141. Array newArray = property.GetValue<Array>();
  142. if (array == null && newArray != null)
  143. {
  144. array = newArray;
  145. numElements = array.Length;
  146. BuildGUI();
  147. }
  148. else if (newArray == null && array != null)
  149. {
  150. array = null;
  151. numElements = 0;
  152. BuildGUI();
  153. }
  154. else
  155. {
  156. if (array != null)
  157. {
  158. if (numElements != array.Length)
  159. {
  160. numElements = array.Length;
  161. BuildGUI();
  162. }
  163. }
  164. }
  165. return base.Refresh();
  166. }
  167. /// <inheritdoc/>
  168. protected override GUIListFieldRow CreateRow()
  169. {
  170. return new InspectableArrayGUIRow();
  171. }
  172. /// <inheritdoc/>
  173. protected override bool IsNull()
  174. {
  175. return array == null;
  176. }
  177. /// <inheritdoc/>
  178. protected override int GetNumRows()
  179. {
  180. if (array != null)
  181. return array.Length;
  182. return 0;
  183. }
  184. /// <inheritdoc/>
  185. protected internal override object GetValue(int seqIndex)
  186. {
  187. SerializableArray serzArray = property.GetArray();
  188. // Create a property wrapper for native arrays so we get notified when the array values change
  189. if (style.StyleFlags.HasFlag(InspectableFieldStyleFlags.NativeWrapper))
  190. {
  191. SerializableProperty.Getter getter = () =>
  192. {
  193. Array array = property.GetValue<Array>();
  194. if (array != null)
  195. return array.GetValue(seqIndex);
  196. else
  197. return null;
  198. };
  199. SerializableProperty.Setter setter = (object value) =>
  200. {
  201. Array array = property.GetValue<Array>();
  202. if (array != null)
  203. {
  204. array.SetValue(value, seqIndex);
  205. property.SetValue(array);
  206. }
  207. };
  208. return new SerializableProperty(serzArray.ElementPropertyType, serzArray.ElementType, getter, setter);
  209. }
  210. else
  211. return serzArray.GetProperty(seqIndex);
  212. }
  213. /// <inheritdoc/>
  214. protected internal override void SetValue(int seqIndex, object value)
  215. {
  216. // Setting the value should be done through the property
  217. throw new InvalidOperationException();
  218. }
  219. /// <inheritdoc/>
  220. protected override void CreateList()
  221. {
  222. array = property.CreateArrayInstance(new int[1] { 0 });
  223. property.SetValue(array);
  224. numElements = 0;
  225. }
  226. /// <inheritdoc/>
  227. protected override void ResizeList()
  228. {
  229. int size = guiSizeField.Value; // TODO - Support multi-rank arrays
  230. Array newArray = property.CreateArrayInstance(new int[] { size });
  231. int maxSize = MathEx.Min(size, array.Length);
  232. for (int i = 0; i < maxSize; i++)
  233. newArray.SetValue(array.GetValue(i), i);
  234. property.SetValue(newArray);
  235. array = newArray;
  236. numElements = size;
  237. }
  238. /// <inheritdoc/>
  239. protected override void ClearList()
  240. {
  241. property.SetValue<object>(null);
  242. array = null;
  243. numElements = 0;
  244. }
  245. /// <inheritdoc/>
  246. protected internal override void DeleteElement(int index)
  247. {
  248. int size = MathEx.Max(0, array.Length - 1);
  249. Array newArray = property.CreateArrayInstance(new int[] { size });
  250. int destIdx = 0;
  251. for (int i = 0; i < array.Length; i++)
  252. {
  253. if (i == index)
  254. continue;
  255. newArray.SetValue(array.GetValue(i), destIdx);
  256. destIdx++;
  257. }
  258. property.SetValue(newArray);
  259. array = newArray;
  260. numElements = array.Length;
  261. }
  262. /// <inheritdoc/>
  263. protected internal override void CloneElement(int index)
  264. {
  265. SerializableArray array = property.GetArray();
  266. int size = array.GetLength() + 1;
  267. Array newArray = property.CreateArrayInstance(new int[] { size });
  268. object clonedEntry = null;
  269. for (int i = 0; i < array.GetLength(); i++)
  270. {
  271. object value = array.GetProperty(i).GetValue<object>();
  272. newArray.SetValue(value, i);
  273. if (i == index)
  274. {
  275. clonedEntry = SerializableUtility.Clone(array.GetProperty(i).GetValue<object>());
  276. }
  277. }
  278. newArray.SetValue(clonedEntry, size - 1);
  279. property.SetValue(newArray);
  280. this.array = newArray;
  281. numElements = newArray.Length;
  282. }
  283. /// <inheritdoc/>
  284. protected internal override void MoveUpElement(int index)
  285. {
  286. if ((index - 1) >= 0)
  287. {
  288. object previousEntry = array.GetValue(index - 1);
  289. array.SetValue(array.GetValue(index), index - 1);
  290. array.SetValue(previousEntry, index);
  291. // Natively wrapped arrays are passed by copy
  292. if(style.StyleFlags.HasFlag(InspectableFieldStyleFlags.NativeWrapper))
  293. property.SetValue(array);
  294. }
  295. }
  296. /// <inheritdoc/>
  297. protected internal override void MoveDownElement(int index)
  298. {
  299. if ((index + 1) < array.Length)
  300. {
  301. object nextEntry = array.GetValue(index + 1);
  302. array.SetValue(array.GetValue(index), index + 1);
  303. array.SetValue(nextEntry, index);
  304. // Natively wrapped arrays are passed by copy
  305. if(style.StyleFlags.HasFlag(InspectableFieldStyleFlags.NativeWrapper))
  306. property.SetValue(array);
  307. }
  308. }
  309. }
  310. /// <summary>
  311. /// Contains GUI elements for a single entry in the array.
  312. /// </summary>
  313. private class InspectableArrayGUIRow : GUIListFieldRow
  314. {
  315. private InspectableField field;
  316. /// <inheritdoc/>
  317. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  318. {
  319. InspectableArrayGUI arrayParent = (InspectableArrayGUI)parent;
  320. SerializableProperty property = GetValue<SerializableProperty>();
  321. InspectableFieldStyleInfo styleInfo = arrayParent.Style.Clone();
  322. styleInfo.StyleFlags &= ~InspectableFieldStyleFlags.NativeWrapper;
  323. string entryPath = arrayParent.Path + "[" + SeqIndex + "]";
  324. field = CreateInspectable(arrayParent.Inspector, SeqIndex + ".", entryPath, 0, Depth + 1,
  325. new InspectableFieldLayout(layout), property, styleInfo);
  326. return field.GetTitleLayout();
  327. }
  328. /// <inheritdoc/>
  329. protected internal override InspectableState Refresh()
  330. {
  331. field.Property = GetValue<SerializableProperty>();
  332. return field.Refresh(0);
  333. }
  334. }
  335. }
  336. /** @} */
  337. }