using System.Buffers; using System.Globalization; using System.Collections; using System.Collections.Generic; using UnityEngine; using ObjectScripts; using CSVReader; using System.Linq; namespace UIScripts { public class UIEvents : MonoBehaviour { // Start is called before the first frame update [SerializeField] AbstractManager ManagerObject; [SerializeField] GameObject GameVelocityTextObject; [SerializeField] GameObject TimeLineObject; [SerializeField] AbstractEventHandler EventHandler; [SerializeField] GameObject UICamera; [SerializeField] Canvas UICanvas; [SerializeField] GameObject Radar; [SerializeField] GameObject EventButtonPrefab; [SerializeField] List EventButtonImages; [SerializeField] GameObject EventDropdownPrefab; [SerializeField] GameObject EventListDropdownPrefab; [SerializeField] UnityEngine.UI.InputField ChunkStartInputField; [SerializeField] UnityEngine.UI.InputField ChunkEndInputField; private UnityEngine.UI.Text GameVelocityText; private UnityEngine.UI.Slider GameTimelineSlider; [SerializeField] UnityEngine.UI.Text TimestampText; private bool running; private bool DraggingSlider; private Canvas ParentCanvas; private Vector2 RadarSize; private UnityEngine.UI.Image RadarImage; private Texture2D RadarTexture; private Color[] clearArray; private Color[] RadarPoint; private Color[] RadarCenter; private List EventButtons; private float Radarradius; private RectTransform SliderRectTransform; private RectTransform TimeLineObjectRectTransform; private RectTransform CanvasRectTransform; private Dictionary EventTimestamps; void Start() { EventTimestamps = new Dictionary(); EventButtons = new List(); // CanvasRectTransform = UICanvas.GetComponent(); // TimeLineObjectRectTransform = TimeLineObject.GetComponent(); // TimeLineObjectRectTransform.sizeDelta = new Vector2(CanvasRectTransform.sizeDelta.x, TimeLineObjectRectTransform.sizeDelta.y); // RectTransform radarRect = Radar.GetComponent(); // RadarSize = radarRect.sizeDelta; // SetupRadar(RadarSize, 100); // SetUpSliderValues(); running = true; DraggingSlider = false; StartCoroutine(ChangeSlider()); } private void SetUpSliderValues() { SliderRectTransform = this.TimeLineObject.GetComponent().GetComponent(); } public void SetupRadar(Vector2 radarSize, float radius) { Radarradius = radius; RadarImage = Radar.GetComponent(); RadarTexture = new Texture2D((int)radarSize.x, (int)radarSize.y); RadarImage.material.mainTexture = RadarTexture; clearArray = new Color[(int)(RadarSize.x * RadarSize.y)]; for (int i = 0; i < clearArray.Length; i++) { clearArray[i] = new Color(1, 1, 1, 0f); } RadarPoint = new Color[25]; RadarCenter = new Color[25]; for (int i = 0; i < RadarPoint.Length; i++) { RadarPoint[i] = Color.red; RadarCenter[i] = Color.black; } } private void OnDestroy() { running = false; } /// /// Coroutine.Updates the Slider every ManagerObject.UpdateRateInSeconds. /// /// private IEnumerator ChangeSlider() { double startChunk; double endChunk; while (running) { //if the slider isn't dragged if (!DraggingSlider) { //get the Slider this.GameTimelineSlider = this.TimeLineObject.GetComponent(); startChunk = ManagerObject.startChunk; endChunk = ManagerObject.endChunk; //Get the timestamps float gameTimeStamp = (float)this.ManagerObject.GameTimestamp; float minTimeStamp = double.IsNaN(startChunk) ? (float)this.ManagerObject.EarliestTimestamp : (float)startChunk; float maxTimeStamp = double.IsNaN(endChunk) ? (float)this.ManagerObject.NewestTimestamp : (float)endChunk; // gameTimeStamp = float.IsNaN(gameTimeStamp) ? 0 : gameTimeStamp; // minTimeStamp = float.IsPositiveInfinity(minTimeStamp) ? 0 : minTimeStamp; // maxTimeStamp = float.IsNegativeInfinity(maxTimeStamp) ? 0 : maxTimeStamp; //set the Values of the slider // Maybe normalize it to 0-1 this.GameTimelineSlider.maxValue = maxTimeStamp; this.GameTimelineSlider.minValue = minTimeStamp; // this.GameTimelineSlider.value = gameTimeStamp; this.GameTimelineSlider.SetValueWithoutNotify(gameTimeStamp); // print(this.GameTimelineSlider.normalizedValue.ToString()); } yield return new WaitForSecondsRealtime(this.ManagerObject.UpdateRateInSeconds); } } // Update is called once per frame void Update() { // Show the current Veloctiy this.GameVelocityText = GameVelocityTextObject.GetComponent(); this.GameVelocityText.text = string.Format("{0}{1}", ManagerObject.forward ? "" : "-", ManagerObject.GameVelocity.ToString()); //Show the current Gametimestamp // this.TimestampText = GameObject.Find("TimestampText").GetComponent(); this.TimestampText.text = this.GameTimelineSlider.value.ToString("0.00") + " : " + this.ManagerObject.NewestTimestamp.ToString("0.00"); //--------------------------------------------- List eventList = EventHandler.EventList; Transform camera = UICamera.transform; // RadarTexture.SetPixels(clearArray); // RadarTexture.SetPixels((int)RadarSize.x / 2, (int)RadarSize.y / 2, 5, 5, RadarCenter); // RadarTexture.Apply(); this.GameTimelineSlider = this.TimeLineObject.GetComponent(); for (int i = 0; i < eventList.Count; i++) { AbstractEventScript e = eventList[i]; HandleEventButtons(e); // if (EventInRadar(e, camera.position, Radarradius)) // DrawEventOnRadar(camera, e); } RadarTexture.Apply(); } private void addEventToEventDropdown(uint id, int type, double timestamp) { UnityEngine.UI.Dropdown dropdown = EventListDropdownPrefab.GetComponent(); string optionText = id.ToString() + ") " + "Type: " + type.ToString() + "timestamp: " + timestamp.ToString(); UnityEngine.UI.Dropdown.OptionData option = new UnityEngine.UI.Dropdown.OptionData(optionText); dropdown.options.Add(option); } public void ScrollbarUP() { UnityEngine.UI.Scrollbar scrollbar = EventListDropdownPrefab.GetComponentInChildren(); UnityEngine.UI.Dropdown dropdown = EventListDropdownPrefab.GetComponent(); int size = dropdown.options.Count; if (size > 0) { float stepsize = 1 / (float)size; //Debug.LogError(stepsize); scrollbar.value = scrollbar.value + stepsize; } } public void ScrollbarDOWN() { UnityEngine.UI.Scrollbar scrollbar = EventListDropdownPrefab.GetComponentInChildren(); UnityEngine.UI.Dropdown dropdown = EventListDropdownPrefab.GetComponent(); int size = dropdown.options.Count; if (size > 0) { float stepsize = 1 / (float)size; //Debug.LogError(stepsize); scrollbar.value = scrollbar.value - stepsize; } } public void EventListJumpButtonPressed() { UnityEngine.UI.Dropdown dropdown = EventListDropdownPrefab.GetComponent(); if (dropdown.options.Count > 0) { string optionText = dropdown.options[dropdown.value].text; int id = int.Parse(optionText.Split(')')[0]); AbstractEventScript ev = EventHandler.EventList.Find(e => e.ID == id); if (ev != null) { OnEventButtonClick(ev); } } } private void HandleEventButtons(AbstractEventScript e) { Transform GameTimeLineSliderTransform = this.GameTimelineSlider.transform; double timestamp = e.Event.TimestampStart; uint id = e.ID; int type = (int)e.Event.Type; EventTimestamp eventTimestamp; if (this.EventTimestamps.TryGetValue(e.Event.TimestampStart, out eventTimestamp)) { if (!eventTimestamp.EventIDs.Contains(id)) { eventTimestamp.addEvent(e, EventButtonImages[type], () => OnEventButtonClick(e)); addEventToEventDropdown(id, type, timestamp); } } else { GameObject obj = Instantiate(this.EventDropdownPrefab); obj.transform.SetParent(this.transform.parent); obj.transform.localPosition = Vector3.zero; eventTimestamp = new EventTimestamp(timestamp, obj, this.GameTimelineSlider); this.EventTimestamps.Add(timestamp, eventTimestamp); } eventTimestamp.SetEventButtonPositon(e, this.UICamera); } private void SetEventButtonPositon(AbstractEventScript e, GameObject button) { Transform GameTimeLineSliderTransform = this.GameTimelineSlider.transform; Vector3 buttonPos = button.transform.localPosition; double timestamp = e.Event.TimestampStart; Vector2 leftCord = new Vector2(GameTimeLineSliderTransform.localPosition.x - SliderRectTransform.sizeDelta.x * SliderRectTransform.pivot.x, GameTimeLineSliderTransform.localPosition.y - SliderRectTransform.sizeDelta.y * SliderRectTransform.pivot.y); float minValue = this.GameTimelineSlider.minValue; float maxValue = this.GameTimelineSlider.maxValue; if (float.IsInfinity(minValue) || float.IsInfinity(maxValue) || float.IsNaN(minValue) || float.IsNaN(maxValue)) { minValue = 0; maxValue = 1; } float normalized = (float)(timestamp - minValue) / (maxValue - minValue); if (float.IsNaN(normalized)) normalized = -1; if (float.IsInfinity(normalized)) normalized = -1; if (normalized >= 0 && normalized < 1) { // if (!button.GetComponent().enabled || // !button.GetComponent().enabled) // { // button.GetComponent().enabled = true; // button.GetComponent().enabled = true; // } button.transform.localPosition = new Vector3(leftCord.x + SliderRectTransform.sizeDelta.x * normalized, leftCord.y, 0); } else { button.transform.position = new Vector3(0, 0, this.UICamera.transform.position.z); // button.GetComponent().enabled = false; // button.GetComponent().enabled = false; } } private void OnEventButtonClick(AbstractEventScript e) { this.GameTimelineSlider.SetValueWithoutNotify((float)e.Event.TimestampStart); ManagerObject.JumpToTimestamp((float)e.Event.TimestampStart); ManagerObject.StopPressed(); } private bool EventInRadar(AbstractEventScript e, Vector3 cameraPosition, float radius) { Vector2 cameraPosition2D = new Vector2(cameraPosition.x, cameraPosition.z); Vector3 e3 = e.EventPoint.transform.position; return Vector2.Distance(cameraPosition2D, new Vector2(e3.x, e3.z)) < radius; } public void BeginDrag() { this.DraggingSlider = true; } public void TimelineValueChanged() { if (!DraggingSlider) { float newValue = this.GameTimelineSlider.value; ManagerObject.JumpToTimestamp(newValue); } } public void ChunkStartInputFieldEndEdit() { double startValue = double.Parse(ChunkStartInputField.text, NumberStyles.Float); this.ManagerObject.SetChunkStarttimestamp(startValue); } public void ChunkEndInputFieldEndEdit() { double endValue = double.Parse(ChunkEndInputField.text, NumberStyles.Float); this.ManagerObject.SetChunkEndtimestamp(endValue); } public void StopDrag() { this.GameTimelineSlider = this.TimeLineObject.GetComponent(); //Calculate the new timestamp and set the Gametimestamp float newValue = this.GameTimelineSlider.value; this.GameTimelineSlider.SetValueWithoutNotify(newValue); ManagerObject.JumpToTimestamp(newValue); this.DraggingSlider = false; //Right now the objects will move to the new Place //Should implement a function that will directly set the "new" positions. //Also while dragging the simulation doesn't stop. Dont know if that should be changed. } public void PlayPressed() { ManagerObject.PlayPressed(); } public void StopPressed() { ManagerObject.StopPressed(); } public void ReversePressed() { ManagerObject.ReversePressed(); } public void PlusPressed() { ManagerObject.PlusPressed(); } public void MinusPressed() { ManagerObject.MinusPressed(); } } }