InspectableArray.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. public EntryRow(GUILayout parentLayout, int seqIndex, InspectableArray parent)
  16. {
  17. rowLayout = parentLayout.AddLayoutX();
  18. contentLayout = rowLayout.AddLayoutY();
  19. GUILayoutY buttonLayout = rowLayout.AddLayoutY();
  20. GUIButton cloneBtn = new GUIButton("C");
  21. GUIButton deleteBtn = new GUIButton("X");
  22. GUIButton moveUpBtn = new GUIButton("Up");
  23. GUIButton moveDownBtn = new GUIButton("Down");
  24. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(seqIndex);
  25. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(seqIndex);
  26. moveUpBtn.OnClick += () => parent.OnMoveUpButtonClicked(seqIndex);
  27. moveDownBtn.OnClick += () => parent.OnMoveDownButtonClicked(seqIndex);
  28. buttonLayout.AddElement(cloneBtn);
  29. buttonLayout.AddElement(deleteBtn);
  30. buttonLayout.AddElement(moveUpBtn);
  31. buttonLayout.AddElement(moveDownBtn);
  32. buttonLayout.AddFlexibleSpace();
  33. }
  34. public void Destroy()
  35. {
  36. rowLayout.Destroy();
  37. }
  38. }
  39. private const int IndentAmount = 15;
  40. private object propertyValue; // TODO - This will unnecessarily hold references to the object
  41. private int numArrayElements;
  42. private List<EntryRow> rows = new List<EntryRow>();
  43. private GUIIntField guiSizeField;
  44. private GUILayoutX guiChildLayout;
  45. private bool isExpanded;
  46. private bool forceUpdate = true;
  47. public InspectableArray(string title, InspectableFieldLayout layout, SerializableProperty property)
  48. : base(title, layout, property)
  49. {
  50. }
  51. protected override bool IsModified()
  52. {
  53. if (forceUpdate)
  54. return true;
  55. object newPropertyValue = property.GetValue<object>();
  56. if (propertyValue == null)
  57. return newPropertyValue != null;
  58. if (newPropertyValue == null)
  59. return propertyValue != null;
  60. if (!propertyValue.Equals(newPropertyValue))
  61. return true;
  62. SerializableArray array = property.GetArray();
  63. if (array.GetLength() != numArrayElements)
  64. return true;
  65. return base.IsModified();
  66. }
  67. public override bool Refresh(int layoutIndex)
  68. {
  69. bool anythingModified = false;
  70. if (IsModified())
  71. {
  72. Update(layoutIndex);
  73. anythingModified = true;
  74. }
  75. for (int i = 0; i < GetChildCount(); i++)
  76. anythingModified |= GetChild(i).Refresh(0);
  77. return anythingModified;
  78. }
  79. protected override void Update(int layoutIndex)
  80. {
  81. base.Update(layoutIndex);
  82. forceUpdate = false;
  83. if (property.Type != SerializableProperty.FieldType.Array || property.InternalType.GetArrayRank() != 1) // We don't support multirank arrays
  84. return;
  85. foreach (var row in rows)
  86. row.Destroy();
  87. rows.Clear();
  88. layout.DestroyElements();
  89. propertyValue = property.GetValue<object>();
  90. if (propertyValue == null)
  91. {
  92. guiChildLayout = null;
  93. GUILayoutX guiTitleLayout = layout.AddLayoutX(layoutIndex);
  94. guiTitleLayout.AddElement(new GUILabel(title));
  95. guiTitleLayout.AddElement(new GUILabel("Empty"));
  96. if (!property.IsValueType)
  97. {
  98. GUIButton createBtn = new GUIButton("Create");
  99. createBtn.OnClick += OnCreateButtonClicked;
  100. guiTitleLayout.AddElement(createBtn);
  101. }
  102. numArrayElements = 0;
  103. }
  104. else
  105. {
  106. GUIFoldout guiFoldout = new GUIFoldout(title);
  107. guiFoldout.SetExpanded(isExpanded);
  108. guiFoldout.OnToggled += OnFoldoutToggled;
  109. guiSizeField = new GUIIntField();
  110. guiSizeField.SetRange(0, int.MaxValue);
  111. GUIButton guiResizeBtn = new GUIButton("Resize");
  112. guiResizeBtn.OnClick += OnResizeButtonClicked;
  113. GUIButton guiClearBtn = new GUIButton("Clear");
  114. guiClearBtn.OnClick += OnClearButtonClicked;
  115. GUILayoutX guiTitleLayout = layout.AddLayoutX(layoutIndex);
  116. guiTitleLayout.AddElement(guiFoldout);
  117. guiTitleLayout.AddElement(guiSizeField);
  118. guiTitleLayout.AddElement(guiResizeBtn);
  119. guiTitleLayout.AddElement(guiClearBtn);
  120. SerializableArray array = property.GetArray();
  121. numArrayElements = array.GetLength();
  122. guiSizeField.Value = numArrayElements;
  123. if (isExpanded)
  124. {
  125. guiChildLayout = layout.AddLayoutX(layoutIndex);
  126. guiChildLayout.AddSpace(IndentAmount);
  127. GUILayoutY guiContentLayout = guiChildLayout.AddLayoutY();
  128. for (int i = 0; i < numArrayElements; i++)
  129. {
  130. EntryRow newRow = new EntryRow(guiContentLayout, i, this);
  131. rows.Add(newRow);
  132. InspectableObjectBase childObj = CreateDefaultInspectable(i + ".", new InspectableFieldLayout(newRow.contentLayout), array.GetProperty(i));
  133. AddChild(childObj);
  134. childObj.Refresh(0);
  135. }
  136. }
  137. else
  138. guiChildLayout = null;
  139. }
  140. }
  141. private void OnFoldoutToggled(bool expanded)
  142. {
  143. isExpanded = expanded;
  144. forceUpdate = true;
  145. }
  146. private void OnResizeButtonClicked()
  147. {
  148. int size = guiSizeField.Value; // TODO - Support multi-rank arrays
  149. Array newArray = property.CreateArrayInstance(new int[] {size});
  150. Array array = property.GetValue<Array>();
  151. int maxSize = MathEx.Min(size, array.Length);
  152. for (int i = 0; i < maxSize; i++)
  153. newArray.SetValue(array.GetValue(i), i);
  154. property.SetValue(newArray);
  155. }
  156. private void OnDeleteButtonClicked(int index)
  157. {
  158. Array array = property.GetValue<Array>();
  159. int size = MathEx.Max(0, array.Length - 1);
  160. Array newArray = property.CreateArrayInstance(new int[] { size });
  161. int destIdx = 0;
  162. for (int i = 0; i < array.Length; i++)
  163. {
  164. if (i == index)
  165. continue;
  166. newArray.SetValue(array.GetValue(i), destIdx);
  167. destIdx++;
  168. }
  169. property.SetValue(newArray);
  170. }
  171. private void OnCloneButtonClicked(int index)
  172. {
  173. SerializableArray array = property.GetArray();
  174. int size = array.GetLength() + 1;
  175. Array newArray = property.CreateArrayInstance(new int[] { size });
  176. object clonedEntry = null;
  177. for (int i = 0; i < array.GetLength(); i++)
  178. {
  179. object value = array.GetProperty(i).GetValue<object>();
  180. newArray.SetValue(value, i);
  181. if (i == index)
  182. {
  183. clonedEntry = array.GetProperty(i).GetValueCopy<object>();
  184. }
  185. }
  186. newArray.SetValue(clonedEntry, size - 1);
  187. property.SetValue(newArray);
  188. }
  189. private void OnMoveUpButtonClicked(int index)
  190. {
  191. Array array = property.GetValue<Array>();
  192. if ((index - 1) >= 0)
  193. {
  194. object previousEntry = array.GetValue(index - 1);
  195. array.SetValue(array.GetValue(index), index - 1);
  196. array.SetValue(previousEntry, index);
  197. }
  198. }
  199. private void OnMoveDownButtonClicked(int index)
  200. {
  201. Array array = property.GetValue<Array>();
  202. if ((index + 1) < array.Length)
  203. {
  204. object nextEntry = array.GetValue(index + 1);
  205. array.SetValue(array.GetValue(index), index + 1);
  206. array.SetValue(nextEntry, index);
  207. }
  208. }
  209. private void OnCreateButtonClicked()
  210. {
  211. property.SetValue(property.CreateArrayInstance(new int[1] { 0 }));
  212. }
  213. private void OnClearButtonClicked()
  214. {
  215. property.SetValue<object>(null);
  216. }
  217. }
  218. }