GUIArray.cs 20 KB

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