GUIArray.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using BansheeEngine;
  8. namespace BansheeEditor
  9. {
  10. /// <summary>
  11. /// Base class for objects that display GUI for a modifyable list of elements. Elements can be added, removed and moved.
  12. /// </summary>
  13. public abstract class GUIListBase
  14. {
  15. private const int IndentAmount = 5;
  16. protected IList list;
  17. protected Type listType;
  18. protected List<GUIListRow> rows = new List<GUIListRow>();
  19. protected GUIIntField guiSizeField;
  20. protected GUILayoutX guiChildLayout;
  21. protected GUILayoutX guiTitleLayout;
  22. protected bool isExpanded;
  23. /// <summary>
  24. /// Constructs a new GUI list.
  25. /// </summary>
  26. protected GUIListBase()
  27. { }
  28. /// <summary>
  29. /// Constructs a new GUI list with the specified row types. Must be called right after the constructor.
  30. /// </summary>
  31. /// <typeparam name="T">Type of rows that are used to handle GUI for individual list elements.</typeparam>
  32. /// <param name="title">Label to display on the list GUI title.</param>
  33. /// <param name="list">Object containing the list data. Can be null.</param>
  34. /// <param name="listType">Type of the <paramref name="list"/> parameter. Needs to be specified in case that
  35. /// parameter is null.</param>
  36. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  37. protected void Construct<T>(LocString title, IList list, Type listType, GUILayout layout) where T : GUIListRow, new()
  38. {
  39. this.list = list;
  40. this.listType = listType;
  41. if (list == null)
  42. {
  43. guiChildLayout = null;
  44. guiTitleLayout = layout.AddLayoutX();
  45. guiTitleLayout.AddElement(new GUILabel(title));
  46. guiTitleLayout.AddElement(new GUILabel("Empty", GUIOption.FixedWidth(100)));
  47. GUIContent createIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Create));
  48. GUIButton createBtn = new GUIButton(createIcon, GUIOption.FixedWidth(30));
  49. createBtn.OnClick += OnCreateButtonClicked;
  50. guiTitleLayout.AddElement(createBtn);
  51. }
  52. else
  53. {
  54. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  55. guiFoldout.Value = isExpanded;
  56. guiFoldout.OnToggled += OnFoldoutToggled;
  57. guiSizeField = new GUIIntField("", GUIOption.FixedWidth(50));
  58. guiSizeField.SetRange(0, int.MaxValue);
  59. GUIContent resizeIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Resize));
  60. GUIButton guiResizeBtn = new GUIButton(resizeIcon, GUIOption.FixedWidth(30));
  61. guiResizeBtn.OnClick += OnResizeButtonClicked;
  62. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clear));
  63. GUIButton guiClearBtn = new GUIButton(clearIcon, GUIOption.FixedWidth(30));
  64. guiClearBtn.OnClick += OnClearButtonClicked;
  65. guiTitleLayout = layout.AddLayoutX();
  66. guiTitleLayout.AddElement(guiFoldout);
  67. guiTitleLayout.AddElement(guiSizeField);
  68. guiTitleLayout.AddElement(guiResizeBtn);
  69. guiTitleLayout.AddElement(guiClearBtn);
  70. guiSizeField.Value = list.Count;
  71. guiChildLayout = layout.AddLayoutX();
  72. guiChildLayout.AddSpace(IndentAmount);
  73. guiChildLayout.Visible = isExpanded;
  74. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  75. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  76. guiIndentLayoutX.AddSpace(IndentAmount);
  77. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  78. guiIndentLayoutY.AddSpace(IndentAmount);
  79. GUILayoutY guiContentLayout = guiIndentLayoutY.AddLayoutY();
  80. guiIndentLayoutY.AddSpace(IndentAmount);
  81. guiIndentLayoutX.AddSpace(IndentAmount);
  82. guiChildLayout.AddSpace(IndentAmount);
  83. GUIPanel backgroundPanel = guiContentPanel.AddPanel(Inspector.START_BACKGROUND_DEPTH);
  84. GUITexture inspectorContentBg = new GUITexture(null, EditorStyles.InspectorContentBg);
  85. backgroundPanel.AddElement(inspectorContentBg);
  86. for (int i = 0; i < list.Count; i++)
  87. {
  88. GUIListRow newRow = new T();
  89. newRow.Update(this, guiContentLayout, i);
  90. rows.Add(newRow);
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// Gets a value of an element at the specified index in the list.
  96. /// </summary>
  97. /// <param name="seqIndex">Sequential index of the element whose value to retrieve.</param>
  98. /// <returns>Value of the list element at the specified index.</returns>
  99. protected internal abstract object GetValue(int seqIndex);
  100. /// <summary>
  101. /// Sets a value of an element at the specified index in the list.
  102. /// </summary>
  103. /// <param name="seqIndex">Sequential index of the element whose value to set.</param>
  104. /// <param name="value">Value to assign to the element. Caller must ensure it is of valid type.</param>
  105. protected internal abstract void SetValue(int seqIndex, object value);
  106. /// <summary>
  107. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  108. /// </summary>
  109. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  110. private void OnFoldoutToggled(bool expanded)
  111. {
  112. isExpanded = expanded;
  113. if (guiChildLayout != null)
  114. guiChildLayout.Visible = isExpanded;
  115. }
  116. /// <summary>
  117. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new list with zero
  118. /// elements in the place of the current list.
  119. /// </summary>
  120. protected abstract void OnCreateButtonClicked();
  121. /// <summary>
  122. /// Triggered when the user clicks on the resize button on the title bar. Changes the size of the list while
  123. /// preserving existing contents.
  124. /// </summary>
  125. protected abstract void OnResizeButtonClicked();
  126. /// <summary>
  127. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current list object.
  128. /// </summary>
  129. protected abstract void OnClearButtonClicked();
  130. /// <summary>
  131. /// Triggered when the user clicks on the delete button next to the list entry. Deletes an element in the list.
  132. /// </summary>
  133. /// <param name="index">Sequential index of the element in the list to remove.</param>
  134. protected internal abstract void OnDeleteButtonClicked(int index);
  135. /// <summary>
  136. /// Triggered when the user clicks on the clone button next to the list entry. Clones an element in the list and
  137. /// adds the clone to the back of the list. Non-value types must implement the <see cref="ICloneable"/> interface
  138. /// in order to be cloned. If it doesn't the clone will point to a null reference.
  139. /// </summary>
  140. /// <param name="index">Sequential index of the element in the list to clone.</param>
  141. protected internal abstract void OnCloneButtonClicked(int index);
  142. /// <summary>
  143. /// Triggered when the user clicks on the move up button next to the list entry. Moves an element from the current
  144. /// list index to the one right before it, if not at zero.
  145. /// </summary>
  146. /// <param name="index">Sequential index of the element in the list to move.</param>
  147. protected internal abstract void OnMoveUpButtonClicked(int index);
  148. /// <summary>
  149. /// Triggered when the user clicks on the move down button next to the list entry. Moves an element from the current
  150. /// list index to the one right after it, if the element isn't already the last element.
  151. /// </summary>
  152. /// <param name="index">Sequential index of the element in the list to move.</param>
  153. protected internal abstract void OnMoveDownButtonClicked(int index);
  154. }
  155. /// <summary>
  156. /// Creates GUI elements that allow viewing and manipulation of a <see cref="System.Array"/>. When constructing the
  157. /// object user can provide a custom type that manages GUI for individual array elements.
  158. /// </summary>
  159. public class GUIArray : GUIListBase
  160. {
  161. /// <summary>
  162. /// Triggered when the reference array has been changed. This does not include changes that only happen to its
  163. /// internal elements.
  164. /// </summary>
  165. public Action<Array> OnChanged;
  166. /// <summary>
  167. /// Triggered when an element in the array has been changed.
  168. /// </summary>
  169. public Action OnValueChanged;
  170. /// <summary>
  171. /// Array object whose contents are displayed.
  172. /// </summary>
  173. public Array Array { get { return (Array)list; } }
  174. /// <summary>
  175. /// Constructs a new GUI array.
  176. /// </summary>
  177. private GUIArray()
  178. { }
  179. /// <summary>
  180. /// Creates a new GUI array.
  181. /// </summary>
  182. /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual list elements.</typeparam>
  183. /// <typeparam name="ElementType">Type of elements stored in the array.</typeparam>
  184. /// <param name="title">Label to display on the list GUI title.</param>
  185. /// <param name="array">Object containing the list data. Cannot be null.</param>
  186. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  187. public static GUIArray Create<RowType, ElementType>(LocString title, ElementType[] array, GUILayout layout)
  188. where RowType : GUIListRow, new()
  189. {
  190. GUIArray newArray = new GUIArray();
  191. newArray.Construct<RowType>(title, array, typeof(ElementType[]), layout);
  192. return newArray;
  193. }
  194. /// <summary>
  195. /// Refreshes contents of all array rows and checks if anything was modified.
  196. /// </summary>
  197. /// <returns>True if any entry in the array was modified, false otherwise.</returns>
  198. public bool Refresh()
  199. {
  200. bool anythingModified = false;
  201. for (int i = 0; i < rows.Count; i++)
  202. anythingModified |= rows[i].Refresh();
  203. return anythingModified;
  204. }
  205. /// <summary>
  206. /// Destroys the GUI elements.
  207. /// </summary>
  208. public void Destroy()
  209. {
  210. if (guiTitleLayout != null)
  211. {
  212. guiTitleLayout.Destroy();
  213. guiTitleLayout = null;
  214. }
  215. if (guiChildLayout != null)
  216. {
  217. guiChildLayout.Destroy();
  218. guiChildLayout = null;
  219. }
  220. for (int i = 0; i < rows.Count; i++)
  221. rows[i].Destroy();
  222. rows.Clear();
  223. }
  224. /// <inheritdoc/>
  225. protected override void OnCreateButtonClicked()
  226. {
  227. list = Array.CreateInstance(listType.GetElementType(), 0);
  228. if (OnChanged != null)
  229. OnChanged((Array)list);
  230. }
  231. /// <inheritdoc/>
  232. protected override void OnResizeButtonClicked()
  233. {
  234. int size = guiSizeField.Value;
  235. Array newArray = Array.CreateInstance(listType.GetElementType(), size);
  236. int maxSize = MathEx.Min(size, list.Count);
  237. for (int i = 0; i < maxSize; i++)
  238. newArray.SetValue(list[i], i);
  239. list = newArray;
  240. if(OnChanged != null)
  241. OnChanged((Array)list);
  242. }
  243. /// <inheritdoc/>
  244. protected override void OnClearButtonClicked()
  245. {
  246. list = null;
  247. if (OnChanged != null)
  248. OnChanged((Array)list);
  249. }
  250. /// <inheritdoc/>
  251. protected internal override object GetValue(int seqIndex)
  252. {
  253. return list[seqIndex];
  254. }
  255. /// <inheritdoc/>
  256. protected internal override void SetValue(int seqIndex, object value)
  257. {
  258. list[seqIndex] = value;
  259. if (OnValueChanged != null)
  260. OnValueChanged();
  261. }
  262. /// <inheritdoc/>
  263. protected internal override void OnDeleteButtonClicked(int index)
  264. {
  265. int size = MathEx.Max(0, list.Count - 1);
  266. Array newArray = Array.CreateInstance(listType.GetElementType(), size);
  267. int destIdx = 0;
  268. for (int i = 0; i < list.Count; i++)
  269. {
  270. if (i == index)
  271. continue;
  272. newArray.SetValue(list[i], destIdx);
  273. destIdx++;
  274. }
  275. list = newArray;
  276. if (OnChanged != null)
  277. OnChanged((Array)list);
  278. }
  279. /// <inheritdoc/>
  280. protected internal override void OnCloneButtonClicked(int index)
  281. {
  282. int size = list.Count + 1;
  283. Array newArray = Array.CreateInstance(listType.GetElementType(), size);
  284. object clonedEntry = null;
  285. for (int i = 0; i < list.Count; i++)
  286. {
  287. object value = list[i];
  288. newArray.SetValue(value, i);
  289. if (i == index)
  290. {
  291. if (value == null)
  292. clonedEntry = null;
  293. else
  294. {
  295. ValueType valueType = value as ValueType;
  296. if (valueType != null)
  297. clonedEntry = valueType;
  298. else
  299. {
  300. ICloneable cloneable = value as ICloneable;
  301. if (cloneable != null)
  302. clonedEntry = cloneable.Clone();
  303. else
  304. clonedEntry = null;
  305. }
  306. }
  307. }
  308. }
  309. newArray.SetValue(clonedEntry, size - 1);
  310. list = newArray;
  311. if (OnChanged != null)
  312. OnChanged((Array)list);
  313. }
  314. /// <inheritdoc/>
  315. protected internal override void OnMoveUpButtonClicked(int index)
  316. {
  317. if ((index - 1) >= 0)
  318. {
  319. object previousEntry = list[index - 1];
  320. list[index - 1] = list[index];
  321. list[index] = previousEntry;
  322. if (OnValueChanged != null)
  323. OnValueChanged();
  324. }
  325. }
  326. /// <inheritdoc/>
  327. protected internal override void OnMoveDownButtonClicked(int index)
  328. {
  329. if ((index + 1) < list.Count)
  330. {
  331. object nextEntry = list[index + 1];
  332. list[index + 1] = list[index];
  333. list[index] = nextEntry;
  334. if (OnValueChanged != null)
  335. OnValueChanged();
  336. }
  337. }
  338. }
  339. /// <summary>
  340. /// Contains GUI elements for a single entry in a list.
  341. /// </summary>
  342. public abstract class GUIListRow
  343. {
  344. private GUILayoutX rowLayout;
  345. private GUIListBase parent;
  346. protected int seqIndex;
  347. /// <summary>
  348. /// Constructs a new array row object.
  349. /// </summary>
  350. protected GUIListRow()
  351. {
  352. }
  353. /// <summary>
  354. /// Recreates all row GUI elements.
  355. /// </summary>
  356. /// <param name="parent">Parent array GUI object that the entry is contained in.</param>
  357. /// <param name="parentLayout">Parent layout that row GUI elements will be added to.</param>
  358. /// <param name="seqIndex">Sequential index of the array entry.</param>
  359. public void Update(GUIListBase parent, GUILayout parentLayout, int seqIndex)
  360. {
  361. this.parent = parent;
  362. this.seqIndex = seqIndex;
  363. if (rowLayout != null)
  364. {
  365. rowLayout.Destroy();
  366. rowLayout = null;
  367. }
  368. rowLayout = parentLayout.AddLayoutX();
  369. GUILayoutY contentLayout = rowLayout.AddLayoutY();
  370. GUILayoutX titleLayout = CreateGUI(contentLayout);
  371. if (titleLayout == null)
  372. {
  373. GUILayoutY buttonCenter = rowLayout.AddLayoutY();
  374. buttonCenter.AddFlexibleSpace();
  375. titleLayout = buttonCenter.AddLayoutX();
  376. buttonCenter.AddFlexibleSpace();
  377. }
  378. GUIContent cloneIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clone));
  379. GUIContent deleteIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Delete));
  380. GUIContent moveUp = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveUp));
  381. GUIContent moveDown = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveDown));
  382. GUIButton cloneBtn = new GUIButton(cloneIcon, GUIOption.FixedWidth(30));
  383. GUIButton deleteBtn = new GUIButton(deleteIcon, GUIOption.FixedWidth(30));
  384. GUIButton moveUpBtn = new GUIButton(moveUp, GUIOption.FixedWidth(30));
  385. GUIButton moveDownBtn = new GUIButton(moveDown, GUIOption.FixedWidth(30));
  386. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(seqIndex);
  387. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(seqIndex);
  388. moveUpBtn.OnClick += () => parent.OnMoveUpButtonClicked(seqIndex);
  389. moveDownBtn.OnClick += () => parent.OnMoveDownButtonClicked(seqIndex);
  390. titleLayout.AddElement(cloneBtn);
  391. titleLayout.AddElement(deleteBtn);
  392. titleLayout.AddElement(moveUpBtn);
  393. titleLayout.AddElement(moveDownBtn);
  394. }
  395. /// <summary>
  396. /// Creates GUI elements specific to type in the array row.
  397. /// </summary>
  398. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  399. /// <returns>An optional title bar layout that the standard array buttons will be appended to.</returns>
  400. protected abstract GUILayoutX CreateGUI(GUILayoutY layout);
  401. /// <summary>
  402. /// Refreshes the GUI for the list row and checks if anything was modified.
  403. /// </summary>
  404. /// <returns>True if any modifications were made, false otherwise.</returns>
  405. internal protected virtual bool Refresh()
  406. {
  407. return false;
  408. }
  409. /// <summary>
  410. /// Gets the value contained in this list row.
  411. /// </summary>
  412. /// <typeparam name="T">Type of the value. Must match the list's element type.</typeparam>
  413. /// <returns>Value in this list row.</returns>
  414. protected T GetValue<T>()
  415. {
  416. return (T)parent.GetValue(seqIndex);
  417. }
  418. /// <summary>
  419. /// Sets the value contained in this list row.
  420. /// </summary>
  421. /// <typeparam name="T">Type of the value. Must match the list's element type.</typeparam>
  422. /// <param name="value">Value to set.</param>
  423. protected void SetValue<T>(T value)
  424. {
  425. parent.SetValue(seqIndex, value);
  426. }
  427. /// <summary>
  428. /// Destroys all row GUI elements.
  429. /// </summary>
  430. public void Destroy()
  431. {
  432. rowLayout.Destroy();
  433. }
  434. }
  435. }