GUIArray.cs 19 KB

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