-
Notifications
You must be signed in to change notification settings - Fork 0
/
director.lua
408 lines (356 loc) · 11 KB
/
director.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
module(..., package.seeall)
--====================================================================--
-- DIRECTOR CLASS
--====================================================================--
--
-- Version: 1.1
-- Made by Ricardo Rauber Pereira @ 2010
-- Blog: http://rauberlabs.blogspot.com/
-- Mail: [email protected]
--
-- This class is free to use, feel free to change but please send new versions
-- or new features like new effects to me and help us to make it better!
--
--====================================================================--
-- CHANGES
--====================================================================--
--
-- 06-OCT-2010 - Ricardo Rauber - Created
-- 07-OCT-2010 - Ricardo Rauber - Functions loadScene and fxEnded were
-- taken off from the changeScene function;
-- Added function cleanGroups for best
-- memory clean up;
-- Added directorView and effectView groups
-- for better and easier control;
-- Please see INFORMATION to know how to use it
--
--====================================================================--
-- VARIABLES
--====================================================================--
--
-- currentView: Main display group
-- nextView: Display group for transitions
-- currentScreen: Active module
-- nextScreen: New module
-- lastScene: Active module in string for control
-- nextScene: New module in string for control
-- effect: Transition type
-- arg[N]: Arguments for each transition
-- fxTime: Time for transition.to
--
--====================================================================--
-- INFORMATION
--====================================================================--
--
-- * For best practices, use fps=60, scale = "zoomStretch" on config.lua
--
-- * In main.lua file, you have to import the class like this:
--
-- director = require("director")
-- local g = display.newGroup()
-- g:insert(director.directorView)
--
-- * To change scenes, use this command [use the effect of your choice]
--
-- director:changeScene("settings","moveFromLeft")
--
-- * Every scene is a lua module file and must have a new() function that
-- must return a local display group, like this: [see template.lua]
--
-- module(..., package.seeall)
-- function new()
-- local lg = display.newGroup()
-- ------ Your code here ------
-- return lg
-- end
--
-- * Every display object must be inserted on the local display group
--
-- local background = display.newImage("background.png")
-- lg:insert(background)
--
-- * This class doesn't clean timers! If you want to stop timers when
-- change scenes, you'll have to do it manually.
--
--====================================================================--
directorView = display.newGroup()
currentView = display.newGroup()
nextView = display.newGroup()
effectView = display.newGroup()
--
local currentScreen, nextScreen
local lastScene = "main"
local fxTime = 200
--
directorView:insert(currentView)
directorView:insert(nextView)
directorView:insert(effectView)
------------------------------------------------------------------------
-- CLEAN GROUP
------------------------------------------------------------------------
local function cleanGroups ( curGroup, level )
if curGroup.numChildren then
while curGroup.numChildren > 0 do
cleanGroups ( curGroup[curGroup.numChildren], level+1 )
end
if level > 0 then
curGroup:removeSelf()
end
else
curGroup:removeSelf()
curGroup = nil
return
end
end
------------------------------------------------------------------------
-- LOAD SCENE
------------------------------------------------------------------------
local function loadScene ( nextScene )
nextScreen = require(nextScene).new()
nextView:insert(nextScreen)
end
------------------------------------------------------------------------
-- EFFECT ENDED
------------------------------------------------------------------------
local function fxEnded ( event )
currentView.x = 0
currentView.y = 0
currentView.xScale = 1
currentView.yScale = 1
--
cleanGroups(currentView,0)
--
currentScreen = nextScreen
currentView:insert(currentScreen)
nextView.x = display.contentWidth
nextView.y = 0
nextView.xScale = 1
nextView.yScale = 1
end
------------------------------------------------------------------------
-- CHANGE SCENE
------------------------------------------------------------------------
function director:changeScene(nextScene,
effect,
arg1,
arg2,
arg3)
-----------------------------------
-- If is the same, don't change
-----------------------------------
if lastScene then
if string.lower(lastScene) == string.lower(nextScene) then
return true
end
end
local showFx
-----------------------------------
-- EFFECT: Move From Right
-----------------------------------
if effect == "moveFromRight" then
nextView.x = display.contentWidth
nextView.y = 0
--
loadScene (nextScene)
--
showFx = transition.to ( nextView, { x=0, time=fxTime } )
showFx = transition.to ( currentView, { x=display.contentWidth*-1, time=fxTime } )
--
timer.performWithDelay( fxTime, fxEnded )
-----------------------------------
-- EFFECT: Over From Right
-----------------------------------
elseif effect == "overFromRight" then
nextView.x = display.contentWidth
nextView.y = 0
--
loadScene (nextScene)
--
showFx = transition.to ( nextView, { x=0, time=fxTime } )
--
timer.performWithDelay( fxTime, fxEnded )
-----------------------------------
-- EFFECT: Move From Left
-----------------------------------
elseif effect == "moveFromLeft" then
nextView.x = display.contentWidth*-1
nextView.y = 0
--
loadScene (nextScene)
--
showFx = transition.to ( nextView, { x=0, time=fxTime } )
showFx = transition.to ( currentView, { x=display.contentWidth, time=fxTime } )
--
timer.performWithDelay( fxTime, fxEnded )
-----------------------------------
-- EFFECT: Over From Left
-----------------------------------
elseif effect == "overFromLeft" then
nextView.x = display.contentWidth*-1
nextView.y = 0
--
loadScene (nextScene)
--
showFx = transition.to ( nextView, { x=0, time=fxTime } )
--
timer.performWithDelay( fxTime, fxEnded )
-----------------------------------
-- EFFECT: Over From Top
-----------------------------------
elseif effect == "overFromTop" then
nextView.x = 0
nextView.y = display.contentHeight*-1
--
loadScene (nextScene)
--
showFx = transition.to ( nextView, { y=0, time=fxTime } )
--
timer.performWithDelay( fxTime, fxEnded )
-----------------------------------
-- EFFECT: Over From Bottom
-----------------------------------
elseif effect == "overFromBottom" then
nextView.x = 0
nextView.y = display.contentHeight
--
loadScene (nextScene)
--
showFx = transition.to ( nextView, { y=0, time=fxTime } )
--
timer.performWithDelay( fxTime, fxEnded )
-----------------------------------
-- EFFECT: Fade
-----------------------------------
-- ARG1 = color [string]
-----------------------------------
-- ARG1 = red [number]
-- ARG2 = green [number]
-- ARG3 = blue [number]
-----------------------------------
elseif effect == "fade" then
local r, g, b
--
if type(arg1) == "nil" then
arg1 = "black"
end
--
if string.lower(arg1) == "red" then
r=255
g=0
b=0
elseif string.lower(arg1) == "green" then
r=0
g=255
b=0
elseif string.lower(arg1) == "blue" then
r=0
g=0
b=255
elseif string.lower(arg1) == "yellow" then
r=255
g=255
b=0
elseif string.lower(arg1) == "pink" then
r=255
g=0
b=255
elseif string.lower(arg1) == "white" then
r=255
g=255
b=255
elseif type (arg1) == "number"
and type (arg2) == "number"
and type (arg3) == "number" then
r=arg1
g=arg2
b=arg3
else
r=0
g=0
b=0
end
--
nextView.x = display.contentWidth
nextView.y = 0
--
loadScene (nextScene)
--
local fade = display.newRect( 0 - display.contentWidth, 0 - display.contentHeight, display.contentWidth * 3, display.contentHeight * 3 )
fade.alpha = 0
fade:setFillColor( r,g,b )
effectView:insert(fade)
--
showFx = transition.to ( fade, { alpha=1.0, time=fxTime } )
--
timer.performWithDelay( fxTime, fxEnded )
--
local function returnFade ( event )
showFx = transition.to ( fade, { alpha=0, time=fxTime } )
--
local function removeFade ( event )
fade:removeSelf()
end
--
timer.performWithDelay( fxTime, removeFade )
end
--
timer.performWithDelay( fxTime+1, returnFade )
-----------------------------------
-- EFFECT: Flip
-----------------------------------
elseif effect == "flip" then
showFx = transition.to ( currentView, { xScale=0.001, time=fxTime } )
showFx = transition.to ( currentView, { x=display.contentWidth*0.5, time=fxTime } )
--
loadScene (nextScene)
--
nextView.xScale=0.001
nextView.x=display.contentWidth*0.5
--
showFx = transition.to ( nextView, { xScale=1, delay=fxTime, time=fxTime } )
showFx = transition.to ( nextView, { x=0, delay=fxTime, time=fxTime } )
--
timer.performWithDelay( fxTime*2, fxEnded )
-----------------------------------
-- EFFECT: Down Flip
-----------------------------------
elseif effect == "downFlip" then
showFx = transition.to ( currentView, { xScale=0.7, time=fxTime } )
showFx = transition.to ( currentView, { yScale=0.7, time=fxTime } )
showFx = transition.to ( currentView, { x=display.contentWidth*0.15, time=fxTime } )
showFx = transition.to ( currentView, { y=display.contentHeight*0.15, time=fxTime } )
showFx = transition.to ( currentView, { xScale=0.001, delay=fxTime, time=fxTime } )
showFx = transition.to ( currentView, { x=display.contentWidth*0.5, delay=fxTime, time=fxTime } )
--
loadScene (nextScene)
--
nextView.x = display.contentWidth*0.5
nextView.xScale=0.001
nextView.yScale=0.7
nextView.y=display.contentHeight*0.15
--
showFx = transition.to ( nextView, { x=display.contentWidth*0.15, delay=fxTime*2, time=fxTime } )
showFx = transition.to ( nextView, { xScale=0.7, delay=fxTime*2, time=fxTime } )
showFx = transition.to ( nextView, { xScale=1, delay=fxTime*3, time=fxTime } )
showFx = transition.to ( nextView, { yScale=1, delay=fxTime*3, time=fxTime } )
showFx = transition.to ( nextView, { x=0, delay=fxTime*3, time=fxTime } )
showFx = transition.to ( nextView, { y=0, delay=fxTime*3, time=fxTime } )
--
timer.performWithDelay( fxTime*4, fxEnded )
-----------------------------------
-- EFFECT: None
-----------------------------------
else
timer.performWithDelay( 0, fxEnded )
loadScene (nextScene)
end
-----------------------------------
-- Clean up memory
-----------------------------------
if lastScene then
package.loaded[lastScene] = nil
end
lastScene = nextScene
collectgarbage("collect")
return true
end