123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- 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<Sprite> 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<GameObject> EventButtons;
- private float Radarradius;
- private RectTransform SliderRectTransform;
- private RectTransform TimeLineObjectRectTransform;
- private RectTransform CanvasRectTransform;
- private Dictionary<double, EventTimestamp> EventTimestamps;
- void Start()
- {
- EventTimestamps = new Dictionary<double, EventTimestamp>();
- EventButtons = new List<GameObject>();
- // CanvasRectTransform = UICanvas.GetComponent<RectTransform>();
- // TimeLineObjectRectTransform = TimeLineObject.GetComponent<RectTransform>();
- // TimeLineObjectRectTransform.sizeDelta = new Vector2(CanvasRectTransform.sizeDelta.x, TimeLineObjectRectTransform.sizeDelta.y);
- // RectTransform radarRect = Radar.GetComponent<RectTransform>();
- // RadarSize = radarRect.sizeDelta;
- // SetupRadar(RadarSize, 100);
- // SetUpSliderValues();
- running = true;
- DraggingSlider = false;
- StartCoroutine(ChangeSlider());
- }
- private void SetUpSliderValues()
- {
- SliderRectTransform = this.TimeLineObject.GetComponent<UnityEngine.UI.Slider>().GetComponent<RectTransform>();
- }
- public void SetupRadar(Vector2 radarSize, float radius)
- {
- Radarradius = radius;
- RadarImage = Radar.GetComponent<UnityEngine.UI.Image>();
- 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;
- }
- ///<summary>
- /// Coroutine.Updates the Slider every ManagerObject.UpdateRateInSeconds.
- ///
- ///</summary>
- 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<UnityEngine.UI.Slider>();
- 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<UnityEngine.UI.Text>();
- this.GameVelocityText.text = string.Format("{0}{1}", ManagerObject.forward ? "" : "-", ManagerObject.GameVelocity.ToString());
- //Show the current Gametimestamp
- // this.TimestampText = GameObject.Find("TimestampText").GetComponent<UnityEngine.UI.Text>();
- this.TimestampText.text = this.GameTimelineSlider.value.ToString("0.00") + " : " + this.ManagerObject.NewestTimestamp.ToString("0.00");
- //---------------------------------------------
- List<AbstractEventScript> 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<UnityEngine.UI.Slider>();
- 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<UnityEngine.UI.Dropdown>();
- 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.Scrollbar>();
- UnityEngine.UI.Dropdown dropdown = EventListDropdownPrefab.GetComponent<UnityEngine.UI.Dropdown>();
- 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.Scrollbar>();
- UnityEngine.UI.Dropdown dropdown = EventListDropdownPrefab.GetComponent<UnityEngine.UI.Dropdown>();
- 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<UnityEngine.UI.Dropdown>();
- 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<UnityEngine.UI.Button>().enabled ||
- // !button.GetComponent<UnityEngine.UI.Image>().enabled)
- // {
- // button.GetComponent<UnityEngine.UI.Button>().enabled = true;
- // button.GetComponent<UnityEngine.UI.Image>().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<UnityEngine.UI.Button>().enabled = false;
- // button.GetComponent<UnityEngine.UI.Image>().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<UnityEngine.UI.Slider>();
- //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();
- }
- }
- }
|