EventStamp.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.Linq;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using ObjectScripts;
  6. namespace UIScripts
  7. {
  8. public class EventStamp : MonoBehaviour
  9. {
  10. // Start is called before the first frame update
  11. ///<summary>
  12. /// Manager to control
  13. ///</summary>
  14. public AbstractManager Manager { get; set; }
  15. ///<summary>
  16. /// Timeline slider
  17. ///</summary>
  18. public UnityEngine.UI.Slider Slider { get; set; }
  19. ///<summary>
  20. /// timestamp
  21. ///</summary>
  22. public double Timestamp;
  23. ///<summary>
  24. /// ReactTransform of timeline slider
  25. ///</summary>
  26. private RectTransform SliderRect;
  27. ///<summary>
  28. /// Transform of timeline slider
  29. ///</summary>
  30. private Transform SliderTransform;
  31. ///<summary>
  32. ///
  33. ///</summary>
  34. private float leftx, minValue, maxValue, normalized;
  35. ///<summary>
  36. /// Number of events for that timestamp
  37. ///</summary>
  38. private int Ncontent = 0;
  39. ///<summary>
  40. /// Contentfield
  41. ///</summary>
  42. [SerializeField] public GameObject ContentField;
  43. ///<summary>
  44. /// EventField
  45. ///</summary>
  46. [SerializeField] public GameObject EventField;
  47. ///<summary>
  48. /// Button to jump to timestamp
  49. ///</summary>
  50. [SerializeField] public GameObject EventButton;
  51. ///<summary>
  52. ///
  53. ///</summary>
  54. [SerializeField] UnityEngine.UI.Scrollbar Scrollbar;
  55. ///<summary>
  56. ///
  57. ///</summary>
  58. [SerializeField] UnityEngine.UI.ScrollRect ScrollRect;
  59. ///<summary>
  60. /// size of the contentfield
  61. ///</summary>
  62. private float size;
  63. ///<summary>
  64. /// size of one event
  65. ///</summary>
  66. private float EventFieldSize;
  67. ///<summary>
  68. /// timechunks
  69. ///</summary>
  70. private float EndChunk, StartChunk;
  71. private RectTransform ContentFieldRect;
  72. ///<summary>
  73. /// Stepsize for one scroll up/down press
  74. ///</summary>
  75. private float Stepsize = 30;
  76. ///<summary>
  77. /// CanavasObject to change visbility
  78. ///</summary>
  79. private Canvas CanvasObject;
  80. void Start()
  81. {
  82. SliderRect = Slider.GetComponent<RectTransform>();
  83. SliderTransform = Slider.transform;
  84. ContentFieldRect = ContentField.GetComponent<RectTransform>();
  85. CanvasObject = GetComponent<Canvas>();
  86. }
  87. // Update is called once per frame
  88. void Update()
  89. {
  90. //Move Timestamp on timeline
  91. EndChunk = (float)Manager.endChunk;
  92. StartChunk = (float)Manager.startChunk;
  93. //left slider point
  94. leftx = SliderTransform.localPosition.x - SliderRect.sizeDelta.x * SliderRect.pivot.x;
  95. //values of the slider
  96. minValue = Slider.minValue;
  97. maxValue = Slider.maxValue;
  98. if (float.IsInfinity(minValue) || float.IsInfinity(maxValue) || float.IsNaN(minValue) || float.IsNaN(maxValue))
  99. {
  100. minValue = 0;
  101. maxValue = 1;
  102. }
  103. //get normalized value of timestamp for slider
  104. normalized = (float)(Timestamp - minValue) / (maxValue - minValue);
  105. if (float.IsNaN(normalized))
  106. normalized = -1;
  107. if (float.IsInfinity(normalized))
  108. normalized = -1;
  109. if (normalized >= 0 && normalized < 1)
  110. {
  111. //set visible and position
  112. CanvasObject.enabled = true;
  113. transform.localPosition = new Vector3(leftx + SliderRect.sizeDelta.x * normalized, 0, 0);
  114. }
  115. else
  116. {
  117. CanvasObject.enabled = false;
  118. }
  119. // }
  120. }
  121. ///<summary>
  122. /// On button pressed
  123. ///</summary>
  124. public void OnEventButtonClick()
  125. {
  126. Manager.JumpToTimestamp(Timestamp);
  127. Manager.StopPressed();
  128. }
  129. ///<summary>
  130. /// add event to timestamp
  131. ///</summary>
  132. public void addContent(AbstractEventScript eventScript, Sprite sprite)
  133. {
  134. GameObject eventField = Instantiate(EventField, EventField.transform.position, EventField.transform.rotation, EventField.transform.parent);
  135. GameObject eventButton = Instantiate(EventButton, eventField.transform);
  136. RectTransform eventFieldRect = eventField.GetComponent<RectTransform>();
  137. EventFieldSize = eventFieldRect.sizeDelta.y;
  138. eventFieldRect.anchorMax = new Vector2(0.5f, 1);
  139. eventFieldRect.anchorMin = new Vector2(0.5f, 1);
  140. ContentField.GetComponent<RectTransform>().sizeDelta = new Vector2(0, Mathf.RoundToInt(Ncontent * EventFieldSize));
  141. size = ContentField.GetComponent<RectTransform>().sizeDelta.y;
  142. eventFieldRect.localPosition -= new Vector3(0, Mathf.RoundToInt(Ncontent * EventFieldSize + EventFieldSize / 2), 0);
  143. eventButton.GetComponent<UnityEngine.UI.Image>().sprite = sprite;
  144. eventField.SetActive(true);
  145. eventButton.SetActive(true);
  146. Ncontent += 1;
  147. }
  148. ///<summary>
  149. /// On scrollbar up pressed
  150. ///</summary>
  151. public void ScrollbarUP()
  152. {
  153. ContentField.GetComponent<RectTransform>().anchoredPosition = ContentField.GetComponent<RectTransform>().anchoredPosition - new Vector2(0, Stepsize);
  154. }
  155. ///<summary>
  156. /// on scrollbar down pressed
  157. ///</summary>
  158. public void ScrollbarDOWN()
  159. {
  160. ContentField.GetComponent<RectTransform>().anchoredPosition = ContentField.GetComponent<RectTransform>().anchoredPosition + new Vector2(0, Stepsize);
  161. }
  162. }
  163. }