GUIListField.cs 35 KB

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