InspectableList.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 InspectableList : 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, InspectableList 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 GUIIntField guiSizeField;
  50. private GUILayoutX guiChildLayout;
  51. private List<EntryRow> rows = new List<EntryRow>();
  52. private bool forceUpdate = true;
  53. private bool isExpanded;
  54. public InspectableList(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. SerializableList list = property.GetList();
  68. if (list.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.List)
  78. return;
  79. foreach (var row in rows)
  80. row.Destroy();
  81. rows.Clear();
  82. propertyValue = property.GetValue<object>();
  83. if (propertyValue == null)
  84. {
  85. guiChildLayout = null;
  86. GUILayoutX guiTitleLayout = layout.AddLayoutX(layoutIndex);
  87. guiTitleLayout.AddElement(new GUILabel(title));
  88. guiTitleLayout.AddElement(new GUILabel("Empty"));
  89. if (!property.IsValueType)
  90. {
  91. GUIButton createBtn = new GUIButton("Create");
  92. createBtn.OnClick += OnCreateButtonClicked;
  93. guiTitleLayout.AddElement(createBtn);
  94. }
  95. numArrayElements = 0;
  96. }
  97. else
  98. {
  99. GUIFoldout guiFoldout = new GUIFoldout(title);
  100. guiFoldout.SetExpanded(isExpanded);
  101. guiFoldout.OnToggled += OnFoldoutToggled;
  102. guiSizeField = new GUIIntField();
  103. guiSizeField.SetRange(0, int.MaxValue);
  104. GUIButton guiResizeBtn = new GUIButton("Resize");
  105. guiResizeBtn.OnClick += OnResizeButtonClicked;
  106. GUIButton guiClearBtn = new GUIButton("Clear");
  107. guiClearBtn.OnClick += OnClearButtonClicked;
  108. GUILayoutX guiTitleLayout = layout.AddLayoutX(layoutIndex);
  109. guiTitleLayout.AddElement(guiFoldout);
  110. guiTitleLayout.AddElement(guiSizeField);
  111. guiTitleLayout.AddElement(guiResizeBtn);
  112. guiTitleLayout.AddElement(guiClearBtn);
  113. SerializableList list = property.GetList();
  114. numArrayElements = list.GetLength();
  115. guiSizeField.Value = numArrayElements;
  116. if (isExpanded)
  117. {
  118. guiChildLayout = layout.AddLayoutX(layoutIndex);
  119. guiChildLayout.AddSpace(IndentAmount);
  120. GUILayoutY guiContentLayout = guiChildLayout.AddLayoutY();
  121. for (int i = 0; i < numArrayElements; i++)
  122. {
  123. EntryRow newRow = new EntryRow(guiContentLayout, i, this);
  124. rows.Add(newRow);
  125. InspectableObjectBase childObj = CreateDefaultInspectable(i + ".", new InspectableFieldLayout(newRow.contentLayout), list.GetProperty(i));
  126. AddChild(childObj);
  127. childObj.Refresh(0);
  128. }
  129. }
  130. else
  131. guiChildLayout = null;
  132. }
  133. }
  134. private void OnFoldoutToggled(bool expanded)
  135. {
  136. isExpanded = expanded;
  137. forceUpdate = true;
  138. }
  139. private void OnResizeButtonClicked()
  140. {
  141. int size = guiSizeField.Value;
  142. IList newList = property.CreateListInstance(size);
  143. IList list = property.GetValue<IList>();
  144. int maxSize = MathEx.Min(size, list.Count);
  145. for (int i = 0; i < maxSize; i++)
  146. newList[i] = list[i];
  147. property.SetValue(newList);
  148. }
  149. private void OnDeleteButtonClicked(int index)
  150. {
  151. IList list = property.GetValue<IList>();
  152. if (index >= 0 && index < list.Count)
  153. list.RemoveAt(index);
  154. }
  155. private void OnCloneButtonClicked(int index)
  156. {
  157. SerializableList serializableList = property.GetList();
  158. IList list = property.GetValue<IList>();
  159. int size = serializableList.GetLength() + 1;
  160. if (index >= 0 && index < list.Count)
  161. {
  162. list.Add(serializableList.GetProperty(index).GetValueCopy<object>());
  163. }
  164. }
  165. private void OnMoveUpButtonClicked(int index)
  166. {
  167. IList list = property.GetValue<IList>();
  168. if ((index - 1) >= 0)
  169. {
  170. object previousEntry = list[index - 1];
  171. list[index - 1] = list[index];
  172. list[index] = previousEntry;
  173. }
  174. }
  175. private void OnMoveDownButtonClicked(int index)
  176. {
  177. IList list = property.GetValue<IList>();
  178. if ((index + 1) < list.Count)
  179. {
  180. object nextEntry = list[index + 1];
  181. list[index + 1] = list[index];
  182. list[index] = nextEntry;
  183. }
  184. }
  185. private void OnCreateButtonClicked()
  186. {
  187. property.SetValue(property.CreateListInstance(0));
  188. }
  189. private void OnClearButtonClicked()
  190. {
  191. property.SetValue<object>(null);
  192. }
  193. }
  194. }