-
Notifications
You must be signed in to change notification settings - Fork 10
/
scenemanager.lua
359 lines (279 loc) · 8.68 KB
/
scenemanager.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
--[[
SceneManager v1.0.4
changelog:
----------
v1.0.4 - 08.04.2012
Added option to filter a list of events during transitions
Moved increment of time to end of onEnterFrame so that time goes from 0 to 1
Added additional "real time" argument to dispatched transitions
Added option to pass user data to a scene when it gets created
v1.0.3 - 19.11.2011
Fixed incorrect calculation of width/height in landscape modes
v1.0.2 - 17.11.2011
Change event names
v1.0.1 - 06.11.2011
Add collectgarbage() to the end of transition
v1.0 - 06.11.2011
Initial release
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
(C) 2010 - 2011 Gideros Mobile
]]
SceneManager = Core.class(Sprite)
--[[
The following two deltas will correct the screen offsets for different screen sizes and
resolutions depending on a projects scale mode setting. Without them transitions between
scenes would take the wrong dimensions and end with ugly flickering or sudden disappearance
of objects.
]]
local dx = 2 * math.ceil(application:getLogicalTranslateX() / application:getLogicalScaleX())
local dy = 2 * math.ceil(application:getLogicalTranslateY() / application:getLogicalScaleY())
function SceneManager.moveFromRight(scene1, scene2, t)
local width = application:getContentWidth() + dx
scene1:setX(-t * width)
scene2:setX((1 - t) * width)
end
function SceneManager.moveFromLeft(scene1, scene2, t)
local width = application:getContentWidth() + dx
scene1:setX(t * width)
scene2:setX((t - 1) * width)
end
function SceneManager.overFromRight(scene1, scene2, t)
local width = application:getContentWidth() + dx
scene2:setX((1 - t) * width)
end
function SceneManager.overFromLeft(scene1, scene2, t)
local width = application:getContentWidth() + dx
scene2:setX((t - 1) * width)
end
function SceneManager.moveFromRightWithFade(scene1, scene2, t)
local width = application:getContentWidth() + dx
scene1:setAlpha(1 - t)
scene1:setX(-t * width)
scene2:setX((1 - t) * width)
end
function SceneManager.moveFromLeftWithFade(scene1, scene2, t)
local width = application:getContentWidth() + dx
scene1:setAlpha(1 - t)
scene1:setX(t * width)
scene2:setX((t - 1) * width)
end
function SceneManager.overFromRightWithFade(scene1, scene2, t)
local width = application:getContentWidth() + dx
scene1:setAlpha(1 - t)
scene2:setX((1 - t) * width)
end
function SceneManager.overFromLeftWithFade(scene1, scene2, t)
local width = application:getContentWidth() + dx
scene1:setAlpha(1 - t)
scene2:setX((t - 1) * width)
end
function SceneManager.moveFromBottom(scene1, scene2, t)
local height = application:getContentHeight() + dy
scene1:setY(-t * height)
scene2:setY((1 - t) * height)
end
function SceneManager.moveFromTop(scene1, scene2, t)
local height = application:getContentHeight() + dy
scene1:setY(t * height)
scene2:setY((t - 1) * height)
end
function SceneManager.overFromBottom(scene1, scene2, t)
local height = application:getContentHeight() + dy
scene2:setY((1 - t) * height)
end
function SceneManager.overFromTop(scene1, scene2, t)
local height = application:getContentHeight() + dy
scene2:setY((t - 1) * height)
end
function SceneManager.moveFromBottomWithFade(scene1, scene2, t)
local height = application:getContentHeight() + dy
scene1:setAlpha(1 - t)
scene1:setY(-t * height)
scene2:setY((1 - t) * height)
end
function SceneManager.moveFromTopWithFade(scene1, scene2, t)
local height = application:getContentHeight() + dy
scene1:setAlpha(1 - t)
scene1:setY(t * height)
scene2:setY((t - 1) * height)
end
function SceneManager.overFromBottomWithFade(scene1, scene2, t)
local height = application:getContentHeight() + dy
scene1:setAlpha(1 - t)
scene2:setY((1 - t) * height)
end
function SceneManager.overFromTopWithFade(scene1, scene2, t)
local height = application:getContentHeight() + dy
scene1:setAlpha(1 - t)
scene2:setY((t - 1) * height)
end
function SceneManager.fade(scene1, scene2, t)
if t < 0.5 then
scene1:setAlpha((0.5 - t) * 2)
else
scene1:setAlpha(0)
end
if t < 0.5 then
scene2:setAlpha(0)
else
scene2:setAlpha((t - 0.5) * 2)
end
end
function SceneManager.crossfade(scene1, scene2, t)
scene1:setAlpha(1 - t)
scene2:setAlpha(t)
end
function SceneManager.flip(scene1, scene2, t)
local width = application:getContentWidth() + dx
if t < 0.5 then
local s = (0.5 - t) * 2
scene1:setScaleX(s)
scene1:setX((1 - s) * width * 0.5)
else
scene1:setScaleX(0)
scene1:setX(width * 0.5)
end
if t < 0.5 then
scene2:setScaleX(0)
scene2:setX(width * 0.5)
else
local s = (t - 0.5) * 2
scene2:setScaleX(s)
scene2:setX((1 - s) * width * 0.5)
end
end
function SceneManager.flipWithFade(scene1, scene2, t)
local width = application:getContentWidth() + dx
if t < 0.5 then
local s = (0.5 - t) * 2
scene1:setScaleX(s)
scene1:setX((1 - s) * width * 0.5)
scene1:setAlpha(s)
else
scene1:setScaleX(0)
scene1:setX(width * 0.5)
scene1:setAlpha(0)
end
if t < 0.5 then
scene2:setScaleX(0)
scene2:setX(width * 0.5)
scene2:setAlpha(0)
else
local s = (t - 0.5) * 2
scene2:setScaleX(s)
scene2:setX((1 - s) * width * 0.5)
scene2:setAlpha(s)
end
end
function SceneManager.flipWithShade(scene1, scene2, t)
local width = application:getContentWidth() + dx
if t < 0.5 then
local s = (0.5 - t) * 2
scene1:setScaleX(s)
scene1:setX((1 - s) * width * 0.5)
scene1:setColorTransform(1 - t, 1 - t, 1 - t, 1)
else
scene1:setScaleX(0)
scene1:setX(width * 0.5)
scene1:setColorTransform(0.5, 0.5, 0.5, 1)
end
if t < 0.5 then
scene2:setScaleX(0)
scene2:setX(width * 0.5)
scene2:setColorTransform(0.5, 0.5, 0.5, 1)
else
local s = (t - 0.5) * 2
scene2:setScaleX(s)
scene2:setX((1 - s) * width * 0.5)
scene2:setColorTransform(t, t, t, 1)
end
end
local function dispatchEvent(dispatcher, name)
if dispatcher:hasEventListener(name) then
dispatcher:dispatchEvent(Event.new(name))
end
end
local function defaultEase(ratio)
return ratio
end
function SceneManager:init(scenes)
self.scenes = scenes
self.tweening = false
self.transitionEventCatcher = Sprite.new()
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
function SceneManager:changeScene(scene, duration, transition, ease, options)
self.eventFilter = options and options.eventFilter
if self.tweening then
return
end
if self.scene1 == nil then
self.scene1 = self.scenes[scene].new(options and options.userData)
self:addChild(self.scene1)
dispatchEvent(self, "transitionBegin")
dispatchEvent(self.scene1, "enterBegin")
dispatchEvent(self, "transitionEnd")
dispatchEvent(self.scene1, "enterEnd")
return
end
self.duration = duration
self.transition = transition
self.ease = ease or defaultEase
self.scene2 = self.scenes[scene].new(options and options.userData)
self.scene2:setVisible(false)
self:addChild(self.scene2)
self.time = 0
self.currentTimer = os.timer()
self.tweening = true
end
function SceneManager:filterTransitionEvents(event)
event:stopPropagation()
end
function SceneManager:onTransitionBegin()
if self.eventFilter then
stage:addChild(self.transitionEventCatcher)
for i,event in ipairs(self.eventFilter) do
self.transitionEventCatcher:addEventListener(event, self.filterTransitionEvents, self)
end
end
end
function SceneManager:onTransitionEnd()
if self.eventFilter then
for i,event in ipairs(self.eventFilter) do
self.transitionEventCatcher:removeEventListener(event, self.filterTransitionEvents, self)
end
self.transitionEventCatcher:removeFromParent()
end
end
function SceneManager:onEnterFrame(event)
if not self.tweening then
return
end
if self.time == 0 then
self:onTransitionBegin()
self.scene2:setVisible(true)
dispatchEvent(self, "transitionBegin")
dispatchEvent(self.scene1, "exitBegin")
dispatchEvent(self.scene2, "enterBegin")
end
local timer = os.timer()
local deltaTime = timer - self.currentTimer
self.currentTimer = timer
local t = (self.duration == 0) and 1 or (self.time / self.duration)
self.transition(self.scene1, self.scene2, self.ease(t), t)
if self.time == self.duration then
dispatchEvent(self, "transitionEnd")
dispatchEvent(self.scene1, "exitEnd")
dispatchEvent(self.scene2, "enterEnd")
self:onTransitionEnd()
self:removeChild(self.scene1)
self.scene1 = self.scene2
self.scene2 = nil
self.tweening = false
collectgarbage()
end
self.time = self.time + deltaTime
if self.time > self.duration then
self.time = self.duration
end
end