GUIListField.cs 37 KB

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