DiscreteTime.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. namespace UnityEngine.Timeline
  3. {
  4. struct DiscreteTime : IComparable
  5. {
  6. const double k_Tick = 1e-12;
  7. public static readonly DiscreteTime kMaxTime = new DiscreteTime(Int64.MaxValue);
  8. readonly Int64 m_DiscreteTime;
  9. public static double tickValue { get { return k_Tick; } }
  10. public DiscreteTime(DiscreteTime time)
  11. {
  12. m_DiscreteTime = time.m_DiscreteTime;
  13. }
  14. DiscreteTime(Int64 time)
  15. {
  16. m_DiscreteTime = time;
  17. }
  18. public DiscreteTime(double time)
  19. {
  20. m_DiscreteTime = DoubleToDiscreteTime(time);
  21. }
  22. public DiscreteTime(float time)
  23. {
  24. m_DiscreteTime = FloatToDiscreteTime(time);
  25. }
  26. public DiscreteTime(int time)
  27. {
  28. m_DiscreteTime = IntToDiscreteTime(time);
  29. }
  30. public DiscreteTime(int frame, double fps)
  31. {
  32. m_DiscreteTime = DoubleToDiscreteTime(frame * fps);
  33. }
  34. public DiscreteTime OneTickBefore()
  35. {
  36. return new DiscreteTime(m_DiscreteTime - 1);
  37. }
  38. public DiscreteTime OneTickAfter()
  39. {
  40. return new DiscreteTime(m_DiscreteTime + 1);
  41. }
  42. public Int64 GetTick()
  43. {
  44. return m_DiscreteTime;
  45. }
  46. public static DiscreteTime FromTicks(Int64 ticks)
  47. {
  48. return new DiscreteTime(ticks);
  49. }
  50. public int CompareTo(object obj)
  51. {
  52. if (obj is DiscreteTime)
  53. return m_DiscreteTime.CompareTo(((DiscreteTime)obj).m_DiscreteTime);
  54. return 1;
  55. }
  56. public bool Equals(DiscreteTime other)
  57. {
  58. return m_DiscreteTime == other.m_DiscreteTime;
  59. }
  60. public override bool Equals(object obj)
  61. {
  62. if (obj is DiscreteTime)
  63. return Equals((DiscreteTime)obj);
  64. return false;
  65. }
  66. static Int64 DoubleToDiscreteTime(double time)
  67. {
  68. double number = (time / k_Tick) + 0.5;
  69. if (number < Int64.MaxValue && number > Int64.MinValue)
  70. return (Int64)number;
  71. throw new ArgumentOutOfRangeException("Time is over the discrete range.");
  72. }
  73. static Int64 FloatToDiscreteTime(float time)
  74. {
  75. float number = (time / (float)k_Tick) + 0.5f;
  76. if (number < Int64.MaxValue && number > Int64.MinValue)
  77. return (Int64)number;
  78. throw new ArgumentOutOfRangeException("Time is over the discrete range.");
  79. }
  80. static Int64 IntToDiscreteTime(int time)
  81. {
  82. return DoubleToDiscreteTime(time);
  83. }
  84. static double ToDouble(Int64 time)
  85. {
  86. return time * k_Tick;
  87. }
  88. static float ToFloat(Int64 time)
  89. {
  90. return (float)ToDouble(time);
  91. }
  92. public static explicit operator double(DiscreteTime b)
  93. {
  94. return ToDouble(b.m_DiscreteTime);
  95. }
  96. public static explicit operator float(DiscreteTime b)
  97. {
  98. return ToFloat(b.m_DiscreteTime);
  99. }
  100. public static explicit operator Int64(DiscreteTime b)
  101. {
  102. return b.m_DiscreteTime;
  103. }
  104. public static explicit operator DiscreteTime(double time)
  105. {
  106. return new DiscreteTime(time);
  107. }
  108. public static explicit operator DiscreteTime(float time)
  109. {
  110. return new DiscreteTime(time);
  111. }
  112. public static implicit operator DiscreteTime(Int32 time)
  113. {
  114. return new DiscreteTime(time);
  115. }
  116. public static explicit operator DiscreteTime(Int64 time)
  117. {
  118. return new DiscreteTime(time);
  119. }
  120. public static bool operator==(DiscreteTime lhs, DiscreteTime rhs)
  121. {
  122. return lhs.m_DiscreteTime == rhs.m_DiscreteTime;
  123. }
  124. public static bool operator!=(DiscreteTime lhs, DiscreteTime rhs)
  125. {
  126. return !(lhs == rhs);
  127. }
  128. public static bool operator>(DiscreteTime lhs, DiscreteTime rhs)
  129. {
  130. return lhs.m_DiscreteTime > rhs.m_DiscreteTime;
  131. }
  132. public static bool operator<(DiscreteTime lhs, DiscreteTime rhs)
  133. {
  134. return lhs.m_DiscreteTime < rhs.m_DiscreteTime;
  135. }
  136. public static bool operator<=(DiscreteTime lhs, DiscreteTime rhs)
  137. {
  138. return lhs.m_DiscreteTime <= rhs.m_DiscreteTime;
  139. }
  140. public static bool operator>=(DiscreteTime lhs, DiscreteTime rhs)
  141. {
  142. return lhs.m_DiscreteTime >= rhs.m_DiscreteTime;
  143. }
  144. public static DiscreteTime operator+(DiscreteTime lhs, DiscreteTime rhs)
  145. {
  146. return new DiscreteTime(lhs.m_DiscreteTime + rhs.m_DiscreteTime);
  147. }
  148. public static DiscreteTime operator-(DiscreteTime lhs, DiscreteTime rhs)
  149. {
  150. return new DiscreteTime(lhs.m_DiscreteTime - rhs.m_DiscreteTime);
  151. }
  152. public override string ToString()
  153. {
  154. return m_DiscreteTime.ToString();
  155. }
  156. public override int GetHashCode()
  157. {
  158. return m_DiscreteTime.GetHashCode();
  159. }
  160. public static DiscreteTime Min(DiscreteTime lhs, DiscreteTime rhs)
  161. {
  162. return new DiscreteTime(Math.Min(lhs.m_DiscreteTime, rhs.m_DiscreteTime));
  163. }
  164. public static DiscreteTime Max(DiscreteTime lhs, DiscreteTime rhs)
  165. {
  166. return new DiscreteTime(Math.Max(lhs.m_DiscreteTime, rhs.m_DiscreteTime));
  167. }
  168. public static double SnapToNearestTick(double time)
  169. {
  170. Int64 discreteTime = DoubleToDiscreteTime(time);
  171. return ToDouble(discreteTime);
  172. }
  173. public static float SnapToNearestTick(float time)
  174. {
  175. Int64 discreteTime = FloatToDiscreteTime(time);
  176. return ToFloat(discreteTime);
  177. }
  178. public static Int64 GetNearestTick(double time)
  179. {
  180. return DoubleToDiscreteTime(time);
  181. }
  182. }
  183. }