GUIListView.cs 12 KB

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