123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using System.Linq;
- using System.Net.Mime;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using CSVReader;
- using ObjectScripts;
- namespace UIScripts
- {
- public class EventList : MonoBehaviour
- {
- ///<summary>
- /// The Canvas Containing the EventList
- ///</summary>
- [SerializeField] GameObject ScrollCanvas;
- ///<summary>
- /// The Prefab for a eventpage in the eventlist
- ///</summary>
- [SerializeField] GameObject EventPage;
- ///<summary>
- /// the prefab for a eventpage entry in the eventlist
- ///</summary>
- [SerializeField] GameObject EventPageEntry;
- ///<summary>
- /// the prafab for a entry in the eventpage entry
- ///</summary>
- [SerializeField] GameObject EventEntry;
- ///<summary>
- /// Eventhandler to get events
- ///</summary>
- [SerializeField] AbstractEventHandler EventHandler;
- ///<summary>
- /// Text that shows the curren page timestamp
- ///</summary>
- [SerializeField] UnityEngine.UI.Text TimestampText;
- ///<summary>
- /// Button prefab for each entry
- ///</summary>
- [SerializeField] UnityEngine.UI.Button JumpButton;
- ///<summary>
- /// Manager to control
- ///</summary>
- [SerializeField] AbstractManager Manager;
- ///<summary>
- /// Dictionary to keep track of each page in the eventlist
- ///</summary>
- Dictionary<double, GameObject> PageDictionary;
- ///<summary>
- /// Dicitonary to keep track of each entry in a page
- ///</summary>
- Dictionary<GameObject, List<GameObject>> EventPageEntries;
- ///<summary>
- /// number of already read events
- ///</summary>
- int KnownCount = 0;
- ///<summary>
- /// Anchor for positioning the entries
- ///</summary>
- Vector2 Anchor = new Vector2(0.5f, 1);
- ///<summary>
- /// Pivot for positioning the entries
- ///</summary>
- Vector2 Pivot = new Vector2(0.5f, 0.5f);
- ///<summary>
- /// The currently shown page
- ///</summary>
- GameObject CurrentPage = null;
- ///<summary>
- /// the currently shown timestamp of the page
- ///</summary>
- double CurrentTimestamp = -1;
- ///<summary>
- /// Scrollrect to control the list
- ///</summary>
- UnityEngine.UI.ScrollRect ScrollRect;
- ///<summary>
- /// size of each step after pressing up or down
- ///</summary>
- float Stepsize = 30;
- void Start()
- {
- PageDictionary = new Dictionary<double, GameObject>();
- EventPageEntries = new Dictionary<GameObject, List<GameObject>>();
- ScrollRect = ScrollCanvas.GetComponent<UnityEngine.UI.ScrollRect>();
- }
- // Update is called once per frame
- void Update()
- {
- //handle new events
- for (int i = KnownCount; i < EventHandler.EventList.Count; i++)
- {
- //get timestamp
- double timestamp = EventHandler.EventList[i].Event.TimestampStart;
- GameObject page;
- GameObject pageEntry;
- List<GameObject> eventPageEntries;
- //check if there is a page for that timestamp
- //if not create a new page
- if (PageDictionary.TryGetValue(timestamp, out page))
- {
- eventPageEntries = EventPageEntries[page];
- }
- else
- {
- page = Instantiate(EventPage, EventPage.transform.parent);
- page.SetActive(false);
- PageDictionary.Add(timestamp, page);
- eventPageEntries = new List<GameObject>();
- EventPageEntries.Add(page, eventPageEntries);
- if (CurrentPage == null)
- {
- ScrollRect.content = page.GetComponent<RectTransform>();
- CurrentTimestamp = timestamp;
- TimestampText.text = timestamp.ToString();
- page.SetActive(true);
- CurrentPage = page;
- }
- }
- //create page entry
- pageEntry = Instantiate(EventPageEntry, page.transform);
- pageEntry.SetActive(true);
- GameObject entry = pageEntry.transform.Find(EventEntry.name).gameObject;
- UnityEngine.UI.Text text = entry.transform.Find("Text").gameObject.GetComponent<UnityEngine.UI.Text>();
- UnityEngine.UI.Button button = entry.transform.Find(JumpButton.name).gameObject.GetComponent<UnityEngine.UI.Button>();
- button.onClick.AddListener(delegate { onJumpButton(timestamp); });
- //the text to show
- 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);
- //set the postion
- RectTransform rectPageEntry = pageEntry.GetComponent<RectTransform>();
- rectPageEntry.anchorMax = Anchor;
- rectPageEntry.anchorMin = Anchor;
- Vector2 pos = -new Vector2(0, eventPageEntries.Count * rectPageEntry.sizeDelta.y + rectPageEntry.sizeDelta.y / 2);
- rectPageEntry.anchoredPosition = pos;
- //add the entry to the page
- eventPageEntries.Add(pageEntry);
- //increase the page size
- page.GetComponent<RectTransform>().sizeDelta += new Vector2(0, rectPageEntry.sizeDelta.y);
- //increase the count
- KnownCount += 1;
- }
- }
- ///<summary>
- /// On Button in List pressed
- ///</summary>
- void onJumpButton(double timestamp)
- {
- Manager.JumpToTimestamp(timestamp);
- Manager.StopPressed();
- }
- ///<summary>
- /// On up button pressed
- ///</summary>
- public void ScrollbarUP()
- {
- CurrentPage.GetComponent<RectTransform>().anchoredPosition = CurrentPage.GetComponent<RectTransform>().anchoredPosition - new Vector2(0, Stepsize);
- }
- ///<summary>
- /// On down button pressed
- ///</summary>
- public void ScrollbarDOWN()
- {
- CurrentPage.GetComponent<RectTransform>().anchoredPosition = CurrentPage.GetComponent<RectTransform>().anchoredPosition + new Vector2(0, Stepsize);
- }
- ///<summary>
- /// get previous page
- ///</summary>
- public void OnLeft()
- {
- double nextPageTimestamp = PageDictionary.Keys.Where(k => k < CurrentTimestamp).DefaultIfEmpty().Max();
- GameObject nextPage;
- if (PageDictionary.TryGetValue(nextPageTimestamp, out nextPage))
- {
- nextPage = PageDictionary[nextPageTimestamp];
- ScrollRect.content = nextPage.GetComponent<RectTransform>();
- CurrentPage.SetActive(false);
- CurrentTimestamp = nextPageTimestamp;
- TimestampText.text = CurrentTimestamp.ToString();
- nextPage.SetActive(true);
- CurrentPage = nextPage;
- CurrentPage.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -CurrentPage.GetComponent<RectTransform>().sizeDelta.y);
- }
- }
- ///<summary>
- /// get next page
- ///</summary>
- public void OnRight()
- {
- double nextPageTimestamp = PageDictionary.Keys.Where(k => k > CurrentTimestamp).DefaultIfEmpty().Min();
- GameObject nextPage;
- if (PageDictionary.TryGetValue(nextPageTimestamp, out nextPage))
- {
- nextPage = PageDictionary[nextPageTimestamp];
- ScrollRect.content = nextPage.GetComponent<RectTransform>();
- CurrentPage.SetActive(false);
- CurrentTimestamp = nextPageTimestamp;
- TimestampText.text = CurrentTimestamp.ToString();
- nextPage.SetActive(true);
- CurrentPage = nextPage;
- CurrentPage.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -CurrentPage.GetComponent<RectTransform>().sizeDelta.y);
- }
- }
- }
- }
|