GUIListField.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 GUIIntField guiSizeField;
  15. protected GUILayoutX guiChildLayout;
  16. protected GUILayoutX guiTitleLayout;
  17. protected GUILayoutY guiContentLayout;
  18. protected bool isExpanded;
  19. protected int depth;
  20. /// <summary>
  21. /// Constructs a new GUI list.
  22. /// </summary>
  23. protected GUIListFieldBase()
  24. { }
  25. /// <summary>
  26. /// Updates the GUI list contents. Must be called at least once in order for the contents to be populated.
  27. /// </summary>
  28. /// <typeparam name="T">Type of rows that are used to handle GUI for individual list elements.</typeparam>
  29. /// <param name="title">Label to display on the list GUI title.</param>
  30. /// <param name="empty">Should the created field represent a null object.</param>
  31. /// <param name="numRows">Number of rows to create GUI for. Only matters for a non-null list.</param>
  32. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  33. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  34. /// nested containers whose backgrounds are overlaping. Also determines background style,
  35. /// depths divisible by two will use an alternate style.</param>
  36. protected void Update<T>(LocString title, bool empty, int numRows, GUILayout layout,
  37. int depth = 0) where T : GUIListFieldRow, new()
  38. {
  39. Destroy();
  40. this.depth = depth;
  41. if (empty)
  42. {
  43. guiChildLayout = null;
  44. guiContentLayout = null;
  45. guiTitleLayout = layout.AddLayoutX();
  46. guiTitleLayout.AddElement(new GUILabel(title));
  47. guiTitleLayout.AddElement(new GUILabel("Empty", GUIOption.FixedWidth(100)));
  48. GUIContent createIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Create));
  49. GUIButton createBtn = new GUIButton(createIcon, GUIOption.FixedWidth(30));
  50. createBtn.OnClick += OnCreateButtonClicked;
  51. guiTitleLayout.AddElement(createBtn);
  52. }
  53. else
  54. {
  55. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  56. guiFoldout.Value = isExpanded;
  57. guiFoldout.OnToggled += OnFoldoutToggled;
  58. guiSizeField = new GUIIntField("", GUIOption.FixedWidth(50));
  59. guiSizeField.SetRange(0, int.MaxValue);
  60. GUIContent resizeIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Resize));
  61. GUIButton guiResizeBtn = new GUIButton(resizeIcon, GUIOption.FixedWidth(30));
  62. guiResizeBtn.OnClick += OnResizeButtonClicked;
  63. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clear));
  64. GUIButton guiClearBtn = new GUIButton(clearIcon, GUIOption.FixedWidth(30));
  65. guiClearBtn.OnClick += OnClearButtonClicked;
  66. guiTitleLayout = layout.AddLayoutX();
  67. guiTitleLayout.AddElement(guiFoldout);
  68. guiTitleLayout.AddElement(guiSizeField);
  69. guiTitleLayout.AddElement(guiResizeBtn);
  70. guiTitleLayout.AddElement(guiClearBtn);
  71. guiSizeField.Value = numRows;
  72. if (numRows > 0)
  73. {
  74. guiChildLayout = layout.AddLayoutX();
  75. guiChildLayout.AddSpace(IndentAmount);
  76. guiChildLayout.Enabled = isExpanded;
  77. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  78. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  79. guiIndentLayoutX.AddSpace(IndentAmount);
  80. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  81. guiIndentLayoutY.AddSpace(IndentAmount);
  82. guiContentLayout = guiIndentLayoutY.AddLayoutY();
  83. guiIndentLayoutY.AddSpace(IndentAmount);
  84. guiIndentLayoutX.AddSpace(IndentAmount);
  85. guiChildLayout.AddSpace(IndentAmount);
  86. short backgroundDepth = (short)(Inspector.START_BACKGROUND_DEPTH - depth - 1);
  87. string bgPanelStyle = depth % 2 == 0
  88. ? EditorStyles.InspectorContentBgAlternate
  89. : EditorStyles.InspectorContentBg;
  90. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  91. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  92. backgroundPanel.AddElement(inspectorContentBg);
  93. for (int i = 0; i < numRows; i++)
  94. {
  95. GUIListFieldRow newRow = new T();
  96. newRow.BuildGUI(this, guiContentLayout, i, depth);
  97. rows.Add(newRow);
  98. }
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// Returns the layout that is used for positioning the elements in the title bar.
  104. /// </summary>
  105. /// <returns>Horizontal layout for positioning the title bar elements.</returns>
  106. public GUILayoutX GetTitleLayout()
  107. {
  108. return guiTitleLayout;
  109. }
  110. /// <summary>
  111. /// Refreshes contents of all list rows and checks if anything was modified.
  112. /// </summary>
  113. public void Refresh()
  114. {
  115. for (int i = 0; i < rows.Count; i++)
  116. {
  117. if (rows[i].Refresh())
  118. rows[i].BuildGUI(this, guiContentLayout, i, depth);
  119. }
  120. }
  121. /// <summary>
  122. /// Destroys the GUI elements.
  123. /// </summary>
  124. public void Destroy()
  125. {
  126. if (guiTitleLayout != null)
  127. {
  128. guiTitleLayout.Destroy();
  129. guiTitleLayout = null;
  130. }
  131. if (guiChildLayout != null)
  132. {
  133. guiChildLayout.Destroy();
  134. guiChildLayout = null;
  135. }
  136. for (int i = 0; i < rows.Count; i++)
  137. rows[i].Destroy();
  138. rows.Clear();
  139. }
  140. /// <summary>
  141. /// Gets a value of an element at the specified index in the list.
  142. /// </summary>
  143. /// <param name="seqIndex">Sequential index of the element whose value to retrieve.</param>
  144. /// <returns>Value of the list element at the specified index.</returns>
  145. protected internal abstract object GetValue(int seqIndex);
  146. /// <summary>
  147. /// Sets a value of an element at the specified index in the list.
  148. /// </summary>
  149. /// <param name="seqIndex">Sequential index of the element whose value to set.</param>
  150. /// <param name="value">Value to assign to the element. Caller must ensure it is of valid type.</param>
  151. protected internal abstract void SetValue(int seqIndex, object value);
  152. /// <summary>
  153. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  154. /// </summary>
  155. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  156. private void OnFoldoutToggled(bool expanded)
  157. {
  158. isExpanded = expanded;
  159. if (guiChildLayout != null)
  160. guiChildLayout.Enabled = isExpanded;
  161. }
  162. /// <summary>
  163. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new list with zero
  164. /// elements in the place of the current list.
  165. /// </summary>
  166. protected abstract void OnCreateButtonClicked();
  167. /// <summary>
  168. /// Triggered when the user clicks on the resize button on the title bar. Changes the size of the list while
  169. /// preserving existing contents.
  170. /// </summary>
  171. protected abstract void OnResizeButtonClicked();
  172. /// <summary>
  173. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current list object.
  174. /// </summary>
  175. protected abstract void OnClearButtonClicked();
  176. /// <summary>
  177. /// Triggered when the user clicks on the delete button next to a list entry. Deletes an element in the list.
  178. /// </summary>
  179. /// <param name="index">Sequential index of the element in the list to remove.</param>
  180. protected internal abstract void OnDeleteButtonClicked(int index);
  181. /// <summary>
  182. /// Triggered when the user clicks on the clone button next to a list entry. Clones the element and adds the clone
  183. /// to the back of the list.
  184. /// </summary>
  185. /// <param name="index">Sequential index of the element in the list to clone.</param>
  186. protected internal abstract void OnCloneButtonClicked(int index);
  187. /// <summary>
  188. /// Triggered when the user clicks on the move up button next to a list entry. Moves an element from the current
  189. /// list index to the one right before it, if not at zero.
  190. /// </summary>
  191. /// <param name="index">Sequential index of the element in the list to move.</param>
  192. protected internal abstract void OnMoveUpButtonClicked(int index);
  193. /// <summary>
  194. /// Triggered when the user clicks on the move down button next to a list entry. Moves an element from the current
  195. /// list index to the one right after it, if the element isn't already the last element.
  196. /// </summary>
  197. /// <param name="index">Sequential index of the element in the list to move.</param>
  198. protected internal abstract void OnMoveDownButtonClicked(int index);
  199. }
  200. /// <summary>
  201. /// Creates GUI elements that allow viewing and manipulation of a <see cref="System.Array"/>. When constructing the
  202. /// object user can provide a custom type that manages GUI for individual array elements.
  203. /// </summary>
  204. /// <typeparam name="ElementType">Type of elements stored in the array.</typeparam>
  205. public class GUIArrayField<ElementType> : GUIListFieldBase
  206. {
  207. /// <summary>
  208. /// Triggered when the reference array has been changed. This does not include changes that only happen to its
  209. /// internal elements.
  210. /// </summary>
  211. public Action<ElementType[]> OnChanged;
  212. /// <summary>
  213. /// Triggered when an element in the array has been changed.
  214. /// </summary>
  215. public Action OnValueChanged;
  216. /// <summary>
  217. /// Array object whose contents are displayed.
  218. /// </summary>
  219. public ElementType[] Array { get { return array; } }
  220. protected ElementType[] array;
  221. /// <summary>
  222. /// Constructs a new empty GUI array.
  223. /// </summary>
  224. public GUIArrayField()
  225. { }
  226. /// <summary>
  227. /// Updates the GUI array contents. Must be called at least once in order for the contents to be populated.
  228. /// </summary>
  229. /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual array elements.</typeparam>
  230. /// <param name="title">Label to display on the array GUI title.</param>
  231. /// <param name="array">Object containing the array data. Can be null.</param>
  232. /// <param name="layout">Layout to which to append the array GUI elements to.</param>
  233. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  234. /// nested containers whose backgrounds are overlaping. Also determines background style,
  235. /// depths divisible by two will use an alternate style.</param>
  236. public void Update<RowType>(LocString title, ElementType[] array, GUILayout layout, int depth = 0)
  237. where RowType : GUIListFieldRow, new()
  238. {
  239. this.array = array;
  240. if (array != null)
  241. base.Update<RowType>(title, false, array.Length, layout, depth);
  242. else
  243. base.Update<RowType>(title, true, 0, layout, depth);
  244. }
  245. /// <inheritdoc/>
  246. protected internal override object GetValue(int seqIndex)
  247. {
  248. return array.GetValue(seqIndex);
  249. }
  250. /// <inheritdoc/>
  251. protected internal override void SetValue(int seqIndex, object value)
  252. {
  253. array.SetValue(value, seqIndex);
  254. if (OnValueChanged != null)
  255. OnValueChanged();
  256. }
  257. /// <inheritdoc/>
  258. protected override void OnCreateButtonClicked()
  259. {
  260. array = new ElementType[0];
  261. if (OnChanged != null)
  262. OnChanged(array);
  263. }
  264. /// <inheritdoc/>
  265. protected override void OnResizeButtonClicked()
  266. {
  267. int size = guiSizeField.Value;
  268. ElementType[] newArray = new ElementType[size];
  269. int maxSize = MathEx.Min(size, array.GetLength(0));
  270. for (int i = 0; i < maxSize; i++)
  271. newArray.SetValue(array.GetValue(i), i);
  272. array = newArray;
  273. if(OnChanged != null)
  274. OnChanged(array);
  275. }
  276. /// <inheritdoc/>
  277. protected override void OnClearButtonClicked()
  278. {
  279. array = null;
  280. if (OnChanged != null)
  281. OnChanged(array);
  282. }
  283. /// <inheritdoc/>
  284. protected internal override void OnDeleteButtonClicked(int index)
  285. {
  286. int size = MathEx.Max(0, array.GetLength(0) - 1);
  287. ElementType[] newArray = new ElementType[size];
  288. int destIdx = 0;
  289. for (int i = 0; i < array.GetLength(0); i++)
  290. {
  291. if (i == index)
  292. continue;
  293. newArray.SetValue(array.GetValue(i), destIdx);
  294. destIdx++;
  295. }
  296. array = newArray;
  297. if (OnChanged != null)
  298. OnChanged(array);
  299. }
  300. /// <inheritdoc/>
  301. protected internal override void OnCloneButtonClicked(int index)
  302. {
  303. int size = array.GetLength(0) + 1;
  304. ElementType[] newArray = new ElementType[size];
  305. object clonedEntry = null;
  306. for (int i = 0; i < array.GetLength(0); i++)
  307. {
  308. object value = array.GetValue(i);
  309. newArray.SetValue(value, i);
  310. if (i == index)
  311. {
  312. if (value == null)
  313. clonedEntry = null;
  314. else
  315. clonedEntry = SerializableUtility.Clone(value);
  316. }
  317. }
  318. newArray.SetValue(clonedEntry, size - 1);
  319. array = newArray;
  320. if (OnChanged != null)
  321. OnChanged(array);
  322. }
  323. /// <inheritdoc/>
  324. protected internal override void OnMoveUpButtonClicked(int index)
  325. {
  326. if ((index - 1) >= 0)
  327. {
  328. object previousEntry = array.GetValue(index - 1);
  329. array.SetValue(array.GetValue(index), index - 1);
  330. array.SetValue(previousEntry, index);
  331. if (OnValueChanged != null)
  332. OnValueChanged();
  333. }
  334. }
  335. /// <inheritdoc/>
  336. protected internal override void OnMoveDownButtonClicked(int index)
  337. {
  338. if ((index + 1) < array.GetLength(0))
  339. {
  340. object nextEntry = array.GetValue(index + 1);
  341. array.SetValue(array.GetValue(index), index + 1);
  342. array.SetValue(nextEntry, index);
  343. if (OnValueChanged != null)
  344. OnValueChanged();
  345. }
  346. }
  347. }
  348. /// <summary>
  349. /// Creates GUI elements that allow viewing and manipulation of a <see cref="List{T}"/>. When constructing the
  350. /// object user can provide a custom type that manages GUI for individual list elements.
  351. /// </summary>
  352. /// <typeparam name="ElementType">Type of elements stored in the list.</typeparam>
  353. public class GUIListField<ElementType> : GUIListFieldBase
  354. {
  355. /// <summary>
  356. /// Triggered when the reference list has been changed. This does not include changes that only happen to its
  357. /// internal elements.
  358. /// </summary>
  359. public Action<List<ElementType>> OnChanged;
  360. /// <summary>
  361. /// Triggered when an element in the list has been changed.
  362. /// </summary>
  363. public Action OnValueChanged;
  364. /// <summary>
  365. /// List object whose contents are displayed.
  366. /// </summary>
  367. public List<ElementType> List { get { return list; } }
  368. protected List<ElementType> list;
  369. /// <summary>
  370. /// Constructs a new empty GUI array.
  371. /// </summary>
  372. public GUIListField()
  373. { }
  374. /// <summary>
  375. /// Updates the GUI list contents. Must be called at least once in order for the contents to be populated.
  376. /// </summary>
  377. /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual list elements.</typeparam>
  378. /// <param name="title">Label to display on the list GUI title.</param>
  379. /// <param name="list">Object containing the list data. Can be null.</param>
  380. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  381. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  382. /// nested containers whose backgrounds are overlaping. Also determines background style,
  383. /// depths divisible by two will use an alternate style.</param>
  384. public void Update<RowType>(LocString title, List<ElementType> list, GUILayout layout, int depth = 0)
  385. where RowType : GUIListFieldRow, new()
  386. {
  387. this.list = list;
  388. if (list != null)
  389. base.Update<RowType>(title, false, list.Count, layout, depth);
  390. else
  391. base.Update<RowType>(title, true, 0, layout, depth);
  392. }
  393. /// <inheritdoc/>
  394. protected internal override object GetValue(int seqIndex)
  395. {
  396. return list[seqIndex];
  397. }
  398. /// <inheritdoc/>
  399. protected internal override void SetValue(int seqIndex, object value)
  400. {
  401. list[seqIndex] = (ElementType)value;
  402. if (OnValueChanged != null)
  403. OnValueChanged();
  404. }
  405. /// <inheritdoc/>
  406. protected override void OnCreateButtonClicked()
  407. {
  408. list = new List<ElementType>();
  409. if (OnChanged != null)
  410. OnChanged(list);
  411. }
  412. /// <inheritdoc/>
  413. protected override void OnResizeButtonClicked()
  414. {
  415. int size = guiSizeField.Value;
  416. if(size == list.Count)
  417. return;
  418. if (size < list.Count)
  419. list.RemoveRange(size, list.Count - size);
  420. else
  421. {
  422. ElementType[] extraElements = new ElementType[size - list.Count];
  423. list.AddRange(extraElements);
  424. }
  425. if (OnChanged != null)
  426. OnValueChanged();
  427. }
  428. /// <inheritdoc/>
  429. protected override void OnClearButtonClicked()
  430. {
  431. list = null;
  432. if (OnChanged != null)
  433. OnChanged(list);
  434. }
  435. /// <inheritdoc/>
  436. protected internal override void OnDeleteButtonClicked(int index)
  437. {
  438. list.RemoveAt(index);
  439. if (OnChanged != null)
  440. OnChanged(list);
  441. }
  442. /// <inheritdoc/>
  443. protected internal override void OnCloneButtonClicked(int index)
  444. {
  445. object clonedEntry = null;
  446. if (list[index] != null)
  447. clonedEntry = SerializableUtility.Clone(list[index]);
  448. list.Add((ElementType)clonedEntry);
  449. if (OnChanged != null)
  450. OnChanged(list);
  451. }
  452. /// <inheritdoc/>
  453. protected internal override void OnMoveUpButtonClicked(int index)
  454. {
  455. if ((index - 1) >= 0)
  456. {
  457. ElementType previousEntry = list[index - 1];
  458. list[index - 1] = list[index];
  459. list[index] = previousEntry;
  460. if (OnValueChanged != null)
  461. OnValueChanged();
  462. }
  463. }
  464. /// <inheritdoc/>
  465. protected internal override void OnMoveDownButtonClicked(int index)
  466. {
  467. if ((index + 1) < list.Count)
  468. {
  469. ElementType nextEntry = list[index + 1];
  470. list[index + 1] = list[index];
  471. list[index] = nextEntry;
  472. if (OnValueChanged != null)
  473. OnValueChanged();
  474. }
  475. }
  476. }
  477. /// <summary>
  478. /// Contains GUI elements for a single entry in a list.
  479. /// </summary>
  480. public abstract class GUIListFieldRow
  481. {
  482. private GUILayoutX rowLayout;
  483. private GUILayoutY contentLayout;
  484. private GUILayoutX titleLayout;
  485. private bool localTitleLayout;
  486. private GUIListFieldBase parent;
  487. protected int seqIndex;
  488. protected int depth;
  489. /// <summary>
  490. /// Constructs a new list row object.
  491. /// </summary>
  492. protected GUIListFieldRow()
  493. {
  494. }
  495. /// <summary>
  496. /// (Re)creates all row GUI elements.
  497. /// </summary>
  498. /// <param name="parent">Parent array GUI object that the entry is contained in.</param>
  499. /// <param name="parentLayout">Parent layout that row GUI elements will be added to.</param>
  500. /// <param name="seqIndex">Sequential index of the array entry.</param>
  501. /// <param name="depth">Determines the depth at which the element is rendered.</param>
  502. internal void BuildGUI(GUIListFieldBase parent, GUILayout parentLayout, int seqIndex, int depth)
  503. {
  504. this.parent = parent;
  505. this.seqIndex = seqIndex;
  506. this.depth = depth;
  507. if (rowLayout == null)
  508. rowLayout = parentLayout.AddLayoutX();
  509. if (contentLayout == null)
  510. contentLayout = rowLayout.AddLayoutY();
  511. GUILayoutX externalTitleLayout = CreateGUI(contentLayout);
  512. if (localTitleLayout || (titleLayout != null && titleLayout == externalTitleLayout))
  513. return;
  514. if (externalTitleLayout != null)
  515. {
  516. localTitleLayout = false;
  517. titleLayout = externalTitleLayout;
  518. }
  519. else
  520. {
  521. GUILayoutY buttonCenter = rowLayout.AddLayoutY();
  522. buttonCenter.AddFlexibleSpace();
  523. titleLayout = buttonCenter.AddLayoutX();
  524. buttonCenter.AddFlexibleSpace();
  525. localTitleLayout = true;
  526. }
  527. GUIContent cloneIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clone));
  528. GUIContent deleteIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Delete));
  529. GUIContent moveUp = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveUp));
  530. GUIContent moveDown = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveDown));
  531. GUIButton cloneBtn = new GUIButton(cloneIcon, GUIOption.FixedWidth(30));
  532. GUIButton deleteBtn = new GUIButton(deleteIcon, GUIOption.FixedWidth(30));
  533. GUIButton moveUpBtn = new GUIButton(moveUp, GUIOption.FixedWidth(30));
  534. GUIButton moveDownBtn = new GUIButton(moveDown, GUIOption.FixedWidth(30));
  535. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(seqIndex);
  536. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(seqIndex);
  537. moveUpBtn.OnClick += () => parent.OnMoveUpButtonClicked(seqIndex);
  538. moveDownBtn.OnClick += () => parent.OnMoveDownButtonClicked(seqIndex);
  539. titleLayout.AddElement(cloneBtn);
  540. titleLayout.AddElement(deleteBtn);
  541. titleLayout.AddElement(moveUpBtn);
  542. titleLayout.AddElement(moveDownBtn);
  543. }
  544. /// <summary>
  545. /// Creates GUI elements specific to type in the array row.
  546. /// </summary>
  547. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  548. /// <returns>An optional title bar layout that the standard array buttons will be appended to.</returns>
  549. protected abstract GUILayoutX CreateGUI(GUILayoutY layout);
  550. /// <summary>
  551. /// Refreshes the GUI for the list row and checks if anything was modified.
  552. /// </summary>
  553. /// <returns>True if any modifications were made, false otherwise.</returns>
  554. internal protected virtual bool Refresh()
  555. {
  556. return false;
  557. }
  558. /// <summary>
  559. /// Gets the value contained in this list row.
  560. /// </summary>
  561. /// <typeparam name="T">Type of the value. Must match the list's element type.</typeparam>
  562. /// <returns>Value in this list row.</returns>
  563. protected T GetValue<T>()
  564. {
  565. return (T)parent.GetValue(seqIndex);
  566. }
  567. /// <summary>
  568. /// Sets the value contained in this list row.
  569. /// </summary>
  570. /// <typeparam name="T">Type of the value. Must match the list's element type.</typeparam>
  571. /// <param name="value">Value to set.</param>
  572. protected void SetValue<T>(T value)
  573. {
  574. parent.SetValue(seqIndex, value);
  575. }
  576. /// <summary>
  577. /// Destroys all row GUI elements.
  578. /// </summary>
  579. public void Destroy()
  580. {
  581. if (rowLayout != null)
  582. {
  583. rowLayout.Destroy();
  584. rowLayout = null;
  585. }
  586. }
  587. }
  588. }