InspectableArray.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 InspectableArray : 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, InspectableArray 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 List<EntryRow> rows = new List<EntryRow>();
  57. private GUIIntField guiSizeField;
  58. private GUILayoutX guiChildLayout;
  59. private GUILayoutX guiTitleLayout;
  60. private bool isExpanded;
  61. private bool forceUpdate = true;
  62. public InspectableArray(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. SerializableArray array = property.GetArray();
  80. if (array.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.Array || property.InternalType.GetArrayRank() != 1) // We don't support multirank arrays
  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. SerializableArray array = property.GetArray();
  147. numArrayElements = array.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), array.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; // TODO - Support multi-rank arrays
  176. Array newArray = property.CreateArrayInstance(new int[] {size});
  177. Array array = property.GetValue<Array>();
  178. int maxSize = MathEx.Min(size, array.Length);
  179. for (int i = 0; i < maxSize; i++)
  180. newArray.SetValue(array.GetValue(i), i);
  181. property.SetValue(newArray);
  182. }
  183. private void OnDeleteButtonClicked(int index)
  184. {
  185. Array array = property.GetValue<Array>();
  186. int size = MathEx.Max(0, array.Length - 1);
  187. Array newArray = property.CreateArrayInstance(new int[] { size });
  188. int destIdx = 0;
  189. for (int i = 0; i < array.Length; i++)
  190. {
  191. if (i == index)
  192. continue;
  193. newArray.SetValue(array.GetValue(i), destIdx);
  194. destIdx++;
  195. }
  196. property.SetValue(newArray);
  197. }
  198. private void OnCloneButtonClicked(int index)
  199. {
  200. SerializableArray array = property.GetArray();
  201. int size = array.GetLength() + 1;
  202. Array newArray = property.CreateArrayInstance(new int[] { size });
  203. object clonedEntry = null;
  204. for (int i = 0; i < array.GetLength(); i++)
  205. {
  206. object value = array.GetProperty(i).GetValue<object>();
  207. newArray.SetValue(value, i);
  208. if (i == index)
  209. {
  210. clonedEntry = array.GetProperty(i).GetValueCopy<object>();
  211. }
  212. }
  213. newArray.SetValue(clonedEntry, size - 1);
  214. property.SetValue(newArray);
  215. }
  216. private void OnMoveUpButtonClicked(int index)
  217. {
  218. Array array = property.GetValue<Array>();
  219. if ((index - 1) >= 0)
  220. {
  221. object previousEntry = array.GetValue(index - 1);
  222. array.SetValue(array.GetValue(index), index - 1);
  223. array.SetValue(previousEntry, index);
  224. }
  225. }
  226. private void OnMoveDownButtonClicked(int index)
  227. {
  228. Array array = property.GetValue<Array>();
  229. if ((index + 1) < array.Length)
  230. {
  231. object nextEntry = array.GetValue(index + 1);
  232. array.SetValue(array.GetValue(index), index + 1);
  233. array.SetValue(nextEntry, index);
  234. }
  235. }
  236. private void OnCreateButtonClicked()
  237. {
  238. property.SetValue(property.CreateArrayInstance(new int[1] { 0 }));
  239. }
  240. private void OnClearButtonClicked()
  241. {
  242. property.SetValue<object>(null);
  243. }
  244. }
  245. }