This repository was archived by the owner on Jan 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcftTween.monkey
More file actions
executable file
·653 lines (554 loc) · 16.8 KB
/
cftTween.monkey
File metadata and controls
executable file
·653 lines (554 loc) · 16.8 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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#Rem
'This is a copy of the monkey sample script in bananas/skn3/tweening/tween.monkey
#End
'#DOCOFF#
Import mojo
'--------------------------------------------------------------------------------------------------
'Tween module contents
Const PI := 3.14159265
'---------------
'base classes
Class Tween
Global Linear:TweenEquationCall = New TweenEquationCallLinear
Global Back:TweenEquation = New TweenEquationBack
Global Bounce:TweenEquation = New TweenEquationBounce
Global Circ:TweenEquation = New TweenEquationCirc
Global Cubic:TweenEquation = New TweenEquationCubic
Global Elastic:TweenEquation = New TweenEquationElastic
Global Expo:TweenEquation = New TweenEquationExpo
Global Quad:TweenEquation = New TweenEquationQuad
Global Quart:TweenEquation = New TweenEquationQuart
Global Quint:TweenEquation = New TweenEquationQuint
Global Sine:TweenEquation = New TweenEquationSine
Field equation:TweenEquationCall
Field start:Float
Field change:Float
Field current:Float
Field isActive := False
Field isLooping := False
Field isYoYo := False
Field duration:Int
Field loopCount:Int
Field timeStart:Int
Field timeCurrent:Int
Field timePrevious:Int
' --- constructors ---
Method New(equation:TweenEquationCall,startValue:Float,endValue:Float,duration:Int)
SetEquation(equation)
SetDuration(duration)
SetValue(startValue,endValue)
End
' --- private methods ---
Private
Method UpdateValue()
current = equation.Call(timeCurrent,start,change,duration)
End
Public
' --- value methods ---
Method Value:Float()
Return current
End
' --- setup methods methods ---
Method SetEquation(equation:TweenEquationCall)
Self.equation = equation
End
Method SetDuration(duration:Int)
Self.duration = duration
End
Method SetValue(startValue:Float,endValue:Float)
start = startValue
change = endValue - startValue
End
Method SetYoYo(flag:Bool)
isYoYo = flag
End
Method SetLooping(flag:Bool)
isLooping = flag
End
' --- control methods ---
Method Start()
' --- this will re-start the tween ---
Rewind()
isActive = True
loopCount = 0
End
Method Stop()
' --- stop the tween from updating ---
isActive = False
End
Method Resume()
' --- resume the tween from a paused state ---
isActive = True
'update the start timer!
End
Method Rewind()
' --- put the tween at the start of its tween ---
timeCurrent = 0
timeStart = Millisecs()
UpdateValue()
End
Method FastForward()
' --- set the tween at the end ---
timeCurrent = duration
UpdateValue()
Stop()
End
Method ContinueTo(endValue:Float,duration:Int=0)
' --- change the end target for teh value
start = current
SetValue(start,endValue)
If isActive
'isActive so need to continue operation
If duration = 0
'no duration specified so use previous duration!
Self.duration = duration - timeCurrent
Else
'override duration
Self.duration = duration
Endif
timeStart = Millisecs()
timeCurrent = 0
If duration <= 0
duration = 0
Stop()
Endif
Else
If duration > 0 SetDuration(duration)
Start()
Endif
End
Method YoYo()
ContinueTo(start)
End
' --- class methods ---
Method Update()
If isActive
'update the current time!
timePrevious = timeCurrent
Local time := Millisecs() - timeStart
If time > duration
'time is beyond length of tween
If isLooping Or isYoYo
'increase the loop count
loopCount += 1
'look at yoyoing the values
If isYoYo
timeCurrent = duration
current = start + change
If isLooping Or loopCount <= 1
ContinueTo(start,duration)
Else
Stop()
Endif
Else
'wrap around the tween to the start
timeCurrent = 0'
timeStart = Millisecs()
Endif
Else
'set the tween to the end
timeCurrent = duration
current = start + change
Stop()
Endif
Else If time < 0
'time is before the start of the tween
timeCurrent = 0
timeStart = Millisecs()
UpdateValue()
Else
'time is within the tween
timeCurrent = time
UpdateValue()
Endif
Endif
End
End
Class TweenEquation
' --- really just a grouping class for user access ---
Field EaseIn:TweenEquationCall
Field EaseOut:TweenEquationCall
Field EaseInOut:TweenEquationCall
End
Class TweenEquationCall
' --- base class to provide a call function pointer work around ---
Method Call:Float(t:Float,b:Float,c:Float,d:Float) Abstract
End
'---------------
'Linear
Class TweenEquationCallLinear Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
Return c*t/d + b
End
End
'---------------
'Sine
Class TweenEquationSine Extends TweenEquation
Method New()
EaseIn = New TweenEquationCallSineEaseIn
EaseOut = New TweenEquationCallSineEaseOut
EaseInOut = New TweenEquationCallSineEaseInOut
End
End
Class TweenEquationCallSineEaseIn Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
'return -c * Cos(t / d * (PI / 2)) + c + b
Return -c * Cos((t / d * (PI / 2.0)) * 57.2957795) + c + b
End
End
Class TweenEquationCallSineEaseOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
Return c * Sin((t/d * (PI/2.0)) * 57.2957795) + b
End
End
Class TweenEquationCallSineEaseInOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
Return -c/2.0 * (Cos((PI*t/d) * 57.2957795) - 1.0) + b
End
End
'---------------
'Back
Class TweenEquationBack Extends TweenEquation
Method New()
EaseIn = New TweenEquationCallBackEaseIn
EaseOut = New TweenEquationCallBackEaseOut
EaseInOut = New TweenEquationCallBackEaseInOut
End
End
Class TweenEquationCallBackEaseIn Extends TweenEquationCall
Field s := 1.70158
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d
Return c * t * t * ((s + 1.0) * t - s) + b
End
End
Class TweenEquationCallBackEaseOut Extends TweenEquationCall
Field s := 1.70158
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t = t / d - 1.0
Return c * (t * t * ((s + 1.0) * t + s) + 1.0) + b
End
End
Class TweenEquationCallBackEaseInOut Extends TweenEquationCall
Field s := 1.70158
Field s2:Float
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
s2 = s
t /= d / 2.0
s2 *= 1.525
If t < 1.0
Return c / 2.0 * (t * t *((s2+1.0) * t - s2)) + b
Endif
t -= 2.0
Return c / 2.0 * (t * t * ((s2 + 1.0) * t + s2) + 2.0) + b
End
End
'---------------
'Elastic
Class TweenEquationElastic Extends TweenEquation
Method New()
EaseIn = New TweenEquationCallElasticEaseIn
EaseOut = New TweenEquationCallElasticEaseOut
EaseInOut = New TweenEquationCallElasticEaseInOut
End
End
Class TweenEquationCallElasticEaseIn Extends TweenEquationCall
Field p:Float
Field a:Float
Field s:Float
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
If t = 0.0 Return b
t /= d
If t = 1.0 Return b+c
p = d * 0.3
a = c
s = p / 4.0
t -= 1.0
Return -(a * Pow(2.0,10.0 * (t)) * Sin(((t * d - s) * (2.0 * PI) / p) * 57.2957795)) + b
End
End
Class TweenEquationCallElasticEaseOut Extends TweenEquationCall
Field p:Float
Field a:Float
Field s:Float
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
If t = 0.0 Return b
t /= d
If t = 1.0 Return b+c
p = d * 0.3
a = c
s = p / 4.0
Return (a * Pow(2.0,-10.0 * t) * Sin(((t * d - s) * (2.0 * PI) / p) * 57.2957795) + c + b)
End
End
Class TweenEquationCallElasticEaseInOut Extends TweenEquationCall
Field p:Float
Field a:Float
Field s:Float
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
If t = 0.0 Return b
t /= d / 2.0
If t = 2.0 Return b+c
p = d * (0.3 * 1.5)
a = c
s = p / 4.0
If t < 1.0
t -= 1.0
Return -0.5 * (a * Pow(2.0,10.0 * t) * Sin(((t * d - s) * (2.0 * PI) / p) * 57.2957795)) + b
Endif
t -= 1
Return a * Pow(2.0,-10.0 * t) * Sin(((t * d - s) * (2.0 * PI) / p) * 57.2957795) * 0.5 + c + b
End
End
'---------------
'Bounce
Class TweenEquationBounce Extends TweenEquation
Method New()
EaseIn = New TweenEquationCallBounceEaseIn
EaseOut = New TweenEquationCallBounceEaseOut
EaseInOut = New TweenEquationCallBounceEaseInOut
End
End
Class TweenEquationCallBounceEaseIn Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t = (d-t) / d
If t < 0.3636363
Return c - (c * (7.5625 * t * t)) + b
Else If t < 0.7272727
t -= 0.5454545
Return c - (c * (7.5625 * t * t + 0.75)) + b
Else If t < 0.9090909
t -= 0.8181818
Return c - (c * (7.5625 * t * t + 0.9375)) + b
Else
t -= 0.9636363
Return c - (c * (7.5625 * t * t + 0.984375)) + b
Endif
End
End
Class TweenEquationCallBounceEaseOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d
If t < 0.3636363
Return c *(7.5625 * t * t) + b
Else If t < 0.7272727
t -= 0.5454545
Return c * (7.5625 * t * t + 0.75) + b
Else If t < 0.9090909
t -= 0.8181818
Return c * (7.5625 * t * t + 0.9375) + b
Else
t -= 0.9636363
Return c * (7.5625 * t * t + 0.984375) + b
Endif
End
End
Class TweenEquationCallBounceEaseInOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
If t < d/2.0
t = (d - t * 2.0) / d
If t < 0.3636363
Return (c - (c * (7.5625 * t * t))) * 0.5 + b
Else If t < 0.7272727
t -= 0.5454545
Return (c - (c * (7.5625 * t * t + 0.75))) * 0.5 + b
Else If t < 0.9090909
t -= 0.8181818
Return (c - (c * (7.5625 * t * t + 0.9375))) * 0.5 + b
Else
t -= 0.9636363
Return (c - (c * (7.5625 * t * t + 0.984375))) * 0.5 + b
Endif
Else
t = (t * 2.0 - d) / d
If t < 0.3636363
Return (c *(7.5625 * t * t)) * 0.5 + c * 0.5 + b
Else If t < 0.7272727
t -= 0.5454545
Return (c * (7.5625 * t * t + 0.75)) * 0.5 + c * 0.5 + b
Else If t < 0.9090909
t -= 0.8181818
Return (c * (7.5625 * t * t + 0.9375)) * 0.5 + c * 0.5 + b
Else
t -= 0.9636363
Return (c * (7.5625 * t * t + 0.984375)) * 0.5 + c * 0.5 + b
Endif
Endif
End
End
'---------------
'Circ
Class TweenEquationCirc Extends TweenEquation
Method New()
EaseIn = New TweenEquationCallCircEaseIn
EaseOut = New TweenEquationCallCircEaseOut
EaseInOut = New TweenEquationCallCircEaseInOut
End
End
Class TweenEquationCallCircEaseIn Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d
Return -c * (Sqrt(1.0 - t * t) - 1.0) + b
End
End
Class TweenEquationCallCircEaseOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t = t / d - 1.0
Return c * Sqrt(1.0 - t*t) + b
End
End
Class TweenEquationCallCircEaseInOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d / 2.0
If t < 1.0 Return -c / 2.0 * (Sqrt(1.0 - t * t) - 1.0) + b
t -= 2.0
Return c / 2.0 * (Sqrt(1.0 - t * t) + 1.0) + b
End
End
'---------------
'Cubic
Class TweenEquationCubic Extends TweenEquation
Method New()
EaseIn = New TweenEquationCallCubicEaseIn
EaseOut = New TweenEquationCallCubicEaseOut
EaseInOut = New TweenEquationCallCubicEaseInOut
End
End
Class TweenEquationCallCubicEaseIn Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d
Return c * t * t * t + b
End
End
Class TweenEquationCallCubicEaseOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t = t / d - 1.0
Return c * (t * t * t + 1.0) + b
End
End
Class TweenEquationCallCubicEaseInOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d / 2.0
If t < 1 Return c / 2.0 * t * t * t + b
t -= 2.0
Return c / 2.0 *(t * t * t + 2.0) + b
End
End
'---------------
'Expo
Class TweenEquationExpo Extends TweenEquation
Method New()
EaseIn = New TweenEquationCallExpoEaseIn
EaseOut = New TweenEquationCallExpoEaseOut
EaseInOut = New TweenEquationCallExpoEaseInOut
End
End
Class TweenEquationCallExpoEaseIn Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
If t = 0 Return b
Return c * Pow(2.0,10.0 * (t / d - 1.0)) + b
End
End
Class TweenEquationCallExpoEaseOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
If t = d Return b + c
Return c * (-Pow(2.0,-10.0 * t / d) + 1.0) + b
End
End
Class TweenEquationCallExpoEaseInOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
If t =0.0 Return b
If t = d Return b + c
t /= d / 2.0
If t < 1.0 Return c / 2.0 * Pow(2.0,10.0 * (t - 1.0)) + b
t -= 1.0
Return c / 2.0 * (-Pow(2.0,-10.0 * t) + 2.0) + b
End
End
'---------------
'Quad
Class TweenEquationQuad Extends TweenEquation
Method New()
EaseIn = New TweenEquationCallQuadEaseIn
EaseOut = New TweenEquationCallQuadEaseOut
EaseInOut = New TweenEquationCallQuadEaseInOut
End
End
Class TweenEquationCallQuadEaseIn Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d
Return c * t * t + b
End
End
Class TweenEquationCallQuadEaseOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d
Return -c * t * (t - 2.0) + b
End
End
Class TweenEquationCallQuadEaseInOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d / 2.0
If t < 1.0 Return c / 2.0 * t * t + b
t -= 1.0
Return -c / 2.0 * (t * (t - 2.0) - 1.0) + b
End
End
'---------------
'Quart
Class TweenEquationQuart Extends TweenEquation
Method New()
EaseIn = New TweenEquationCallQuartEaseIn
EaseOut = New TweenEquationCallQuartEaseOut
EaseInOut = New TweenEquationCallQuartEaseInOut
End
End
Class TweenEquationCallQuartEaseIn Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d
Return c * t * t * t * t + b
End
End
Class TweenEquationCallQuartEaseOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t = t / d-1.0
Return -c * (t * t * t * t - 1.0) + b
End
End
Class TweenEquationCallQuartEaseInOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d / 2.0
If t < 1.0 Return c / 2.0 * t * t * t * t + b
t -= 2.0
Return -c / 2.0 * (t * t * t * t - 2.0) + b
End
End
'---------------
'Quint
Class TweenEquationQuint Extends TweenEquation
Method New()
EaseIn = New TweenEquationCallQuintEaseIn
EaseOut = New TweenEquationCallQuintEaseOut
EaseInOut = New TweenEquationCallQuintEaseInOut
End
End
Class TweenEquationCallQuintEaseIn Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d
Return c * t * t * t * t * t + b
End
End
Class TweenEquationCallQuintEaseOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t = t / d - 1.0
Return c * (t * t * t * t * t + 1.0) + b
End
End
Class TweenEquationCallQuintEaseInOut Extends TweenEquationCall
Method Call:Float(t:Float,b:Float,c:Float,d:Float)
t /= d / 2.0
If t < 1.0 Return c / 2.0 * t * t * t * t * t + b
t -= 2.0
Return c / 2.0 * (t * t * t * t * t + 2.0) + b
End
End