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 /// /// Manager to control /// public AbstractManager Manager { get; set; } /// /// Timeline slider /// public UnityEngine.UI.Slider Slider { get; set; } /// /// timestamp /// public double Timestamp; /// /// ReactTransform of timeline slider /// private RectTransform SliderRect; /// /// Transform of timeline slider /// private Transform SliderTransform; /// /// /// private float leftx, minValue, maxValue, normalized; /// /// Number of events for that timestamp /// private int Ncontent = 0; /// /// Contentfield /// [SerializeField] public GameObject ContentField; /// /// EventField /// [SerializeField] public GameObject EventField; /// /// Button to jump to timestamp /// [SerializeField] public GameObject EventButton; /// /// /// [SerializeField] UnityEngine.UI.Scrollbar Scrollbar; /// /// /// [SerializeField] UnityEngine.UI.ScrollRect ScrollRect; /// /// size of the contentfield /// private float size; /// /// size of one event /// private float EventFieldSize; /// /// timechunks /// private float EndChunk, StartChunk; private RectTransform ContentFieldRect; /// /// Stepsize for one scroll up/down press /// private float Stepsize = 30; /// /// CanavasObject to change visbility /// private Canvas CanvasObject; void Start() { SliderRect = Slider.GetComponent(); SliderTransform = Slider.transform; ContentFieldRect = ContentField.GetComponent(); CanvasObject = GetComponent(); } // 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; } // } } /// /// On button pressed /// public void OnEventButtonClick() { Manager.JumpToTimestamp(Timestamp); Manager.StopPressed(); } /// /// add event to timestamp /// 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(); EventFieldSize = eventFieldRect.sizeDelta.y; eventFieldRect.anchorMax = new Vector2(0.5f, 1); eventFieldRect.anchorMin = new Vector2(0.5f, 1); ContentField.GetComponent().sizeDelta = new Vector2(0, Mathf.RoundToInt(Ncontent * EventFieldSize)); size = ContentField.GetComponent().sizeDelta.y; eventFieldRect.localPosition -= new Vector3(0, Mathf.RoundToInt(Ncontent * EventFieldSize + EventFieldSize / 2), 0); eventButton.GetComponent().sprite = sprite; eventField.SetActive(true); eventButton.SetActive(true); Ncontent += 1; } /// /// On scrollbar up pressed /// public void ScrollbarUP() { ContentField.GetComponent().anchoredPosition = ContentField.GetComponent().anchoredPosition - new Vector2(0, Stepsize); } /// /// on scrollbar down pressed /// public void ScrollbarDOWN() { ContentField.GetComponent().anchoredPosition = ContentField.GetComponent().anchoredPosition + new Vector2(0, Stepsize); } } }