GUIListField.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. /** @addtogroup GUI-Editor
  10. * @{
  11. */
  12. /// <summary>
  13. /// Base class for objects that display GUI for a modifyable list of elements. Elements can be added, removed and moved.
  14. /// </summary>
  15. public abstract class GUIListFieldBase
  16. {
  17. private const int IndentAmount = 5;
  18. protected List<GUIListFieldRow> rows = new List<GUIListFieldRow>();
  19. protected GUILayoutY guiLayout;
  20. protected GUIIntField guiSizeField;
  21. protected GUILayoutX guiChildLayout;
  22. protected GUILayoutX guiTitleLayout;
  23. protected GUILayoutX guiInternalTitleLayout;
  24. protected GUILayoutY guiContentLayout;
  25. protected GUIToggle guiFoldout;
  26. protected bool isExpanded;
  27. protected int depth;
  28. protected LocString title;
  29. private State state;
  30. private bool isModified;
  31. /// <summary>
  32. /// Expands or collapses the entries of the dictionary.
  33. /// </summary>
  34. public bool IsExpanded
  35. {
  36. get { return isExpanded; }
  37. set
  38. {
  39. if (isExpanded != value)
  40. ToggleFoldout(value, true);
  41. }
  42. }
  43. /// <summary>
  44. /// Event that triggers when the list foldout is expanded or collapsed (rows are shown or hidden).
  45. /// </summary>
  46. public Action<bool> OnExpand;
  47. /// <summary>
  48. /// Constructs a new GUI list.
  49. /// </summary>
  50. /// <param name="title">Label to display on the list GUI title.</param>
  51. /// <param name="layout">Layout to which to append the array GUI elements to.</param>
  52. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  53. /// nested containers whose backgrounds are overlaping. Also determines background style,
  54. /// depths divisible by two will use an alternate style.</param>
  55. protected GUIListFieldBase(LocString title, GUILayout layout, int depth)
  56. {
  57. this.title = title;
  58. this.depth = depth;
  59. guiLayout = layout.AddLayoutY();
  60. guiTitleLayout = guiLayout.AddLayoutX();
  61. }
  62. /// <summary>
  63. /// (Re)builds the list GUI elements. Must be called at least once in order for the contents to be populated.
  64. /// </summary>
  65. public void BuildGUI()
  66. {
  67. UpdateHeaderGUI();
  68. if (!IsNull())
  69. {
  70. // Hidden dependency: Initialize must be called after all elements are
  71. // in the dictionary so we do it in two steps
  72. int numRows = GetNumRows();
  73. int oldNumRows = rows.Count;
  74. for (int i = oldNumRows; i < numRows; i++)
  75. {
  76. GUIListFieldRow newRow = CreateRow();
  77. rows.Add(newRow);
  78. }
  79. for (int i = oldNumRows - 1; i >= numRows; i--)
  80. {
  81. rows[i].Destroy();
  82. rows.RemoveAt(i);
  83. }
  84. for (int i = oldNumRows; i < numRows; i++)
  85. rows[i].Initialize(this, guiContentLayout, i, depth + 1);
  86. for (int i = 0; i < rows.Count; i++)
  87. rows[i].SetIndex(i);
  88. }
  89. else
  90. {
  91. foreach (var row in rows)
  92. row.Destroy();
  93. rows.Clear();
  94. }
  95. }
  96. /// <summary>
  97. /// Rebuilds the GUI list header if needed.
  98. /// </summary>
  99. protected void UpdateHeaderGUI()
  100. {
  101. Action BuildEmptyGUI = () =>
  102. {
  103. guiInternalTitleLayout = guiTitleLayout.InsertLayoutX(0);
  104. guiInternalTitleLayout.AddElement(new GUILabel(title));
  105. guiInternalTitleLayout.AddElement(new GUILabel("Empty", GUIOption.FixedWidth(100)));
  106. GUIContent createIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Create),
  107. new LocEdString("Create"));
  108. GUIButton createBtn = new GUIButton(createIcon, GUIOption.FixedWidth(30));
  109. createBtn.OnClick += OnCreateButtonClicked;
  110. guiInternalTitleLayout.AddElement(createBtn);
  111. };
  112. Action BuildFilledGUI = () =>
  113. {
  114. guiInternalTitleLayout = guiTitleLayout.InsertLayoutX(0);
  115. guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  116. guiFoldout.Value = isExpanded;
  117. guiFoldout.OnToggled += x => ToggleFoldout(x, false);
  118. guiSizeField = new GUIIntField("", GUIOption.FixedWidth(50));
  119. guiSizeField.SetRange(0, int.MaxValue);
  120. GUIContent resizeIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Resize),
  121. new LocEdString("Resize"));
  122. GUIButton guiResizeBtn = new GUIButton(resizeIcon, GUIOption.FixedWidth(30));
  123. guiResizeBtn.OnClick += OnResizeButtonClicked;
  124. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clear),
  125. new LocEdString("Clear"));
  126. GUIButton guiClearBtn = new GUIButton(clearIcon, GUIOption.FixedWidth(30));
  127. guiClearBtn.OnClick += OnClearButtonClicked;
  128. guiInternalTitleLayout.AddElement(guiFoldout);
  129. guiInternalTitleLayout.AddElement(guiSizeField);
  130. guiInternalTitleLayout.AddElement(guiResizeBtn);
  131. guiInternalTitleLayout.AddElement(guiClearBtn);
  132. guiSizeField.Value = GetNumRows();
  133. guiChildLayout = guiLayout.AddLayoutX();
  134. guiChildLayout.AddSpace(IndentAmount);
  135. guiChildLayout.Active = isExpanded;
  136. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  137. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  138. guiIndentLayoutX.AddSpace(IndentAmount);
  139. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  140. guiIndentLayoutY.AddSpace(IndentAmount);
  141. guiContentLayout = guiIndentLayoutY.AddLayoutY();
  142. guiIndentLayoutY.AddSpace(IndentAmount);
  143. guiIndentLayoutX.AddSpace(IndentAmount);
  144. guiChildLayout.AddSpace(IndentAmount);
  145. short backgroundDepth = (short)(Inspector.START_BACKGROUND_DEPTH - depth - 1);
  146. string bgPanelStyle = depth % 2 == 0
  147. ? EditorStylesInternal.InspectorContentBgAlternate
  148. : EditorStylesInternal.InspectorContentBg;
  149. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  150. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  151. backgroundPanel.AddElement(inspectorContentBg);
  152. };
  153. if (state == State.None)
  154. {
  155. if (!IsNull())
  156. {
  157. BuildFilledGUI();
  158. state = State.Filled;
  159. }
  160. else
  161. {
  162. BuildEmptyGUI();
  163. state = State.Empty;
  164. }
  165. }
  166. else if (state == State.Empty)
  167. {
  168. if (!IsNull())
  169. {
  170. guiInternalTitleLayout.Destroy();
  171. BuildFilledGUI();
  172. state = State.Filled;
  173. }
  174. }
  175. else if (state == State.Filled)
  176. {
  177. if (IsNull())
  178. {
  179. guiInternalTitleLayout.Destroy();
  180. guiChildLayout.Destroy();
  181. BuildEmptyGUI();
  182. state = State.Empty;
  183. }
  184. }
  185. }
  186. /// <summary>
  187. /// Returns the layout that is used for positioning the elements in the title bar.
  188. /// </summary>
  189. /// <returns>Horizontal layout for positioning the title bar elements.</returns>
  190. public GUILayoutX GetTitleLayout()
  191. {
  192. return guiTitleLayout;
  193. }
  194. /// <summary>
  195. /// Refreshes contents of all list rows and checks if anything was modified.
  196. /// </summary>
  197. /// <returns>State representing was anything modified between two last calls to <see cref="Refresh"/>.</returns>
  198. public virtual InspectableState Refresh()
  199. {
  200. InspectableState state = InspectableState.NotModified;
  201. for (int i = 0; i < rows.Count; i++)
  202. state |= rows[i].Refresh();
  203. if (isModified)
  204. {
  205. state |= InspectableState.Modified;
  206. isModified = false;
  207. }
  208. return state;
  209. }
  210. /// <summary>
  211. /// Destroys the GUI elements.
  212. /// </summary>
  213. public void Destroy()
  214. {
  215. if (guiTitleLayout != null)
  216. {
  217. guiTitleLayout.Destroy();
  218. guiTitleLayout = null;
  219. }
  220. if (guiChildLayout != null)
  221. {
  222. guiChildLayout.Destroy();
  223. guiChildLayout = null;
  224. }
  225. for (int i = 0; i < rows.Count; i++)
  226. rows[i].Destroy();
  227. rows.Clear();
  228. }
  229. /// <summary>
  230. /// Creates a new list row GUI.
  231. /// </summary>
  232. /// <returns>Object containing the list row GUI.</returns>
  233. protected abstract GUIListFieldRow CreateRow();
  234. /// <summary>
  235. /// Checks is the list instance not assigned.
  236. /// </summary>
  237. /// <returns>True if there is not a list instance.</returns>
  238. protected abstract bool IsNull();
  239. /// <summary>
  240. /// Returns the number of rows in the list.
  241. /// </summary>
  242. /// <returns>Number of rows in the list.</returns>
  243. protected abstract int GetNumRows();
  244. /// <summary>
  245. /// Gets a value of an element at the specified index in the list.
  246. /// </summary>
  247. /// <param name="seqIndex">Sequential index of the element whose value to retrieve.</param>
  248. /// <returns>Value of the list element at the specified index.</returns>
  249. protected internal abstract object GetValue(int seqIndex);
  250. /// <summary>
  251. /// Sets a value of an element at the specified index in the list.
  252. /// </summary>
  253. /// <param name="seqIndex">Sequential index of the element whose value to set.</param>
  254. /// <param name="value">Value to assign to the element. Caller must ensure it is of valid type.</param>
  255. protected internal abstract void SetValue(int seqIndex, object value);
  256. /// <summary>
  257. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  258. /// </summary>
  259. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  260. /// <param name="external">True if the foldout was expanded/collapsed from outside code.</param>
  261. private void ToggleFoldout(bool expanded, bool external)
  262. {
  263. isExpanded = expanded;
  264. if (guiChildLayout != null)
  265. guiChildLayout.Active = isExpanded;
  266. if (external)
  267. {
  268. if (guiFoldout != null)
  269. guiFoldout.Value = isExpanded;
  270. }
  271. if (OnExpand != null)
  272. OnExpand(expanded);
  273. }
  274. /// <summary>
  275. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new list with zero
  276. /// elements in the place of the current list.
  277. /// </summary>
  278. protected void OnCreateButtonClicked()
  279. {
  280. CreateList();
  281. BuildGUI();
  282. isModified = true;
  283. }
  284. /// <summary>
  285. /// Triggered when the user clicks on the resize button on the title bar. Changes the size of the list while
  286. /// preserving existing contents.
  287. /// </summary>
  288. protected void OnResizeButtonClicked()
  289. {
  290. ResizeList();
  291. BuildGUI();
  292. isModified = true;
  293. }
  294. /// <summary>
  295. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current list object.
  296. /// </summary>
  297. protected void OnClearButtonClicked()
  298. {
  299. ClearList();
  300. BuildGUI();
  301. isModified = true;
  302. }
  303. /// <summary>
  304. /// Triggered when the user clicks on the delete button next to a list entry. Deletes an element in the list.
  305. /// </summary>
  306. /// <param name="index">Sequential index of the element in the list to remove.</param>
  307. protected internal void OnDeleteButtonClicked(int index)
  308. {
  309. DeleteElement(index);
  310. guiSizeField.Value = GetNumRows();
  311. BuildGUI();
  312. isModified = true;
  313. }
  314. /// <summary>
  315. /// Triggered when the user clicks on the clone button next to a list entry. Clones the element and adds the clone
  316. /// to the back of the list.
  317. /// </summary>
  318. /// <param name="index">Sequential index of the element in the list to clone.</param>
  319. protected internal void OnCloneButtonClicked(int index)
  320. {
  321. CloneElement(index);
  322. guiSizeField.Value = GetNumRows();
  323. BuildGUI();
  324. isModified = true;
  325. }
  326. /// <summary>
  327. /// Triggered when the user clicks on the move up button next to a list entry. Moves an element from the current
  328. /// list index to the one right before it, if not at zero.
  329. /// </summary>
  330. /// <param name="index">Sequential index of the element in the list to move.</param>
  331. protected internal void OnMoveUpButtonClicked(int index)
  332. {
  333. MoveUpElement(index);
  334. BuildGUI();
  335. isModified = true;
  336. }
  337. /// <summary>
  338. /// Triggered when the user clicks on the move down button next to a list entry. Moves an element from the current
  339. /// list index to the one right after it, if the element isn't already the last element.
  340. /// </summary>
  341. /// <param name="index">Sequential index of the element in the list to move.</param>
  342. protected internal void OnMoveDownButtonClicked(int index)
  343. {
  344. MoveDownElement(index);
  345. BuildGUI();
  346. isModified = true;
  347. }
  348. /// <summary>
  349. /// Creates a brand new list with zero elements in the place of the current list.
  350. /// </summary>
  351. protected abstract void CreateList();
  352. /// <summary>
  353. /// Changes the size of the list while preserving existing contents.
  354. /// </summary>
  355. protected abstract void ResizeList();
  356. /// <summary>
  357. /// Deletes the current list object.
  358. /// </summary>
  359. protected abstract void ClearList();
  360. /// <summary>
  361. /// Deletes an element in the list.
  362. /// </summary>
  363. /// <param name="index">Sequential index of the element in the list to remove.</param>
  364. protected internal abstract void DeleteElement(int index);
  365. /// <summary>
  366. /// Clones the element and adds the clone to the back of the list.
  367. /// </summary>
  368. /// <param name="index">Sequential index of the element in the list to clone.</param>
  369. protected internal abstract void CloneElement(int index);
  370. /// <summary>
  371. /// Moves an element from the current list index to the one right before it, if not at zero.
  372. /// </summary>
  373. /// <param name="index">Sequential index of the element in the list to move.</param>
  374. protected internal abstract void MoveUpElement(int index);
  375. /// <summary>
  376. /// Moves an element from the current list index to the one right after it, if the element isn't already the last
  377. /// element.
  378. /// </summary>
  379. /// <param name="index">Sequential index of the element in the list to move.</param>
  380. protected internal abstract void MoveDownElement(int index);
  381. /// <summary>
  382. /// Possible states list GUI can be in.
  383. /// </summary>
  384. private enum State
  385. {
  386. None,
  387. Empty,
  388. Filled
  389. }
  390. }
  391. /// <summary>
  392. /// Creates GUI elements that allow viewing and manipulation of a <see cref="System.Array"/>. When constructing the
  393. /// object user can provide a custom type that manages GUI for individual array elements.
  394. /// </summary>
  395. /// <typeparam name="ElementType">Type of elements stored in the array.</typeparam>
  396. /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual array elements.</typeparam>
  397. public class GUIArrayField<ElementType, RowType> : GUIListFieldBase where RowType : GUIListFieldRow, new()
  398. {
  399. /// <summary>
  400. /// Triggered when the reference array has been changed. This does not include changes that only happen to its
  401. /// internal elements.
  402. /// </summary>
  403. public Action<ElementType[]> OnChanged;
  404. /// <summary>
  405. /// Triggered when an element in the array has been changed.
  406. /// </summary>
  407. public Action OnValueChanged;
  408. /// <summary>
  409. /// Array object whose contents are displayed.
  410. /// </summary>
  411. public ElementType[] Array { get { return array; } }
  412. protected ElementType[] array;
  413. /// <summary>
  414. /// Constructs a new GUI array field.
  415. /// </summary>
  416. /// <param name="title">Label to display on the array GUI title.</param>
  417. /// <param name="array">Object containing the array data. Can be null.</param>
  418. /// <param name="layout">Layout to which to append the array GUI elements to.</param>
  419. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  420. /// nested containers whose backgrounds are overlaping. Also determines background style,
  421. /// depths divisible by two will use an alternate style.</param>
  422. protected GUIArrayField(LocString title, ElementType[] array, GUILayout layout, int depth = 0)
  423. :base(title, layout, depth)
  424. {
  425. this.array = array;
  426. }
  427. /// <summary>
  428. /// Creates a array GUI field containing GUI elements for displaying an array.
  429. /// </summary>
  430. /// <param name="title">Label to display on the array GUI title.</param>
  431. /// <param name="array">Object containing the array data. Can be null.</param>
  432. /// <param name="layout">Layout to which to append the array GUI elements to.</param>
  433. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  434. /// nested containers whose backgrounds are overlaping. Also determines background style,
  435. /// depths divisible by two will use an alternate style.</param>
  436. /// <returns>New instance of an array GUI field.</returns>
  437. public static GUIArrayField<ElementType, RowType> Create(LocString title, ElementType[] array, GUILayout layout,
  438. int depth = 0)
  439. {
  440. GUIArrayField<ElementType, RowType> guiArray = new GUIArrayField<ElementType, RowType>(title, array, layout,
  441. depth);
  442. guiArray.BuildGUI();
  443. return guiArray;
  444. }
  445. /// <inheritdoc/>
  446. protected override GUIListFieldRow CreateRow()
  447. {
  448. return new RowType();
  449. }
  450. /// <inheritdoc/>
  451. protected override bool IsNull()
  452. {
  453. return array == null;
  454. }
  455. /// <inheritdoc/>
  456. protected override int GetNumRows()
  457. {
  458. if (array != null)
  459. return array.GetLength(0);
  460. return 0;
  461. }
  462. /// <inheritdoc/>
  463. protected internal override object GetValue(int seqIndex)
  464. {
  465. return array.GetValue(seqIndex);
  466. }
  467. /// <inheritdoc/>
  468. protected internal override void SetValue(int seqIndex, object value)
  469. {
  470. array.SetValue(value, seqIndex);
  471. if (OnValueChanged != null)
  472. OnValueChanged();
  473. }
  474. /// <inheritdoc/>
  475. protected override void CreateList()
  476. {
  477. array = new ElementType[0];
  478. if (OnChanged != null)
  479. OnChanged(array);
  480. }
  481. /// <inheritdoc/>
  482. protected override void ResizeList()
  483. {
  484. int size = guiSizeField.Value;
  485. ElementType[] newArray = new ElementType[size];
  486. int oldSize = array.GetLength(0);
  487. int maxSize = MathEx.Min(size, oldSize);
  488. for (int i = 0; i < maxSize; i++)
  489. newArray.SetValue(array.GetValue(i), i);
  490. array = newArray;
  491. if (OnChanged != null)
  492. OnChanged(array);
  493. }
  494. /// <inheritdoc/>
  495. protected override void ClearList()
  496. {
  497. array = null;
  498. if (OnChanged != null)
  499. OnChanged(array);
  500. }
  501. /// <inheritdoc/>
  502. protected internal override void DeleteElement(int index)
  503. {
  504. int size = MathEx.Max(0, array.GetLength(0) - 1);
  505. ElementType[] newArray = new ElementType[size];
  506. int destIdx = 0;
  507. for (int i = 0; i < array.GetLength(0); i++)
  508. {
  509. if (i == index)
  510. continue;
  511. newArray.SetValue(array.GetValue(i), destIdx);
  512. destIdx++;
  513. }
  514. array = newArray;
  515. if (OnChanged != null)
  516. OnChanged(array);
  517. }
  518. /// <inheritdoc/>
  519. protected internal override void CloneElement(int index)
  520. {
  521. int size = array.GetLength(0) + 1;
  522. ElementType[] newArray = new ElementType[size];
  523. object clonedEntry = null;
  524. for (int i = 0; i < array.GetLength(0); i++)
  525. {
  526. object value = array.GetValue(i);
  527. newArray.SetValue(value, i);
  528. if (i == index)
  529. {
  530. if (value == null)
  531. clonedEntry = null;
  532. else
  533. clonedEntry = SerializableUtility.Clone(value);
  534. }
  535. }
  536. newArray.SetValue(clonedEntry, size - 1);
  537. array = newArray;
  538. if (OnChanged != null)
  539. OnChanged(array);
  540. }
  541. /// <inheritdoc/>
  542. protected internal override void MoveUpElement(int index)
  543. {
  544. if ((index - 1) >= 0)
  545. {
  546. object previousEntry = array.GetValue(index - 1);
  547. array.SetValue(array.GetValue(index), index - 1);
  548. array.SetValue(previousEntry, index);
  549. if (OnValueChanged != null)
  550. OnValueChanged();
  551. }
  552. }
  553. /// <inheritdoc/>
  554. protected internal override void MoveDownElement(int index)
  555. {
  556. if ((index + 1) < array.GetLength(0))
  557. {
  558. object nextEntry = array.GetValue(index + 1);
  559. array.SetValue(array.GetValue(index), index + 1);
  560. array.SetValue(nextEntry, index);
  561. if (OnValueChanged != null)
  562. OnValueChanged();
  563. }
  564. }
  565. }
  566. /// <summary>
  567. /// Creates GUI elements that allow viewing and manipulation of a <see cref="List{T}"/>. When constructing the
  568. /// object user can provide a custom type that manages GUI for individual list elements.
  569. /// </summary>
  570. /// <typeparam name="ElementType">Type of elements stored in the list.</typeparam>
  571. /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual list elements.</typeparam>
  572. public class GUIListField<ElementType, RowType> : GUIListFieldBase where RowType : GUIListFieldRow, new()
  573. {
  574. /// <summary>
  575. /// Triggered when the reference list has been changed. This does not include changes that only happen to its
  576. /// internal elements.
  577. /// </summary>
  578. public Action<List<ElementType>> OnChanged;
  579. /// <summary>
  580. /// Triggered when an element in the list has been changed.
  581. /// </summary>
  582. public Action OnValueChanged;
  583. /// <summary>
  584. /// List object whose contents are displayed.
  585. /// </summary>
  586. public List<ElementType> List { get { return list; } }
  587. protected List<ElementType> list;
  588. /// <summary>
  589. /// Constructs a new GUI list field.
  590. /// </summary>
  591. /// <param name="title">Label to display on the list GUI title.</param>
  592. /// <param name="list">Object containing the list data. Can be null.</param>
  593. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  594. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  595. /// nested containers whose backgrounds are overlaping. Also determines background style,
  596. /// depths divisible by two will use an alternate style.</param>
  597. protected GUIListField(LocString title, List<ElementType> list, GUILayout layout, int depth = 0)
  598. : base(title, layout, depth)
  599. {
  600. this.list = list;
  601. }
  602. /// <summary>
  603. /// Creates a list GUI field containing GUI elements for displaying a list.
  604. /// </summary>
  605. /// <param name="title">Label to display on the list GUI title.</param>
  606. /// <param name="list">Object containing the list data. Can be null.</param>
  607. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  608. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  609. /// nested containers whose backgrounds are overlaping. Also determines background style,
  610. /// depths divisible by two will use an alternate style.</param>
  611. /// <returns>New instance of a list GUI field.</returns>
  612. public static GUIListField<ElementType, RowType> Create(LocString title, List<ElementType> list, GUILayout layout,
  613. int depth = 0)
  614. {
  615. GUIListField<ElementType, RowType> guiList = new GUIListField<ElementType, RowType>(title, list, layout, depth);
  616. guiList.BuildGUI();
  617. return guiList;
  618. }
  619. /// <inheritdoc/>
  620. protected override GUIListFieldRow CreateRow()
  621. {
  622. return new RowType();
  623. }
  624. /// <inheritdoc/>
  625. protected override bool IsNull()
  626. {
  627. return list == null;
  628. }
  629. /// <inheritdoc/>
  630. protected override int GetNumRows()
  631. {
  632. if (list != null)
  633. return list.Count;
  634. return 0;
  635. }
  636. /// <inheritdoc/>
  637. protected internal override object GetValue(int seqIndex)
  638. {
  639. return list[seqIndex];
  640. }
  641. /// <inheritdoc/>
  642. protected internal override void SetValue(int seqIndex, object value)
  643. {
  644. list[seqIndex] = (ElementType)value;
  645. if (OnValueChanged != null)
  646. OnValueChanged();
  647. }
  648. /// <inheritdoc/>
  649. protected override void CreateList()
  650. {
  651. list = new List<ElementType>();
  652. if (OnChanged != null)
  653. OnChanged(list);
  654. }
  655. /// <inheritdoc/>
  656. protected override void ResizeList()
  657. {
  658. int size = guiSizeField.Value;
  659. if(size == list.Count)
  660. return;
  661. if (size < list.Count)
  662. list.RemoveRange(size, list.Count - size);
  663. else
  664. {
  665. ElementType[] extraElements = new ElementType[size - list.Count];
  666. list.AddRange(extraElements);
  667. }
  668. if (OnValueChanged != null)
  669. OnValueChanged();
  670. }
  671. /// <inheritdoc/>
  672. protected override void ClearList()
  673. {
  674. list = null;
  675. if (OnChanged != null)
  676. OnChanged(list);
  677. }
  678. /// <inheritdoc/>
  679. protected internal override void DeleteElement(int index)
  680. {
  681. list.RemoveAt(index);
  682. if (OnValueChanged != null)
  683. OnValueChanged();
  684. }
  685. /// <inheritdoc/>
  686. protected internal override void CloneElement(int index)
  687. {
  688. object clonedEntry = null;
  689. if (list[index] != null)
  690. clonedEntry = SerializableUtility.Clone(list[index]);
  691. list.Add((ElementType)clonedEntry);
  692. if (OnValueChanged != null)
  693. OnValueChanged();
  694. }
  695. /// <inheritdoc/>
  696. protected internal override void MoveUpElement(int index)
  697. {
  698. if ((index - 1) >= 0)
  699. {
  700. ElementType previousEntry = list[index - 1];
  701. list[index - 1] = list[index];
  702. list[index] = previousEntry;
  703. if (OnValueChanged != null)
  704. OnValueChanged();
  705. }
  706. }
  707. /// <inheritdoc/>
  708. protected internal override void MoveDownElement(int index)
  709. {
  710. if ((index + 1) < list.Count)
  711. {
  712. ElementType nextEntry = list[index + 1];
  713. list[index + 1] = list[index];
  714. list[index] = nextEntry;
  715. if (OnValueChanged != null)
  716. OnValueChanged();
  717. }
  718. }
  719. }
  720. /// <summary>
  721. /// Contains GUI elements for a single entry in a list.
  722. /// </summary>
  723. public abstract class GUIListFieldRow
  724. {
  725. private GUILayoutX rowLayout;
  726. private GUILayoutY contentLayout;
  727. private GUILayoutX titleLayout;
  728. private bool localTitleLayout;
  729. private int seqIndex;
  730. private int depth;
  731. private InspectableState modifiedState;
  732. protected GUIListFieldBase parent;
  733. /// <summary>
  734. /// Returns the sequential index of the list entry that this row displays.
  735. /// </summary>
  736. protected int SeqIndex { get { return seqIndex; } }
  737. /// <summary>
  738. /// Returns the depth at which the row is rendered.
  739. /// </summary>
  740. protected int Depth { get { return depth; } }
  741. /// <summary>
  742. /// Constructs a new list row object.
  743. /// </summary>
  744. protected GUIListFieldRow()
  745. {
  746. }
  747. /// <summary>
  748. /// Initializes the row and creates row GUI elements.
  749. /// </summary>
  750. /// <param name="parent">Parent array GUI object that the entry is contained in.</param>
  751. /// <param name="parentLayout">Parent layout that row GUI elements will be added to.</param>
  752. /// <param name="seqIndex">Sequential index of the list entry.</param>
  753. /// <param name="depth">Determines the depth at which the element is rendered.</param>
  754. internal void Initialize(GUIListFieldBase parent, GUILayout parentLayout, int seqIndex, int depth)
  755. {
  756. this.parent = parent;
  757. this.seqIndex = seqIndex;
  758. this.depth = depth;
  759. rowLayout = parentLayout.AddLayoutX();
  760. contentLayout = rowLayout.AddLayoutY();
  761. BuildGUI();
  762. }
  763. /// <summary>
  764. /// Changes the index of the list element this row represents.
  765. /// </summary>
  766. /// <param name="seqIndex">Sequential index of the list entry.</param>
  767. internal void SetIndex(int seqIndex)
  768. {
  769. this.seqIndex = seqIndex;
  770. }
  771. /// <summary>
  772. /// (Re)creates all row GUI elements.
  773. /// </summary>
  774. internal protected void BuildGUI()
  775. {
  776. contentLayout.Clear();
  777. GUILayoutX externalTitleLayout = CreateGUI(contentLayout);
  778. if (localTitleLayout || (titleLayout != null && titleLayout == externalTitleLayout))
  779. return;
  780. if (externalTitleLayout != null)
  781. {
  782. localTitleLayout = false;
  783. titleLayout = externalTitleLayout;
  784. }
  785. else
  786. {
  787. GUILayoutY buttonCenter = rowLayout.AddLayoutY();
  788. buttonCenter.AddFlexibleSpace();
  789. titleLayout = buttonCenter.AddLayoutX();
  790. buttonCenter.AddFlexibleSpace();
  791. localTitleLayout = true;
  792. }
  793. GUIContent cloneIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clone),
  794. new LocEdString("Clone"));
  795. GUIContent deleteIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Delete),
  796. new LocEdString("Delete"));
  797. GUIContent moveUp = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveUp),
  798. new LocEdString("Move up"));
  799. GUIContent moveDown = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveDown),
  800. new LocEdString("Move down"));
  801. GUIButton cloneBtn = new GUIButton(cloneIcon, GUIOption.FixedWidth(30));
  802. GUIButton deleteBtn = new GUIButton(deleteIcon, GUIOption.FixedWidth(30));
  803. GUIButton moveUpBtn = new GUIButton(moveUp, GUIOption.FixedWidth(30));
  804. GUIButton moveDownBtn = new GUIButton(moveDown, GUIOption.FixedWidth(30));
  805. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(seqIndex);
  806. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(seqIndex);
  807. moveUpBtn.OnClick += () => parent.OnMoveUpButtonClicked(seqIndex);
  808. moveDownBtn.OnClick += () => parent.OnMoveDownButtonClicked(seqIndex);
  809. titleLayout.AddElement(cloneBtn);
  810. titleLayout.AddElement(deleteBtn);
  811. titleLayout.AddElement(moveUpBtn);
  812. titleLayout.AddElement(moveDownBtn);
  813. }
  814. /// <summary>
  815. /// Creates GUI elements specific to type in the array row.
  816. /// </summary>
  817. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  818. /// <returns>An optional title bar layout that the standard array buttons will be appended to.</returns>
  819. protected abstract GUILayoutX CreateGUI(GUILayoutY layout);
  820. /// <summary>
  821. /// Refreshes the GUI for the list row and checks if anything was modified.
  822. /// </summary>
  823. /// <returns>State representing was anything modified between two last calls to <see cref="Refresh"/>.</returns>
  824. protected internal virtual InspectableState Refresh()
  825. {
  826. InspectableState oldState = modifiedState;
  827. if (modifiedState.HasFlag(InspectableState.Modified))
  828. modifiedState = InspectableState.NotModified;
  829. return oldState;
  830. }
  831. /// <summary>
  832. /// Marks the contents of the row as modified.
  833. /// </summary>
  834. protected void MarkAsModified()
  835. {
  836. modifiedState |= InspectableState.ModifyInProgress;
  837. }
  838. /// <summary>
  839. /// Confirms any queued modifications, signaling parent elements.
  840. /// </summary>
  841. protected void ConfirmModify()
  842. {
  843. if (modifiedState.HasFlag(InspectableState.ModifyInProgress))
  844. modifiedState |= InspectableState.Modified;
  845. }
  846. /// <summary>
  847. /// Gets the value contained in this list row.
  848. /// </summary>
  849. /// <typeparam name="T">Type of the value. Must match the list's element type.</typeparam>
  850. /// <returns>Value in this list row.</returns>
  851. protected T GetValue<T>()
  852. {
  853. return (T)parent.GetValue(seqIndex);
  854. }
  855. /// <summary>
  856. /// Sets the value contained in this list row.
  857. /// </summary>
  858. /// <typeparam name="T">Type of the value. Must match the list's element type.</typeparam>
  859. /// <param name="value">Value to set.</param>
  860. protected void SetValue<T>(T value)
  861. {
  862. parent.SetValue(seqIndex, value);
  863. }
  864. /// <summary>
  865. /// Destroys all row GUI elements.
  866. /// </summary>
  867. public void Destroy()
  868. {
  869. if (rowLayout != null)
  870. {
  871. rowLayout.Destroy();
  872. rowLayout = null;
  873. }
  874. contentLayout = null;
  875. titleLayout = null;
  876. localTitleLayout = false;
  877. }
  878. }
  879. /** @} */
  880. }