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. ? EditorStyles.InspectorContentBgAlternate
  148. : EditorStyles.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 maxSize = MathEx.Min(size, array.GetLength(0));
  487. for (int i = 0; i < maxSize; i++)
  488. newArray.SetValue(array.GetValue(i), i);
  489. array = newArray;
  490. if(OnChanged != null)
  491. OnChanged(array);
  492. }
  493. /// <inheritdoc/>
  494. protected override void ClearList()
  495. {
  496. array = null;
  497. if (OnChanged != null)
  498. OnChanged(array);
  499. }
  500. /// <inheritdoc/>
  501. protected internal override void DeleteElement(int index)
  502. {
  503. int size = MathEx.Max(0, array.GetLength(0) - 1);
  504. ElementType[] newArray = new ElementType[size];
  505. int destIdx = 0;
  506. for (int i = 0; i < array.GetLength(0); i++)
  507. {
  508. if (i == index)
  509. continue;
  510. newArray.SetValue(array.GetValue(i), destIdx);
  511. destIdx++;
  512. }
  513. array = newArray;
  514. if (OnChanged != null)
  515. OnChanged(array);
  516. }
  517. /// <inheritdoc/>
  518. protected internal override void CloneElement(int index)
  519. {
  520. int size = array.GetLength(0) + 1;
  521. ElementType[] newArray = new ElementType[size];
  522. object clonedEntry = null;
  523. for (int i = 0; i < array.GetLength(0); i++)
  524. {
  525. object value = array.GetValue(i);
  526. newArray.SetValue(value, i);
  527. if (i == index)
  528. {
  529. if (value == null)
  530. clonedEntry = null;
  531. else
  532. clonedEntry = SerializableUtility.Clone(value);
  533. }
  534. }
  535. newArray.SetValue(clonedEntry, size - 1);
  536. array = newArray;
  537. if (OnChanged != null)
  538. OnChanged(array);
  539. }
  540. /// <inheritdoc/>
  541. protected internal override void MoveUpElement(int index)
  542. {
  543. if ((index - 1) >= 0)
  544. {
  545. object previousEntry = array.GetValue(index - 1);
  546. array.SetValue(array.GetValue(index), index - 1);
  547. array.SetValue(previousEntry, index);
  548. if (OnValueChanged != null)
  549. OnValueChanged();
  550. }
  551. }
  552. /// <inheritdoc/>
  553. protected internal override void MoveDownElement(int index)
  554. {
  555. if ((index + 1) < array.GetLength(0))
  556. {
  557. object nextEntry = array.GetValue(index + 1);
  558. array.SetValue(array.GetValue(index), index + 1);
  559. array.SetValue(nextEntry, index);
  560. if (OnValueChanged != null)
  561. OnValueChanged();
  562. }
  563. }
  564. }
  565. /// <summary>
  566. /// Creates GUI elements that allow viewing and manipulation of a <see cref="List{T}"/>. When constructing the
  567. /// object user can provide a custom type that manages GUI for individual list elements.
  568. /// </summary>
  569. /// <typeparam name="ElementType">Type of elements stored in the list.</typeparam>
  570. /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual list elements.</typeparam>
  571. public class GUIListField<ElementType, RowType> : GUIListFieldBase where RowType : GUIListFieldRow, new()
  572. {
  573. /// <summary>
  574. /// Triggered when the reference list has been changed. This does not include changes that only happen to its
  575. /// internal elements.
  576. /// </summary>
  577. public Action<List<ElementType>> OnChanged;
  578. /// <summary>
  579. /// Triggered when an element in the list has been changed.
  580. /// </summary>
  581. public Action OnValueChanged;
  582. /// <summary>
  583. /// List object whose contents are displayed.
  584. /// </summary>
  585. public List<ElementType> List { get { return list; } }
  586. protected List<ElementType> list;
  587. /// <summary>
  588. /// Constructs a new GUI list field.
  589. /// </summary>
  590. /// <param name="title">Label to display on the list GUI title.</param>
  591. /// <param name="list">Object containing the list data. Can be null.</param>
  592. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  593. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  594. /// nested containers whose backgrounds are overlaping. Also determines background style,
  595. /// depths divisible by two will use an alternate style.</param>
  596. protected GUIListField(LocString title, List<ElementType> list, GUILayout layout, int depth = 0)
  597. : base(title, layout, depth)
  598. {
  599. this.list = list;
  600. }
  601. /// <summary>
  602. /// Creates a list GUI field containing GUI elements for displaying a list.
  603. /// </summary>
  604. /// <param name="title">Label to display on the list GUI title.</param>
  605. /// <param name="list">Object containing the list data. Can be null.</param>
  606. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  607. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  608. /// nested containers whose backgrounds are overlaping. Also determines background style,
  609. /// depths divisible by two will use an alternate style.</param>
  610. /// <returns>New instance of a list GUI field.</returns>
  611. public static GUIListField<ElementType, RowType> Create(LocString title, List<ElementType> list, GUILayout layout,
  612. int depth = 0)
  613. {
  614. GUIListField<ElementType, RowType> guiList = new GUIListField<ElementType, RowType>(title, list, layout, depth);
  615. guiList.BuildGUI();
  616. return guiList;
  617. }
  618. /// <inheritdoc/>
  619. protected override GUIListFieldRow CreateRow()
  620. {
  621. return new RowType();
  622. }
  623. /// <inheritdoc/>
  624. protected override bool IsNull()
  625. {
  626. return list == null;
  627. }
  628. /// <inheritdoc/>
  629. protected override int GetNumRows()
  630. {
  631. if (list != null)
  632. return list.Count;
  633. return 0;
  634. }
  635. /// <inheritdoc/>
  636. protected internal override object GetValue(int seqIndex)
  637. {
  638. return list[seqIndex];
  639. }
  640. /// <inheritdoc/>
  641. protected internal override void SetValue(int seqIndex, object value)
  642. {
  643. list[seqIndex] = (ElementType)value;
  644. if (OnValueChanged != null)
  645. OnValueChanged();
  646. }
  647. /// <inheritdoc/>
  648. protected override void CreateList()
  649. {
  650. list = new List<ElementType>();
  651. if (OnChanged != null)
  652. OnChanged(list);
  653. }
  654. /// <inheritdoc/>
  655. protected override void ResizeList()
  656. {
  657. int size = guiSizeField.Value;
  658. if(size == list.Count)
  659. return;
  660. if (size < list.Count)
  661. list.RemoveRange(size, list.Count - size);
  662. else
  663. {
  664. ElementType[] extraElements = new ElementType[size - list.Count];
  665. list.AddRange(extraElements);
  666. }
  667. if (OnValueChanged != null)
  668. OnValueChanged();
  669. }
  670. /// <inheritdoc/>
  671. protected override void ClearList()
  672. {
  673. list = null;
  674. if (OnChanged != null)
  675. OnChanged(list);
  676. }
  677. /// <inheritdoc/>
  678. protected internal override void DeleteElement(int index)
  679. {
  680. list.RemoveAt(index);
  681. if (OnValueChanged != null)
  682. OnValueChanged();
  683. }
  684. /// <inheritdoc/>
  685. protected internal override void CloneElement(int index)
  686. {
  687. object clonedEntry = null;
  688. if (list[index] != null)
  689. clonedEntry = SerializableUtility.Clone(list[index]);
  690. list.Add((ElementType)clonedEntry);
  691. if (OnValueChanged != null)
  692. OnValueChanged();
  693. }
  694. /// <inheritdoc/>
  695. protected internal override void MoveUpElement(int index)
  696. {
  697. if ((index - 1) >= 0)
  698. {
  699. ElementType previousEntry = list[index - 1];
  700. list[index - 1] = list[index];
  701. list[index] = previousEntry;
  702. if (OnValueChanged != null)
  703. OnValueChanged();
  704. }
  705. }
  706. /// <inheritdoc/>
  707. protected internal override void MoveDownElement(int index)
  708. {
  709. if ((index + 1) < list.Count)
  710. {
  711. ElementType nextEntry = list[index + 1];
  712. list[index + 1] = list[index];
  713. list[index] = nextEntry;
  714. if (OnValueChanged != null)
  715. OnValueChanged();
  716. }
  717. }
  718. }
  719. /// <summary>
  720. /// Contains GUI elements for a single entry in a list.
  721. /// </summary>
  722. public abstract class GUIListFieldRow
  723. {
  724. private GUILayoutX rowLayout;
  725. private GUILayoutY contentLayout;
  726. private GUILayoutX titleLayout;
  727. private bool localTitleLayout;
  728. private int seqIndex;
  729. private int depth;
  730. private InspectableState modifiedState;
  731. protected GUIListFieldBase parent;
  732. /// <summary>
  733. /// Returns the sequential index of the list entry that this row displays.
  734. /// </summary>
  735. protected int SeqIndex { get { return seqIndex; } }
  736. /// <summary>
  737. /// Returns the depth at which the row is rendered.
  738. /// </summary>
  739. protected int Depth { get { return depth; } }
  740. /// <summary>
  741. /// Constructs a new list row object.
  742. /// </summary>
  743. protected GUIListFieldRow()
  744. {
  745. }
  746. /// <summary>
  747. /// Initializes the row and creates row GUI elements.
  748. /// </summary>
  749. /// <param name="parent">Parent array GUI object that the entry is contained in.</param>
  750. /// <param name="parentLayout">Parent layout that row GUI elements will be added to.</param>
  751. /// <param name="seqIndex">Sequential index of the list entry.</param>
  752. /// <param name="depth">Determines the depth at which the element is rendered.</param>
  753. internal void Initialize(GUIListFieldBase parent, GUILayout parentLayout, int seqIndex, int depth)
  754. {
  755. this.parent = parent;
  756. this.seqIndex = seqIndex;
  757. this.depth = depth;
  758. rowLayout = parentLayout.AddLayoutX();
  759. contentLayout = rowLayout.AddLayoutY();
  760. BuildGUI();
  761. }
  762. /// <summary>
  763. /// Changes the index of the list element this row represents.
  764. /// </summary>
  765. /// <param name="seqIndex">Sequential index of the list entry.</param>
  766. internal void SetIndex(int seqIndex)
  767. {
  768. this.seqIndex = seqIndex;
  769. }
  770. /// <summary>
  771. /// (Re)creates all row GUI elements.
  772. /// </summary>
  773. internal protected void BuildGUI()
  774. {
  775. contentLayout.Clear();
  776. GUILayoutX externalTitleLayout = CreateGUI(contentLayout);
  777. if (localTitleLayout || (titleLayout != null && titleLayout == externalTitleLayout))
  778. return;
  779. if (externalTitleLayout != null)
  780. {
  781. localTitleLayout = false;
  782. titleLayout = externalTitleLayout;
  783. }
  784. else
  785. {
  786. GUILayoutY buttonCenter = rowLayout.AddLayoutY();
  787. buttonCenter.AddFlexibleSpace();
  788. titleLayout = buttonCenter.AddLayoutX();
  789. buttonCenter.AddFlexibleSpace();
  790. localTitleLayout = true;
  791. }
  792. GUIContent cloneIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clone),
  793. new LocEdString("Clone"));
  794. GUIContent deleteIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Delete),
  795. new LocEdString("Delete"));
  796. GUIContent moveUp = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveUp),
  797. new LocEdString("Move up"));
  798. GUIContent moveDown = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveDown),
  799. new LocEdString("Move down"));
  800. GUIButton cloneBtn = new GUIButton(cloneIcon, GUIOption.FixedWidth(30));
  801. GUIButton deleteBtn = new GUIButton(deleteIcon, GUIOption.FixedWidth(30));
  802. GUIButton moveUpBtn = new GUIButton(moveUp, GUIOption.FixedWidth(30));
  803. GUIButton moveDownBtn = new GUIButton(moveDown, GUIOption.FixedWidth(30));
  804. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(seqIndex);
  805. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(seqIndex);
  806. moveUpBtn.OnClick += () => parent.OnMoveUpButtonClicked(seqIndex);
  807. moveDownBtn.OnClick += () => parent.OnMoveDownButtonClicked(seqIndex);
  808. titleLayout.AddElement(cloneBtn);
  809. titleLayout.AddElement(deleteBtn);
  810. titleLayout.AddElement(moveUpBtn);
  811. titleLayout.AddElement(moveDownBtn);
  812. }
  813. /// <summary>
  814. /// Creates GUI elements specific to type in the array row.
  815. /// </summary>
  816. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  817. /// <returns>An optional title bar layout that the standard array buttons will be appended to.</returns>
  818. protected abstract GUILayoutX CreateGUI(GUILayoutY layout);
  819. /// <summary>
  820. /// Refreshes the GUI for the list row and checks if anything was modified.
  821. /// </summary>
  822. /// <returns>State representing was anything modified between two last calls to <see cref="Refresh"/>.</returns>
  823. internal protected virtual InspectableState Refresh()
  824. {
  825. InspectableState oldState = modifiedState;
  826. if (modifiedState.HasFlag(InspectableState.Modified))
  827. modifiedState = InspectableState.NotModified;
  828. return oldState;
  829. }
  830. /// <summary>
  831. /// Marks the contents of the row as modified.
  832. /// </summary>
  833. protected void MarkAsModified()
  834. {
  835. modifiedState |= InspectableState.ModifyInProgress;
  836. }
  837. /// <summary>
  838. /// Confirms any queued modifications, signaling parent elements.
  839. /// </summary>
  840. protected void ConfirmModify()
  841. {
  842. if (modifiedState.HasFlag(InspectableState.ModifyInProgress))
  843. modifiedState |= InspectableState.Modified;
  844. }
  845. /// <summary>
  846. /// Gets the value contained in this list row.
  847. /// </summary>
  848. /// <typeparam name="T">Type of the value. Must match the list's element type.</typeparam>
  849. /// <returns>Value in this list row.</returns>
  850. protected T GetValue<T>()
  851. {
  852. return (T)parent.GetValue(seqIndex);
  853. }
  854. /// <summary>
  855. /// Sets the value contained in this list row.
  856. /// </summary>
  857. /// <typeparam name="T">Type of the value. Must match the list's element type.</typeparam>
  858. /// <param name="value">Value to set.</param>
  859. protected void SetValue<T>(T value)
  860. {
  861. parent.SetValue(seqIndex, value);
  862. }
  863. /// <summary>
  864. /// Destroys all row GUI elements.
  865. /// </summary>
  866. public void Destroy()
  867. {
  868. if (rowLayout != null)
  869. {
  870. rowLayout.Destroy();
  871. rowLayout = null;
  872. }
  873. contentLayout = null;
  874. titleLayout = null;
  875. localTitleLayout = false;
  876. }
  877. }
  878. /** @} */
  879. }