GUIListField.cs 27 KB

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