-
Notifications
You must be signed in to change notification settings - Fork 1
/
animationchain.lua
86 lines (73 loc) · 2.36 KB
/
animationchain.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
local M={}
animationchain=M
local transition=transition
local setmetatable=setmetatable
local type=type
local error=error
setfenv(1,M)
local function start(anim,options,onStart,onComplete)
return function()
if options.delete then
options.onComplete=function(obj)
obj:removeSelf()
if onComplete then
onComplete()
end
end
else
options.onComplete=onComplete
end
anim()
if onStart then
onStart()
end
end
end
-- args
-- exec - is the current function to execute. It is assumed to be a function that runs a Corona SDK transition
-- options - options for the animation (see Corona SDK). These are assumed to be the same closed over by the exec call - chainFunctions relies on being able to manipulate them for "onComplete" and "whenDone" to work
-- # Additional feature: if options.delete==true the subject of the animation will be deleted (obj:removeSelf() called) when the animation completes
-- runParent (optional) - run the previous function in the chain. It takes a single function as an argument. chainFunction creates this function itself
-- noanim (optional) - true if exec does not represent a function executing a Corona SDK transition. If true, the only valid call to the chain is "start"
function chainFunctions(exec,options,runParent,noanim)
local t={}
local mt={
__index=function(t,k)
local run=function(execChildAnim)
local doIt
-- wrap child animation in relation to previous call
local onStart,onComplete
if k=="onStart" or k=="whenStart" then
onStart=execChildAnim
elseif k=="onComplete" or k=="whenDone" then
onComplete=execChildAnim
end
doIt=start(exec,options,onStart,onComplete)
if runParent then
runParent(doIt)
else
doIt()
end
end
return function(...)
if k=="start" then
return run()
end
if noanim then
error("animationchain: passing in pure functions must terminate the chain. Only call start after passing a single function into the chain")
end
if #arg==1 and type(arg[1])=="function" then
-- just a function has been passed in.
return chainFunctions(arg[1],{},run,true)
end
return anim(arg[1],arg[2],run)
end
end
}
setmetatable(t,mt)
return t
end
function anim(obj,options,runParent)
return chainFunctions(function () return transition.to(obj,options) end, options,runParent)
end
return M