InspectableArray.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using bs;
  5. namespace bs.Editor
  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="context">Context shared by all inspectable fields created by the same parent.</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(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  37. SerializableProperty property, InspectableFieldStyleInfo style)
  38. : base(context, 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(context, title, path, property, arrayLayout, depth, style);
  57. arrayGUIField.IsExpanded = context.Persistent.GetBool(path + "_Expanded");
  58. arrayGUIField.OnExpand += x => context.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 InspectableContext context;
  68. private SerializableProperty property;
  69. private string path;
  70. private InspectableFieldStyleInfo style;
  71. /// <summary>
  72. /// Context shared by all inspectable fields created by the same parent.
  73. /// </summary>
  74. public InspectableContext Context
  75. {
  76. get { return context; }
  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="context">Context shared by all inspectable fields created by the same parent.</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(InspectableContext context, 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.context = context;
  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="context">Context shared by all inspectable fields created by the same parent.</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(InspectableContext context, LocString title, string path,
  131. SerializableProperty property, GUILayout layout, int depth, InspectableFieldStyleInfo style)
  132. {
  133. InspectableArrayGUI guiArray = new InspectableArrayGUI(context, 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. // Native arrays cannot be set to null, so just clear their size to zero instead
  242. if (style.StyleFlags.HasFlag(InspectableFieldStyleFlags.NativeWrapper))
  243. CreateList();
  244. else
  245. {
  246. property.SetValue<object>(null);
  247. array = null;
  248. numElements = 0;
  249. }
  250. }
  251. /// <inheritdoc/>
  252. protected internal override void DeleteElement(int index)
  253. {
  254. int size = MathEx.Max(0, array.Length - 1);
  255. Array newArray = property.CreateArrayInstance(new int[] { size });
  256. int destIdx = 0;
  257. for (int i = 0; i < array.Length; i++)
  258. {
  259. if (i == index)
  260. continue;
  261. newArray.SetValue(array.GetValue(i), destIdx);
  262. destIdx++;
  263. }
  264. property.SetValue(newArray);
  265. array = newArray;
  266. numElements = array.Length;
  267. }
  268. /// <inheritdoc/>
  269. protected internal override void CloneElement(int index)
  270. {
  271. SerializableArray array = property.GetArray();
  272. int size = array.GetLength() + 1;
  273. Array newArray = property.CreateArrayInstance(new int[] { size });
  274. object clonedEntry = null;
  275. for (int i = 0; i < array.GetLength(); i++)
  276. {
  277. object value = array.GetProperty(i).GetValue<object>();
  278. newArray.SetValue(value, i);
  279. if (i == index)
  280. {
  281. clonedEntry = SerializableUtility.Clone(array.GetProperty(i).GetValue<object>());
  282. }
  283. }
  284. newArray.SetValue(clonedEntry, size - 1);
  285. property.SetValue(newArray);
  286. this.array = newArray;
  287. numElements = newArray.Length;
  288. }
  289. /// <inheritdoc/>
  290. protected internal override void MoveUpElement(int index)
  291. {
  292. if ((index - 1) >= 0)
  293. {
  294. object previousEntry = array.GetValue(index - 1);
  295. array.SetValue(array.GetValue(index), index - 1);
  296. array.SetValue(previousEntry, index);
  297. // Natively wrapped arrays are passed by copy
  298. if(style.StyleFlags.HasFlag(InspectableFieldStyleFlags.NativeWrapper))
  299. property.SetValue(array);
  300. }
  301. }
  302. /// <inheritdoc/>
  303. protected internal override void MoveDownElement(int index)
  304. {
  305. if ((index + 1) < array.Length)
  306. {
  307. object nextEntry = array.GetValue(index + 1);
  308. array.SetValue(array.GetValue(index), index + 1);
  309. array.SetValue(nextEntry, index);
  310. // Natively wrapped arrays are passed by copy
  311. if(style.StyleFlags.HasFlag(InspectableFieldStyleFlags.NativeWrapper))
  312. property.SetValue(array);
  313. }
  314. }
  315. }
  316. /// <summary>
  317. /// Contains GUI elements for a single entry in the array.
  318. /// </summary>
  319. private class InspectableArrayGUIRow : GUIListFieldRow
  320. {
  321. private InspectableField field;
  322. /// <inheritdoc/>
  323. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  324. {
  325. InspectableArrayGUI arrayParent = (InspectableArrayGUI)parent;
  326. SerializableProperty property = GetValue<SerializableProperty>();
  327. InspectableFieldStyleInfo styleInfo = arrayParent.Style.Clone();
  328. styleInfo.StyleFlags &= ~InspectableFieldStyleFlags.NativeWrapper;
  329. string entryPath = arrayParent.Path + "[" + SeqIndex + "]";
  330. field = CreateField(arrayParent.Context, SeqIndex + ".", entryPath, 0, Depth + 1,
  331. new InspectableFieldLayout(layout), property, styleInfo);
  332. return field.GetTitleLayout();
  333. }
  334. /// <inheritdoc/>
  335. protected internal override InspectableState Refresh()
  336. {
  337. field.Property = GetValue<SerializableProperty>();
  338. return field.Refresh(0);
  339. }
  340. }
  341. }
  342. /** @} */
  343. }