InspectableList.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. /// <summary>
  10. /// Displays GUI for a serializable property containing a list. List contents are displayed as rows of entries
  11. /// that can be shown, hidden or manipulated.
  12. /// </summary>
  13. public class InspectableList : InspectableField
  14. {
  15. /// <summary>
  16. /// Contains GUI elements for a single entry in the list.
  17. /// </summary>
  18. private class EntryRow
  19. {
  20. public GUILayoutY contentLayout;
  21. private GUILayoutX rowLayout;
  22. private GUILayoutX titleLayout;
  23. private bool ownsTitleLayout;
  24. /// <summary>
  25. /// Constructs a new entry row object.
  26. /// </summary>
  27. /// <param name="parentLayout">Parent layout that row GUI elements will be added to.</param>
  28. public EntryRow(GUILayout parentLayout)
  29. {
  30. rowLayout = parentLayout.AddLayoutX();
  31. contentLayout = rowLayout.AddLayoutY();
  32. }
  33. /// <summary>
  34. /// Recreates all row GUI elements.
  35. /// </summary>
  36. /// <param name="child">Inspectable field of the list entry.</param>
  37. /// <param name="seqIndex">Sequential index of the list entry.</param>
  38. /// <param name="parent">Parent list object that the entry is contained in.</param>
  39. public void Refresh(InspectableField child, int seqIndex, InspectableList parent)
  40. {
  41. if (ownsTitleLayout || (titleLayout != null && titleLayout == child.GetTitleLayout()))
  42. return;
  43. titleLayout = child.GetTitleLayout();
  44. if (titleLayout == null)
  45. {
  46. GUILayoutY buttonCenter = rowLayout.AddLayoutY();
  47. buttonCenter.AddFlexibleSpace();
  48. titleLayout = buttonCenter.AddLayoutX();
  49. buttonCenter.AddFlexibleSpace();
  50. ownsTitleLayout = true;
  51. }
  52. GUIContent cloneIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clone));
  53. GUIContent deleteIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Delete));
  54. GUIContent moveUp = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveUp));
  55. GUIContent moveDown = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveDown));
  56. GUIButton cloneBtn = new GUIButton(cloneIcon, GUIOption.FixedWidth(30));
  57. GUIButton deleteBtn = new GUIButton(deleteIcon, GUIOption.FixedWidth(30));
  58. GUIButton moveUpBtn = new GUIButton(moveUp, GUIOption.FixedWidth(30));
  59. GUIButton moveDownBtn = new GUIButton(moveDown, GUIOption.FixedWidth(30));
  60. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(seqIndex);
  61. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(seqIndex);
  62. moveUpBtn.OnClick += () => parent.OnMoveUpButtonClicked(seqIndex);
  63. moveDownBtn.OnClick += () => parent.OnMoveDownButtonClicked(seqIndex);
  64. titleLayout.AddElement(cloneBtn);
  65. titleLayout.AddElement(deleteBtn);
  66. titleLayout.AddElement(moveUpBtn);
  67. titleLayout.AddElement(moveDownBtn);
  68. }
  69. /// <summary>
  70. /// Destroys all row GUI elements.
  71. /// </summary>
  72. public void Destroy()
  73. {
  74. rowLayout.Destroy();
  75. }
  76. }
  77. private const int IndentAmount = 5;
  78. private object propertyValue; // TODO - This will unnecessarily hold references to the object
  79. private int numArrayElements;
  80. private GUIIntField guiSizeField;
  81. private GUILayoutX guiChildLayout;
  82. private GUILayoutX guiTitleLayout;
  83. private List<EntryRow> rows = new List<EntryRow>();
  84. private bool forceUpdate = true;
  85. private bool isExpanded;
  86. /// <summary>
  87. /// Creates a new inspectable list GUI for the specified property.
  88. /// </summary>
  89. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  90. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field.Some fields may
  91. /// contain other fields, in which case you should increase this value by one.</param>
  92. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  93. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  94. public InspectableList(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  95. : base(title, depth, layout, property)
  96. {
  97. }
  98. /// <inheritdoc/>
  99. public override GUILayoutX GetTitleLayout()
  100. {
  101. return guiTitleLayout;
  102. }
  103. /// <inheritdoc/>
  104. protected override bool IsModified()
  105. {
  106. if (forceUpdate)
  107. return true;
  108. object newPropertyValue = property.GetValue<object>();
  109. if (propertyValue == null)
  110. return newPropertyValue != null;
  111. if (newPropertyValue == null)
  112. return propertyValue != null;
  113. SerializableList list = property.GetList();
  114. if (list.GetLength() != numArrayElements)
  115. return true;
  116. return base.IsModified();
  117. }
  118. /// <inheritdoc/>
  119. public override bool Refresh(int layoutIndex)
  120. {
  121. bool anythingModified = false;
  122. if (IsModified())
  123. {
  124. Update(layoutIndex);
  125. anythingModified = true;
  126. }
  127. for (int i = 0; i < ChildCount; i++)
  128. {
  129. InspectableField child = GetChild(i);
  130. bool childModified = child.Refresh(0);
  131. if (childModified)
  132. rows[i].Refresh(child, i, this);
  133. anythingModified |= childModified;
  134. }
  135. return anythingModified;
  136. }
  137. /// <inheritdoc/>
  138. protected override void Update(int layoutIndex)
  139. {
  140. base.Update(layoutIndex);
  141. forceUpdate = false;
  142. guiTitleLayout = null;
  143. if (property.Type != SerializableProperty.FieldType.List)
  144. return;
  145. foreach (var row in rows)
  146. row.Destroy();
  147. rows.Clear();
  148. layout.DestroyElements();
  149. propertyValue = property.GetValue<object>();
  150. if (propertyValue == null)
  151. {
  152. guiChildLayout = null;
  153. guiTitleLayout = layout.AddLayoutX(layoutIndex);
  154. guiTitleLayout.AddElement(new GUILabel(title));
  155. guiTitleLayout.AddElement(new GUILabel("Empty", GUIOption.FixedWidth(100)));
  156. if (!property.IsValueType)
  157. {
  158. GUIContent createIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Create));
  159. GUIButton createBtn = new GUIButton(createIcon, GUIOption.FixedWidth(30));
  160. createBtn.OnClick += OnCreateButtonClicked;
  161. guiTitleLayout.AddElement(createBtn);
  162. }
  163. numArrayElements = 0;
  164. }
  165. else
  166. {
  167. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  168. guiFoldout.Value = isExpanded;
  169. guiFoldout.OnToggled += OnFoldoutToggled;
  170. guiSizeField = new GUIIntField("", GUIOption.FixedWidth(50));
  171. guiSizeField.SetRange(0, int.MaxValue);
  172. GUIContent resizeIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Resize));
  173. GUIButton guiResizeBtn = new GUIButton(resizeIcon, GUIOption.FixedWidth(30));
  174. guiResizeBtn.OnClick += OnResizeButtonClicked;
  175. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clear));
  176. GUIButton guiClearBtn = new GUIButton(clearIcon, GUIOption.FixedWidth(30));
  177. guiClearBtn.OnClick += OnClearButtonClicked;
  178. guiTitleLayout = layout.AddLayoutX(layoutIndex);
  179. guiTitleLayout.AddElement(guiFoldout);
  180. guiTitleLayout.AddElement(guiSizeField);
  181. guiTitleLayout.AddElement(guiResizeBtn);
  182. guiTitleLayout.AddElement(guiClearBtn);
  183. SerializableList list = property.GetList();
  184. numArrayElements = list.GetLength();
  185. guiSizeField.Value = numArrayElements;
  186. if (isExpanded)
  187. {
  188. if (numArrayElements > 0)
  189. {
  190. guiChildLayout = layout.AddLayoutX(layoutIndex);
  191. guiChildLayout.AddSpace(IndentAmount);
  192. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  193. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  194. guiIndentLayoutX.AddSpace(IndentAmount);
  195. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  196. guiIndentLayoutY.AddSpace(IndentAmount);
  197. GUILayoutY guiContentLayout = guiIndentLayoutY.AddLayoutY();
  198. guiIndentLayoutY.AddSpace(IndentAmount);
  199. guiIndentLayoutX.AddSpace(IndentAmount);
  200. guiChildLayout.AddSpace(IndentAmount);
  201. short backgroundDepth = (short) (Inspector.START_BACKGROUND_DEPTH - depth - 1);
  202. string bgPanelStyle = depth % 2 == 0
  203. ? EditorStyles.InspectorContentBgAlternate
  204. : EditorStyles.InspectorContentBg;
  205. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  206. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  207. backgroundPanel.AddElement(inspectorContentBg);
  208. for (int i = 0; i < numArrayElements; i++)
  209. {
  210. EntryRow newRow = new EntryRow(guiContentLayout);
  211. rows.Add(newRow);
  212. InspectableField childObj = CreateInspectable(i + ".", depth + 1,
  213. new InspectableFieldLayout(newRow.contentLayout), list.GetProperty(i));
  214. AddChild(childObj);
  215. childObj.Refresh(0);
  216. rows[i].Refresh(childObj, i, this);
  217. }
  218. }
  219. }
  220. else
  221. guiChildLayout = null;
  222. }
  223. }
  224. /// <summary>
  225. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  226. /// </summary>
  227. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  228. private void OnFoldoutToggled(bool expanded)
  229. {
  230. isExpanded = expanded;
  231. forceUpdate = true;
  232. }
  233. /// <summary>
  234. /// Triggered when the user clicks on the resize button on the title bar. Changes the size of the list while
  235. /// preserving existing contents.
  236. /// </summary>
  237. private void OnResizeButtonClicked()
  238. {
  239. int size = guiSizeField.Value;
  240. IList newList = property.CreateListInstance(size);
  241. IList list = property.GetValue<IList>();
  242. int maxSize = MathEx.Min(size, list.Count);
  243. for (int i = 0; i < maxSize; i++)
  244. newList[i] = list[i];
  245. property.SetValue(newList);
  246. }
  247. /// <summary>
  248. /// Triggered when the user clicks on the delete button next to the list entry. Deletes an element in the list.
  249. /// </summary>
  250. /// <param name="index">Sequential index of the element in the list to remove.</param>
  251. private void OnDeleteButtonClicked(int index)
  252. {
  253. IList list = property.GetValue<IList>();
  254. if (index >= 0 && index < list.Count)
  255. list.RemoveAt(index);
  256. }
  257. /// <summary>
  258. /// Triggered when the user clicks on the clone button next to the list entry. Clones an element in the list and
  259. /// adds the clone to the back of the list.
  260. /// </summary>
  261. /// <param name="index">Sequential index of the element in the list to clone.</param>
  262. private void OnCloneButtonClicked(int index)
  263. {
  264. SerializableList serializableList = property.GetList();
  265. IList list = property.GetValue<IList>();
  266. int size = serializableList.GetLength() + 1;
  267. if (index >= 0 && index < list.Count)
  268. {
  269. list.Add(serializableList.GetProperty(index).GetValueCopy<object>());
  270. }
  271. }
  272. /// <summary>
  273. /// Triggered when the user clicks on the move up button next to the list entry. Moves an element from the current
  274. /// list index to the one right before it, if not at zero.
  275. /// </summary>
  276. /// <param name="index">Sequential index of the element in the list to move.</param>
  277. private void OnMoveUpButtonClicked(int index)
  278. {
  279. IList list = property.GetValue<IList>();
  280. if ((index - 1) >= 0)
  281. {
  282. object previousEntry = list[index - 1];
  283. list[index - 1] = list[index];
  284. list[index] = previousEntry;
  285. }
  286. }
  287. /// <summary>
  288. /// Triggered when the user clicks on the move down button next to the list entry. Moves an element from the current
  289. /// list index to the one right after it, if the element isn't already the last element.
  290. /// </summary>
  291. /// <param name="index">Sequential index of the element in the list to move.</param>
  292. private void OnMoveDownButtonClicked(int index)
  293. {
  294. IList list = property.GetValue<IList>();
  295. if ((index + 1) < list.Count)
  296. {
  297. object nextEntry = list[index + 1];
  298. list[index + 1] = list[index];
  299. list[index] = nextEntry;
  300. }
  301. }
  302. /// <summary>
  303. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new list with zero
  304. /// elements in the place of the current list.
  305. /// </summary>
  306. private void OnCreateButtonClicked()
  307. {
  308. property.SetValue(property.CreateListInstance(0));
  309. }
  310. /// <summary>
  311. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current list and sets
  312. /// the reference to the list in the parent object to null.
  313. /// </summary>
  314. private void OnClearButtonClicked()
  315. {
  316. property.SetValue<object>(null);
  317. }
  318. }
  319. }