-
Notifications
You must be signed in to change notification settings - Fork 2
/
tran.lua
743 lines (606 loc) · 19.4 KB
/
tran.lua
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
--=====================================================================================--
--[[
Abstract: Improved transition library
Version: 1.0.4 (September 9th 2012)
Copyright (c) 2012 Lian Yuanlin, timespacemagic LLP
Changelog:
v1.0.0 Transition call to replace original Corona SDK function
v1.0.1 Added stage check for object in transition. Kills listener if object missing.
v1.0.2 Added support for mask parameters.
v1.0.3 Fixed bug in scaling affecting reference point.
v1.0.4 Changed to recommended way of using external modules
--]]
--======================================================================================--
local tranImprov = {}
-- table to track all transitions
local transitionStack = {}
-- localising math functions
local sin = math.sin
local asin = math.asin
local cos = math.cos
local sqrt = math.sqrt
local abs = math.abs
local PI = math.pi
-- Foward declaration of listener
local transitFunc
-- forward declare functions
local cancel,pauser,resumer,cancelAll,pauseAll,resumeAll,to,from
---------------------------------------------------------------------------------------------------
-- Transition Easing Functions --
-- Based on Actionscript Easing Equations from Robert Penner http://www.robertpenner.com/easing/ --
-- Open source under the BSD License. --
-- Copyright 2001 Robert Penner --
-- All rights reserved. --
---------------------------------------------------------------------------------------------------
local easings = {}
--[[Linear Easings (t) --]]
easings["linear"] = function(t,b,c,d)
return c*t/d+b
end
--[[Quadratic Easings (t^2)--]]
easings["inQuad"] = function(t,b,c,d)
t=t/d
return c*t*t + b
end
easings["outQuad"] = function(t,b,c,d)
t=t/d
return (-c)*t*(t-2) + b
end
easings["inOutQuad"] = function(t,b,c,d)
t=2*t/d
if t < 1 then
return c*0.5*t*t + b
end
t=t-1
return (-c)*0.5*((t)*(t-2)-1) + b
end
--[[Quadratic Easings (t^3)--]]
easings["inCubic"] = function(t,b,c,d)
return c*((t/d)^3) + b
end
easings["outCubic"] = function(t,b,c,d)
return c*(((t/d-1)^3)+1) + b
end
easings["inOutCubic"] = function(t,b,c,d)
t=2*t/d
if t<1 then
return c*0.5*(t^3) + b;
end
return c*0.5*(((t-2)^3)+2) + b;
end
--[[Quartic Easings (t^4)--]]
easings["inQuart"] = function(t,b,c,d)
t=t/d
return c*(t^4) + b;
end
easings["outQuart"] = function(t,b,c,d)
return -c*(((t/d-1)^4)-1) + b
end
easings["inOutQuart"] = function(t,b,c,d)
t=2*t/d
if t < 1 then
return c*0.5*(t^4) + b
end
return -c*0.5*(((t-2)^4)-2) + b
end
--[[Quintic Easings (t^5)--]]
easings["inQuint"] = function(t,b,c,d)
return c*((t/d)^5) + b
end
easings["outQuint"] = function(t,b,c,d)
return c*((t/d-1)^5+1) + b
end
easings["inOutQuint"] = function(t,b,c,d)
t=2*t/d
if t<1 then
return c*0.5*(t^5) + b
end
return c*0.5*((t-2)^5+2) + b
end
--[[Exponential Easings (2^t)--]]
easings["inExpo"] = function(t,b,c,d)
if t == 0 then
return b
else
return c*(2^(10*(t/d-1))) + b
end
end
easings["outExpo"] = function(t,b,c,d)
if t==d then
return b+c
else
return c*(-2^(-10*t/d)+1) + b
end
end
easings["inOutExpo"] = function(t,b,c,d)
if t==0 then return b end
if t==d then return b+c end
t=2*t/d
if t<1 then
return c*0.5*(2^(10*(t-1))) + b
end
t=t-1
return c*0.5*(2-(2^(-10*t))) + b;
end
--[[Sinusoidal Easings (sin t)--]]
easings["inSine"] = function(t,b,c,d)
return -c*cos(t/d*PI*0.5) + c + b
end
easings["outSine"] = function(t,b,c,d)
return c*sin(t/d*PI*0.5) + b
end
easings["inOutSine"] = function(t,b,c,d)
return -c*0.5*(cos(PI*t/d)-1) + b
end
--[[Circular Easings (sqrt(1-t^2))--]]
easings["inCirc"] = function(t,b,c,d)
t=t/d
return -c*(sqrt(1-t*t)-1) + b
end
easings["outCirc"] = function(t,b,c,d)
t=t/d-1
return c*sqrt(1-t*t) + b;
end
easings["inOutCirc"] = function(t,b,c,d)
t=2*t/d
if t<1 then
return -c*0.5*(sqrt(1 - t*t)-1) + b
end
t=t-2
return c*0.5*(sqrt(1-t*t)+1) + b
end
--[[Elastic Easings (exponentially decaying sine wave)
customizable period and amplitude swapped out for default presets--]]
easings["inElastic"] = function(t,b,c,d)
if t==0 then return b end
t=t/d
if t==1 then return b+c end
local p = d*0.3
local a = abs(c)
local s = p/(2*PI)*asin(c/a)
t=t-1
return -(a*(2^(10*(t)))*sin((t*d-s)*(2*PI)/p)) + b
end
easings["outElastic"] = function(t,b,c,d)
if t==0 then return b end
t=t/d
if t==1 then return b+c end
local p=d*0.3
local a=abs(c)
local s = p/(2*PI)*asin(c/a)
return a*(2^(-10*t))*sin((t*d-s)*(2*PI)/p) + c + b
end
easings["inOutElastic"] = function(t,b,c,d)
if t==0 then return b end
t=2*t/d
if t==2 then return b+c end
local p = d*(0.45)
local a = abs(c)
local s = p/(2*PI)*asin(c/a)
if t<1 then
t=t-1
return -0.5*(a*(2^(10*t))*sin((t*d-s)*(2*PI)/p)) + b
end
t=t-1
return a*(2^(-10*t))*sin((t*d-s)*(2*PI)/p )*0.5 + c + b
end
--[[BACK EASING: overshooting cubic easing: (s+1)*t^3 - s*t^2--]]
easings["inBack"] = function(t,b,c,d)
local s = 1.70158 -- gives a back bounce effect of 10% c, according to Robert Penner anyway
t=t/d
return c*t*t*((s+1)*t-s) + b
end
easings["outBack"] = function(t,b,c,d)
local s = 1.70158
t=t/d-1
return c*(t*t*((s+1)*t+s)+1) + b
end
easings["inOutBack"] = function(t,b,c,d)
local s = 1.70158
t=2*t/d
if t<1 then
s=s*1.525
return c*0.5*(t*t*((s+1)*t-s)) + b
end
t=t-2
s=s*1.525
return c*0.5*(t*t*((s+1)*t+s) +2) + b
end
--[[Bounce Easings (exponentially decaying parabolic bounce)--]]
easings["outBounce"] = function(t,b,c,d)
t=t/d
if t<(1/2.75) then
return c*(7.5625*t*t) + b
elseif t < (2/2.75) then
t=t-(1.5/2.75)
return c*(7.5625*t*t+0.75) + b
elseif t < (2.5/2.75) then
t=t-(2.25/2.75)
return c*(7.5625*t*t+0.9375) + b
else
t=t-(2.625/2.75)
return c*(7.5625*t*t+0.984375) + b
end
end
easings["inBounce"] = function(t,b,c,d)
return c-easings["outBounce"](d-t,0,c,d) + b
end
easings["inOutBounce"] = function(t,b,c,d)
if t < d/2 then
return easings["inBounce"](t*2, 0, c, d)*0.5 + b
end
return easings["outBounce"](t*2-d,0,c,d)*0.5+c*0.5 + b
end
-- function to replace transition.cancel
function cancel(transitionHandle)
local index = #transitionStack
while transitionStack[index] ~= transitionHandle and index > 0 do
index = index - 1
end
Runtime:removeEventListener("enterFrame", transitionHandle)
-- to cancel, locate transition handle to remove, remove enterFrame listener
table.remove(transitionStack, index)
transitionHandle = nil
end
-- function to pauser transitions
function pauser(transitionHandle)
transitionHandle.isActive = false
end
-- function to resumer transitions
function resumer(transitionHandle)
transitionHandle.isActive = true
end
-- Runtime transition control listener
transitFunc = function(self,e)
local eTime = e.time
-- only carry out animation when unpaused
if self.isActive then
local deltaTime = eTime - self.prevTime
local obj = self.obj
-- execute onStart Listener if assigned
if self.onStart then
self.onStart(obj)
self.onStart = nil
end
deltaTime = deltaTime * self.timeScale -- timeScale parameter allows slowing and increasing speed of animation on the fly
self.timePassed = self.timePassed + deltaTime
-- check if object has been removed
if obj.x then
-- make sure delay has passed
if self.timePassed-self.delay > 0 then
local timePassed = self.timePassed - self.delay
-- check if end point reached, ensure object is at required position
if timePassed >= self.time then
if self.x then
obj:translate(self.endX-obj.x,0)
end
if self.y then
obj:translate(0,self.endY-obj.y)
end
if self.width then
obj.width = self.endWidth
end
if self.height then
obj.height = self.endHeight
end
if self.xScale then
obj.xScale = self.endxS
end
if self.yScale then
obj.yScale = self.endyS
end
if self.alpha then
obj.alpha = self.endAlpha
end
if self.rotation then
obj:rotate(self.endRot-obj.rotation)
end
if self.maskX then
obj.maskX = self.endmX
end
if self.maskY then
obj.maskY = self.endmY
end
if self.maskScaleX then
obj.maskScaleX = self.endmxS
end
if self.maskScaleY then
obj.maskScaleY = self.endmyS
end
if self.maskRotation then
obj.maskRotation = self.endmRot
end
if self.onFrac then
if self.onFrac.fraction <= 1 then -- makes sure fractional listener is executed in cases whereby framerate drops, if it's within animation time
self.onFrac.listener(obj)
end
end
if self.onComplete then
self.onComplete(obj)
end
cancel(self)
self=nil
else
-- check for fractional listener
if self.onFrac then
if timePassed/self.time >= self.onFrac.fraction then
self.onFrac.listener(obj)
self.onFrac = nil
end
end
-- change parameters if assigned
if self.x then
obj:translate(self.transition(timePassed,self.x,self.dX,self.time)-obj.x,0)
end
if self.y then
obj:translate(0,self.transition(timePassed,self.y,self.dY,self.time)-obj.y)
end
if self.width then
obj.width = self.transition(timePassed,self.width,self.dWidth,self.time)
end
if self.height then
obj.height = self.transition(timePassed,self.height,self.dHeight,self.time)
end
if self.xScale then
obj.xScale = self.transition(timePassed,self.xScale,self.dxS,self.time)
end
if self.yScale then
obj.yScale = self.transition(timePassed,self.yScale,self.dyS,self.time)
end
if self.alpha then
obj.alpha = self.transition(timePassed,self.alpha,self.dA,self.time)
end
if self.rotation then
obj:rotate(self.transition(timePassed,self.rotation,self.dRot,self.time)-obj.rotation)
end
if self.maskX then
obj.maskX = self.transition(timePassed,self.maskX,self.dmX,self.time)
end
if self.maskY then
obj.maskY = self.transition(timePassed,self.maskY,self.dmY,self.time)
end
if self.maskScaleX then
obj.maskScaleX = self.transition(timePassed,self.maskScaleX,self.dmxS,self.time)
end
if self.maskScaleY then
obj.maskScaleY = self.transition(timePassed,self.maskScaleY,self.dmyS,self.time)
end
if self.maskRotation then
obj.maskRotation = self.transition(timePassed,self.maskRotation,self.dmRot,self.time)
end
end
end
else
-- kill enterFrame listener ie. transition if object has been removed
print("Transition Warning: Object Missing. Cancelling transition.")
cancel(self)
self=nil
end
end
if self then
self.prevTime = eTime
end
end
-- replacing the transition.to function
function to(obj,params)
if obj == nil then
return false
end
local transitionHandle = {}
-- setting up flags for required changes
-- check for delta parameter
if params.delta then
if params.x then transitionHandle.x = obj.x; transitionHandle.endX = params.x + obj.x; transitionHandle.dX = params.x; end
if params.y then transitionHandle.y = obj.y; transitionHandle.endY = params.y + obj.y; transitionHandle.dY = params.y; end
if params.width then transitionHandle.width = obj.width; transitionHandle.endWidth = params.width + obj.width; transitionHandle.dWidth = params.width; end
if params.height then transitionHandle.height = obj.height; transitionHandle.endHeight = params.height + obj.height; transitionHandle.dHeight = params.height; end
if params.xScale then transitionHandle.xScale = obj.xScale; transitionHandle.endxS = params.xScale + obj.xScale; transitionHandle.dxS = params.xScale; end
if params.yScale then transitionHandle.yScale = obj.yScale; transitionHandle.endyS = params.yScale + obj.yScale; transitionHandle.dyS = params.yScale; end
if params.alpha then transitionHandle.alpha = obj.alpha; transitionHandle.endAlpha = params.alpha + obj.alpha; transitionHandle.dA = params.alpha; end
if params.rotation then transitionHandle.rotation = obj.rotation; transitionHandle.endRot = params.rotation + obj.rotation; transitionHandle.dRot = params.rotation; end
if params.maskX then transitionHandle.maskX = obj.maskX; transitionHandle.endmX = params.maskX + obj.maskX; transitionHandle.dmX = params.maskX; end
if params.maskY then transitionHandle.maskY = obj.maskY; transitionHandle.endmY = params.maskY + obj.maskY; transitionHandle.dmY = params.maskY; end
if params.maskScaleX then transitionHandle.maskScaleX = obj.maskScaleX; transitionHandle.endmxS = params.maskScaleX + obj.maskScaleX; transitionHandle.dmxS = params.maskScaleX; end
if params.maskScaleY then transitionHandle.maskScaleY = obj.maskScaleY; transitionHandle.endmyS = params.maskScaleY + obj.maskScaleY; transitionHandle.dmyS = params.maskScaleY; end
if params.maskRotation then transitionHandle.maskRotation = obj.maskRotation; transitionHandle.endmRot = params.maskRotation + obj.maskRotation; transitionHandle.dmRot = params.maskRotation; end
else
if obj == nil or params == nil or transitionHandle == nil then
return false
end
if params.x then transitionHandle.x = obj.x; transitionHandle.endX = params.x; transitionHandle.dX = params.x - obj.x; end
if params.y then transitionHandle.y = obj.y; transitionHandle.endY = params.y; transitionHandle.dY = params.y - obj.y; end
if params.width then transitionHandle.width = obj.width; transitionHandle.endWidth = params.width; transitionHandle.dWidth = params.width - obj.width; end
if params.height then transitionHandle.height = obj.height; transitionHandle.endHeight = params.height; transitionHandle.dHeight = params.height - obj.height; end
if params.xScale then transitionHandle.xScale = obj.xScale; transitionHandle.endxS = params.xScale; transitionHandle.dxS = params.xScale - obj.xScale; end
if params.yScale then transitionHandle.yScale = obj.yScale; transitionHandle.endyS = params.yScale; transitionHandle.dyS = params.yScale - obj.yScale; end
if params.alpha then transitionHandle.alpha = obj.alpha; transitionHandle.endAlpha = params.alpha; transitionHandle.dA = params.alpha - obj.alpha; end
if params.rotation then transitionHandle.rotation = obj.rotation; transitionHandle.endRot = params.rotation; transitionHandle.dRot = params.rotation - obj.rotation; end
if params.maskX then transitionHandle.maskX = obj.maskX; transitionHandle.endmX = params.maskX; transitionHandle.dmX = params.maskX - obj.maskX; end
if params.maskY then transitionHandle.maskY = obj.maskY; transitionHandle.endmY = params.maskY; transitionHandle.dmY = params.maskY - obj.maskY; end
if params.maskScaleX then transitionHandle.maskScaleX = obj.maskScaleX; transitionHandle.endmxS = params.maskScaleX; transitionHandle.dmxS = params.maskScaleX - obj.maskScaleX; end
if params.maskScaleY then transitionHandle.maskScaleY = obj.maskScaleY; transitionHandle.endmyS = params.maskScaleY; transitionHandle.dmyS = params.maskScaleY - obj.maskScaleY; end
if params.maskRotation then transitionHandle.maskRotation = obj.maskRotation; transitionHandle.endmRot = params.maskRotation; transitionHandle.dmRot = params.maskRotation - obj.maskRotation; end
end
-- animation parameters
transitionHandle.transition = easings[params.transition] or easings["linear"]
transitionHandle.time = params.time or 500
transitionHandle.delay = params.delay or 0
transitionHandle.onStart = params.onStart or false
transitionHandle.onComplete = params.onComplete or false
transitionHandle.onFrac = params.onFrac or false
transitionHandle.timeScale = 1
transitionHandle.prevTime = system.getTimer()
transitionHandle.timePassed = 0
-- control parameters
transitionHandle.isActive = true
transitionHandle.obj = obj
-- start enterFrame listener
transitionHandle.enterFrame = transitFunc
Runtime:addEventListener("enterFrame", transitionHandle)
-- add to stack
transitionStack[#transitionStack+1] = transitionHandle
-- return handle
return transitionHandle
end
-- function to replace transition.from function
function from(obj,params)
local temp
-- setting up flags for required changes
-- check for delta parameter
if params.delta then
-- swapping starting and ending points with delta considered
if params.x then
obj:translate(params.x,0)
params.x = -params.x
end
if params.y then
obj:translate(0,params.y)
params.y = -params.y
end
if params.width then
obj.width = obj.width + params.width
params.width = -params.width
end
if params.height then
obj.height = obj.height + params.height
params.height = -params.height
end
if params.xScale then
obj.xScale = params.xScale + obj.xScale
params.xScale = -params.xScale
end
if params.yScale then
obj.yScale = params.yScale + obj.yScale
params.yScale = -params.yScale
end
if params.alpha then
obj.alpha = obj.alpha + params.alpha
params.alpha = -params.alpha
end
if params.rotation then
obj:rotate(params.rotation)
params.rotation = -params.rotation
end
if params.maskX then
obj.maskX = obj.maskX + params.maskX
params.alpha = -params.maskX
end
if params.maskY then
obj.maskY = obj.maskY + params.maskY
params.maskY = -params.maskY
end
if params.maskScaleX then
obj.maskScaleX = obj.maskScaleX + params.maskScaleX
params.maskScaleX= -params.maskScaleX
end
if params.maskScaleY then
obj.maskScaleY = obj.maskScaleY + params.maskScaleY
params.maskScaleY = -params.maskScaleY
end
if params.maskRotation then
obj.maskRotation = obj.maskRotation + params.maskRotation
params.maskRotation = -params.maskRotation
end
else
-- swapping starting and ending points
if params.x then
temp = obj.x
obj:translate(params.x-obj.x,0)
params.x = temp
end
if params.y then
temp = obj.y
obj:translate(0,params.y-obj.y)
params.y = temp
end
if params.width then
temp = obj.width
obj.width = params.width
params.width = temp
end
if params.height then
temp = obj.height
obj.height = params.height
params.height = temp
end
if params.xScale then
temp = obj.xScale
obj.xScale = params.xScale
params.xScale = temp
end
if params.yScale then
temp = obj.yScale
obj.yScale = params.yScale
params.yScale = temp
end
if params.alpha then
temp = obj.alpha
obj.alpha = params.alpha
params.alpha = temp
end
if params.rotation then
temp = obj.rotation
obj:rotate(params.rotation-obj.rotation)
params.rotation = temp
end
if params.maskX then
temp = obj.maskX
obj.maskX = params.maskX
params.maskX = temp
end
if params.maskY then
temp = obj.maskY
obj.maskY = params.maskY
params.maskY = temp
end
if params.maskScaleX then
temp = obj.maskScaleX
obj.maskScaleX = params.maskScaleX
params.maskScaleX = temp
end
if params.maskScaleY then
temp = obj.maskScaleY
obj.maskScaleY = params.maskScaleY
params.maskScaleY = temp
end
if params.maskRotation then
temp = obj.maskRotation
obj.maskRotation = params.maskRotation
params.maskRotation = temp
end
end
return to(obj,params)
end
function pauseAll()
if #transitionStack > 0 then
for i=#transitionStack,1,-1 do
pauser(transitionStack[i])
end
end
end
function resumeAll()
if #transitionStack > 0 then
for i=#transitionStack,1,-1 do
resumer(transitionStack[i])
end
end
end
function cancelAll()
if #transitionStack > 0 then
for i=#transitionStack,1,-1 do
cancel(transitionStack[i])
transitionStack[i] = nil
end
transitionStack = {}
end
end
-- accessibility
tranImprov.cancel = cancel
tranImprov.pauser = pauser
tranImprov.resumer = resumer
tranImprov.to = to
tranImprov.from = from
tranImprov.cancelAll = cancelAll
tranImprov.pauseAll = pauseAll
tranImprov.resumeAll = resumeAll
return tranImprov