InspectableList.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 = new InspectableListGUI();
  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 array 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 bool Refresh(int layoutIndex)
  48. {
  49. bool anythingModified = false;
  50. if (IsModified())
  51. {
  52. Update(layoutIndex);
  53. anythingModified = true;
  54. }
  55. anythingModified |= listGUIField.Refresh();
  56. return anythingModified;
  57. }
  58. /// <inheritdoc/>
  59. public override bool ShouldRebuildOnModify()
  60. {
  61. return true;
  62. }
  63. /// <inheritdoc/>
  64. protected internal override void BuildGUI(int layoutIndex)
  65. {
  66. GUILayout arrayLayout = layout.AddLayoutY(layoutIndex);
  67. listGUIField.Update(title, property, arrayLayout, depth);
  68. }
  69. /// <inheritdoc/>
  70. protected internal override void Update(int layoutIndex)
  71. {
  72. propertyValue = property.GetValue<object>();
  73. if (propertyValue != null)
  74. {
  75. SerializableList list = property.GetList();
  76. numArrayElements = list.GetLength();
  77. }
  78. else
  79. numArrayElements = 0;
  80. layout.DestroyElements();
  81. BuildGUI(layoutIndex);
  82. }
  83. /// <summary>
  84. /// Handles creation of GUI elements for a GUI list field that displays a <see cref="SerializableList"/> object.
  85. /// </summary>
  86. private class InspectableListGUI : GUIListFieldBase
  87. {
  88. private SerializableProperty property;
  89. /// <summary>
  90. /// Constructs a new empty inspectable list GUI.
  91. /// </summary>
  92. public InspectableListGUI()
  93. { }
  94. /// <summary>
  95. /// Updates the contents of the inspectable GUI list. Must be called at least once in order for the contents
  96. /// to be populated.
  97. /// </summary>
  98. /// <param name="title">Label to display on the list GUI title.</param>
  99. /// <param name="property">Serializable property referencing a list.</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. public void Update(LocString title, SerializableProperty property, GUILayout layout, int depth)
  105. {
  106. this.property = property;
  107. object propertyValue = property.GetValue<object>();
  108. if (propertyValue != null)
  109. {
  110. SerializableList list = property.GetList();
  111. base.Update<InspectableListGUIRow>(title, false, list.GetLength(), layout, depth);
  112. }
  113. else
  114. base.Update<InspectableListGUIRow>(title, true, 0, layout, depth);
  115. }
  116. /// <inheritdoc/>
  117. protected internal override object GetValue(int seqIndex)
  118. {
  119. SerializableList array = property.GetList();
  120. return array.GetProperty(seqIndex);
  121. }
  122. /// <inheritdoc/>
  123. protected internal override void SetValue(int seqIndex, object value)
  124. {
  125. // Setting the value should be done through the property
  126. throw new InvalidOperationException();
  127. }
  128. /// <inheritdoc/>
  129. protected override void OnCreateButtonClicked()
  130. {
  131. property.SetValue(property.CreateListInstance(0));
  132. }
  133. /// <inheritdoc/>
  134. protected override void OnResizeButtonClicked()
  135. {
  136. int size = guiSizeField.Value;
  137. IList newList = property.CreateListInstance(size);
  138. IList list = property.GetValue<IList>();
  139. int maxSize = MathEx.Min(size, list.Count);
  140. for (int i = 0; i < maxSize; i++)
  141. newList[i] = list[i];
  142. property.SetValue(newList);
  143. }
  144. /// <inheritdoc/>
  145. protected override void OnClearButtonClicked()
  146. {
  147. property.SetValue<object>(null);
  148. }
  149. /// <inheritdoc/>
  150. protected internal override void OnDeleteButtonClicked(int index)
  151. {
  152. IList list = property.GetValue<IList>();
  153. if (index >= 0 && index < list.Count)
  154. list.RemoveAt(index);
  155. }
  156. /// <inheritdoc/>
  157. protected internal override void OnCloneButtonClicked(int index)
  158. {
  159. SerializableList serializableList = property.GetList();
  160. IList list = property.GetValue<IList>();
  161. if (index >= 0 && index < list.Count)
  162. list.Add(serializableList.GetProperty(index).GetValueCopy<object>());
  163. }
  164. /// <inheritdoc/>
  165. protected internal override void OnMoveUpButtonClicked(int index)
  166. {
  167. IList list = property.GetValue<IList>();
  168. if ((index - 1) >= 0)
  169. {
  170. object previousEntry = list[index - 1];
  171. list[index - 1] = list[index];
  172. list[index] = previousEntry;
  173. }
  174. }
  175. /// <inheritdoc/>
  176. protected internal override void OnMoveDownButtonClicked(int index)
  177. {
  178. IList list = property.GetValue<IList>();
  179. if ((index + 1) < list.Count)
  180. {
  181. object nextEntry = list[index + 1];
  182. list[index + 1] = list[index];
  183. list[index] = nextEntry;
  184. }
  185. }
  186. }
  187. /// <summary>
  188. /// Contains GUI elements for a single entry in the array.
  189. /// </summary>
  190. private class InspectableListGUIRow : GUIListFieldRow
  191. {
  192. private InspectableField field;
  193. /// <inheritdoc/>
  194. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  195. {
  196. if (field == null)
  197. {
  198. SerializableProperty property = GetValue<SerializableProperty>();
  199. field = CreateInspectable(seqIndex + ".", 0, depth + 1,
  200. new InspectableFieldLayout(layout), property);
  201. }
  202. return field.GetTitleLayout();
  203. }
  204. /// <inheritdoc/>
  205. protected internal override bool Refresh(out bool rebuildGUI)
  206. {
  207. if (field.IsModified())
  208. {
  209. rebuildGUI = field.ShouldRebuildOnModify();
  210. return field.Refresh(0);
  211. }
  212. rebuildGUI = false;
  213. return field.Refresh(0);
  214. }
  215. }
  216. }
  217. }