InspectableArray.cs 8.6 KB

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