GUIListField.cs 37 KB

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