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