UIEvents.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using System.Buffers;
  2. using System.Globalization;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using ObjectScripts;
  7. using CSVReader;
  8. using System.Linq;
  9. namespace UIScripts
  10. {
  11. public class UIEvents : MonoBehaviour
  12. {
  13. // Start is called before the first frame update
  14. [SerializeField] AbstractManager ManagerObject;
  15. [SerializeField] GameObject GameVelocityTextObject;
  16. [SerializeField] GameObject TimeLineObject;
  17. [SerializeField] AbstractEventHandler EventHandler;
  18. [SerializeField] GameObject UICamera;
  19. [SerializeField] Canvas UICanvas;
  20. [SerializeField] GameObject Radar;
  21. [SerializeField] GameObject EventButtonPrefab;
  22. [SerializeField] List<Sprite> EventButtonImages;
  23. [SerializeField] GameObject EventDropdownPrefab;
  24. [SerializeField] GameObject EventListDropdownPrefab;
  25. [SerializeField] UnityEngine.UI.InputField ChunkStartInputField;
  26. [SerializeField] UnityEngine.UI.InputField ChunkEndInputField;
  27. private UnityEngine.UI.Text GameVelocityText;
  28. private UnityEngine.UI.Slider GameTimelineSlider;
  29. [SerializeField] UnityEngine.UI.Text TimestampText;
  30. private bool running;
  31. private bool DraggingSlider;
  32. private Canvas ParentCanvas;
  33. private Vector2 RadarSize;
  34. private UnityEngine.UI.Image RadarImage;
  35. private Texture2D RadarTexture;
  36. private Color[] clearArray;
  37. private Color[] RadarPoint;
  38. private Color[] RadarCenter;
  39. private List<GameObject> EventButtons;
  40. private float Radarradius;
  41. private RectTransform SliderRectTransform;
  42. private RectTransform TimeLineObjectRectTransform;
  43. private RectTransform CanvasRectTransform;
  44. private Dictionary<double, EventTimestamp> EventTimestamps;
  45. void Start()
  46. {
  47. EventTimestamps = new Dictionary<double, EventTimestamp>();
  48. EventButtons = new List<GameObject>();
  49. // CanvasRectTransform = UICanvas.GetComponent<RectTransform>();
  50. // TimeLineObjectRectTransform = TimeLineObject.GetComponent<RectTransform>();
  51. // TimeLineObjectRectTransform.sizeDelta = new Vector2(CanvasRectTransform.sizeDelta.x, TimeLineObjectRectTransform.sizeDelta.y);
  52. // RectTransform radarRect = Radar.GetComponent<RectTransform>();
  53. // RadarSize = radarRect.sizeDelta;
  54. // SetupRadar(RadarSize, 100);
  55. // SetUpSliderValues();
  56. running = true;
  57. DraggingSlider = false;
  58. StartCoroutine(ChangeSlider());
  59. }
  60. private void SetUpSliderValues()
  61. {
  62. SliderRectTransform = this.TimeLineObject.GetComponent<UnityEngine.UI.Slider>().GetComponent<RectTransform>();
  63. }
  64. public void SetupRadar(Vector2 radarSize, float radius)
  65. {
  66. Radarradius = radius;
  67. RadarImage = Radar.GetComponent<UnityEngine.UI.Image>();
  68. RadarTexture = new Texture2D((int)radarSize.x, (int)radarSize.y);
  69. RadarImage.material.mainTexture = RadarTexture;
  70. clearArray = new Color[(int)(RadarSize.x * RadarSize.y)];
  71. for (int i = 0; i < clearArray.Length; i++)
  72. {
  73. clearArray[i] = new Color(1, 1, 1, 0f);
  74. }
  75. RadarPoint = new Color[25];
  76. RadarCenter = new Color[25];
  77. for (int i = 0; i < RadarPoint.Length; i++)
  78. {
  79. RadarPoint[i] = Color.red;
  80. RadarCenter[i] = Color.black;
  81. }
  82. }
  83. private void OnDestroy()
  84. {
  85. running = false;
  86. }
  87. ///<summary>
  88. /// Coroutine.Updates the Slider every ManagerObject.UpdateRateInSeconds.
  89. ///
  90. ///</summary>
  91. private IEnumerator ChangeSlider()
  92. {
  93. double startChunk;
  94. double endChunk;
  95. while (running)
  96. {
  97. //if the slider isn't dragged
  98. if (!DraggingSlider)
  99. {
  100. //get the Slider
  101. this.GameTimelineSlider = this.TimeLineObject.GetComponent<UnityEngine.UI.Slider>();
  102. startChunk = ManagerObject.startChunk;
  103. endChunk = ManagerObject.endChunk;
  104. //Get the timestamps
  105. float gameTimeStamp = (float)this.ManagerObject.GameTimestamp;
  106. float minTimeStamp = double.IsNaN(startChunk) ? (float)this.ManagerObject.EarliestTimestamp : (float)startChunk;
  107. float maxTimeStamp = double.IsNaN(endChunk) ? (float)this.ManagerObject.NewestTimestamp : (float)endChunk;
  108. // gameTimeStamp = float.IsNaN(gameTimeStamp) ? 0 : gameTimeStamp;
  109. // minTimeStamp = float.IsPositiveInfinity(minTimeStamp) ? 0 : minTimeStamp;
  110. // maxTimeStamp = float.IsNegativeInfinity(maxTimeStamp) ? 0 : maxTimeStamp;
  111. //set the Values of the slider
  112. // Maybe normalize it to 0-1
  113. this.GameTimelineSlider.maxValue = maxTimeStamp;
  114. this.GameTimelineSlider.minValue = minTimeStamp;
  115. // this.GameTimelineSlider.value = gameTimeStamp;
  116. this.GameTimelineSlider.SetValueWithoutNotify(gameTimeStamp);
  117. // print(this.GameTimelineSlider.normalizedValue.ToString());
  118. }
  119. yield return new WaitForSecondsRealtime(this.ManagerObject.UpdateRateInSeconds);
  120. }
  121. }
  122. // Update is called once per frame
  123. void Update()
  124. {
  125. // Show the current Veloctiy
  126. this.GameVelocityText = GameVelocityTextObject.GetComponent<UnityEngine.UI.Text>();
  127. this.GameVelocityText.text = string.Format("{0}{1}", ManagerObject.forward ? "" : "-", ManagerObject.GameVelocity.ToString());
  128. //Show the current Gametimestamp
  129. // this.TimestampText = GameObject.Find("TimestampText").GetComponent<UnityEngine.UI.Text>();
  130. this.TimestampText.text = this.GameTimelineSlider.value.ToString("0.00") + " : " + this.ManagerObject.NewestTimestamp.ToString("0.00");
  131. //---------------------------------------------
  132. List<AbstractEventScript> eventList = EventHandler.EventList;
  133. Transform camera = UICamera.transform;
  134. // RadarTexture.SetPixels(clearArray);
  135. // RadarTexture.SetPixels((int)RadarSize.x / 2, (int)RadarSize.y / 2, 5, 5, RadarCenter);
  136. // RadarTexture.Apply();
  137. this.GameTimelineSlider = this.TimeLineObject.GetComponent<UnityEngine.UI.Slider>();
  138. for (int i = 0; i < eventList.Count; i++)
  139. {
  140. AbstractEventScript e = eventList[i];
  141. HandleEventButtons(e);
  142. // if (EventInRadar(e, camera.position, Radarradius))
  143. // DrawEventOnRadar(camera, e);
  144. }
  145. RadarTexture.Apply();
  146. }
  147. private void addEventToEventDropdown(uint id, int type, double timestamp)
  148. {
  149. UnityEngine.UI.Dropdown dropdown = EventListDropdownPrefab.GetComponent<UnityEngine.UI.Dropdown>();
  150. string optionText = id.ToString() + ") " + "Type: " + type.ToString() + "timestamp: " + timestamp.ToString();
  151. UnityEngine.UI.Dropdown.OptionData option = new UnityEngine.UI.Dropdown.OptionData(optionText);
  152. dropdown.options.Add(option);
  153. }
  154. public void ScrollbarUP()
  155. {
  156. UnityEngine.UI.Scrollbar scrollbar = EventListDropdownPrefab.GetComponentInChildren<UnityEngine.UI.Scrollbar>();
  157. UnityEngine.UI.Dropdown dropdown = EventListDropdownPrefab.GetComponent<UnityEngine.UI.Dropdown>();
  158. int size = dropdown.options.Count;
  159. if (size > 0)
  160. {
  161. float stepsize = 1 / (float)size;
  162. //Debug.LogError(stepsize);
  163. scrollbar.value = scrollbar.value + stepsize;
  164. }
  165. }
  166. public void ScrollbarDOWN()
  167. {
  168. UnityEngine.UI.Scrollbar scrollbar = EventListDropdownPrefab.GetComponentInChildren<UnityEngine.UI.Scrollbar>();
  169. UnityEngine.UI.Dropdown dropdown = EventListDropdownPrefab.GetComponent<UnityEngine.UI.Dropdown>();
  170. int size = dropdown.options.Count;
  171. if (size > 0)
  172. {
  173. float stepsize = 1 / (float)size;
  174. //Debug.LogError(stepsize);
  175. scrollbar.value = scrollbar.value - stepsize;
  176. }
  177. }
  178. public void EventListJumpButtonPressed()
  179. {
  180. UnityEngine.UI.Dropdown dropdown = EventListDropdownPrefab.GetComponent<UnityEngine.UI.Dropdown>();
  181. if (dropdown.options.Count > 0)
  182. {
  183. string optionText = dropdown.options[dropdown.value].text;
  184. int id = int.Parse(optionText.Split(')')[0]);
  185. AbstractEventScript ev = EventHandler.EventList.Find(e => e.ID == id);
  186. if (ev != null)
  187. {
  188. OnEventButtonClick(ev);
  189. }
  190. }
  191. }
  192. private void HandleEventButtons(AbstractEventScript e)
  193. {
  194. Transform GameTimeLineSliderTransform = this.GameTimelineSlider.transform;
  195. double timestamp = e.Event.TimestampStart;
  196. uint id = e.ID;
  197. int type = (int)e.Event.Type;
  198. EventTimestamp eventTimestamp;
  199. if (this.EventTimestamps.TryGetValue(e.Event.TimestampStart, out eventTimestamp))
  200. {
  201. if (!eventTimestamp.EventIDs.Contains(id))
  202. {
  203. eventTimestamp.addEvent(e, EventButtonImages[type], () => OnEventButtonClick(e));
  204. addEventToEventDropdown(id, type, timestamp);
  205. }
  206. }
  207. else
  208. {
  209. GameObject obj = Instantiate(this.EventDropdownPrefab);
  210. obj.transform.SetParent(this.transform.parent);
  211. obj.transform.localPosition = Vector3.zero;
  212. eventTimestamp = new EventTimestamp(timestamp, obj, this.GameTimelineSlider);
  213. this.EventTimestamps.Add(timestamp, eventTimestamp);
  214. }
  215. eventTimestamp.SetEventButtonPositon(e, this.UICamera);
  216. }
  217. private void SetEventButtonPositon(AbstractEventScript e, GameObject button)
  218. {
  219. Transform GameTimeLineSliderTransform = this.GameTimelineSlider.transform;
  220. Vector3 buttonPos = button.transform.localPosition;
  221. double timestamp = e.Event.TimestampStart;
  222. Vector2 leftCord = new Vector2(GameTimeLineSliderTransform.localPosition.x - SliderRectTransform.sizeDelta.x * SliderRectTransform.pivot.x, GameTimeLineSliderTransform.localPosition.y - SliderRectTransform.sizeDelta.y * SliderRectTransform.pivot.y);
  223. float minValue = this.GameTimelineSlider.minValue;
  224. float maxValue = this.GameTimelineSlider.maxValue;
  225. if (float.IsInfinity(minValue) || float.IsInfinity(maxValue) || float.IsNaN(minValue) || float.IsNaN(maxValue))
  226. {
  227. minValue = 0;
  228. maxValue = 1;
  229. }
  230. float normalized = (float)(timestamp - minValue) / (maxValue - minValue);
  231. if (float.IsNaN(normalized))
  232. normalized = -1;
  233. if (float.IsInfinity(normalized))
  234. normalized = -1;
  235. if (normalized >= 0 && normalized < 1)
  236. {
  237. // if (!button.GetComponent<UnityEngine.UI.Button>().enabled ||
  238. // !button.GetComponent<UnityEngine.UI.Image>().enabled)
  239. // {
  240. // button.GetComponent<UnityEngine.UI.Button>().enabled = true;
  241. // button.GetComponent<UnityEngine.UI.Image>().enabled = true;
  242. // }
  243. button.transform.localPosition = new Vector3(leftCord.x + SliderRectTransform.sizeDelta.x * normalized, leftCord.y, 0);
  244. }
  245. else
  246. {
  247. button.transform.position = new Vector3(0, 0, this.UICamera.transform.position.z);
  248. // button.GetComponent<UnityEngine.UI.Button>().enabled = false;
  249. // button.GetComponent<UnityEngine.UI.Image>().enabled = false;
  250. }
  251. }
  252. private void OnEventButtonClick(AbstractEventScript e)
  253. {
  254. this.GameTimelineSlider.SetValueWithoutNotify((float)e.Event.TimestampStart);
  255. ManagerObject.JumpToTimestamp((float)e.Event.TimestampStart);
  256. ManagerObject.StopPressed();
  257. }
  258. private bool EventInRadar(AbstractEventScript e, Vector3 cameraPosition, float radius)
  259. {
  260. Vector2 cameraPosition2D = new Vector2(cameraPosition.x, cameraPosition.z);
  261. Vector3 e3 = e.EventPoint.transform.position;
  262. return Vector2.Distance(cameraPosition2D, new Vector2(e3.x, e3.z)) < radius;
  263. }
  264. public void BeginDrag()
  265. {
  266. this.DraggingSlider = true;
  267. }
  268. public void TimelineValueChanged()
  269. {
  270. if (!DraggingSlider)
  271. {
  272. float newValue = this.GameTimelineSlider.value;
  273. ManagerObject.JumpToTimestamp(newValue);
  274. }
  275. }
  276. public void ChunkStartInputFieldEndEdit()
  277. {
  278. double startValue = double.Parse(ChunkStartInputField.text, NumberStyles.Float);
  279. this.ManagerObject.SetChunkStarttimestamp(startValue);
  280. }
  281. public void ChunkEndInputFieldEndEdit()
  282. {
  283. double endValue = double.Parse(ChunkEndInputField.text, NumberStyles.Float);
  284. this.ManagerObject.SetChunkEndtimestamp(endValue);
  285. }
  286. public void StopDrag()
  287. {
  288. this.GameTimelineSlider = this.TimeLineObject.GetComponent<UnityEngine.UI.Slider>();
  289. //Calculate the new timestamp and set the Gametimestamp
  290. float newValue = this.GameTimelineSlider.value;
  291. this.GameTimelineSlider.SetValueWithoutNotify(newValue);
  292. ManagerObject.JumpToTimestamp(newValue);
  293. this.DraggingSlider = false;
  294. //Right now the objects will move to the new Place
  295. //Should implement a function that will directly set the "new" positions.
  296. //Also while dragging the simulation doesn't stop. Dont know if that should be changed.
  297. }
  298. public void PlayPressed()
  299. {
  300. ManagerObject.PlayPressed();
  301. }
  302. public void StopPressed()
  303. {
  304. ManagerObject.StopPressed();
  305. }
  306. public void ReversePressed()
  307. {
  308. ManagerObject.ReversePressed();
  309. }
  310. public void PlusPressed()
  311. {
  312. ManagerObject.PlusPressed();
  313. }
  314. public void MinusPressed()
  315. {
  316. ManagerObject.MinusPressed();
  317. }
  318. }
  319. }