GUIListField.cs 22 KB

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