transition.lua
7.34 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
--[[
Copyright (c) 2011-2014 chukong-inc.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]
local transition = {}
local ACTION_EASING = {}
ACTION_EASING["BACKIN"] = {cc.EaseBackIn, 1}
ACTION_EASING["BACKINOUT"] = {cc.EaseBackInOut, 1}
ACTION_EASING["BACKOUT"] = {cc.EaseBackOut, 1}
ACTION_EASING["BOUNCE"] = {cc.EaseBounce, 1}
ACTION_EASING["BOUNCEIN"] = {cc.EaseBounceIn, 1}
ACTION_EASING["BOUNCEINOUT"] = {cc.EaseBounceInOut, 1}
ACTION_EASING["BOUNCEOUT"] = {cc.EaseBounceOut, 1}
ACTION_EASING["ELASTIC"] = {cc.EaseElastic, 2, 0.3}
ACTION_EASING["ELASTICIN"] = {cc.EaseElasticIn, 2, 0.3}
ACTION_EASING["ELASTICINOUT"] = {cc.EaseElasticInOut, 2, 0.3}
ACTION_EASING["ELASTICOUT"] = {cc.EaseElasticOut, 2, 0.3}
ACTION_EASING["EXPONENTIALIN"] = {cc.EaseExponentialIn, 1}
ACTION_EASING["EXPONENTIALINOUT"] = {cc.EaseExponentialInOut, 1}
ACTION_EASING["EXPONENTIALOUT"] = {cc.EaseExponentialOut, 1}
ACTION_EASING["IN"] = {cc.EaseIn, 2, 1}
ACTION_EASING["INOUT"] = {cc.EaseInOut, 2, 1}
ACTION_EASING["OUT"] = {cc.EaseOut, 2, 1}
ACTION_EASING["RATEACTION"] = {cc.EaseRateAction, 2, 1}
ACTION_EASING["SINEIN"] = {cc.EaseSineIn, 1}
ACTION_EASING["SINEINOUT"] = {cc.EaseSineInOut, 1}
ACTION_EASING["SINEOUT"] = {cc.EaseSineOut, 1}
local actionManager = cc.Director:getInstance():getActionManager()
function transition.newEasing(action, easingName, more)
local key = string.upper(tostring(easingName))
local easing
if ACTION_EASING[key] then
local cls, count, default = unpack(ACTION_EASING[key])
if count == 2 then
easing = cls:create(action, more or default)
else
easing = cls:create(action)
end
end
return easing or action
end
function transition.create(action, args)
args = checktable(args)
if args.easing then
if type(args.easing) == "table" then
action = transition.newEasing(action, unpack(args.easing))
else
action = transition.newEasing(action, args.easing)
end
end
local actions = {}
local delay = checknumber(args.delay)
if delay > 0 then
actions[#actions + 1] = cc.DelayTime:create(delay)
end
actions[#actions + 1] = action
local onComplete = args.onComplete
if type(onComplete) ~= "function" then onComplete = nil end
if onComplete then
actions[#actions + 1] = cc.CallFunc:create(onComplete)
end
if args.removeSelf then
actions[#actions + 1] = cc.RemoveSelf:create()
end
if #actions > 1 then
return transition.sequence(actions)
else
return actions[1]
end
end
function transition.execute(target, action, args)
assert(not tolua.isnull(target), "transition.execute() - target is not cc.Node")
local action = transition.create(action, args)
target:runAction(action)
return action
end
function transition.moveTo(target, args)
assert(not tolua.isnull(target), "transition.moveTo() - target is not cc.Node")
local x = args.x or target:getPositionX()
local y = args.y or target:getPositionY()
local action = cc.MoveTo:create(args.time, cc.p(x, y))
return transition.execute(target, action, args)
end
function transition.moveBy(target, args)
assert(not tolua.isnull(target), "transition.moveBy() - target is not cc.Node")
local x = args.x or 0
local y = args.y or 0
local action = cc.MoveBy:create(args.time, cc.p(x, y))
return transition.execute(target, action, args)
end
function transition.fadeIn(target, args)
assert(not tolua.isnull(target), "transition.fadeIn() - target is not cc.Node")
local action = cc.FadeIn:create(args.time)
return transition.execute(target, action, args)
end
function transition.fadeOut(target, args)
assert(not tolua.isnull(target), "transition.fadeOut() - target is not cc.Node")
local action = cc.FadeOut:create(args.time)
return transition.execute(target, action, args)
end
function transition.fadeTo(target, args)
assert(not tolua.isnull(target), "transition.fadeTo() - target is not cc.Node")
local opacity = checkint(args.opacity)
if opacity < 0 then
opacity = 0
elseif opacity > 255 then
opacity = 255
end
local action = cc.FadeTo:create(args.time, opacity)
return transition.execute(target, action, args)
end
function transition.scaleTo(target, args)
assert(not tolua.isnull(target), "transition.scaleTo() - target is not cc.Node")
local action
if args.scale then
action = cc.ScaleTo:create(checknumber(args.time), checknumber(args.scale))
elseif args.scaleX or args.scaleY then
local scaleX, scaleY
if args.scaleX then
scaleX = checknumber(args.scaleX)
else
scaleX = target:getScaleX()
end
if args.scaleY then
scaleY = checknumber(args.scaleY)
else
scaleY = target:getScaleY()
end
action = cc.ScaleTo:create(checknumber(args.time), scaleX, scaleY)
end
return transition.execute(target, action, args)
end
function transition.rotateTo(target, args)
assert(not tolua.isnull(target), "transition.rotateTo() - target is not cc.Node")
local rotation = args.rotation or target:getRotation()
local action = cc.RotateTo:create(args.time, rotation)
return transition.execute(target, action, args)
end
function transition.rotateBy(target, args)
assert(not tolua.isnull(target), "transition.rotateTo() - target is not cc.Node")
local rotation = args.rotation or 0
local action = cc.RotateBy:create(args.time, rotation)
return transition.execute(target, action, args)
end
function transition.sequence(actions)
if #actions < 1 then return end
if #actions < 2 then return actions[1] end
return cc.Sequence:create(actions)
end
function transition.removeAction(action)
if not tolua.isnull(action) then
actionManager:removeAction(action)
end
end
function transition.stopTarget(target)
if not tolua.isnull(target) then
actionManager:removeAllActionsFromTarget(target)
end
end
function transition.pauseTarget(target)
if not tolua.isnull(target) then
actionManager:pauseTarget(target)
end
end
function transition.resumeTarget(target)
if not tolua.isnull(target) then
actionManager:resumeTarget(target)
end
end
return transition