GUIListView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System.Collections.Generic;
  2. namespace BansheeEngine
  3. {
  4. /// <summary>
  5. /// GUI element that can efficiently display a list of entries that share the same height. This element is mostly an
  6. /// optimization as only visible entries have actual GUI elements, as opposed to just adding GUI elements directly
  7. /// in a vertical GUI layout. This allows the list view to have thousands of elements with little performance impact.
  8. ///
  9. /// Contains shared functionality used by all instances of <see cref="GUIListView{TEntry,TData}"/>.
  10. /// </summary>
  11. /// <typeparam name="TData">>Type used for storing the data for all list entries.</typeparam>
  12. public abstract class GUIListViewBase<TData>
  13. where TData : GUIListViewData
  14. {
  15. // TODO - Only fixed size is supported. It should be nice if this object could just be placed in layout like any
  16. // other GUI element. Would likely need some kind of a way to get notified when parent layout changes.
  17. // (Possibly add a callback to GUIPanel when updateLayout is called?)
  18. protected List<TData> entries = new List<TData>();
  19. protected GUIScrollArea scrollArea;
  20. protected GUILabel topPadding;
  21. protected GUILabel bottomPadding;
  22. protected int width;
  23. protected int height;
  24. protected int entryHeight;
  25. protected float scrollPct;
  26. protected bool scrollToLatest;
  27. protected int totalHeight;
  28. protected internal bool contentsDirty = true;
  29. /// <summary>
  30. /// Total number of entries in the list.
  31. /// </summary>
  32. public int NumEntries
  33. {
  34. get { return entries.Count; }
  35. }
  36. /// <summary>
  37. /// Height of a single entry in the list, in pixels.
  38. /// </summary>
  39. public int EntryHeight
  40. {
  41. get { return entryHeight; }
  42. set { entryHeight = value; }
  43. }
  44. /// <summary>
  45. /// Primary GUI scroll area that all entries are contained within.
  46. /// </summary>
  47. internal GUIScrollArea ScrollArea
  48. {
  49. get { return scrollArea; }
  50. }
  51. /// <summary>
  52. /// Creates a new empty list view.
  53. /// </summary>
  54. /// <param name="width">Width of the list view, in pixels.</param>
  55. /// <param name="height">Height of the list view, in pixels.</param>
  56. /// <param name="entryHeight">Height of a single element in the list, in pixels.</param>
  57. /// <param name="layout">GUI layout into which the list view will be placed into.</param>
  58. protected GUIListViewBase(int width, int height, int entryHeight, GUILayout layout)
  59. {
  60. scrollArea = new GUIScrollArea(ScrollBarType.ShowIfDoesntFit, ScrollBarType.NeverShow,
  61. GUIOption.FixedWidth(width), GUIOption.FixedHeight(height));
  62. layout.AddElement(scrollArea);
  63. topPadding = new GUILabel(new LocString());
  64. bottomPadding = new GUILabel(new LocString());
  65. scrollArea.Layout.AddElement(topPadding);
  66. scrollArea.Layout.AddElement(bottomPadding);
  67. this.width = width;
  68. this.height = height;
  69. this.entryHeight = entryHeight;
  70. }
  71. /// <summary>
  72. /// Adds a new entry to the end of the list.
  73. /// </summary>
  74. /// <param name="data">Data of the entry to add.</param>
  75. public void AddEntry(TData data)
  76. {
  77. entries.Add(data);
  78. contentsDirty = true;
  79. }
  80. /// <summary>
  81. /// Removes an entry from the specified index. If the index is out of range nothing happens.
  82. /// </summary>
  83. /// <param name="index">Sequential index of the element to remove from the list.</param>
  84. public void RemoveEntry(int index)
  85. {
  86. if (index >= 0 && index < entries.Count)
  87. {
  88. entries.RemoveAt(index);
  89. contentsDirty = true;
  90. }
  91. }
  92. /// <summary>
  93. /// Removes all entries from the list.
  94. /// </summary>
  95. public void Clear()
  96. {
  97. entries.Clear();
  98. contentsDirty = true;
  99. }
  100. /// <summary>
  101. /// Finds an index of the specified entry in the list.
  102. /// </summary>
  103. /// <param name="data">Data of the entry to search for.</param>
  104. /// <returns>Index of the entry if found, -1 otherwise.</returns>
  105. public int FindEntry(TData data)
  106. {
  107. return entries.FindIndex(x => x.Equals(data));
  108. }
  109. /// <summary>
  110. /// Adds a new entry at the specified index. If the index is out of range the entry is added at the end of the list.
  111. /// </summary>
  112. /// <param name="index">Sequential index at which to insert the entry. </param>
  113. /// <param name="data">Data of the entry to insert.</param>
  114. public void InsertEntry(int index, TData data)
  115. {
  116. if (index >= 0 && index <= entries.Count)
  117. entries.Insert(index, data);
  118. else
  119. entries.Add(data);
  120. contentsDirty = true;
  121. }
  122. /// <summary>
  123. /// Changes the size of the list view.
  124. /// </summary>
  125. /// <param name="width">Width in pixels.</param>
  126. /// <param name="height">Height in pixels.</param>
  127. public void SetSize(int width, int height)
  128. {
  129. if (width != this.width || height != this.height)
  130. {
  131. this.width = width;
  132. this.height = height;
  133. Rect2I bounds = scrollArea.Bounds;
  134. bounds.width = width;
  135. bounds.height = height;
  136. scrollArea.Bounds = bounds;
  137. }
  138. }
  139. /// <summary>
  140. /// Updates the visuals of the list view. Should be called once per frame.
  141. /// </summary>
  142. public abstract void Update();
  143. }
  144. /// <summary>
  145. /// GUI element that can efficiently display a list of entries that share the same height. This element is mostly an
  146. /// optimization as only visible entries have actual GUI elements, as opposed to just adding GUI elements directly
  147. /// in a vertical GUI layout. This allows the list view to have thousands of elements with little performance impact.
  148. /// </summary>
  149. /// <typeparam name="TEntry">Type used for creating and updating the GUI elements of the visible entries.</typeparam>
  150. /// <typeparam name="TData">Type used for storing the data for all list entries.</typeparam>
  151. public class GUIListView<TEntry, TData> : GUIListViewBase<TData>
  152. where TEntry : GUIListViewEntry<TData>, new()
  153. where TData : GUIListViewData
  154. {
  155. private List<TEntry> visibleEntries = new List<TEntry>();
  156. /// <summary>
  157. /// Creates a new empty list view.
  158. /// </summary>
  159. /// <param name="width">Width of the list view, in pixels.</param>
  160. /// <param name="height">Height of the list view, in pixels.</param>
  161. /// <param name="entryHeight">Height of a single element in the list, in pixels.</param>
  162. /// <param name="layout">GUI layout into which the list view will be placed into.</param>
  163. public GUIListView(int width, int height, int entryHeight, GUILayout layout)
  164. :base(width, height, entryHeight, layout)
  165. { }
  166. /// <inheritdoc/>
  167. public override void Update()
  168. {
  169. int numVisibleEntries = MathEx.CeilToInt(height / (float)entryHeight) + 1;
  170. numVisibleEntries = MathEx.Min(numVisibleEntries, entries.Count);
  171. while (visibleEntries.Count < numVisibleEntries)
  172. {
  173. TEntry newEntry = new TEntry();
  174. newEntry.Initialize(this);
  175. newEntry.panel.SetHeight(entryHeight);
  176. visibleEntries.Add(newEntry);
  177. contentsDirty = true;
  178. }
  179. while (numVisibleEntries < visibleEntries.Count)
  180. {
  181. int lastIdx = visibleEntries.Count - 1;
  182. visibleEntries[lastIdx].Destroy();
  183. visibleEntries.RemoveAt(lastIdx);
  184. contentsDirty = true;
  185. }
  186. if (scrollPct != scrollArea.VerticalScroll)
  187. {
  188. scrollPct = scrollArea.VerticalScroll;
  189. contentsDirty = true;
  190. }
  191. if (contentsDirty)
  192. {
  193. int newHeight = entries.Count * entryHeight;
  194. if (scrollToLatest)
  195. {
  196. if (totalHeight > height && scrollPct < 1.0f)
  197. scrollToLatest = false;
  198. }
  199. else
  200. {
  201. if (totalHeight <= height || scrollPct >= 1.0f)
  202. scrollToLatest = true;
  203. }
  204. totalHeight = newHeight;
  205. int maxScrollOffset = MathEx.Max(0, totalHeight - height - 1);
  206. int startPos = MathEx.FloorToInt(scrollPct * maxScrollOffset);
  207. int startIndex = MathEx.FloorToInt(startPos / (float)entryHeight);
  208. // Check if we're at the list bottom and the extra element is out of bounds
  209. if ((startIndex + visibleEntries.Count) > entries.Count)
  210. startIndex--; // Keep the extra element at the top always
  211. topPadding.SetHeight(startIndex * entryHeight);
  212. for (int i = 0; i < visibleEntries.Count; i++)
  213. {
  214. visibleEntries[i].UpdateContents(startIndex + i, entries[startIndex + i]);
  215. visibleEntries[i].panel.SetPosition(0, i * entryHeight);
  216. }
  217. int bottomPosition = MathEx.Min(totalHeight, (startIndex + visibleEntries.Count) * entryHeight);
  218. bottomPadding.SetHeight(totalHeight - bottomPosition);
  219. if (scrollToLatest)
  220. {
  221. if (newHeight <= height)
  222. scrollArea.VerticalScroll = 0.0f;
  223. else
  224. scrollArea.VerticalScroll = 1.0f;
  225. }
  226. contentsDirty = false;
  227. }
  228. }
  229. }
  230. /// <summary>
  231. /// Base class that contains data for individual entries used in <see cref="GUIListView{TEntry,TData}"/>.
  232. /// </summary>
  233. public class GUIListViewData
  234. {
  235. }
  236. /// <summary>
  237. /// Base class that displays GUI elements for visible entries used in <see cref="GUIListView{TEntry,TData}"/>.
  238. /// </summary>
  239. /// <typeparam name="TData">Type of object that contains data used for initializing the GUI elements.</typeparam>
  240. public abstract class GUIListViewEntry<TData>
  241. where TData : GUIListViewData
  242. {
  243. private GUIListViewBase<TData> parent;
  244. internal GUIPanel panel;
  245. internal GUILayoutY layout;
  246. protected GUILayout Layout { get { return layout; } }
  247. /// <summary>
  248. /// Initializes the GUI elements for the entry.
  249. /// </summary>
  250. /// <param name="parent">Scroll area into whose layout to insert the GUI elements.</param>
  251. internal void Initialize(GUIListViewBase<TData> parent)
  252. {
  253. this.parent = parent;
  254. GUIScrollArea scrollArea = parent.ScrollArea;
  255. int numElements = scrollArea.Layout.ChildCount;
  256. // Last panel is always the padding panel, so keep it there
  257. panel = scrollArea.Layout.InsertPanel(numElements - 1);
  258. layout = panel.AddLayoutY();
  259. BuildGUI();
  260. }
  261. /// <summary>
  262. /// Destoys the GUI elements for the entry.
  263. /// </summary>
  264. internal void Destroy()
  265. {
  266. panel.Destroy();
  267. }
  268. /// <summary>
  269. /// Causes all visible entries in the parent list to be updated.
  270. /// </summary>
  271. protected void RefreshEntries()
  272. {
  273. parent.contentsDirty = true;
  274. }
  275. /// <summary>
  276. /// Allows child classes to create GUI elements required by their entry specialization.
  277. /// </summary>
  278. public abstract void BuildGUI();
  279. /// <summary>
  280. /// Allows child classes to update GUI element(s) with new contents.
  281. /// </summary>
  282. /// <param name="index">Sequential index of the entry in the list.</param>
  283. /// <param name="data">Data of the entry to display.</param>
  284. public abstract void UpdateContents(int index, TData data);
  285. }
  286. }