GUIListField.cs 26 KB

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