123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using ObjectScripts;
- namespace UIScripts
- {
- public class EventStamp : MonoBehaviour
- {
- // Start is called before the first frame update
- ///<summary>
- /// Manager to control
- ///</summary>
- public AbstractManager Manager { get; set; }
- ///<summary>
- /// Timeline slider
- ///</summary>
- public UnityEngine.UI.Slider Slider { get; set; }
- ///<summary>
- /// timestamp
- ///</summary>
- public double Timestamp;
- ///<summary>
- /// ReactTransform of timeline slider
- ///</summary>
- private RectTransform SliderRect;
- ///<summary>
- /// Transform of timeline slider
- ///</summary>
- private Transform SliderTransform;
- ///<summary>
- ///
- ///</summary>
- private float leftx, minValue, maxValue, normalized;
- ///<summary>
- /// Number of events for that timestamp
- ///</summary>
- private int Ncontent = 0;
- ///<summary>
- /// Contentfield
- ///</summary>
- [SerializeField] public GameObject ContentField;
- ///<summary>
- /// EventField
- ///</summary>
- [SerializeField] public GameObject EventField;
- ///<summary>
- /// Button to jump to timestamp
- ///</summary>
- [SerializeField] public GameObject EventButton;
- ///<summary>
- ///
- ///</summary>
- [SerializeField] UnityEngine.UI.Scrollbar Scrollbar;
- ///<summary>
- ///
- ///</summary>
- [SerializeField] UnityEngine.UI.ScrollRect ScrollRect;
- ///<summary>
- /// size of the contentfield
- ///</summary>
- private float size;
- ///<summary>
- /// size of one event
- ///</summary>
- private float EventFieldSize;
- ///<summary>
- /// timechunks
- ///</summary>
- private float EndChunk, StartChunk;
- private RectTransform ContentFieldRect;
- ///<summary>
- /// Stepsize for one scroll up/down press
- ///</summary>
- private float Stepsize = 30;
- ///<summary>
- /// CanavasObject to change visbility
- ///</summary>
- private Canvas CanvasObject;
- void Start()
- {
- SliderRect = Slider.GetComponent<RectTransform>();
- SliderTransform = Slider.transform;
- ContentFieldRect = ContentField.GetComponent<RectTransform>();
- CanvasObject = GetComponent<Canvas>();
- }
- // Update is called once per frame
- void Update()
- {
- //Move Timestamp on timeline
- EndChunk = (float)Manager.endChunk;
- StartChunk = (float)Manager.startChunk;
- //left slider point
- leftx = SliderTransform.localPosition.x - SliderRect.sizeDelta.x * SliderRect.pivot.x;
- //values of the slider
- minValue = Slider.minValue;
- maxValue = Slider.maxValue;
- if (float.IsInfinity(minValue) || float.IsInfinity(maxValue) || float.IsNaN(minValue) || float.IsNaN(maxValue))
- {
- minValue = 0;
- maxValue = 1;
- }
- //get normalized value of timestamp for slider
- normalized = (float)(Timestamp - minValue) / (maxValue - minValue);
- if (float.IsNaN(normalized))
- normalized = -1;
- if (float.IsInfinity(normalized))
- normalized = -1;
- if (normalized >= 0 && normalized < 1)
- {
- //set visible and position
- CanvasObject.enabled = true;
- transform.localPosition = new Vector3(leftx + SliderRect.sizeDelta.x * normalized, 0, 0);
- }
- else
- {
- CanvasObject.enabled = false;
- }
- // }
- }
- ///<summary>
- /// On button pressed
- ///</summary>
- public void OnEventButtonClick()
- {
- Manager.JumpToTimestamp(Timestamp);
- Manager.StopPressed();
- }
- ///<summary>
- /// add event to timestamp
- ///</summary>
- public void addContent(AbstractEventScript eventScript, Sprite sprite)
- {
- GameObject eventField = Instantiate(EventField, EventField.transform.position, EventField.transform.rotation, EventField.transform.parent);
- GameObject eventButton = Instantiate(EventButton, eventField.transform);
- RectTransform eventFieldRect = eventField.GetComponent<RectTransform>();
- EventFieldSize = eventFieldRect.sizeDelta.y;
- eventFieldRect.anchorMax = new Vector2(0.5f, 1);
- eventFieldRect.anchorMin = new Vector2(0.5f, 1);
- ContentField.GetComponent<RectTransform>().sizeDelta = new Vector2(0, Mathf.RoundToInt(Ncontent * EventFieldSize));
- size = ContentField.GetComponent<RectTransform>().sizeDelta.y;
- eventFieldRect.localPosition -= new Vector3(0, Mathf.RoundToInt(Ncontent * EventFieldSize + EventFieldSize / 2), 0);
- eventButton.GetComponent<UnityEngine.UI.Image>().sprite = sprite;
- eventField.SetActive(true);
- eventButton.SetActive(true);
- Ncontent += 1;
- }
- ///<summary>
- /// On scrollbar up pressed
- ///</summary>
- public void ScrollbarUP()
- {
- ContentField.GetComponent<RectTransform>().anchoredPosition = ContentField.GetComponent<RectTransform>().anchoredPosition - new Vector2(0, Stepsize);
- }
- ///<summary>
- /// on scrollbar down pressed
- ///</summary>
- public void ScrollbarDOWN()
- {
- ContentField.GetComponent<RectTransform>().anchoredPosition = ContentField.GetComponent<RectTransform>().anchoredPosition + new Vector2(0, Stepsize);
- }
- }
- }
|