InspectableList.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 object propertyValue; // TODO - This will unnecessarily hold references to the object
  14. private int numArrayElements;
  15. private InspectableListGUI listGUIField;
  16. /// <summary>
  17. /// Creates a new inspectable list GUI for the specified property.
  18. /// </summary>
  19. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  20. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  21. /// contain other fields, in which case you should increase this value by one.</param>
  22. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  23. /// <param name="property">Serializable property referencing the list whose contents to display.</param>
  24. public InspectableList(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  25. : base(title, depth, layout, property)
  26. {
  27. }
  28. /// <inheritdoc/>
  29. public override GUILayoutX GetTitleLayout()
  30. {
  31. return listGUIField.GetTitleLayout();
  32. }
  33. /// <inheritdoc/>
  34. public override bool IsModified()
  35. {
  36. object newPropertyValue = property.GetValue<object>();
  37. if (propertyValue == null)
  38. return newPropertyValue != null;
  39. if (newPropertyValue == null)
  40. return propertyValue != null;
  41. SerializableList list = property.GetList();
  42. if (list.GetLength() != numArrayElements)
  43. return true;
  44. return base.IsModified();
  45. }
  46. /// <inheritdoc/>
  47. public override void Refresh(int layoutIndex)
  48. {
  49. if (IsModified())
  50. Update(layoutIndex);
  51. listGUIField.Refresh();
  52. }
  53. /// <inheritdoc/>
  54. public override bool ShouldRebuildOnModify()
  55. {
  56. return true;
  57. }
  58. /// <inheritdoc/>
  59. protected internal override void BuildGUI(int layoutIndex)
  60. {
  61. GUILayout arrayLayout = layout.AddLayoutY(layoutIndex);
  62. listGUIField = InspectableListGUI.Create(title, property, arrayLayout, depth);
  63. }
  64. /// <inheritdoc/>
  65. protected internal override void Update(int layoutIndex)
  66. {
  67. propertyValue = property.GetValue<object>();
  68. if (propertyValue != null)
  69. {
  70. SerializableList list = property.GetList();
  71. numArrayElements = list.GetLength();
  72. }
  73. else
  74. numArrayElements = 0;
  75. layout.DestroyElements();
  76. BuildGUI(layoutIndex);
  77. }
  78. /// <summary>
  79. /// Handles creation of GUI elements for a GUI list field that displays a <see cref="SerializableList"/> object.
  80. /// </summary>
  81. private class InspectableListGUI : GUIListFieldBase
  82. {
  83. private SerializableProperty property;
  84. /// <summary>
  85. /// Constructs a new empty inspectable list GUI.
  86. /// </summary>
  87. /// <param name="title">Label to display on the list GUI title.</param>
  88. /// <param name="property">Serializable property referencing a list.</param>
  89. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  90. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  91. /// nested containers whose backgrounds are overlaping. Also determines background style,
  92. /// depths divisible by two will use an alternate style.</param>
  93. public InspectableListGUI(LocString title, SerializableProperty property, GUILayout layout, int depth)
  94. : base(title, layout, depth)
  95. {
  96. this.property = property;
  97. }
  98. /// <summary>
  99. /// Creates a new inspectable list GUI object that displays the contents of the provided serializable property.
  100. /// </summary>
  101. /// <param name="title">Label to display on the list GUI title.</param>
  102. /// <param name="property">Serializable property referencing a list.</param>
  103. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  104. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  105. /// nested containers whose backgrounds are overlaping. Also determines background style,
  106. /// depths divisible by two will use an alternate style.</param>
  107. public static InspectableListGUI Create(LocString title, SerializableProperty property, GUILayout layout, int depth)
  108. {
  109. InspectableListGUI listGUI = new InspectableListGUI(title, property, layout, depth);
  110. listGUI.BuildGUI();
  111. return listGUI;
  112. }
  113. /// <inheritdoc/>
  114. protected override GUIListFieldRow CreateRow()
  115. {
  116. return new InspectableListGUIRow();
  117. }
  118. /// <inheritdoc/>
  119. protected override bool IsNull()
  120. {
  121. IList list = property.GetValue<IList>();
  122. return list == null;
  123. }
  124. /// <inheritdoc/>
  125. protected override int GetNumRows()
  126. {
  127. IList list = property.GetValue<IList>();
  128. if (list != null)
  129. return list.Count;
  130. return 0;
  131. }
  132. /// <inheritdoc/>
  133. protected internal override object GetValue(int seqIndex)
  134. {
  135. SerializableList list = property.GetList();
  136. return list.GetProperty(seqIndex);
  137. }
  138. /// <inheritdoc/>
  139. protected internal override void SetValue(int seqIndex, object value)
  140. {
  141. // Setting the value should be done through the property
  142. throw new InvalidOperationException();
  143. }
  144. /// <inheritdoc/>
  145. protected override void CreateList()
  146. {
  147. property.SetValue(property.CreateListInstance(0));
  148. }
  149. /// <inheritdoc/>
  150. protected override void ResizeList()
  151. {
  152. int size = guiSizeField.Value;
  153. IList newList = property.CreateListInstance(size);
  154. IList list = property.GetValue<IList>();
  155. int maxSize = MathEx.Min(size, list.Count);
  156. for (int i = 0; i < maxSize; i++)
  157. newList[i] = list[i];
  158. property.SetValue(newList);
  159. }
  160. /// <inheritdoc/>
  161. protected override void ClearList()
  162. {
  163. property.SetValue<object>(null);
  164. }
  165. /// <inheritdoc/>
  166. protected internal override void DeleteElement(int index)
  167. {
  168. IList list = property.GetValue<IList>();
  169. if (index >= 0 && index < list.Count)
  170. list.RemoveAt(index);
  171. }
  172. /// <inheritdoc/>
  173. protected internal override void CloneElement(int index)
  174. {
  175. SerializableList serializableList = property.GetList();
  176. IList list = property.GetValue<IList>();
  177. if (index >= 0 && index < list.Count)
  178. list.Add(SerializableUtility.Clone(serializableList.GetProperty(index).GetValue<object>()));
  179. }
  180. /// <inheritdoc/>
  181. protected internal override void MoveUpElement(int index)
  182. {
  183. IList list = property.GetValue<IList>();
  184. if ((index - 1) >= 0)
  185. {
  186. object previousEntry = list[index - 1];
  187. list[index - 1] = list[index];
  188. list[index] = previousEntry;
  189. }
  190. }
  191. /// <inheritdoc/>
  192. protected internal override void MoveDownElement(int index)
  193. {
  194. IList list = property.GetValue<IList>();
  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. if (field == null)
  213. {
  214. SerializableProperty property = GetValue<SerializableProperty>();
  215. field = CreateInspectable(seqIndex + ".", 0, depth + 1,
  216. new InspectableFieldLayout(layout), property);
  217. }
  218. return field.GetTitleLayout();
  219. }
  220. /// <inheritdoc/>
  221. protected internal override bool Refresh()
  222. {
  223. if (field.IsModified())
  224. {
  225. field.Refresh(0);
  226. return field.ShouldRebuildOnModify();
  227. }
  228. field.Refresh(0);
  229. return false;
  230. }
  231. }
  232. }
  233. }