InspectableArray.cs 7.4 KB

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