InspectableList.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Displays GUI for a serializable property containing a list. List contents are displayed as rows of entries
  9. /// that can be shown, hidden or manipulated.
  10. /// </summary>
  11. public class InspectableList : InspectableField
  12. {
  13. private InspectableListGUI listGUIField;
  14. /// <summary>
  15. /// Creates a new inspectable list 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 list whose contents to display.</param>
  22. public InspectableList(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  23. : base(title, SerializableProperty.FieldType.List, depth, layout, property)
  24. {
  25. }
  26. /// <inheritdoc/>
  27. public override GUILayoutX GetTitleLayout()
  28. {
  29. return listGUIField.GetTitleLayout();
  30. }
  31. /// <inheritdoc/>
  32. public override InspectableState Refresh(int layoutIndex)
  33. {
  34. return listGUIField.Refresh();
  35. }
  36. /// <inheritdoc/>
  37. protected internal override void Initialize(int layoutIndex)
  38. {
  39. GUILayout arrayLayout = layout.AddLayoutY(layoutIndex);
  40. listGUIField = InspectableListGUI.Create(title, property, arrayLayout, depth);
  41. }
  42. /// <summary>
  43. /// Handles creation of GUI elements for a GUI list field that displays a <see cref="SerializableList"/> object.
  44. /// </summary>
  45. private class InspectableListGUI : GUIListFieldBase
  46. {
  47. private IList list;
  48. private int numElements;
  49. private SerializableProperty property;
  50. /// <summary>
  51. /// Constructs a new empty inspectable list GUI.
  52. /// </summary>
  53. /// <param name="title">Label to display on the list GUI title.</param>
  54. /// <param name="property">Serializable property referencing a list.</param>
  55. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  56. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  57. /// nested containers whose backgrounds are overlaping. Also determines background style,
  58. /// depths divisible by two will use an alternate style.</param>
  59. public InspectableListGUI(LocString title, SerializableProperty property, GUILayout layout, int depth)
  60. : base(title, layout, depth)
  61. {
  62. this.property = property;
  63. list = property.GetValue<IList>();
  64. if (list != null)
  65. numElements = list.Count;
  66. }
  67. /// <summary>
  68. /// Creates a new inspectable list GUI object that displays the contents of the provided serializable property.
  69. /// </summary>
  70. /// <param name="title">Label to display on the list GUI title.</param>
  71. /// <param name="property">Serializable property referencing a list.</param>
  72. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  73. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  74. /// nested containers whose backgrounds are overlaping. Also determines background style,
  75. /// depths divisible by two will use an alternate style.</param>
  76. public static InspectableListGUI Create(LocString title, SerializableProperty property, GUILayout layout, int depth)
  77. {
  78. InspectableListGUI listGUI = new InspectableListGUI(title, property, layout, depth);
  79. listGUI.BuildGUI();
  80. return listGUI;
  81. }
  82. /// <inheritdoc/>
  83. public override InspectableState Refresh()
  84. {
  85. // Check if any modifications to the array were made outside the inspector
  86. IList newList = property.GetValue<IList>();
  87. if (list == null && newList != null)
  88. {
  89. list = newList;
  90. numElements = list.Count;
  91. BuildGUI();
  92. }
  93. else if (newList == null && list != null)
  94. {
  95. list = null;
  96. numElements = 0;
  97. BuildGUI();
  98. }
  99. else
  100. {
  101. if (list != null)
  102. {
  103. if (numElements != list.Count)
  104. {
  105. numElements = list.Count;
  106. BuildGUI();
  107. }
  108. }
  109. }
  110. return base.Refresh();
  111. }
  112. /// <inheritdoc/>
  113. protected override GUIListFieldRow CreateRow()
  114. {
  115. return new InspectableListGUIRow();
  116. }
  117. /// <inheritdoc/>
  118. protected override bool IsNull()
  119. {
  120. return list == null;
  121. }
  122. /// <inheritdoc/>
  123. protected override int GetNumRows()
  124. {
  125. if (list != null)
  126. return list.Count;
  127. return 0;
  128. }
  129. /// <inheritdoc/>
  130. protected internal override object GetValue(int seqIndex)
  131. {
  132. SerializableList list = property.GetList();
  133. return list.GetProperty(seqIndex);
  134. }
  135. /// <inheritdoc/>
  136. protected internal override void SetValue(int seqIndex, object value)
  137. {
  138. // Setting the value should be done through the property
  139. throw new InvalidOperationException();
  140. }
  141. /// <inheritdoc/>
  142. protected override void CreateList()
  143. {
  144. list = property.CreateListInstance(0);
  145. property.SetValue(list);
  146. numElements = 0;
  147. }
  148. /// <inheritdoc/>
  149. protected override void ResizeList()
  150. {
  151. int size = guiSizeField.Value;
  152. IList newList = property.CreateListInstance(size);
  153. int maxSize = MathEx.Min(size, list.Count);
  154. for (int i = 0; i < maxSize; i++)
  155. newList[i] = list[i];
  156. property.SetValue(newList);
  157. list = newList;
  158. numElements = list.Count;
  159. }
  160. /// <inheritdoc/>
  161. protected override void ClearList()
  162. {
  163. property.SetValue<object>(null);
  164. list = null;
  165. numElements = 0;
  166. }
  167. /// <inheritdoc/>
  168. protected internal override void DeleteElement(int index)
  169. {
  170. if (index >= 0 && index < list.Count)
  171. list.RemoveAt(index);
  172. numElements = list.Count;
  173. }
  174. /// <inheritdoc/>
  175. protected internal override void CloneElement(int index)
  176. {
  177. SerializableList serializableList = property.GetList();
  178. if (index >= 0 && index < list.Count)
  179. list.Add(SerializableUtility.Clone(serializableList.GetProperty(index).GetValue<object>()));
  180. numElements = list.Count;
  181. }
  182. /// <inheritdoc/>
  183. protected internal override void MoveUpElement(int index)
  184. {
  185. if ((index - 1) >= 0)
  186. {
  187. object previousEntry = list[index - 1];
  188. list[index - 1] = list[index];
  189. list[index] = previousEntry;
  190. }
  191. }
  192. /// <inheritdoc/>
  193. protected internal override void MoveDownElement(int index)
  194. {
  195. if ((index + 1) < list.Count)
  196. {
  197. object nextEntry = list[index + 1];
  198. list[index + 1] = list[index];
  199. list[index] = nextEntry;
  200. }
  201. }
  202. }
  203. /// <summary>
  204. /// Contains GUI elements for a single entry in the list.
  205. /// </summary>
  206. private class InspectableListGUIRow : GUIListFieldRow
  207. {
  208. private InspectableField field;
  209. /// <inheritdoc/>
  210. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  211. {
  212. SerializableProperty property = GetValue<SerializableProperty>();
  213. field = CreateInspectable(SeqIndex + ".", 0, Depth + 1,
  214. new InspectableFieldLayout(layout), property);
  215. return field.GetTitleLayout();
  216. }
  217. /// <inheritdoc/>
  218. protected internal override InspectableState Refresh()
  219. {
  220. field.Property = GetValue<SerializableProperty>();
  221. return field.Refresh(0);
  222. }
  223. }
  224. }
  225. }