GUIListField.cs 21 KB

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