Timeline.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ObjectScripts;
  5. namespace UIScripts
  6. {
  7. public class Timeline : MonoBehaviour
  8. {
  9. ///<summary>
  10. /// Manager to control and get GameTimestamp
  11. ///</summary>
  12. [SerializeField] AbstractManager ManagerObject;
  13. ///<summary>
  14. /// Timeline slider
  15. ///</summary>
  16. [SerializeField] UnityEngine.UI.Slider GameTimelineSlider;
  17. ///<summary>
  18. /// Text for showing current and newest timestamp
  19. ///</summary>
  20. [SerializeField] UnityEngine.UI.Text TimestampText;
  21. ///<summary>
  22. /// Start of time interval
  23. ///</summary>
  24. private double StartChunk;
  25. ///<summary>
  26. /// End of time interval
  27. ///</summary>
  28. private double EndChunk;
  29. ///<summary>
  30. ///
  31. ///</summary>
  32. private bool Dragging = false;
  33. ///<summary>
  34. /// Current Gametimestamp
  35. ///</summary>
  36. private float Gametimestamp = 0;
  37. ///<summary>
  38. /// Current newest timestamp
  39. ///</summary>
  40. private float NewestTimestamp;
  41. ///<summary>
  42. /// Manager UpdateRate
  43. ///</summary>
  44. private float UpdateRate;
  45. void Start()
  46. {
  47. UpdateRate = ManagerObject.UpdateRateInSeconds;
  48. StartCoroutine(ChangeSlider());
  49. }
  50. ///<summary>
  51. /// set slider values each 1/updaterate seconds
  52. ///</summary>
  53. private IEnumerator ChangeSlider()
  54. {
  55. while (true)
  56. {
  57. StartChunk = ManagerObject.startChunk;
  58. EndChunk = ManagerObject.endChunk;
  59. Gametimestamp = (float)ManagerObject.GameTimestamp;
  60. NewestTimestamp = (float)ManagerObject.NewestTimestamp;
  61. if (!Dragging)
  62. {
  63. float minTimeStamp = double.IsNaN(StartChunk) ? (float)ManagerObject.EarliestTimestamp : (float)StartChunk;
  64. float maxTimeStamp = double.IsNaN(EndChunk) ? (float)ManagerObject.NewestTimestamp : (float)EndChunk;
  65. GameTimelineSlider.maxValue = maxTimeStamp;
  66. GameTimelineSlider.minValue = minTimeStamp;
  67. GameTimelineSlider.SetValueWithoutNotify(Gametimestamp);
  68. }
  69. TimestampText.text = Gametimestamp.ToString("0.00") + " : " + NewestTimestamp.ToString("0.00");
  70. yield return new WaitForSecondsRealtime(UpdateRate);
  71. }
  72. }
  73. ///<summary>
  74. /// On dragging slider
  75. ///</summary>
  76. public void BeginDrag()
  77. {
  78. Dragging = true;
  79. }
  80. ///<summary>
  81. /// on slider value change
  82. ///</summary>
  83. public void TimelineValueChanged()
  84. {
  85. if (!Dragging)
  86. {
  87. ManagerObject.JumpToTimestamp(GameTimelineSlider.value);
  88. }
  89. }
  90. ///<summary>
  91. /// on stop dragging slider
  92. ///</summary>
  93. public void StopDrag()
  94. {
  95. GameTimelineSlider.SetValueWithoutNotify(GameTimelineSlider.value);
  96. ManagerObject.JumpToTimestamp(GameTimelineSlider.value);
  97. Dragging = false;
  98. }
  99. }
  100. }