InspectableList.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. guiChildLayout = layout.AddLayoutX(layoutIndex);
  114. guiChildLayout.SetVisible(isExpanded);
  115. guiChildLayout.AddSpace(IndentAmount);
  116. GUILayoutY guiContentLayout = guiChildLayout.AddLayoutY();
  117. SerializableList list = property.GetList();
  118. numArrayElements = list.GetLength();
  119. for (int i = 0; i < numArrayElements; i++)
  120. {
  121. EntryRow newRow = new EntryRow(guiContentLayout, i, this);
  122. rows.Add(newRow);
  123. InspectableObjectBase childObj = CreateDefaultInspectable(i + ".", new InspectableFieldLayout(newRow.contentLayout), list.GetProperty(i));
  124. AddChild(childObj);
  125. childObj.Refresh(0);
  126. }
  127. guiSizeField.Value = numArrayElements;
  128. }
  129. }
  130. private void OnFoldoutToggled(bool expanded)
  131. {
  132. if (guiChildLayout != null)
  133. guiChildLayout.SetVisible(expanded);
  134. isExpanded = expanded;
  135. forceUpdate = true;
  136. }
  137. private void OnResizeButtonClicked()
  138. {
  139. int size = guiSizeField.Value;
  140. IList newList = property.CreateListInstance(size);
  141. IList list = property.GetValue<IList>();
  142. int maxSize = MathEx.Min(size, list.Count);
  143. for (int i = 0; i < maxSize; i++)
  144. newList[i] = list[i];
  145. property.SetValue(newList);
  146. }
  147. private void OnDeleteButtonClicked(int index)
  148. {
  149. IList list = property.GetValue<IList>();
  150. if (index >= 0 && index < list.Count)
  151. list.RemoveAt(index);
  152. }
  153. private void OnCloneButtonClicked(int index)
  154. {
  155. SerializableList serializableList = property.GetList();
  156. IList list = property.GetValue<IList>();
  157. int size = serializableList.GetLength() + 1;
  158. if (index >= 0 && index < list.Count)
  159. {
  160. list.Add(serializableList.GetProperty(index).GetValueCopy<object>());
  161. }
  162. }
  163. private void OnMoveUpButtonClicked(int index)
  164. {
  165. IList list = property.GetValue<IList>();
  166. if ((index - 1) >= 0)
  167. {
  168. object previousEntry = list[index - 1];
  169. list[index - 1] = list[index];
  170. list[index] = previousEntry;
  171. }
  172. }
  173. private void OnMoveDownButtonClicked(int index)
  174. {
  175. IList list = property.GetValue<IList>();
  176. if ((index + 1) < list.Count)
  177. {
  178. object nextEntry = list[index + 1];
  179. list[index + 1] = list[index];
  180. list[index] = nextEntry;
  181. }
  182. }
  183. private void OnCreateButtonClicked()
  184. {
  185. property.SetValue(property.CreateListInstance(0));
  186. }
  187. private void OnClearButtonClicked()
  188. {
  189. property.SetValue<object>(null);
  190. }
  191. }
  192. }