-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTween.cs
574 lines (503 loc) · 16.6 KB
/
Tween.cs
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Rucrede
{
public delegate float EasingFunction (float start,float end,float value);
public delegate void TweenDelegate (Tween tween);
public delegate void VoidDelegate ();
public class TweenDescriptor
{
public Type ObjectType;
public object fromValue;
public object toValue;
public Tween.EaseType easeType;
public bool baseOnSpeed = false;
public float speed;
public float time;
public float delay;
public bool yoyo;
public int loops;
public TweenDelegate updateMethod;
public TweenDelegate completeMethod;
}
public class Tween
{
public static List<Tween> Tweens = new List<Tween> ();
public event TweenDelegate UpdateEvent;
public event TweenDelegate CompleteEvent;
public bool Unpausable = false;
public float Progress { get { return _easeValue; } }
public object StartValue { get { return _startValue; } }
public object EndValue { get { return _endValue; } }
public object Value { get { return _value; } }
public bool Active { get { return _running; } }
public bool Yoyo { get { return _yoyo; } set { _yoyo = value; } }
protected TweenController TweenController;
protected int _tweenID;
protected object _value;
protected object _startValue;
protected object _endValue;
protected float _easeValue;
protected float _progress;
protected float _range;
protected float _startTime;
protected float _currentTime;
protected float _duration;
protected bool _running;
protected float _delay;
protected float _delayTimeStamp;
protected bool _paused;
protected bool _yoyo;
protected int _loopCount;
protected int _loops;
protected bool _reversing = false;
protected float _pauseTimeStamp;
protected IEaser _easer;
protected TweenDescriptor _descriptor;
protected Tween (TweenDescriptor aDescriptor)
{
_descriptor = aDescriptor;
_tweenID = Tweens.Count;
_startValue = aDescriptor.fromValue;
_endValue = aDescriptor.toValue;
_range = GetRange (_startValue, _endValue);
if (!aDescriptor.baseOnSpeed) {
_duration = aDescriptor.time;
} else {
_duration = GetDuration (aDescriptor.speed, _startValue, _endValue);
}
_easer = GetEaser (aDescriptor.easeType);
_progress = 0.0f;
_delayTimeStamp = Time.timeSinceLevelLoad;
_delay = aDescriptor.delay;
_yoyo = aDescriptor.yoyo;
_loopCount = 0;
_loops = aDescriptor.loops;
if (_yoyo && _loops == 0) //if its a yoyo effect, the loop will be atleast 1:
_loops = 1;
_reversing = false;
if (aDescriptor.updateMethod != null)
UpdateEvent += aDescriptor.updateMethod;
if (aDescriptor.completeMethod != null)
CompleteEvent += aDescriptor.completeMethod;
Tweens.Add (this);
TweenController.Singleton.UpdateEvent += Update;
Play ();
}
public void Update ()
{
if (_running == false || _paused)
return;
if ((_delayTimeStamp + _delay) > Time.timeSinceLevelLoad)
return;
_currentTime = (Time.timeSinceLevelLoad - _startTime);
if (_currentTime >= _duration)
_currentTime = _duration;
if (_duration > 0f) {
_progress = _currentTime / _duration;
} else {
_progress = 1.0f;
}
float directionalProgress = _reversing ? 1.0f - _progress : _progress;
_easeValue = _easer.ease (directionalProgress);
UpdateObjectValue (_easeValue);
if (UpdateEvent != null) {
UpdateEvent.Invoke (this);
}
if (_progress == 1.0f) {
if (_loopCount >= _loops && _loops != -1) { //-1 is infinite loop
_running = false;
if (CompleteEvent != null)
CompleteEvent (this);
Destroy ();
} else {
if (_yoyo)
_reversing = !_reversing;
_loopCount++;
Rewind ();
}
}
}
public bool isPaused ()
{
return _paused;
}
public void Play ()
{
if (!_paused) {
_startTime = Time.timeSinceLevelLoad + _delay;
_delayTimeStamp = Time.timeSinceLevelLoad;
_running = true;
} else {
Resume();
}
}
public void Pause ()
{
if (!Unpausable) {
_paused = true;
_pauseTimeStamp = Time.timeSinceLevelLoad;
}
}
public void Resume ()
{
if (_paused) {
_paused = false;
float thePauseTime = Time.timeSinceLevelLoad - _pauseTimeStamp;
_startTime += thePauseTime;
_delayTimeStamp += thePauseTime;
}
}
protected void Rewind ()
{
_startTime = Time.timeSinceLevelLoad;
}
public void Stop ()
{
_running = false;
_loopCount = 0;
}
protected void UpdateObjectValue (float easeValue)
{
if (_startValue is float) {
_value = (float)_startValue + _range * easeValue;
}
if (_startValue is Vector2) {
Vector2 direction = ((Vector2)_endValue - (Vector2)_startValue).normalized;
direction *= _range;
_value = (Vector2)_startValue + direction * easeValue;
}
if (_startValue is Vector3) {
Vector3 direction = ((Vector3)_endValue - (Vector3)_startValue).normalized;
direction *= _range;
_value = (Vector3)_startValue + direction * easeValue;
}
if (_startValue is Quaternion) {
_value = Quaternion.Lerp ((Quaternion)_startValue, (Quaternion)_endValue, easeValue);
}
if (_startValue is Color) {
float rangeR = ((Color)_endValue).r - ((Color)_startValue).r;
float rangeG = ((Color)_endValue).g - ((Color)_startValue).g;
float rangeB = ((Color)_endValue).b - ((Color)_startValue).b;
float rangeA = ((Color)_endValue).a - ((Color)_startValue).a;
_value = new Color (((Color)_startValue).r + rangeR * easeValue,
((Color)_startValue).g + rangeG * easeValue,
((Color)_startValue).b + rangeB * easeValue,
((Color)_startValue).a + rangeA * easeValue
);
}
}
float GetDuration (float speed, object start, object end)
{
return Mathf.Abs (GetRange (start, end) / speed);
}
float GetRange (object start, object end)
{
if (start is float) {
return (float)end - (float)start;
}
if (start is Vector2) {
return Mathf.Abs (Vector2.Distance ((Vector2)start, (Vector2)end));
}
if (start is Vector3) {
return Vector3.Distance ((Vector3)start, (Vector3)end);
}
if (start is Quaternion) {
return Quaternion.Angle ((Quaternion)start, (Quaternion)end);
}
return 0f;
}
public float LifeTime ()
{
return Time.timeSinceLevelLoad - _startTime;
}
public void OnCompleteEvent ()
{
Destroy ();
if (CompleteEvent != null)
CompleteEvent (this);
}
public void Destroy ()
{
Stop ();
TweenController.Singleton.UpdateEvent -= Update;
if (_descriptor.updateMethod != null)
UpdateEvent -= _descriptor.updateMethod;
if (_descriptor.completeMethod != null)
CompleteEvent -= _descriptor.completeMethod;
//remove from list
if (Tweens.Contains (this))
Tweens.Remove (this);
}
public static Tween to (TweenDescriptor descriptor)
{
Tween t = new Tween (descriptor);
return t;
}
public static Tween delayedCall (float aDelay, VoidDelegate onComplete)
{
return delayedCall (aDelay, (Tween Tween) => {
onComplete.Invoke ();});
}
public static Tween delayedCall (float aDelay, TweenDelegate onComplete)
{
return to (0.0f, 1.0f, aDelay, Tween.EaseType.linear, null, onComplete, 0f);
}
public static Tween to (object startValue, object endValue, float time, Tween.EaseType easeType, TweenDelegate onUpdate = null, TweenDelegate onComplete = null, float delay = 0.0f, bool yoyo = false, int loops = 0)
{
TweenDescriptor descriptor = new TweenDescriptor ();
descriptor.fromValue = startValue;
descriptor.toValue = endValue;
descriptor.easeType = easeType;
descriptor.delay = delay;
descriptor.time = time;
descriptor.yoyo = yoyo;
descriptor.loops = loops;
descriptor.updateMethod = onUpdate;
descriptor.completeMethod = onComplete;
return to (descriptor);
}
public static Tween toWithSpeed (object startValue, object endValue, float speed, Tween.EaseType easeType, TweenDelegate onUpdate = null, TweenDelegate onComplete = null, float delay = 0.0f, bool yoyo = false, int loops = 0)
{
TweenDescriptor descriptor = new TweenDescriptor ();
descriptor.baseOnSpeed = true;
descriptor.fromValue = startValue;
descriptor.toValue = endValue;
descriptor.easeType = easeType;
descriptor.delay = delay;
descriptor.speed = speed;
descriptor.yoyo = yoyo;
descriptor.loops = loops;
descriptor.updateMethod = onUpdate;
descriptor.completeMethod = onComplete;
return to (descriptor);
}
protected IEaser GetEaser (EaseType easeType)
{
IEaser easer = null;
switch (easeType) {
case EaseType.easeInQuad:
easer = new Easer (new EasingFunction (EasingFunctions.easeInQuad));
break;
case EaseType.easeOutQuad:
easer = new Easer (new EasingFunction (EasingFunctions.easeOutQuad));
break;
case EaseType.easeInOutQuad:
easer = new Easer (new EasingFunction (EasingFunctions.easeInOutQuad));
break;
case EaseType.easeInCubic:
easer = new Easer (new EasingFunction (EasingFunctions.easeInCubic));
break;
case EaseType.easeOutCubic:
easer = new Easer (new EasingFunction (EasingFunctions.easeOutCubic));
break;
case EaseType.easeInOutCubic:
easer = new Easer (new EasingFunction (EasingFunctions.easeInOutCubic));
break;
case EaseType.easeInQuart:
easer = new Easer (new EasingFunction (EasingFunctions.easeInQuart));
break;
case EaseType.easeOutQuart:
easer = new Easer (new EasingFunction (EasingFunctions.easeOutQuart));
break;
case EaseType.easeInOutQuart:
easer = new Easer (new EasingFunction (EasingFunctions.easeInOutQuart));
break;
case EaseType.easeInQuint:
easer = new Easer (new EasingFunction (EasingFunctions.easeInQuint));
break;
case EaseType.easeOutQuint:
easer = new Easer (new EasingFunction (EasingFunctions.easeOutQuint));
break;
case EaseType.easeInOutQuint:
easer = new Easer (new EasingFunction (EasingFunctions.easeInOutQuint));
break;
case EaseType.easeInSine:
easer = new Easer (new EasingFunction (EasingFunctions.easeInSine));
break;
case EaseType.easeOutSine:
easer = new Easer (new EasingFunction (EasingFunctions.easeOutSine));
break;
case EaseType.easeInOutSine:
easer = new Easer (new EasingFunction (EasingFunctions.easeInOutSine));
break;
case EaseType.easeInExpo:
easer = new Easer (new EasingFunction (EasingFunctions.easeInExpo));
break;
case EaseType.easeOutExpo:
easer = new Easer (new EasingFunction (EasingFunctions.easeOutExpo));
break;
case EaseType.easeInOutExpo:
easer = new Easer (new EasingFunction (EasingFunctions.easeInOutExpo));
break;
case EaseType.easeInCirc:
easer = new Easer (new EasingFunction (EasingFunctions.easeInCirc));
break;
case EaseType.easeOutCirc:
easer = new Easer (new EasingFunction (EasingFunctions.easeOutCirc));
break;
case EaseType.easeInOutCirc:
easer = new Easer (new EasingFunction (EasingFunctions.easeInOutCirc));
break;
case EaseType.linear:
easer = new Easer (new EasingFunction (EasingFunctions.linear));
break;
case EaseType.spring:
easer = new Easer (new EasingFunction (EasingFunctions.spring));
break;
case EaseType.easeInBounce:
easer = new Easer (new EasingFunction (EasingFunctions.easeInBounce));
break;
case EaseType.easeOutBounce:
easer = new Easer (new EasingFunction (EasingFunctions.easeOutBounce));
break;
case EaseType.easeInOutBounce:
easer = new Easer (new EasingFunction (EasingFunctions.easeInOutBounce));
break;
case EaseType.easeInBack:
easer = new Easer (new EasingFunction (EasingFunctions.easeInBack));
break;
case EaseType.easeOutBack:
easer = new Easer (new EasingFunction (EasingFunctions.easeOutBack));
break;
case EaseType.easeInOutBack:
easer = new Easer (new EasingFunction (EasingFunctions.easeInOutBack));
break;
case EaseType.easeInElastic:
easer = new Easer (new EasingFunction (EasingFunctions.easeInElastic));
break;
case EaseType.easeOutElastic:
easer = new Easer (new EasingFunction (EasingFunctions.easeOutElastic));
break;
case EaseType.easeInOutElastic:
easer = new Easer (new EasingFunction (EasingFunctions.easeInOutElastic));
break;
//COMBINED EASERS:
case EaseType.easeInQuartOutSine:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInQuart),
new EasingFunction (EasingFunctions.easeOutSine),
new EasingFunction (EasingFunctions.linear));
break;
case EaseType.easeInQuartOutQuad:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInQuart),
new EasingFunction (EasingFunctions.easeOutQuad),
new EasingFunction (EasingFunctions.linear));
break;
case EaseType.easeInQuartOutCubic:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInQuart),
new EasingFunction (EasingFunctions.easeOutCubic),
new EasingFunction (EasingFunctions.linear));
break;
case EaseType.easeInSineOutQuart:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInSine),
new EasingFunction (EasingFunctions.easeOutQuart),
new EasingFunction (EasingFunctions.linear));
break;
case EaseType.easeInSineOutQuad:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInSine),
new EasingFunction (EasingFunctions.easeOutQuad),
new EasingFunction (EasingFunctions.linear));
break;
case EaseType.easeInSineOutCubic:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInSine),
new EasingFunction (EasingFunctions.easeOutCubic),
new EasingFunction (EasingFunctions.linear));
break;
case EaseType.easeInQuadOutQuart:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInQuad),
new EasingFunction (EasingFunctions.easeOutQuart),
new EasingFunction (EasingFunctions.linear));
break;
case EaseType.easeInQuadOutSine:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInQuad),
new EasingFunction (EasingFunctions.easeOutSine),
new EasingFunction (EasingFunctions.linear));
break;
case EaseType.easeInQuadOutCubic:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInQuad),
new EasingFunction (EasingFunctions.easeOutCubic),
new EasingFunction (EasingFunctions.linear));
break;
case EaseType.easeInCubicOutQuad:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInCubic),
new EasingFunction (EasingFunctions.easeOutQuad),
new EasingFunction (EasingFunctions.linear));
break;
case EaseType.easeInCubicOutSine:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInCubic),
new EasingFunction (EasingFunctions.easeOutSine),
new EasingFunction (EasingFunctions.linear));
break;
case EaseType.easeInCubicOutQuart:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInCubic),
new EasingFunction (EasingFunctions.easeOutQuart),
new EasingFunction (EasingFunctions.linear));
break;
case EaseType.easeInOutSineFlatLine:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInOutSine),
new EasingFunction (EasingFunctions.linear),
new EasingFunction (EasingFunctions.constant));
break;
case EaseType.easeInOutSineOutLinear:
easer = new CombinedEaser (new EasingFunction (EasingFunctions.easeInOutQuint),
new EasingFunction (EasingFunctions.easeInOutSine),
new EasingFunction (EasingFunctions.linear));
break;
}
return easer;
}
public enum EaseType
{
easeInQuad,
easeOutQuad,
easeInOutQuad,
easeInCubic,
easeOutCubic,
easeInOutCubic,
easeInQuart,
easeOutQuart,
easeInOutQuart,
easeInQuint,
easeOutQuint,
easeInOutQuint,
easeInSine,
easeOutSine,
easeInOutSine,
easeInExpo,
easeOutExpo,
easeInOutExpo,
easeInCirc,
easeOutCirc,
easeInOutCirc,
linear,
spring,
easeInBounce,
easeOutBounce,
easeInOutBounce,
easeInBack,
easeOutBack,
easeInOutBack,
easeInElastic,
easeOutElastic,
easeInOutElastic,
punch,
//combined tweens:
easeInQuartOutSine,
easeInQuartOutQuad,
easeInQuartOutCubic,
easeInSineOutQuart,
easeInSineOutQuad,
easeInSineOutCubic,
easeInQuadOutQuart,
easeInQuadOutSine,
easeInQuadOutCubic,
easeInCubicOutQuad,
easeInCubicOutSine,
easeInCubicOutQuart,
easeInOutSineFlatLine,
easeInOutSineOutLinear
}
}
}