InspectableArray.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 oldPropertyValue; // 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 bool isInitialized;
  52. public InspectableArray(string title, InspectableFieldLayout layout, SerializableProperty property)
  53. : base(title, layout, property)
  54. {
  55. }
  56. protected override bool IsModified()
  57. {
  58. if (!isInitialized)
  59. return true;
  60. object newPropertyValue = property.GetValue<object>();
  61. if (oldPropertyValue != newPropertyValue)
  62. {
  63. oldPropertyValue = newPropertyValue;
  64. return true;
  65. }
  66. if (newPropertyValue != null)
  67. {
  68. SerializableArray array = property.GetArray();
  69. if (array.GetLength() != numArrayElements)
  70. return true;
  71. }
  72. return base.IsModified();
  73. }
  74. protected override void Update(int layoutIndex)
  75. {
  76. base.Update(layoutIndex);
  77. isInitialized = true;
  78. if (property.Type != SerializableProperty.FieldType.Array)
  79. return;
  80. foreach (var row in rows)
  81. row.Destroy();
  82. rows.Clear();
  83. layout.DestroyElements();
  84. if (property.GetValue<object>() == null)
  85. {
  86. GUILayoutX guiChildLayout = layout.AddLayoutX(layoutIndex);
  87. guiChildLayout.AddElement(new GUILabel(title));
  88. guiChildLayout.AddElement(new GUILabel("Empty"));
  89. if (!property.IsValueType)
  90. {
  91. GUIButton createBtn = new GUIButton("Create");
  92. createBtn.OnClick += OnCreateButtonClicked;
  93. guiChildLayout.AddElement(createBtn);
  94. }
  95. numArrayElements = 0;
  96. }
  97. else
  98. {
  99. GUILabel guiLabel = new GUILabel(title); // TODO - Add foldout and hook up its callbacks
  100. guiSizeField = new GUIIntField();
  101. guiSizeField.SetRange(0, int.MaxValue);
  102. GUIButton guiResizeBtn = new GUIButton("Resize");
  103. guiResizeBtn.OnClick += OnResizeButtonClicked;
  104. GUIButton guiClearBtn = new GUIButton("Clear");
  105. guiClearBtn.OnClick += OnClearButtonClicked;
  106. GUILayoutX guiTitleLayout = layout.AddLayoutX(layoutIndex);
  107. guiTitleLayout.AddElement(guiLabel);
  108. guiTitleLayout.AddElement(guiSizeField);
  109. guiTitleLayout.AddElement(guiResizeBtn);
  110. guiTitleLayout.AddElement(guiClearBtn);
  111. GUILayoutX guiChildLayout = layout.AddLayoutX(layoutIndex);
  112. guiChildLayout.AddSpace(IndentAmount);
  113. GUILayoutY guiContentLayout = guiChildLayout.AddLayoutY();
  114. SerializableArray array = property.GetArray();
  115. numArrayElements = array.GetLength();
  116. for (int i = 0; i < numArrayElements; i++)
  117. {
  118. EntryRow newRow = new EntryRow(guiContentLayout, i, this);
  119. rows.Add(newRow);
  120. InspectableObjectBase childObj = CreateDefaultInspectable(i + ".", new InspectableFieldLayout(newRow.contentLayout), array.GetProperty(i));
  121. AddChild(childObj);
  122. childObj.Refresh(0);
  123. }
  124. guiSizeField.Value = numArrayElements;
  125. }
  126. }
  127. private void OnResizeButtonClicked()
  128. {
  129. int size = guiSizeField.Value; // TODO - Support multi-rank arrays
  130. Array newArray = property.CreateArrayInstance(new int[] {size});
  131. Array array = property.GetValue<Array>();
  132. int maxSize = MathEx.Min(size, array.Length);
  133. for (int i = 0; i < maxSize; i++)
  134. newArray.SetValue(array.GetValue(i), i);
  135. property.SetValue(newArray);
  136. }
  137. private void OnDeleteButtonClicked(int index)
  138. {
  139. Array array = property.GetValue<Array>();
  140. int size = MathEx.Max(0, array.Length - 1);
  141. Array newArray = property.CreateArrayInstance(new int[] { size });
  142. int destIdx = 0;
  143. for (int i = 0; i < array.Length; i++)
  144. {
  145. if (i == index)
  146. continue;
  147. newArray.SetValue(array.GetValue(i), destIdx);
  148. destIdx++;
  149. }
  150. property.SetValue(newArray);
  151. }
  152. private void OnCloneButtonClicked(int index)
  153. {
  154. SerializableArray array = property.GetArray();
  155. int size = array.GetLength() + 1;
  156. Array newArray = property.CreateArrayInstance(new int[] { size });
  157. object clonedEntry = null;
  158. for (int i = 0; i < array.GetLength(); i++)
  159. {
  160. object value = array.GetProperty(i).GetValue<object>();
  161. newArray.SetValue(value, i);
  162. if (i == index)
  163. {
  164. clonedEntry = array.GetProperty(i).GetValueCopy<object>();
  165. }
  166. }
  167. newArray.SetValue(clonedEntry, size - 1);
  168. property.SetValue(newArray);
  169. }
  170. private void OnMoveUpButtonClicked(int index)
  171. {
  172. Array array = property.GetValue<Array>();
  173. if ((index - 1) >= 0)
  174. {
  175. object previousEntry = array.GetValue(index - 1);
  176. array.SetValue(array.GetValue(index), index - 1);
  177. array.SetValue(previousEntry, index);
  178. }
  179. }
  180. private void OnMoveDownButtonClicked(int index)
  181. {
  182. Array array = property.GetValue<Array>();
  183. if ((index + 1) < array.Length)
  184. {
  185. object nextEntry = array.GetValue(index + 1);
  186. array.SetValue(array.GetValue(index), index + 1);
  187. array.SetValue(nextEntry, index);
  188. }
  189. }
  190. private void OnCreateButtonClicked()
  191. {
  192. property.SetValue(property.CreateArrayInstance(new int[1] { 0 }));
  193. }
  194. private void OnClearButtonClicked()
  195. {
  196. property.SetValue<object>(null);
  197. }
  198. }
  199. }