forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpolatedValue.cs
More file actions
305 lines (267 loc) · 11.1 KB
/
InterpolatedValue.cs
File metadata and controls
305 lines (267 loc) · 11.1 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using UnityEngine;
namespace HoloToolkit.Unity
{
/// <summary>
/// Base class that provides the common logic for interpolating between values. This class does not
/// inherit from MonoBehaviour in order to enable various scenarios under which it used. To perform the
/// interpolation step, call FrameUpdate.
/// </summary>
/// <typeparam name="T">Type of value used for interpolation.</typeparam>
[Serializable]
public abstract class InterpolatedValue<T>
{
public const float SmallNumber = 0.0000001f;
public const float SmallNumberSquared = SmallNumber * SmallNumber;
/// <summary>
/// Implict cast operator that returns the current value of the Interpolator.
/// </summary>
/// <param name="interpolatedValue">The interpolator casting from.</param>
public static implicit operator T(InterpolatedValue<T> interpolatedValue)
{
return interpolatedValue.Value;
}
/// <summary>
/// Event that is triggered when interpolation starts.
/// </summary>
public event Action<InterpolatedValue<T>> Started;
/// <summary>
/// Event that is triggered when interpolation completes.
/// </summary>
public event Action<InterpolatedValue<T>> Completed;
/// <summary>
/// Event that is triggered when the current interpolated value is changed.
/// </summary>
public event Action<InterpolatedValue<T>> ValueChanged;
private T targetValue;
private T startValue;
private float timeInterpolationStartedAt;
private bool firstUpdateFrameSkipped;
private bool skipFirstUpdateFrame;
private bool performingInterpolativeSnap;
[SerializeField]
[Tooltip("Time the interpolator takes to get from current value to the target value")]
private float duration = 1.0f;
/// <summary>
/// Time the interpolator takes to get from current value to the target value.
/// </summary>
public float Duration
{
get { return duration; }
set { duration = value; }
}
[SerializeField]
[Tooltip("Time the interpolator takes to get from current value to the target value")]
private AnimationCurve curve = null;
/// <summary>
/// The AnimationCurve used for evaluatng the interpolation value.
/// </summary>
public AnimationCurve Curve
{
get { return curve; }
set { curve = value; }
}
/// <summary>
/// Checks if the interpolator can be used by ensuring an AnimatorCurve has been set.
/// </summary>
public bool IsValid { get { return Curve != null; } }
/// <summary>
/// Checks whether the interpolator is currently interpolating.
/// </summary>
public bool IsRunning { get; private set; }
private T value;
/// <summary>
/// Retruns the current interpolated value.
/// </summary>
public T Value
{
get { return value; }
private set
{
if (!DoValuesEqual(this.value, value))
{
this.value = value;
ValueChanged.RaiseEvent(this);
}
}
}
/// <summary>
/// Retruns the current interpolation target value.
/// </summary>
public T Target
{
get { return targetValue; }
}
/// <summary>
/// Wrapper for getting time that supports EditTime updates.
/// </summary>
protected float CurrentTime
{
get
{
#if UNITY_EDITOR
return !Application.isPlaying ? (float)UnityEditor.EditorApplication.timeSinceStartup : Time.time;
#else
return Time.time;
#endif
}
}
/// <summary>
/// Instantiates a new InterpolatedValue with an initial value and a setting of whether to skip first update frame.
/// </summary>
/// <param name="initialValue">Initial current value to use.</param>
/// <param name="skipFirstUpdateFrame">A flag to skip first update frame after the interpolation target has been set.</param>
public InterpolatedValue(T initialValue, bool skipFirstUpdateFrame)
{
IsRunning = false;
Value = targetValue = initialValue;
Duration = 1.0f;
firstUpdateFrameSkipped = false;
this.skipFirstUpdateFrame = skipFirstUpdateFrame;
performingInterpolativeSnap = false;
}
/// <summary>
/// Updates the target value and starts the interpolator if it is not running already.
/// </summary>
/// <param name="updateTargetValue">The new target value.</param>
public void UpdateTarget(T updateTargetValue)
{
UpdateTarget(updateTargetValue, false);
}
/// <summary>
/// Updates the target value and starts the interpolator if it is not running already.
/// </summary>
/// <param name="updateTargetValue">The new target value.</param>
/// <param name="forceUpdate">A flag for forcing an update propagation.</param>
public void UpdateTarget(T updateTargetValue, bool forceUpdate)
{
performingInterpolativeSnap = false;
targetValue = updateTargetValue;
startValue = Value;
timeInterpolationStartedAt = CurrentTime;
if (!DoValuesEqual(updateTargetValue, Value))
{
EnsureEnabled();
}
else if (forceUpdate && !IsRunning)
{
ValueChanged.RaiseEvent(this);
}
}
/// <summary>
/// Snap (set) the interpolated value to the current target value.
/// </summary>
public void SnapToTarget()
{
SnapToTarget(targetValue);
}
/// <summary>
/// Update the target to a new value and snap (set) the interpolated value to it.
/// </summary>
/// <param name="snapTargetValue">The new target value.</param>
public void SnapToTarget(T snapTargetValue)
{
performingInterpolativeSnap = false;
Value = startValue = snapTargetValue;
EnsureDisabled();
}
/// <summary>
/// Interpolative snap to target will interpolate until it reaches the given target value, after which subsequent calls to this method it will snap to the target value given.
/// </summary>
/// <remarks>SnapToTarget and UpdateTarget resets this.</remarks>
/// <param name="snapTargetValue">The target value to set and interpolatively snap to.</param>
public void InterpolateThenSnapToTarget(T snapTargetValue)
{
if (performingInterpolativeSnap)
{
// If we are running, just update the target, otherwise just snap to target
if (IsRunning)
{
targetValue = snapTargetValue;
}
else
{
Value = startValue = snapTargetValue;
}
}
else
{
UpdateTarget(snapTargetValue);
performingInterpolativeSnap = true;
}
}
/// <summary>
/// Starts interpolation if it is not currently running.
/// </summary>
/// <remarks>This forces a start if not currently running and does not check if the interpolated value is at the target value.</remarks>
public void EnsureEnabled()
{
if (!IsRunning)
{
if (skipFirstUpdateFrame)
{
firstUpdateFrameSkipped = false;
}
IsRunning = true;
Started.RaiseEvent(this);
}
}
/// <summary>
/// Stops the interpolation if it is currently running.
/// </summary>
/// <remarks>This forces a stop if currently running and does not check if the interpolated value has not reached the target value.</remarks>
public void EnsureDisabled()
{
if (IsRunning)
{
IsRunning = false;
Completed.RaiseEvent(this);
}
}
/// <summary>
/// Increments the interpolation step. This function should be called each frame.
/// </summary>
/// <remarks>To enable multiple scenarios for using the InterpolatedValues, the class does not inherit from MonoBehaviour.</remarks>
/// <returns>The new interpolated value after performing the interpolation step.</returns>
public T FrameUpdate()
{
if (IsRunning)
{
if (skipFirstUpdateFrame && !firstUpdateFrameSkipped)
{
firstUpdateFrameSkipped = true;
timeInterpolationStartedAt = CurrentTime;
return Value;
}
float timeDelta = CurrentTime - timeInterpolationStartedAt;
// Normalize the delta to curve duration
timeDelta *= Curve.Duration() / Duration;
Value = ApplyCurveValue(startValue, targetValue, Curve.Evaluate(timeDelta));
if (timeDelta >= Curve.Duration())
{
EnsureDisabled();
}
}
return Value;
}
/// <summary>
/// A method to check whether two values are equal. This should be overriden by inheriting classes.
/// </summary>
/// <remarks>This method is public because of a Unity compilation bug when dealing with abstract methods on generics.</remarks>
/// <param name="one">First value.</param>
/// <param name="other">Second value.</param>
/// <returns>True if values are equal or are "close enough".</returns>
public abstract bool DoValuesEqual(T one, T other);
/// <summary>
/// A method to calculate the current interpolated value based on the start value, a target value and the curve evaluated interpolation position value. This should be overriden by inheriting classes.
/// </summary>
/// <remarks>This method is public because of a Unity compilation bug when dealing with abstract methods on generics.</remarks>
/// <param name="startValue">The value that the interpolation started at.</param>
/// <param name="targetValue">The target value that the interpolation is moving to.</param>
/// <param name="curveValue">A curve evaluated interpolation position value. This will be in range of [0, 1]</param>
/// <returns>The new calculated interpolation value.</returns>
public abstract T ApplyCurveValue(T startValue, T targetValue, float curveValue);
}
}