GUIListField.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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 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. }