EventList.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System.Linq;
  2. using System.Net.Mime;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using CSVReader;
  8. using ObjectScripts;
  9. namespace UIScripts
  10. {
  11. public class EventList : MonoBehaviour
  12. {
  13. ///<summary>
  14. /// The Canvas Containing the EventList
  15. ///</summary>
  16. [SerializeField] GameObject ScrollCanvas;
  17. ///<summary>
  18. /// The Prefab for a eventpage in the eventlist
  19. ///</summary>
  20. [SerializeField] GameObject EventPage;
  21. ///<summary>
  22. /// the prefab for a eventpage entry in the eventlist
  23. ///</summary>
  24. [SerializeField] GameObject EventPageEntry;
  25. ///<summary>
  26. /// the prafab for a entry in the eventpage entry
  27. ///</summary>
  28. [SerializeField] GameObject EventEntry;
  29. ///<summary>
  30. /// Eventhandler to get events
  31. ///</summary>
  32. [SerializeField] AbstractEventHandler EventHandler;
  33. ///<summary>
  34. /// Text that shows the curren page timestamp
  35. ///</summary>
  36. [SerializeField] UnityEngine.UI.Text TimestampText;
  37. ///<summary>
  38. /// Button prefab for each entry
  39. ///</summary>
  40. [SerializeField] UnityEngine.UI.Button JumpButton;
  41. ///<summary>
  42. /// Manager to control
  43. ///</summary>
  44. [SerializeField] AbstractManager Manager;
  45. ///<summary>
  46. /// Dictionary to keep track of each page in the eventlist
  47. ///</summary>
  48. Dictionary<double, GameObject> PageDictionary;
  49. ///<summary>
  50. /// Dicitonary to keep track of each entry in a page
  51. ///</summary>
  52. Dictionary<GameObject, List<GameObject>> EventPageEntries;
  53. ///<summary>
  54. /// number of already read events
  55. ///</summary>
  56. int KnownCount = 0;
  57. ///<summary>
  58. /// Anchor for positioning the entries
  59. ///</summary>
  60. Vector2 Anchor = new Vector2(0.5f, 1);
  61. ///<summary>
  62. /// Pivot for positioning the entries
  63. ///</summary>
  64. Vector2 Pivot = new Vector2(0.5f, 0.5f);
  65. ///<summary>
  66. /// The currently shown page
  67. ///</summary>
  68. GameObject CurrentPage = null;
  69. ///<summary>
  70. /// the currently shown timestamp of the page
  71. ///</summary>
  72. double CurrentTimestamp = -1;
  73. ///<summary>
  74. /// Scrollrect to control the list
  75. ///</summary>
  76. UnityEngine.UI.ScrollRect ScrollRect;
  77. ///<summary>
  78. /// size of each step after pressing up or down
  79. ///</summary>
  80. float Stepsize = 30;
  81. void Start()
  82. {
  83. PageDictionary = new Dictionary<double, GameObject>();
  84. EventPageEntries = new Dictionary<GameObject, List<GameObject>>();
  85. ScrollRect = ScrollCanvas.GetComponent<UnityEngine.UI.ScrollRect>();
  86. }
  87. // Update is called once per frame
  88. void Update()
  89. {
  90. //handle new events
  91. for (int i = KnownCount; i < EventHandler.EventList.Count; i++)
  92. {
  93. //get timestamp
  94. double timestamp = EventHandler.EventList[i].Event.TimestampStart;
  95. GameObject page;
  96. GameObject pageEntry;
  97. List<GameObject> eventPageEntries;
  98. //check if there is a page for that timestamp
  99. //if not create a new page
  100. if (PageDictionary.TryGetValue(timestamp, out page))
  101. {
  102. eventPageEntries = EventPageEntries[page];
  103. }
  104. else
  105. {
  106. page = Instantiate(EventPage, EventPage.transform.parent);
  107. page.SetActive(false);
  108. PageDictionary.Add(timestamp, page);
  109. eventPageEntries = new List<GameObject>();
  110. EventPageEntries.Add(page, eventPageEntries);
  111. if (CurrentPage == null)
  112. {
  113. ScrollRect.content = page.GetComponent<RectTransform>();
  114. CurrentTimestamp = timestamp;
  115. TimestampText.text = timestamp.ToString();
  116. page.SetActive(true);
  117. CurrentPage = page;
  118. }
  119. }
  120. //create page entry
  121. pageEntry = Instantiate(EventPageEntry, page.transform);
  122. pageEntry.SetActive(true);
  123. GameObject entry = pageEntry.transform.Find(EventEntry.name).gameObject;
  124. UnityEngine.UI.Text text = entry.transform.Find("Text").gameObject.GetComponent<UnityEngine.UI.Text>();
  125. UnityEngine.UI.Button button = entry.transform.Find(JumpButton.name).gameObject.GetComponent<UnityEngine.UI.Button>();
  126. button.onClick.AddListener(delegate { onJumpButton(timestamp); });
  127. //the text to show
  128. text.text = string.Format("EventType: {0} - Pos:{1} - EndTime:{2}", EventHandler.EventList[i].Event.Type, EventHandler.EventList[i].Event.Pos, EventHandler.EventList[i].Event.TimestampEnd);
  129. //set the postion
  130. RectTransform rectPageEntry = pageEntry.GetComponent<RectTransform>();
  131. rectPageEntry.anchorMax = Anchor;
  132. rectPageEntry.anchorMin = Anchor;
  133. Vector2 pos = -new Vector2(0, eventPageEntries.Count * rectPageEntry.sizeDelta.y + rectPageEntry.sizeDelta.y / 2);
  134. rectPageEntry.anchoredPosition = pos;
  135. //add the entry to the page
  136. eventPageEntries.Add(pageEntry);
  137. //increase the page size
  138. page.GetComponent<RectTransform>().sizeDelta += new Vector2(0, rectPageEntry.sizeDelta.y);
  139. //increase the count
  140. KnownCount += 1;
  141. }
  142. }
  143. ///<summary>
  144. /// On Button in List pressed
  145. ///</summary>
  146. void onJumpButton(double timestamp)
  147. {
  148. Manager.JumpToTimestamp(timestamp);
  149. Manager.StopPressed();
  150. }
  151. ///<summary>
  152. /// On up button pressed
  153. ///</summary>
  154. public void ScrollbarUP()
  155. {
  156. CurrentPage.GetComponent<RectTransform>().anchoredPosition = CurrentPage.GetComponent<RectTransform>().anchoredPosition - new Vector2(0, Stepsize);
  157. }
  158. ///<summary>
  159. /// On down button pressed
  160. ///</summary>
  161. public void ScrollbarDOWN()
  162. {
  163. CurrentPage.GetComponent<RectTransform>().anchoredPosition = CurrentPage.GetComponent<RectTransform>().anchoredPosition + new Vector2(0, Stepsize);
  164. }
  165. ///<summary>
  166. /// get previous page
  167. ///</summary>
  168. public void OnLeft()
  169. {
  170. double nextPageTimestamp = PageDictionary.Keys.Where(k => k < CurrentTimestamp).DefaultIfEmpty().Max();
  171. GameObject nextPage;
  172. if (PageDictionary.TryGetValue(nextPageTimestamp, out nextPage))
  173. {
  174. nextPage = PageDictionary[nextPageTimestamp];
  175. ScrollRect.content = nextPage.GetComponent<RectTransform>();
  176. CurrentPage.SetActive(false);
  177. CurrentTimestamp = nextPageTimestamp;
  178. TimestampText.text = CurrentTimestamp.ToString();
  179. nextPage.SetActive(true);
  180. CurrentPage = nextPage;
  181. CurrentPage.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -CurrentPage.GetComponent<RectTransform>().sizeDelta.y);
  182. }
  183. }
  184. ///<summary>
  185. /// get next page
  186. ///</summary>
  187. public void OnRight()
  188. {
  189. double nextPageTimestamp = PageDictionary.Keys.Where(k => k > CurrentTimestamp).DefaultIfEmpty().Min();
  190. GameObject nextPage;
  191. if (PageDictionary.TryGetValue(nextPageTimestamp, out nextPage))
  192. {
  193. nextPage = PageDictionary[nextPageTimestamp];
  194. ScrollRect.content = nextPage.GetComponent<RectTransform>();
  195. CurrentPage.SetActive(false);
  196. CurrentTimestamp = nextPageTimestamp;
  197. TimestampText.text = CurrentTimestamp.ToString();
  198. nextPage.SetActive(true);
  199. CurrentPage = nextPage;
  200. CurrentPage.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -CurrentPage.GetComponent<RectTransform>().sizeDelta.y);
  201. }
  202. }
  203. }
  204. }