-
Notifications
You must be signed in to change notification settings - Fork 2
/
transition.lua
1232 lines (1025 loc) · 39 KB
/
transition.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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-------------------------------------------------------------------------------
-- Corona Labs
--
-- transition.lua
--
-- Version: 2.0
--
-- Code is MIT licensed; see https://www.coronalabs.com/links/code/license
--
-------------------------------------------------------------------------------
local Library = require "CoronaLibrary"
-- the transition object
local lib = Library:new{ name='transition', publisherId='com.coronalabs', version=2 }
-----------------------------------------------------------------------------------------
-- constants
-----------------------------------------------------------------------------------------
local DEBUG_STRING = "Transition 2.0: "
-----------------------------------------------------------------------------------------
-- lib variables
-----------------------------------------------------------------------------------------
-- a table holding all transitions, active or paused
lib._transitionTable = {}
-- a table holding all the transitions, to be iterated by the pause / resume / cancel methods
lib._enterFrameTweens = {}
-- a table holding all the sequences
lib._sequenceTable = {}
-- the last time the application was suspended
lib._prevSuspendTime = 0
-- control variable for the runtime listener
lib._hasEventListener = false
-- reserved properties that cannot be transitioned
lib._reservedProperties =
{
time = true, delay = true, delta = true, iterations = true, tag = true, transition = true,
onComplete = true, onPause = true, onResume = true, onCancel = true, onRepeat = true, onStart = true
}
-- keys that have a number value
lib._numberKeys =
{
x=true, y=true, xScale=true, yScale=true, rotation=true, width=true, height=true,
alpha=true,
xReference=true, yReference=true,
maskX=true, maskY=true, maskScaleX=true, maskScaleY=true, maskRotation=true,
delta=true,
}
lib.debugEnabled = false
-- have pause(), resume() or cancel() API calls ignore empty (nil) reference
lib.ignoreEmptyReference = false
-----------------------------------------------------------------------------------------
-- local functions
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
-- _validateValue( k, v )
-- validates input values. Ensures they are number format
-----------------------------------------------------------------------------------------
local function _validateValue( k, v )
if ( "number" == type( v ) and lib._numberKeys[k] ) then
local isNan = ( v ~= v )
if ( isNan ) then
print( "ERROR: Invalid number argument provided. Attempt to set property " .. k .. " to NaN." )
end
assert( not isNan )
end
return v
end
-----------------------------------------------------------------------------------------
-- copyTable( src )
-- copies the contents of a table to another table
-----------------------------------------------------------------------------------------
local function _copyTable( src )
local t = {}
for k,v in pairs( src ) do
t[k] = v
end
return t
end
-----------------------------------------------------------------------------------------
-- _initTween( tween, parameters )
-- assigns the start and end values of a tween, accounting for delta and valid values
-----------------------------------------------------------------------------------------
lib._initTween = function( tween, parameters )
local target = tween._target
local listener = tween._onStart
if listener then
Runtime.callListener( listener, "onStart", target )
tween._onStart = nil -- only call once
end
local keysStart = {}
local keysFinish = {}
local invalidKeys = lib._reservedProperties
local isDeltaValue = _validateValue( "delta", parameters.delta )
for k,v in pairs( parameters ) do
if not lib._reservedProperties[k] then
-- Only tween properties that have a non-nil starting value
local startValue = target[k]
if startValue then
keysStart[k] = startValue
local finishValue = (isDeltaValue and (startValue+v)) or v
keysFinish[k] = _validateValue( k, finishValue )
end
end
end
tween._keysStart = keysStart
tween._keysFinish = keysFinish
end
-----------------------------------------------------------------------------------------
-- _addTween( tween )
-- inserts a tween to the active tweens table. Sets up the enterFrame listener if
-- the added tween is the first one
-----------------------------------------------------------------------------------------
lib._addTween = function( tween )
local activeTweens = lib._transitionTable
-- Check for any tweens that are already moved to enterframe from lib._transitionTable
if #activeTweens == 0 and #lib._enterFrameTweens > 0 then
activeTweens = lib._enterFrameTweens
end
-- Once we have at least one tween, register for frame events
if #activeTweens == 0 and not lib._hasEventListener then
lib._hasEventListener = true
Runtime:addEventListener( "enterFrame", lib )
end
-- Set a flag so we can mark the tween
tween._instantiatedByTransLib = true
table.insert( activeTweens, tween )
end
-----------------------------------------------------------------------------------------
-- _handleSuspendResume( event )
-- handles the suspending / resuming of transitions in case of suspend / resume events
-----------------------------------------------------------------------------------------
local function _handleSuspendResume( event )
-- if the application got suspended
if "applicationSuspend" == event.type then
-- assign the prevSuspendTime variable to the current internal time
lib._prevSuspendTime = system.getTimer()
-- if the application resumed
elseif "applicationResume" == event.type then
-- calculate the difference between the suspension time and the current internal time
local nextSuspendedTime = system.getTimer() - lib._prevSuspendTime
-- assign the difference to all the transitions that are in the table
for i = 1, #lib._transitionTable do
-- only do this for non-completed transitions
if not lib._transitionTable[ i ]._transitionHasCompleted then
lib._transitionTable[ i ]._timeStart = lib._transitionTable[ i ]._timeStart + nextSuspendedTime
end
end
end
end
-----------------------------------------------------------------------------------------
-- to( targetObject, transitionParams )
-- transitions an object to the specified transitionParams
-----------------------------------------------------------------------------------------
lib.to = function( targetObject, transitionParams )
if nil == targetObject then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a display object to a transition.to call." )
end
end
if nil == transitionParams then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a params table to a transition.to call." )
end
end
local tween = nil
if targetObject and transitionParams then
-- faster to access a local timer var than a global one
local lib = lib
tween = {}
local t = system.getTimer()
tween._target = targetObject
tween._timeStart = t
tween._duration = transitionParams.time or 500
tween.iterations = transitionParams.iterations or 1
tween.tag = transitionParams.tag or ""
tween._lastPausedTime = nil
tween._transition = transitionParams.transition or easing.linear
tween._onStart = Runtime.verifyListener( transitionParams.onStart, "onStart" )
tween._onComplete = Runtime.verifyListener( transitionParams.onComplete, "onComplete" )
tween._onPause = Runtime.verifyListener( transitionParams.onPause, "onPause" )
tween._onResume = Runtime.verifyListener( transitionParams.onResume, "onResume" )
tween._onCancel = Runtime.verifyListener( transitionParams.onCancel, "onCancel" )
tween._onRepeat = Runtime.verifyListener( transitionParams.onRepeat, "onRepeat" )
if transitionParams.generatedBy and transitionParams.generatedBy == "composer" then
tween._generatedByComposer = true
end
local delay = transitionParams.delay
if type(delay) == "number" and delay ~= 0 then
tween._delay = delay
-- save off params: init after delay to minimize race conditions
local params = _copyTable( transitionParams )
tween._delayParams = params
else
-- no delay, so init immediately
lib._initTween( tween, transitionParams )
end
lib._addTween( tween )
end
return tween
end
-----------------------------------------------------------------------------------------
-- from( targetObject, transitionParams )
-- transitions an object from the specified transitionParams
-----------------------------------------------------------------------------------------
lib.from = function( targetObject, transitionParams )
if nil == targetObject then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a display object to a transition.from call." )
end
end
if nil == transitionParams then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a params table to a transition.from call." )
end
end
local newParams = {}
if targetObject and transitionParams then
local isDelta = transitionParams.delta
-- we copy the transition params from the target object and set them as final transition params
for k, v in pairs( transitionParams ) do
if targetObject[ k ] then
local startPos, endPos
if isDelta then
endPos = - v
startPos = v + targetObject[ k ]
else
endPos = targetObject[ k ]
startPos = v
end
newParams[ k ] = endPos
targetObject[ k ] = startPos
else
newParams[ k ] = v
end
end
end
-- create the transition and return the object
return lib.to( targetObject, newParams )
end
lib.loop = function( targetObject, transitionParams )
local tween = lib.to(targetObject, transitionParams)
tween._isLoop = true
return tween
end
-----------------------------------------------------------------------------------------
-- pause( whatToPause )
-- pauses the whatToPause transition object, sequence, tag or display object
-----------------------------------------------------------------------------------------
lib.pause = function( whatToPause, _pauseAll )
-- we use the targetType variable to establish how we iterate at the end of this method
local targetType = nil
local iterationTarget = nil
-- transition object or display object
if "table" == type( whatToPause ) then
-- if the ._instantiatedByTransLib field exists, then we have a transition object
if whatToPause._instantiatedByTransLib then
targetType = "transition"
whatToPause._paused = true
-- otherwise, we have a display object
else
targetType = "displayobject"
end
-- sequence name or tag
elseif "string" == type( whatToPause ) then
targetType = "tag"
-- pause all
elseif nil == whatToPause and not lib.ignoreEmptyReference or _pauseAll then
targetType = "all"
end
if targetType then
if "all" ~= targetType then iterationTarget = whatToPause end
local transitionTable = lib._transitionTable
-- if enterframe was run already, if we have any running tweens they won't be in the lib._transitionTable variable, but in lib._enterFrameTweens
if #transitionTable == 0 then
transitionTable = lib._enterFrameTweens
end
-- iterate the table
for i,tween in ipairs( transitionTable ) do
-- only cancel if the tween was not generated by the composer library
if targetType and not tween._generatedByComposer then
if targetType == "all" then
tween._paused = true
tween._resume = false
elseif targetType == "tag" then
if tween.tag == iterationTarget then
tween._paused = true
tween._resume = false
end
elseif targetType == "displayobject" then
if tween._target == iterationTarget then
tween._paused = true
tween._resume = false
end
elseif targetType == "transition" then
if tween == iterationTarget then
tween._paused = true
tween._resume = false
end
end
end
-- dispatch the onPause control event
local listener = tween._onPause
if listener and tween._paused and not tween._cancelled and not tween._pauseTriggered then
local target = tween._target
tween._pauseTriggered = true
tween._resumeTriggered = false
Runtime.callListener( listener, "onPause", target )
end
end
end
end
-----------------------------------------------------------------------------------------
-- resume( whatToResume )
-- resumes the whatToResume transition object, display object, sequence, tag or nil for all
-----------------------------------------------------------------------------------------
lib.resume = function( whatToResume, _resumeAll )
-- we use the targetType variable to establish how we iterate at the end of this method
local targetType = nil
local iterationTarget = nil
-- transition object or display object
if "table" == type( whatToResume ) then
-- if the ._instantiatedByTransLib field exists, then we have a transition object
if whatToResume._instantiatedByTransLib then
targetType = "transition"
whatToResume._resume = true
-- otherwise, we have a display object
else
targetType = "displayobject"
end
-- sequence name or tag
elseif "string" == type( whatToResume ) then
targetType = "tag"
-- pause all
elseif nil == whatToResume and not lib.ignoreEmptyReference or _resumeAll then
targetType = "all"
end
if targetType then
if "all" ~= targetType then iterationTarget = whatToResume end
-- iterate the table
local transitionTable = lib._transitionTable
-- if enterframe was run already, if we have any running tweens they won't be in the lib._transitionTable variable, but in lib._enterFrameTweens
if #transitionTable == 0 then
transitionTable = lib._enterFrameTweens
end
for i,tween in ipairs( transitionTable ) do
-- check for any resume lib variables
-- only resume if the tween was not generated by the composer library
if targetType and not tween._generatedByComposer then
if targetType == "all" then
tween._resume = true
tween._paused = false
elseif targetType == "tag" then
if tween.tag == iterationTarget then
tween._resume = true
tween._paused = false
end
elseif targetType == "displayobject" then
if tween._target == iterationTarget then
tween._resume = true
tween._paused = false
end
elseif targetType == "transition" then
if tween == whatToResume then
tween._resume = true
tween._paused = false
end
end
end
-- dispatch the onResume method on the object
local listener = tween._onResume
if listener and tween._resume and not tween._cancelled and not tween._resumeTriggered then
local target = tween._target
tween._pauseTriggered = false
tween._resumeTriggered = true
Runtime.callListener( listener, "onResume", target )
end
end
end
end
-----------------------------------------------------------------------------------------
-- cancel( transitionObject )
-- cancels the transitionObject transition
-----------------------------------------------------------------------------------------
lib.cancel = function( whatToCancel, _cancelAll )
-- if no transitions, then don't cancel anything
-- this is important because if one calls transition.cancel("tagname") followed by transition.to( obj, { tag = "tagname" } )
-- the newly created transition would be cancelled automatically.
local cancelType, cancelTarget
-- transition object or display object
if "table" == type( whatToCancel ) then
if whatToCancel._instantiatedByTransLib then
-- set the tween's _cancelled property
whatToCancel._cancelled = true
else
cancelType = "displayobject"
cancelTarget = whatToCancel
end
-- sequence name or tag
elseif "string" == type( whatToCancel ) then
cancelType = "tag"
cancelTarget = whatToCancel
-- cancel all
elseif nil == whatToCancel and not lib.ignoreEmptyReference or _cancelAll then
cancelType = "all"
cancelTarget = nil
end
local transitionTable = lib._transitionTable
-- if enterframe was run already, if we have any running tweens they won't be in the lib._transitionTable variable, but in lib._enterFrameTweens
if #transitionTable == 0 then
transitionTable = lib._enterFrameTweens
end
for i,tween in ipairs( transitionTable ) do
-- check for any cancel lib variables
-- only cancel if the tween was not generated by the composer library
if cancelType and not tween._generatedByComposer then
if cancelType == "all" then
tween._cancelled = true
elseif cancelType == "tag" then
if tween.tag == cancelTarget then
tween._cancelled = true
end
elseif cancelType == "displayobject" then
if tween._target == cancelTarget then
tween._cancelled = true
end
end
end
-- dispatch the onCancel control event
local listener = tween._onCancel
if listener and tween._cancelled and not tween._cancelTriggered then
local target = tween._target
tween._cancelTriggered = true
Runtime.callListener( listener, "onCancel", target )
end
end
end
lib.pauseAll = function()
lib.pause( nil, true )
end
lib.resumeAll = function()
lib.resume( nil, true )
end
lib.cancelAll = function()
lib.cancel( nil, true )
end
-- function lib:enterFrame(), and then Runtime:addEventListener( "enterFrame", lib )
-----------------------------------------------------------------------------------------
-- enterFrame( event )
-- the frame listener for the transitions
-----------------------------------------------------------------------------------------
function lib:enterFrame ( event )
-- create a local copy of the transition table, to avoid a race condition
local currentActiveTweens = lib._transitionTable
lib._enterFrameTweens = lib._transitionTable
lib._transitionTable = {}
-- get the current event time
local currentTime = event.time
-- create a local completed transitions table which we will empty at the end of the function's execution
local completedTransitions = {}
-- iterate the transition table
for i,tween in ipairs( currentActiveTweens ) do
-- if the transition object is paused
if tween._paused then
-- handle tweens marked as paused
-- only set the pausedTime if that did not happen already
if nil == tween._lastPausedTime then
-- set the pausedTime to the current time
tween._lastPausedTime = system.getTimer()
end
end
if tween._resume then
if not tween._transitionHasCompleted and tween._lastPausedTime then
-- we calculate the time interval the transition was paused for
local transitionPausedInterval = system.getTimer() - tween._lastPausedTime
-- we adjust the transition object's begin transition variable with the calculated time interval
tween._timeStart = tween._timeStart + transitionPausedInterval
-- nil out the lastPausedTime variable of the transition object
tween._lastPausedTime = nil
tween._paused = nil
tween._resume = nil
end
end
if tween._cancelled then
table.insert( completedTransitions, i )
end
if not tween._paused and not tween._cancelled then
local delay = tween._delay
if delay and ( currentTime >= (tween._timeStart + delay) ) then
tween._delay = nil
tween._timeStart = currentTime
delay = nil
end
if not delay then
local params = tween._delayParams
if params then
lib._initTween( tween, params )
tween._delayParams = nil
end
local target = tween._target
local keysFinish = tween._keysFinish
local t = currentTime - tween._timeStart
if t < 0 then t = 0 end
local tMax = tween._duration
if t < tMax then
if tween._isLoop then
t = t*2
if tMax < t then
t = tMax*2 - t
end
end
for k,v in pairs( tween._keysStart ) do
target[k] = tween._transition( t, tMax, v, keysFinish[k] - v )
end
else
-- the easing function easing.continuousLoop with infinite iterations cannot set the object keys to the finish values.
-- also, the last iteration of a transition with easing.continousLoop has to return the object to the start properties,
-- not to the end ones.
if tween._transition == easing.continuousLoop or tween._isLoop then
if tween.iterations == 1 then
for k, v in pairs( tween._keysStart ) do
target[k] = v
end
end
else
for k,v in pairs( keysFinish ) do
target[k] = v
end
end
if tween.iterations == 1 then
-- the transition has completed
-- onComplete listener
local listener = tween._onComplete
if listener then
Runtime.callListener( listener, "onComplete", target )
end
-- queue up complete tween for removal
table.insert( completedTransitions, i )
else
-- we have more iterations
tween._transitionHasCompleted = false
if tween.iterations > 0 then
tween.iterations = tween.iterations-1
end
tween._timeStart = tween._timeStart + tMax
-- onRepeat listener
local listener = tween._onRepeat
if listener then
Runtime.callListener( listener, "onRepeat", target )
end
end
end
end
end
end
-- Remove the transitions that are done
for i=#completedTransitions,1,-1 do
table.remove(currentActiveTweens, completedTransitions[i])
end
-- Swap out tmp tween table and restore lib._transitionTable.
-- Append any new tweens in the tmp table back into lib._transitionTable
local tmpTweens = lib._transitionTable
if #tmpTweens > 0 then
for _,tween in ipairs( tmpTweens ) do
table.insert( currentActiveTweens, tween )
end
end
lib._transitionTable = currentActiveTweens
-- also restore the _enterFrameTweens variable here
lib._enterFrameTweens = currentActiveTweens
-- TODO: Should also unregister when there are only paused transitions
if #currentActiveTweens == 0 then
Runtime:removeEventListener( "enterFrame", lib )
lib._hasEventListener = false
end
end
-----------------------------------------------------------------------------------------
-- createSequence( targetObject, sequenceData )
-- create a transition sequence
-- targetObject is the display object to create the sequence on
-- sequenceData is a table, containing name - the sequence name, and transitions - the list of transitions
-- for a sequence transition, you pass in an extra parameter, mode = withPrevious or afterPrevious, which defines
-- the execution order of the transitions.
-----------------------------------------------------------------------------------------
lib.newSequence = function( targetObject, params )
if targetObject == nil then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a target object to a transition.createSequence call." )
end
end
if params == nil then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a params table to a transition.createSequence call." )
end
end
if params.name == nil then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a name in the params table to a transition.createSequence call." )
end
end
if params.transitions == nil then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a table of transitions in the params table to a transition.createSequence call." )
end
end
if targetObject and params and params.transitions and params.name then
-- create a sequence with the name params.name
lib._sequenceTable[params.name] = {}
-- assign the transitions to it
lib._sequenceTable[params.name].transitions = params.transitions
-- assign the target object to it
lib._sequenceTable[params.name].object = targetObject
-- localize it
local currentSequence = lib._sequenceTable[params.name]
-- create a temp table for the delays
local tranDelays = {}
for i = 1, #currentSequence.transitions do
local delayValue = 0
if currentSequence.transitions[ i ].delay then
delayValue = delayValue + currentSequence.transitions[ i ].delay
end
-- if we are at least at the second transition in the table
if i > 1 then
for j = i - 1, 1, -1 do
local addedDelay = 0
local prevDelay = 0
if currentSequence.transitions[ j ].delay then
addedDelay = currentSequence.transitions[ j ].delay
end
if currentSequence.transitions[ j ].mode ~= "withPrevious" then
prevDelay = prevDelay + currentSequence.transitions[ j ].time
end
prevDelay = prevDelay + addedDelay
delayValue = delayValue + prevDelay
end
if currentSequence.transitions[ i ].mode == "withPrevious" then
delayValue = delayValue - currentSequence.transitions[ i - 1 ].time
if currentSequence.transitions[ i - 1 ].delay then
delayValue = delayValue - currentSequence.transitions[ i - 1 ].delay
end
end
--currentSequence.transitions[ i ].delay = delayValue
tranDelays[ i ] = delayValue
end
end
end
-- assign the values from the temp table
for i = 1, #tranDelays do
currentSequence.transitions[ i ].delay = tranDelays[ i ]
end
end
-----------------------------------------------------------------------------------------
-- runSequence( sequenceName )
-- runs the sequence sequenceName
-----------------------------------------------------------------------------------------
lib.runSequence = function( sequenceName )
if sequenceName == nil then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a sequence name to a transition.runSequence call." )
end
end
if nil == lib._sequenceTable[ sequenceName ] then
if lib.debugEnabled then
error( DEBUG_STRING .. " the sequence name passed to the transition.runSequence call does not exist." )
end
end
local currentSequence = lib._sequenceTable[ sequenceName ]
for i, v in ipairs ( lib._sequenceTable[ sequenceName ].transitions ) do
v.mode = nil
lib.to( lib._sequenceTable[ sequenceName ].object, v)
end
end
-----------------------------------------------------------------------------------------
-- convenience methods
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
-- blink( targetObject, actionDuration )
-- blinks the targetObject with the transition duration actionDuration
-----------------------------------------------------------------------------------------
lib.blink = function( targetObject, params )
if targetObject == nil then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a target object to a transition.blink call." )
end
end
local paramsTable = params or {}
local actionTime = paramsTable.time or 500
local actionDelay = paramsTable.delay or 0
local actionEasing = paramsTable.transition or easing.linear
local actionOnComplete = paramsTable.onComplete or nil
local actionOnPause = paramsTable.onPause or nil
local actionOnResume = paramsTable.onResume or nil
local actionOnCancel = paramsTable.onCancel or nil
local actionOnStart = paramsTable.onStart or nil
local actionOnRepeat = paramsTable.onRepeat or nil
local actionTag = paramsTable.tag or nil
local actionTime = actionTime or 500
local addedTransition = lib.to( targetObject,
{
delay = actionDelay,
time = actionTime * 0.5,
transition = easing.continousLoop,
iterations = -1,
onComplete = actionOnComplete,
onPause = actionOnPause,
onResume = actionOnResume,
onCancel = actionOnCancel,
onStart = actionOnStart,
onRepeat = actionOnRepeat,
alpha = 0,
tag = actionTag
} )
--local addedTransition = lib.to( targetObject, { time = actionTime * 0.5, alpha = 0, transition="continuousLoop", iterations = -1 } )
return addedTransition
end
-----------------------------------------------------------------------------------------
-- moveTo( targetObject, xCoord, yCoord, actionTime, actionDelay )
-- moves the targetObject to the xCoord, yCoord coordinates with the transition duration actionDuration and delay actionDelay
-----------------------------------------------------------------------------------------
lib.moveTo = function( targetObject, params )
if targetObject == nil then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a target object to a transition.moveTo call." )
end
end
local paramsTable = params or {}
local addedTransition = nil
if targetObject then
local actionTime = paramsTable.time or 500
local actionDelay = paramsTable.delay or 0
local actionEasing = paramsTable.transition or easing.linear
local actionOnComplete = paramsTable.onComplete or nil
local actionOnPause = paramsTable.onPause or nil
local actionOnResume = paramsTable.onResume or nil
local actionOnCancel = paramsTable.onCancel or nil
local actionOnStart = paramsTable.onStart or nil
local actionOnRepeat = paramsTable.onRepeat or nil
local actionXScale = paramsTable.xScale or nil
local actionYScale = paramsTable.yScale or nil
local actionAlpha = paramsTable.alpha or nil
local actionTag = paramsTable.tag or nil
local actionX = paramsTable.x or nil
local actionY = paramsTable.y or nil
addedTransition = lib.to( targetObject,
{
delay = actionDelay,
time = actionTime,
transition = actionEasing,
onComplete = actionOnComplete,
onPause = actionOnPause,
onResume = actionOnResume,
onCancel = actionOnCancel,
onStart = actionOnStart,
onRepeat = actionOnRepeat,
alpha = actionAlpha,
xScale = actionXScale,
yScale = actionYScale,
x = actionX,
y = actionY,
tag = actionTag
} )
end
return addedTransition
end
-----------------------------------------------------------------------------------------
-- moveBy( targetObject, xCoord, yCoord, actionTime, actionDelay )
-- moves the targetObject by xCoord, yCoord from the actual position with the transition duration actionDuration and delay actionDelay
-----------------------------------------------------------------------------------------
lib.moveBy = function( targetObject, params )
if targetObject == nil then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a target object to a transition.moveBy call." )
end
end
local paramsTable = params or {}
local addedTransition = nil
if targetObject then
local actionTime = paramsTable.time or 500
local actionDelay = paramsTable.delay or 0
local actionEasing = paramsTable.transition or easing.linear
local actionOnComplete = paramsTable.onComplete or nil
local actionOnPause = paramsTable.onPause or nil
local actionOnResume = paramsTable.onResume or nil
local actionOnCancel = paramsTable.onCancel or nil
local actionOnStart = paramsTable.onStart or nil
local actionOnRepeat = paramsTable.onRepeat or nil
local actionXScale = paramsTable.xScale or nil
local actionYScale = paramsTable.yScale or nil
local actionAlpha = paramsTable.alpha or nil
local actionTag = paramsTable.tag or nil
local actionX = paramsTable.x or 0
local actionY = paramsTable.y or 0
addedTransition = lib.to( targetObject,
{
delay = actionDelay,
time = actionTime,
transition = actionEasing,
onComplete = actionOnComplete,
onPause = actionOnPause,
onResume = actionOnResume,
onCancel = actionOnCancel,
onStart = actionOnStart,
onRepeat = actionOnRepeat,
alpha = actionAlpha,
xScale = actionXScale,
yScale = actionYScale,
x = targetObject.x + actionX,
y = targetObject.y + actionY,
tag = actionTag
} )
end
return addedTransition
end
-----------------------------------------------------------------------------------------
-- scaleTo( targetObject, xScale, yScale, actionTime, actionDelay )
-- scales the targetObject to the xScale, yScale scale values with the transition duration actionDuration and delay actionDelay
-----------------------------------------------------------------------------------------
lib.scaleTo = function( targetObject, params )
if targetObject == nil then
if lib.debugEnabled then
error( DEBUG_STRING .. " you have to pass a target object to a transition.scaleTo call." )
end
end
local paramsTable = params or {}
local addedTransition = nil
if targetObject then
local actionTime = paramsTable.time or 500
local actionDelay = paramsTable.delay or 0
local actionEasing = paramsTable.transition or easing.linear
local actionOnComplete = paramsTable.onComplete or nil
local actionOnPause = paramsTable.onPause or nil
local actionOnResume = paramsTable.onResume or nil
local actionOnCancel = paramsTable.onCancel or nil
local actionOnStart = paramsTable.onStart or nil
local actionOnRepeat = paramsTable.onRepeat or nil