InspectableList.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. public class InspectableList : InspectableObjectBase
  10. {
  11. private class EntryRow
  12. {
  13. public GUILayoutY contentLayout;
  14. private GUILayoutX rowLayout;
  15. private GUILayoutX titleLayout;
  16. private bool ownsTitleLayout;
  17. public EntryRow(GUILayout parentLayout)
  18. {
  19. rowLayout = parentLayout.AddLayoutX();
  20. contentLayout = rowLayout.AddLayoutY();
  21. }
  22. public void Refresh(InspectableObjectBase child, int seqIndex, InspectableList parent)
  23. {
  24. if (ownsTitleLayout || (titleLayout != null && titleLayout == child.GetTitleLayout()))
  25. return;
  26. titleLayout = child.GetTitleLayout();
  27. if (titleLayout == null)
  28. {
  29. GUILayoutY buttonCenter = rowLayout.AddLayoutY();
  30. buttonCenter.AddFlexibleSpace();
  31. titleLayout = buttonCenter.AddLayoutX();
  32. buttonCenter.AddFlexibleSpace();
  33. ownsTitleLayout = true;
  34. }
  35. GUIButton cloneBtn = new GUIButton("C", GUIOption.FixedWidth(20));
  36. GUIButton deleteBtn = new GUIButton("X", GUIOption.FixedWidth(20));
  37. GUIButton moveUpBtn = new GUIButton("U", GUIOption.FixedWidth(20));
  38. GUIButton moveDownBtn = new GUIButton("D", GUIOption.FixedWidth(20));
  39. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(seqIndex);
  40. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(seqIndex);
  41. moveUpBtn.OnClick += () => parent.OnMoveUpButtonClicked(seqIndex);
  42. moveDownBtn.OnClick += () => parent.OnMoveDownButtonClicked(seqIndex);
  43. titleLayout.AddElement(cloneBtn);
  44. titleLayout.AddElement(deleteBtn);
  45. titleLayout.AddElement(moveUpBtn);
  46. titleLayout.AddElement(moveDownBtn);
  47. }
  48. public void Destroy()
  49. {
  50. rowLayout.Destroy();
  51. }
  52. }
  53. private const int IndentAmount = 15;
  54. private object propertyValue; // TODO - This will unnecessarily hold references to the object
  55. private int numArrayElements;
  56. private GUIIntField guiSizeField;
  57. private GUILayoutX guiChildLayout;
  58. private GUILayoutX guiTitleLayout;
  59. private List<EntryRow> rows = new List<EntryRow>();
  60. private bool forceUpdate = true;
  61. private bool isExpanded;
  62. public InspectableList(string title, InspectableFieldLayout layout, SerializableProperty property)
  63. : base(title, layout, property)
  64. {
  65. }
  66. public override GUILayoutX GetTitleLayout()
  67. {
  68. return guiTitleLayout;
  69. }
  70. protected override bool IsModified()
  71. {
  72. if (forceUpdate)
  73. return true;
  74. object newPropertyValue = property.GetValue<object>();
  75. if (propertyValue == null)
  76. return newPropertyValue != null;
  77. if (newPropertyValue == null)
  78. return propertyValue != null;
  79. SerializableList list = property.GetList();
  80. if (list.GetLength() != numArrayElements)
  81. return true;
  82. return base.IsModified();
  83. }
  84. public override bool Refresh(int layoutIndex)
  85. {
  86. bool anythingModified = false;
  87. if (IsModified())
  88. {
  89. Update(layoutIndex);
  90. anythingModified = true;
  91. }
  92. for (int i = 0; i < GetChildCount(); i++)
  93. {
  94. InspectableObjectBase child = GetChild(i);
  95. bool childModified = child.Refresh(0);
  96. if (childModified)
  97. rows[i].Refresh(child, i, this);
  98. anythingModified |= childModified;
  99. }
  100. return anythingModified;
  101. }
  102. protected override void Update(int layoutIndex)
  103. {
  104. base.Update(layoutIndex);
  105. forceUpdate = false;
  106. guiTitleLayout = null;
  107. if (property.Type != SerializableProperty.FieldType.List)
  108. return;
  109. foreach (var row in rows)
  110. row.Destroy();
  111. rows.Clear();
  112. layout.DestroyElements();
  113. propertyValue = property.GetValue<object>();
  114. if (propertyValue == null)
  115. {
  116. guiChildLayout = null;
  117. guiTitleLayout = layout.AddLayoutX(layoutIndex);
  118. guiTitleLayout.AddElement(new GUILabel(title, GUIOption.FixedWidth(100)));
  119. guiTitleLayout.AddElement(new GUILabel("Empty"));
  120. if (!property.IsValueType)
  121. {
  122. GUIButton createBtn = new GUIButton("Cr", GUIOption.FixedWidth(20));
  123. createBtn.OnClick += OnCreateButtonClicked;
  124. guiTitleLayout.AddFlexibleSpace();
  125. guiTitleLayout.AddElement(createBtn);
  126. }
  127. numArrayElements = 0;
  128. }
  129. else
  130. {
  131. GUIFoldout guiFoldout = new GUIFoldout(title, GUIOption.FixedWidth(100));
  132. guiFoldout.SetExpanded(isExpanded);
  133. guiFoldout.OnToggled += OnFoldoutToggled;
  134. guiSizeField = new GUIIntField();
  135. guiSizeField.SetRange(0, int.MaxValue);
  136. GUIButton guiResizeBtn = new GUIButton("R", GUIOption.FixedWidth(20));
  137. guiResizeBtn.OnClick += OnResizeButtonClicked;
  138. GUIButton guiClearBtn = new GUIButton("Cl", GUIOption.FixedWidth(20));
  139. guiClearBtn.OnClick += OnClearButtonClicked;
  140. guiTitleLayout = layout.AddLayoutX(layoutIndex);
  141. guiTitleLayout.AddElement(guiFoldout);
  142. guiTitleLayout.AddElement(guiSizeField);
  143. guiTitleLayout.AddFlexibleSpace();
  144. guiTitleLayout.AddElement(guiResizeBtn);
  145. guiTitleLayout.AddElement(guiClearBtn);
  146. SerializableList list = property.GetList();
  147. numArrayElements = list.GetLength();
  148. guiSizeField.Value = numArrayElements;
  149. if (isExpanded)
  150. {
  151. guiChildLayout = layout.AddLayoutX(layoutIndex);
  152. guiChildLayout.AddSpace(IndentAmount);
  153. GUILayoutY guiContentLayout = guiChildLayout.AddLayoutY();
  154. for (int i = 0; i < numArrayElements; i++)
  155. {
  156. EntryRow newRow = new EntryRow(guiContentLayout);
  157. rows.Add(newRow);
  158. InspectableObjectBase childObj = CreateDefaultInspectable(i + ".", new InspectableFieldLayout(newRow.contentLayout), list.GetProperty(i));
  159. AddChild(childObj);
  160. childObj.Refresh(0);
  161. rows[i].Refresh(childObj, i, this);
  162. }
  163. }
  164. else
  165. guiChildLayout = null;
  166. }
  167. }
  168. private void OnFoldoutToggled(bool expanded)
  169. {
  170. isExpanded = expanded;
  171. forceUpdate = true;
  172. }
  173. private void OnResizeButtonClicked()
  174. {
  175. int size = guiSizeField.Value;
  176. IList newList = property.CreateListInstance(size);
  177. IList list = property.GetValue<IList>();
  178. int maxSize = MathEx.Min(size, list.Count);
  179. for (int i = 0; i < maxSize; i++)
  180. newList[i] = list[i];
  181. property.SetValue(newList);
  182. }
  183. private void OnDeleteButtonClicked(int index)
  184. {
  185. IList list = property.GetValue<IList>();
  186. if (index >= 0 && index < list.Count)
  187. list.RemoveAt(index);
  188. }
  189. private void OnCloneButtonClicked(int index)
  190. {
  191. SerializableList serializableList = property.GetList();
  192. IList list = property.GetValue<IList>();
  193. int size = serializableList.GetLength() + 1;
  194. if (index >= 0 && index < list.Count)
  195. {
  196. list.Add(serializableList.GetProperty(index).GetValueCopy<object>());
  197. }
  198. }
  199. private void OnMoveUpButtonClicked(int index)
  200. {
  201. IList list = property.GetValue<IList>();
  202. if ((index - 1) >= 0)
  203. {
  204. object previousEntry = list[index - 1];
  205. list[index - 1] = list[index];
  206. list[index] = previousEntry;
  207. }
  208. }
  209. private void OnMoveDownButtonClicked(int index)
  210. {
  211. IList list = property.GetValue<IList>();
  212. if ((index + 1) < list.Count)
  213. {
  214. object nextEntry = list[index + 1];
  215. list[index + 1] = list[index];
  216. list[index] = nextEntry;
  217. }
  218. }
  219. private void OnCreateButtonClicked()
  220. {
  221. property.SetValue(property.CreateListInstance(0));
  222. }
  223. private void OnClearButtonClicked()
  224. {
  225. property.SetValue<object>(null);
  226. }
  227. }
  228. }