GUIListField.cs 37 KB

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