Tanoda
Tween.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using System;
11
12namespace Leap.Unity.Animation {
13 using Internal;
14
15 public partial struct Tween {
16 private int _id;
17 private TweenInstance _instance;
18
19 private Tween(bool isSingle) {
20 _instance = Pool<TweenInstance>.Spawn();
21 _id = _instance.instanceId;
22 _instance.returnToPoolUponStop = isSingle;
23 }
24
28 public static Tween Single() {
29 return new Tween(isSingle: true);
30 }
31
36 public static Tween Persistent() {
37 return new Tween(isSingle: false);
38 }
39
43 public static Tween AfterDelay(float delay, Action onReachEnd) {
44 return Single().Value(0F, 1F, (x) => { }).OverTime(delay).OnReachEnd(onReachEnd).Play();
45 }
46
47 #region PUBLIC PROPERTIES
54 public bool isValid {
55 get {
56 return _instance != null && _id == _instance.instanceId;
57 }
58 }
59
63 public bool isRunning {
64 get {
65 throwIfInvalid();
66 return _instance.runnerIndex != -1;
67 }
68 }
69
74 get {
75 throwIfInvalid();
76 return _instance.direction;
77 }
78 set {
79 throwIfInvalid();
80 _instance.direction = value;
81 _instance.dstPercent = value == Direction.Backward ? 0 : 1;
82 }
83 }
84
88 public float timeLeft {
89 get {
90 throwIfInvalid();
91 return Mathf.Abs((_instance.curPercent - _instance.dstPercent) / _instance.velPercent);
92 }
93 }
94
99 public float progress {
100 get {
101 throwIfInvalid();
102 return _instance.curPercent;
103 }
104 set {
105 throwIfInvalid();
106
107 if (_instance.curPercent == value) {
108 return;
109 }
110
111 if (value < 0 || value > 1) {
112 throw new ArgumentException("Progress must be a value from 0 - 1");
113 }
114
115 if (_instance.curPercent == 0.0f) {
116 if (_instance.OnLeaveStart != null) {
117 _instance.OnLeaveStart();
118 }
119 } else if (_instance.curPercent == 1.0f) {
120 if (_instance.OnLeaveEnd != null) {
121 _instance.OnLeaveEnd();
122 }
123 }
124
125 _instance.curPercent = value;
126
127 if (_instance.curPercent == 0.0f) {
128 if (_instance.OnReachStart != null) {
129 _instance.OnReachStart();
130 }
131 } else if (_instance.curPercent == 1.0f) {
132 if (_instance.OnReachEnd != null) {
133 _instance.OnReachEnd();
134 }
135 }
136
137 if (_instance.runnerIndex == -1) {
138 _instance.interpolatePercent();
139 }
140 }
141 }
142 #endregion
143
144 #region PUBLIC METHODS
145
151 public Tween AddInterpolator(IInterpolator interpolator) {
152 throwIfInvalid();
153
154 if (_instance.interpolatorCount >= _instance.interpolators.Length) {
155 Utils.DoubleCapacity(ref _instance.interpolators);
156 }
157
158 _instance.interpolators[_instance.interpolatorCount++] = interpolator;
159 return this;
160 }
161
165 public Tween OverTime(float seconds) {
166 throwIfInvalid();
167 _instance.velPercent = 1.0f / seconds;
168 return this;
169 }
170
175 public Tween AtRate(float unitsPerSecond) {
176 throwIfInvalid();
177 _instance.velPercent = unitsPerSecond / _instance.interpolators[0].length;
178 return this;
179 }
180
184 public Tween Smooth(SmoothType type = SmoothType.Smooth) {
185 throwIfInvalid();
186 _instance.smoothType = type;
187 _instance.smoothFunction = null;
188 return this;
189 }
194 public Tween Smooth(AnimationCurve curve) {
195 throwIfInvalid();
196 _instance.smoothType = 0;
197 _instance.smoothFunction = curve.Evaluate;
198 return this;
199 }
200
205 public Tween Smooth(Func<float, float> smoothFunction) {
206 throwIfInvalid();
207 _instance.smoothType = 0;
208 _instance.smoothFunction = smoothFunction;
209 return this;
210 }
211
222 public Tween OnProgress(Action<float> action) {
223 throwIfInvalid();
224 _instance.OnProgress += action;
225 return this;
226 }
227
232 public Tween OnLeaveStart(Action action) {
233 throwIfInvalid();
234 _instance.OnLeaveStart += action;
235 return this;
236 }
237
242 public Tween OnReachStart(Action action) {
243 throwIfInvalid();
244 _instance.OnReachStart += action;
245 return this;
246 }
247
252 public Tween OnLeaveEnd(Action action) {
253 throwIfInvalid();
254 _instance.OnLeaveEnd += action;
255 return this;
256 }
257
262 public Tween OnReachEnd(Action action) {
263 throwIfInvalid();
264 _instance.OnReachEnd += action;
265 return this;
266 }
267
272 public Tween Play() {
273 throwIfInvalid();
274
275 if (_instance.curPercent == _instance.dstPercent) {
276 return this;
277 }
278
279 if (_instance.runnerIndex != TweenInstance.NOT_RUNNING) {
280 return this;
281 }
282
283 TweenRunner.instance.AddTween(_instance);
284
285 return this;
286 }
287
293 throwIfInvalid();
294
295 this.direction = direction;
296 Play();
297
298 return this;
299 }
300
306 public Tween Play(float destinationPercent) {
307 throwIfInvalid();
308
309 if (destinationPercent < 0 || destinationPercent > 1) {
310 throw new ArgumentException("Destination percent must be within the range [0-1]");
311 }
312
313 direction = destinationPercent >= _instance.curPercent ? Direction.Forward : Direction.Backward;
314 _instance.dstPercent = destinationPercent;
315 Play();
316
317 return this;
318 }
319
326 throwIfInvalid();
327
328 return _instance.yieldInstruction;
329 }
330
334 public void Pause() {
335 throwIfInvalid();
336
337 if (_instance.runnerIndex != -1) {
339 }
340 }
341
345 public void Stop() {
346 throwIfInvalid();
347
348 progress = 0;
349 direction = Direction.Forward;
350
351 Pause();
352
353 if (isValid && _instance.returnToPoolUponStop) {
354 Release();
355 }
356 }
357
362 public void Release() {
363 throwIfInvalid();
364
365 Pause();
366
368 }
369
370 #endregion
371
372 private void throwIfInvalid() {
373 if (!isValid) {
374 //Only way for the id to be an unused ID is if the user constructed this Tween with the
375 //default constructor (which we cannot remove because this is a struct).
376 if (_id == TweenInstance.ID_UNUSED) {
377 throw new InvalidOperationException("Tween is invalid. Make sure you use Tween.Single or Tween.Persistant to create your Tween instead of the default constructor.");
378 } else {
379 throw new InvalidOperationException("Tween is invalid or was recycled. Make sure to use Tween.Persistant if you want to keep a tween around after it finishes playing.");
380 }
381 }
382 }
383 }
384}
Implement this interface to add your own interpolators to Tween!
Tween Play()
Starts playing this Tween. It will continue from the same position it left off on,...
Definition: Tween.cs:272
void Pause()
Pauses this Tween. It retains its position and direction.
Definition: Tween.cs:334
Direction direction
Gets or sets whether or not this Tween is moving forwards or backwards.
Definition: Tween.cs:73
bool isRunning
Returns whether or not the Tween is currently running.
Definition: Tween.cs:63
Tween Play(Direction direction)
Starts playing this Tween in a specific direction. It will condition from the same position it left o...
Definition: Tween.cs:292
Tween OverTime(float seconds)
Specifies that this Tween should travel from begining to end over a certain number of seconds.
Definition: Tween.cs:165
Tween Smooth(Func< float, float > smoothFunction)
Specifies that this Tween should use the given Function for its smoothing. The function should map fr...
Definition: Tween.cs:205
float timeLeft
Gets how much time is left before the Tween stops.
Definition: Tween.cs:88
Tween OnProgress(Action< float > action)
Specifies an action to be called every step of the Tween. This callback happens after:
Definition: Tween.cs:222
Tween AddInterpolator(IInterpolator interpolator)
Adds a new Interpolator to this Tween. This Interpolator will have it's Interpolate method called eve...
Definition: Tween.cs:151
static Tween Persistent()
Creates a persistant Tween that will not ever auto-release itself. You must specifically call Release...
Definition: Tween.cs:36
Tween OnReachEnd(Action action)
Specifies an action to be called whenever this Tween reaches the end.
Definition: Tween.cs:262
Tween OnLeaveStart(Action action)
Specifies an action to be called whenever this Tween is Played forward when at the start.
Definition: Tween.cs:232
static Tween Single()
Create a single-use Tween that will auto-release itself as soon as it is finished playing.
Definition: Tween.cs:28
static Tween AfterDelay(float delay, Action onReachEnd)
Create a single-use Tween that will fire onReachEnd after the specified delay in seconds.
Definition: Tween.cs:43
Tween Smooth(SmoothType type=SmoothType.Smooth)
Specifies that this Tween should the given smoothing method.
Definition: Tween.cs:184
Tween Value(float a, float b, Action< float > onValue)
Tween Play(float destinationPercent)
Starts playing this Tween towards a destination percent. Once it reaches that value,...
Definition: Tween.cs:306
Tween AtRate(float unitsPerSecond)
Specifies that this Tween should travel at the given rate. This rate is measured against the FIRST in...
Definition: Tween.cs:175
bool isValid
Returns whether or not this Tween is considered valid. A Tween can become invalid under the following...
Definition: Tween.cs:54
void Release()
Forces this Tween to be recycled right away. Once this method is called, the Tween will be invalid an...
Definition: Tween.cs:362
Tween OnLeaveEnd(Action action)
Specifies an action to be called whenever this Tween is Played backwards when at the end.
Definition: Tween.cs:252
void Stop()
Stops this Tween. If it is not a persistant Tween, it will be recycled right away.
Definition: Tween.cs:345
float progress
Gets or sets how far along completion this Tween is. This value is a percent that ranges from 0 to 1.
Definition: Tween.cs:99
Tween Smooth(AnimationCurve curve)
Specifies that this Tween should use the given Animation curve for its smoothing. The curve should ma...
Definition: Tween.cs:194
TweenInstance.TweenYieldInstruction Yield()
Returns a custom yield instruction that can be yielded to in order to wait for the completion of this...
Definition: Tween.cs:325
Tween OnReachStart(Action action)
Specifies an action to be called whenever this Tween reaches the start.
Definition: Tween.cs:242