InspectableArray.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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");
  36. GUIButton deleteBtn = new GUIButton("X");
  37. GUIButton moveUpBtn = new GUIButton("Up");
  38. GUIButton moveDownBtn = new GUIButton("Down");
  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));
  119. guiTitleLayout.AddElement(new GUILabel("Empty"));
  120. if (!property.IsValueType)
  121. {
  122. GUIButton createBtn = new GUIButton("Create");
  123. createBtn.OnClick += OnCreateButtonClicked;
  124. guiTitleLayout.AddElement(createBtn);
  125. }
  126. numArrayElements = 0;
  127. }
  128. else
  129. {
  130. GUIFoldout guiFoldout = new GUIFoldout(title);
  131. guiFoldout.SetExpanded(isExpanded);
  132. guiFoldout.OnToggled += OnFoldoutToggled;
  133. guiSizeField = new GUIIntField();
  134. guiSizeField.SetRange(0, int.MaxValue);
  135. GUIButton guiResizeBtn = new GUIButton("Resize");
  136. guiResizeBtn.OnClick += OnResizeButtonClicked;
  137. GUIButton guiClearBtn = new GUIButton("Clear");
  138. guiClearBtn.OnClick += OnClearButtonClicked;
  139. guiTitleLayout = layout.AddLayoutX(layoutIndex);
  140. guiTitleLayout.AddElement(guiFoldout);
  141. guiTitleLayout.AddElement(guiSizeField);
  142. guiTitleLayout.AddElement(guiResizeBtn);
  143. guiTitleLayout.AddElement(guiClearBtn);
  144. SerializableArray array = property.GetArray();
  145. numArrayElements = array.GetLength();
  146. guiSizeField.Value = numArrayElements;
  147. if (isExpanded)
  148. {
  149. guiChildLayout = layout.AddLayoutX(layoutIndex);
  150. guiChildLayout.AddSpace(IndentAmount);
  151. GUILayoutY guiContentLayout = guiChildLayout.AddLayoutY();
  152. for (int i = 0; i < numArrayElements; i++)
  153. {
  154. EntryRow newRow = new EntryRow(guiContentLayout);
  155. rows.Add(newRow);
  156. InspectableObjectBase childObj = CreateDefaultInspectable(i + ".", new InspectableFieldLayout(newRow.contentLayout), array.GetProperty(i));
  157. AddChild(childObj);
  158. childObj.Refresh(0);
  159. rows[i].Refresh(childObj, i, this);
  160. }
  161. }
  162. else
  163. guiChildLayout = null;
  164. }
  165. }
  166. private void OnFoldoutToggled(bool expanded)
  167. {
  168. isExpanded = expanded;
  169. forceUpdate = true;
  170. }
  171. private void OnResizeButtonClicked()
  172. {
  173. int size = guiSizeField.Value; // TODO - Support multi-rank arrays
  174. Array newArray = property.CreateArrayInstance(new int[] {size});
  175. Array array = property.GetValue<Array>();
  176. int maxSize = MathEx.Min(size, array.Length);
  177. for (int i = 0; i < maxSize; i++)
  178. newArray.SetValue(array.GetValue(i), i);
  179. property.SetValue(newArray);
  180. }
  181. private void OnDeleteButtonClicked(int index)
  182. {
  183. Array array = property.GetValue<Array>();
  184. int size = MathEx.Max(0, array.Length - 1);
  185. Array newArray = property.CreateArrayInstance(new int[] { size });
  186. int destIdx = 0;
  187. for (int i = 0; i < array.Length; i++)
  188. {
  189. if (i == index)
  190. continue;
  191. newArray.SetValue(array.GetValue(i), destIdx);
  192. destIdx++;
  193. }
  194. property.SetValue(newArray);
  195. }
  196. private void OnCloneButtonClicked(int index)
  197. {
  198. SerializableArray array = property.GetArray();
  199. int size = array.GetLength() + 1;
  200. Array newArray = property.CreateArrayInstance(new int[] { size });
  201. object clonedEntry = null;
  202. for (int i = 0; i < array.GetLength(); i++)
  203. {
  204. object value = array.GetProperty(i).GetValue<object>();
  205. newArray.SetValue(value, i);
  206. if (i == index)
  207. {
  208. clonedEntry = array.GetProperty(i).GetValueCopy<object>();
  209. }
  210. }
  211. newArray.SetValue(clonedEntry, size - 1);
  212. property.SetValue(newArray);
  213. }
  214. private void OnMoveUpButtonClicked(int index)
  215. {
  216. Array array = property.GetValue<Array>();
  217. if ((index - 1) >= 0)
  218. {
  219. object previousEntry = array.GetValue(index - 1);
  220. array.SetValue(array.GetValue(index), index - 1);
  221. array.SetValue(previousEntry, index);
  222. }
  223. }
  224. private void OnMoveDownButtonClicked(int index)
  225. {
  226. Array array = property.GetValue<Array>();
  227. if ((index + 1) < array.Length)
  228. {
  229. object nextEntry = array.GetValue(index + 1);
  230. array.SetValue(array.GetValue(index), index + 1);
  231. array.SetValue(nextEntry, index);
  232. }
  233. }
  234. private void OnCreateButtonClicked()
  235. {
  236. property.SetValue(property.CreateArrayInstance(new int[1] { 0 }));
  237. }
  238. private void OnClearButtonClicked()
  239. {
  240. property.SetValue<object>(null);
  241. }
  242. }
  243. }