GUIArray.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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, an empty object is created instead of a clone.
  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. /// Array object whose contents are displayed.
  168. /// </summary>
  169. public Array Array { get { return (Array)list; } }
  170. /// <summary>
  171. /// Constructs a new GUI array.
  172. /// </summary>
  173. private GUIArray()
  174. { }
  175. /// <summary>
  176. /// Creates a new GUI array.
  177. /// </summary>
  178. /// <typeparam name="T">Type of rows that are used to handle GUI for individual list elements.</typeparam>
  179. /// <param name="title">Label to display on the list GUI title.</param>
  180. /// <param name="array">Object containing the list data. Cannot be null.</param>
  181. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  182. public static GUIArray Create<T>(LocString title, Array array, GUILayout layout) where T : GUIListRow, new()
  183. {
  184. GUIArray newArray = new GUIArray();
  185. newArray.Construct<T>(title, array, array.GetType(), layout);
  186. return newArray;
  187. }
  188. /// <summary>
  189. /// Creates a new GUI array with an initially empty array.
  190. /// </summary>
  191. /// <typeparam name="T">Type of rows that are used to handle GUI for individual list elements.</typeparam>
  192. /// <param name="title">Label to display on the list GUI title.</param>
  193. /// <param name="arrayType">Type of the array to create GUI for. Must be of <see cref="System.Array"/> type.</param>
  194. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  195. public static GUIArray Create<T>(LocString title, Type arrayType, GUILayout layout) where T : GUIListRow, new()
  196. {
  197. GUIArray newArray = new GUIArray();
  198. newArray.Construct<T>(title, null, arrayType, layout);
  199. return newArray;
  200. }
  201. /// <summary>
  202. /// Refreshes contents of all array rows and checks if anything was modified.
  203. /// </summary>
  204. /// <returns>True if any entry in the array was modified, false otherwise.</returns>
  205. public bool Refresh()
  206. {
  207. bool anythingModified = false;
  208. for (int i = 0; i < rows.Count; i++)
  209. anythingModified |= rows[i].Refresh();
  210. return anythingModified;
  211. }
  212. /// <summary>
  213. /// Destroys the GUI elements.
  214. /// </summary>
  215. public void Destroy()
  216. {
  217. if (guiTitleLayout != null)
  218. {
  219. guiTitleLayout.Destroy();
  220. guiTitleLayout = null;
  221. }
  222. if (guiChildLayout != null)
  223. {
  224. guiChildLayout.Destroy();
  225. guiChildLayout = null;
  226. }
  227. for (int i = 0; i < rows.Count; i++)
  228. rows[i].Destroy();
  229. rows.Clear();
  230. }
  231. /// <inheritdoc/>
  232. protected override void OnCreateButtonClicked()
  233. {
  234. list = Array.CreateInstance(listType.GetElementType(), 0);
  235. OnChanged((Array)list);
  236. }
  237. /// <inheritdoc/>
  238. protected override void OnResizeButtonClicked()
  239. {
  240. int size = guiSizeField.Value;
  241. Array newArray = Array.CreateInstance(listType.GetElementType(), size);
  242. int maxSize = MathEx.Min(size, list.Count);
  243. for (int i = 0; i < maxSize; i++)
  244. newArray.SetValue(list[i], i);
  245. list = newArray;
  246. OnChanged((Array)list);
  247. }
  248. /// <inheritdoc/>
  249. protected override void OnClearButtonClicked()
  250. {
  251. list = null;
  252. OnChanged((Array)list);
  253. }
  254. /// <inheritdoc/>
  255. protected internal override object GetValue(int seqIndex)
  256. {
  257. return list[seqIndex];
  258. }
  259. /// <inheritdoc/>
  260. protected internal override void SetValue(int seqIndex, object value)
  261. {
  262. list[seqIndex] = value;
  263. }
  264. /// <inheritdoc/>
  265. protected internal override void OnDeleteButtonClicked(int index)
  266. {
  267. int size = MathEx.Max(0, list.Count - 1);
  268. Array newArray = Array.CreateInstance(listType.GetElementType(), size);
  269. int destIdx = 0;
  270. for (int i = 0; i < list.Count; i++)
  271. {
  272. if (i == index)
  273. continue;
  274. newArray.SetValue(list[i], destIdx);
  275. destIdx++;
  276. }
  277. list = newArray;
  278. OnChanged((Array)list);
  279. }
  280. /// <inheritdoc/>
  281. protected internal override void OnCloneButtonClicked(int index)
  282. {
  283. int size = list.Count + 1;
  284. Array newArray = Array.CreateInstance(listType.GetElementType(), size);
  285. object clonedEntry = null;
  286. for (int i = 0; i < list.Count; i++)
  287. {
  288. object value = list[i];
  289. newArray.SetValue(value, i);
  290. if (i == index)
  291. {
  292. if (value == null)
  293. clonedEntry = null;
  294. else
  295. {
  296. ValueType valueType = value as ValueType;
  297. if (valueType != null)
  298. clonedEntry = valueType;
  299. else
  300. {
  301. ICloneable cloneable = value as ICloneable;
  302. if (cloneable != null)
  303. clonedEntry = cloneable.Clone();
  304. else
  305. clonedEntry = Activator.CreateInstance(value.GetType());
  306. }
  307. }
  308. }
  309. }
  310. newArray.SetValue(clonedEntry, size - 1);
  311. list = newArray;
  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. }
  323. }
  324. /// <inheritdoc/>
  325. protected internal override void OnMoveDownButtonClicked(int index)
  326. {
  327. if ((index + 1) < list.Count)
  328. {
  329. object nextEntry = list[index + 1];
  330. list[index + 1] = list[index];
  331. list[index] = nextEntry;
  332. }
  333. }
  334. }
  335. /// <summary>
  336. /// Contains GUI elements for a single entry in a list.
  337. /// </summary>
  338. public abstract class GUIListRow
  339. {
  340. private GUILayoutX rowLayout;
  341. private GUIListBase parent;
  342. protected int seqIndex;
  343. /// <summary>
  344. /// Constructs a new array row object.
  345. /// </summary>
  346. protected GUIListRow()
  347. {
  348. }
  349. /// <summary>
  350. /// Recreates all row GUI elements.
  351. /// </summary>
  352. /// <param name="parent">Parent array GUI object that the entry is contained in.</param>
  353. /// <param name="parentLayout">Parent layout that row GUI elements will be added to.</param>
  354. /// <param name="seqIndex">Sequential index of the array entry.</param>
  355. public void Update(GUIListBase parent, GUILayout parentLayout, int seqIndex)
  356. {
  357. this.parent = parent;
  358. this.seqIndex = seqIndex;
  359. if (rowLayout != null)
  360. {
  361. rowLayout.Destroy();
  362. rowLayout = null;
  363. }
  364. rowLayout = parentLayout.AddLayoutX();
  365. GUILayoutY contentLayout = rowLayout.AddLayoutY();
  366. GUILayoutX titleLayout = CreateGUI(contentLayout);
  367. if (titleLayout == null)
  368. {
  369. GUILayoutY buttonCenter = rowLayout.AddLayoutY();
  370. buttonCenter.AddFlexibleSpace();
  371. titleLayout = buttonCenter.AddLayoutX();
  372. buttonCenter.AddFlexibleSpace();
  373. }
  374. GUIContent cloneIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clone));
  375. GUIContent deleteIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Delete));
  376. GUIContent moveUp = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveUp));
  377. GUIContent moveDown = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveDown));
  378. GUIButton cloneBtn = new GUIButton(cloneIcon, GUIOption.FixedWidth(30));
  379. GUIButton deleteBtn = new GUIButton(deleteIcon, GUIOption.FixedWidth(30));
  380. GUIButton moveUpBtn = new GUIButton(moveUp, GUIOption.FixedWidth(30));
  381. GUIButton moveDownBtn = new GUIButton(moveDown, GUIOption.FixedWidth(30));
  382. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(seqIndex);
  383. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(seqIndex);
  384. moveUpBtn.OnClick += () => parent.OnMoveUpButtonClicked(seqIndex);
  385. moveDownBtn.OnClick += () => parent.OnMoveDownButtonClicked(seqIndex);
  386. titleLayout.AddElement(cloneBtn);
  387. titleLayout.AddElement(deleteBtn);
  388. titleLayout.AddElement(moveUpBtn);
  389. titleLayout.AddElement(moveDownBtn);
  390. }
  391. /// <summary>
  392. /// Creates GUI elements specific to type in the array row.
  393. /// </summary>
  394. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  395. /// <returns>An optional title bar layout that the standard array buttons will be appended to.</returns>
  396. protected abstract GUILayoutX CreateGUI(GUILayoutY layout);
  397. /// <summary>
  398. /// Refreshes the GUI for the list row and checks if anything was modified.
  399. /// </summary>
  400. /// <returns>True if any modifications were made, false otherwise.</returns>
  401. internal protected virtual bool Refresh()
  402. {
  403. return false;
  404. }
  405. /// <summary>
  406. /// Gets the value contained in this list row.
  407. /// </summary>
  408. /// <typeparam name="T">Type of the value. Must match the list's element type.</typeparam>
  409. /// <returns>Value in this list row.</returns>
  410. protected T GetValue<T>()
  411. {
  412. return (T)parent.GetValue(seqIndex);
  413. }
  414. /// <summary>
  415. /// Sets the value contained in this list row.
  416. /// </summary>
  417. /// <typeparam name="T">Type of the value. Must match the list's element type.</typeparam>
  418. /// <param name="value">Value to set.</param>
  419. protected void SetValue<T>(T value)
  420. {
  421. parent.SetValue(seqIndex, value);
  422. }
  423. /// <summary>
  424. /// Destroys all row GUI elements.
  425. /// </summary>
  426. public void Destroy()
  427. {
  428. rowLayout.Destroy();
  429. }
  430. }
  431. }