InspectableArray.cs 8.2 KB

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