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
{
///
/// The Canvas Containing the EventList
///
[SerializeField] GameObject ScrollCanvas;
///
/// The Prefab for a eventpage in the eventlist
///
[SerializeField] GameObject EventPage;
///
/// the prefab for a eventpage entry in the eventlist
///
[SerializeField] GameObject EventPageEntry;
///
/// the prafab for a entry in the eventpage entry
///
[SerializeField] GameObject EventEntry;
///
/// Eventhandler to get events
///
[SerializeField] AbstractEventHandler EventHandler;
///
/// Text that shows the curren page timestamp
///
[SerializeField] UnityEngine.UI.Text TimestampText;
///
/// Button prefab for each entry
///
[SerializeField] UnityEngine.UI.Button JumpButton;
///
/// Manager to control
///
[SerializeField] AbstractManager Manager;
///
/// Dictionary to keep track of each page in the eventlist
///
Dictionary PageDictionary;
///
/// Dicitonary to keep track of each entry in a page
///
Dictionary> EventPageEntries;
///
/// number of already read events
///
int KnownCount = 0;
///
/// Anchor for positioning the entries
///
Vector2 Anchor = new Vector2(0.5f, 1);
///
/// Pivot for positioning the entries
///
Vector2 Pivot = new Vector2(0.5f, 0.5f);
///
/// The currently shown page
///
GameObject CurrentPage = null;
///
/// the currently shown timestamp of the page
///
double CurrentTimestamp = -1;
///
/// Scrollrect to control the list
///
UnityEngine.UI.ScrollRect ScrollRect;
///
/// size of each step after pressing up or down
///
float Stepsize = 30;
void Start()
{
PageDictionary = new Dictionary();
EventPageEntries = new Dictionary>();
ScrollRect = ScrollCanvas.GetComponent();
}
// 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 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();
EventPageEntries.Add(page, eventPageEntries);
if (CurrentPage == null)
{
ScrollRect.content = page.GetComponent();
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.Button button = entry.transform.Find(JumpButton.name).gameObject.GetComponent();
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();
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().sizeDelta += new Vector2(0, rectPageEntry.sizeDelta.y);
//increase the count
KnownCount += 1;
}
}
///
/// On Button in List pressed
///
void onJumpButton(double timestamp)
{
Manager.JumpToTimestamp(timestamp);
Manager.StopPressed();
}
///
/// On up button pressed
///
public void ScrollbarUP()
{
CurrentPage.GetComponent().anchoredPosition = CurrentPage.GetComponent().anchoredPosition - new Vector2(0, Stepsize);
}
///
/// On down button pressed
///
public void ScrollbarDOWN()
{
CurrentPage.GetComponent().anchoredPosition = CurrentPage.GetComponent().anchoredPosition + new Vector2(0, Stepsize);
}
///
/// get previous page
///
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();
CurrentPage.SetActive(false);
CurrentTimestamp = nextPageTimestamp;
TimestampText.text = CurrentTimestamp.ToString();
nextPage.SetActive(true);
CurrentPage = nextPage;
CurrentPage.GetComponent().anchoredPosition = new Vector2(0, -CurrentPage.GetComponent().sizeDelta.y);
}
}
///
/// get next page
///
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();
CurrentPage.SetActive(false);
CurrentTimestamp = nextPageTimestamp;
TimestampText.text = CurrentTimestamp.ToString();
nextPage.SetActive(true);
CurrentPage = nextPage;
CurrentPage.GetComponent().anchoredPosition = new Vector2(0, -CurrentPage.GetComponent().sizeDelta.y);
}
}
}
}