nakor 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. data/lib/nakor/generators/corona-game-template/README +32 -0
  2. data/lib/nakor/generators/corona-game-template/about.lua +34 -0
  3. data/lib/nakor/generators/corona-game-template/bk_default.png +0 -0
  4. data/lib/nakor/generators/corona-game-template/btn_about.png +0 -0
  5. data/lib/nakor/generators/corona-game-template/btn_about_over.png +0 -0
  6. data/lib/nakor/generators/corona-game-template/btn_help.png +0 -0
  7. data/lib/nakor/generators/corona-game-template/btn_help_over.png +0 -0
  8. data/lib/nakor/generators/corona-game-template/btn_play.png +0 -0
  9. data/lib/nakor/generators/corona-game-template/btn_play_over.png +0 -0
  10. data/lib/nakor/generators/corona-game-template/btn_settings.png +0 -0
  11. data/lib/nakor/generators/corona-game-template/btn_settings_over.png +0 -0
  12. data/lib/nakor/generators/corona-game-template/build.settings +15 -0
  13. data/lib/nakor/generators/corona-game-template/config.lua +13 -0
  14. data/lib/nakor/generators/corona-game-template/director.lua +2226 -0
  15. data/lib/nakor/generators/corona-game-template/help.lua +34 -0
  16. data/lib/nakor/generators/corona-game-template/init_buttons.lua +55 -0
  17. data/lib/nakor/generators/corona-game-template/loadmenu.lua +33 -0
  18. data/lib/nakor/generators/corona-game-template/main.lua +12 -0
  19. data/lib/nakor/generators/corona-game-template/menu.lua +93 -0
  20. data/lib/nakor/generators/corona-game-template/newgame.rb +56 -0
  21. data/lib/nakor/generators/corona-game-template/play.lua +34 -0
  22. data/lib/nakor/generators/corona-game-template/radlib.lua +73 -0
  23. data/lib/nakor/generators/corona-game-template/settings.lua +34 -0
  24. data/lib/nakor/generators/corona-game-template/splash_screen.png +0 -0
  25. data/lib/nakor/generators/corona-game-template/ui.lua +318 -0
  26. data/lib/nakor/version.rb +1 -1
  27. metadata +28 -4
  28. data/.gitmodules +0 -3
@@ -0,0 +1,32 @@
1
+ Corona Game Template version 1.0
2
+ (c) 2011 by Radamanthus Batnag
3
+
4
+ This is a template project for building games using Ansca Mobile's Corona SDK (http://www.anscamobile.com/corona/)
5
+
6
+ USAGE
7
+ Out of the box, this is a working Corona project. It includes a splash screen (loadmenu.lua, displays images/splashScreen.png for 1 second) and the following menu buttons:
8
+ - Play
9
+ - Settings
10
+ - Help
11
+ - About
12
+
13
+ Note the naming convention for button images: play.png is the button for Play, settings.png for Settings, etc.
14
+
15
+ The project directory structure follows my personal convention for projects:
16
+
17
+ appname
18
+ +-- assets
19
+ +-- appname
20
+ +-- doc
21
+
22
+ - All code is in appname/appname.
23
+ - Game assets (e.g. Illustrator .ai files) are in appname/assets, finished PNGs are copied over to appname/appname because that's where Corona SDK expects them to be.
24
+ - The game design document (yes you should have one) and all other documentation are in appname/doc
25
+
26
+ CREDITS
27
+
28
+ The code incorporates the latest version of Director class. Director class was written by Ricardo Rauber (http://developer.anscamobile.com/code/director-class-10)
29
+
30
+ The code also includes some code adopted from the Ghosts vs. Monsters project by Beebe Games (http://blog.anscamobile.com/2010/12/ghosts-vs-monsters-open-source-game-in-corona-sdk/)
31
+
32
+ This code is MIT licensed, see http://developer.anscamobile.com/code/license
@@ -0,0 +1,34 @@
1
+ module(..., package.seeall)
2
+
3
+ -- Main function - MUST return a display.newGroup()
4
+ function new()
5
+ local localGroup = display.newGroup()
6
+
7
+ -- Background
8
+ local background = display.newImageRect("bk_default.png", 480, 320)
9
+ background.x = display.contentCenterX
10
+ background.y = display.contentCenterY
11
+ localGroup:insert(background)
12
+
13
+ -- Title
14
+ local title = display.newText("Touch to go back", 0, 0, native.systemFontBold, 16)
15
+ title:setTextColor( 255,255,255)
16
+ title.x = display.contentCenterX
17
+ title.y = display.contentCenterY
18
+ title.name = "title"
19
+ localGroup:insert(title)
20
+
21
+ -- Touch to go back
22
+ local function touched ( event )
23
+ if ("ended" == event.phase) then
24
+ director:changeScene("menu","fade")
25
+ end
26
+ end
27
+ background:addEventListener("touch",touched)
28
+
29
+ unloadMe = function()
30
+ end
31
+
32
+ -- MUST return a display.newGroup()
33
+ return localGroup
34
+ end
@@ -0,0 +1,15 @@
1
+ settings =
2
+ {
3
+ orientation =
4
+ {
5
+ default = "landscapeRight",
6
+ supported =
7
+ {
8
+ "landscapeLeft", "landscapeRight",
9
+ },
10
+ },
11
+ androidPermissions =
12
+ {
13
+ "android.permission.INTERNET"
14
+ },
15
+ }
@@ -0,0 +1,13 @@
1
+ application =
2
+ {
3
+ content =
4
+ {
5
+ width = 320,
6
+ height = 480,
7
+ scale = "letterbox",
8
+ imageSuffix =
9
+ {
10
+ ["@2"] = 2,
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,2226 @@
1
+ module( ..., package.seeall )
2
+
3
+ --====================================================================--
4
+ -- DIRECTOR CLASS
5
+ --====================================================================--
6
+
7
+ --[[
8
+
9
+ - Version: 1.4
10
+ - Made by Ricardo Rauber Pereira @ 2010
11
+ - Blog: http://rauberlabs.blogspot.com/
12
+ - Mail: ricardorauber@gmail.com
13
+ - Twitter: @rauberlabs
14
+
15
+ ******************
16
+ - INFORMATION
17
+ ******************
18
+
19
+ - This class is free to use, feel free to change but please send new versions
20
+ or new features like new effects to me and help us to make it better!
21
+ - Please take a look on the template.lua file and don't forget to always
22
+ insert your display objects into the localGroup.
23
+ - If you like Director Class, please help us donating at my blog, so I could
24
+ keep doing it for free. http://rauberlabs.blogspot.com/
25
+
26
+ ******************
27
+ - HISTORY
28
+ ******************
29
+
30
+ - 06-OCT-2010 - Ricardo Rauber - Created;
31
+ - 07-OCT-2010 - Ricardo Rauber - Functions loadScene and fxEnded were
32
+ taken off from the changeScene function;
33
+ Added function cleanGroups for best
34
+ memory clean up;
35
+ Added directorView and effectView groups
36
+ for better and easier control;
37
+ Please see INFORMATION to know how to use it;
38
+ - 14-NOV-2010 - Ricardo Rauber - Bux fixes and new getScene function to get
39
+ the name of the active scene(lua file);
40
+ - 14-FEB-2011 - Ricardo Rauber - General Bug Fixes;
41
+ - 26-APR-2011 - Ricardo Rauber - cleanGroups() changed; Added Pop Up;
42
+ - 21-JUN-2011 - Ricardo Rauber - Added error control; cleanGroups() removed;
43
+ Added touch protection; loadScene() changed;
44
+ Effects improved; Send Parameters; Bug fixes;
45
+ - 28-JUN-2011 - Ricardo Rauber - Added Books;
46
+ - 05-JUL-2011 - Ricardo Rauber - Added rebuildGroup(), initVars() and Toggle Degug;
47
+ - 21-JUL-2011 - Ricardo Rauber - Search for missed objects to insert into the
48
+ right group and prevent keeping on the screen;
49
+ - 25-AUG-2011 - Ricardo Rauber - Bug fixes;
50
+
51
+ -- ]]
52
+
53
+ print( "-----------------------------------------------" )
54
+
55
+ --====================================================================--
56
+ -- TOGGLE DEBUG
57
+ --====================================================================--
58
+
59
+ showDebug = true
60
+ --showDebug = false
61
+
62
+ --====================================================================--
63
+ -- CONTENT INFO
64
+ --====================================================================--
65
+
66
+ local _W = display.contentWidth
67
+ local _H = display.contentHeight
68
+
69
+ --====================================================================--
70
+ -- DISPLAY GROUPS
71
+ --====================================================================--
72
+
73
+ directorView = display.newGroup()
74
+ --
75
+ local currView = display.newGroup()
76
+ local prevView = display.newGroup()
77
+ local nextView = display.newGroup()
78
+ local protectionView = display.newGroup()
79
+ local popupView = display.newGroup()
80
+ local effectView = display.newGroup()
81
+ --
82
+ local initViews = function()
83
+ directorView:insert( currView )
84
+ directorView:insert( prevView )
85
+ directorView:insert( nextView )
86
+ directorView:insert( protectionView )
87
+ directorView:insert( popupView )
88
+ directorView:insert( effectView )
89
+ end
90
+
91
+ --====================================================================--
92
+ -- VARIABLES
93
+ --====================================================================--
94
+
95
+ local prevScreen, currScreen, nextScreen, popupScreen
96
+ local prevScene, currScene, nextScene, popupScene = "main", "main", "main", "main"
97
+ local newScene
98
+ local fxTime = 200
99
+ local safeDelay = 50
100
+ local isChangingScene = false
101
+ local popUpOnClose
102
+ local isBook = false
103
+ local bookPages = {}
104
+ local currBookPage = 1
105
+ local moveBookPage
106
+ --
107
+ prevView.x = -_W
108
+ prevView.y = 0
109
+ currView.x = 0
110
+ currView.y = 0
111
+ nextView.x = _W
112
+ nextView.y = 0
113
+ popupView.x = 0
114
+ popupView.y = 0
115
+
116
+ --====================================================================--
117
+ -- GET COLOR
118
+ --====================================================================--
119
+
120
+ local getColor = function( strValue1, strValue2, strValue3 )
121
+
122
+ ------------------
123
+ -- Variables
124
+ ------------------
125
+
126
+ local r, g, b
127
+
128
+ ------------------
129
+ -- Test Parameters
130
+ ------------------
131
+
132
+ if type( strValue1 ) == "nil" then
133
+ strValue1 = "black"
134
+ end
135
+
136
+ ------------------
137
+ -- Find Color
138
+ ------------------
139
+
140
+ if string.lower( tostring( strValue1 ) ) == "red" then
141
+ r = 255
142
+ g = 0
143
+ b = 0
144
+ elseif string.lower( tostring( strValue1 ) ) == "green" then
145
+ r = 0
146
+ g = 255
147
+ b = 0
148
+ elseif string.lower( tostring( strValue1 ) ) == "blue" then
149
+ r = 0
150
+ g = 0
151
+ b = 255
152
+ elseif string.lower( tostring( strValue1 ) ) == "yellow" then
153
+ r = 255
154
+ g = 255
155
+ b = 0
156
+ elseif string.lower( tostring( strValue1 ) ) == "pink" then
157
+ r = 255
158
+ g = 0
159
+ b = 255
160
+ elseif string.lower( tostring( strValue1 ) ) == "white" then
161
+ r = 255
162
+ g = 255
163
+ b = 255
164
+ elseif type( strValue1 ) == "number"
165
+ and type( strValue2 ) == "number"
166
+ and type( strValue3 ) == "number" then
167
+ r = strValue1
168
+ g = strValue2
169
+ b = strValue3
170
+ else
171
+ r = 0
172
+ g = 0
173
+ b = 0
174
+ end
175
+
176
+ ------------------
177
+ -- Return
178
+ ------------------
179
+
180
+ return r, g, b
181
+
182
+ end
183
+
184
+ --====================================================================--
185
+ -- SHOW ERRORS
186
+ --====================================================================--
187
+
188
+ local showError = function( errorMessage, debugMessage )
189
+ local message = "Director ERROR: " .. tostring( errorMessage )
190
+ local function onComplete( event )
191
+ print()
192
+ print( "-----------------------" )
193
+ print( message )
194
+ print( "-----------------------" )
195
+ print( debugMessage )
196
+ print( "-----------------------" )
197
+ error()
198
+ end
199
+ --
200
+ if showDebug then
201
+ local alert = native.showAlert( "Director Class - ERROR", message, { "OK" }, onComplete )
202
+ else
203
+ onComplete()
204
+ end
205
+ end
206
+
207
+ --====================================================================--
208
+ -- GARBAGE COLLECTOR
209
+ --====================================================================--
210
+
211
+ local garbageCollect = function( event )
212
+ collectgarbage( "collect" )
213
+ end
214
+
215
+ --====================================================================--
216
+ -- IS DISPLAY OBJECT ?
217
+ --====================================================================--
218
+
219
+ local coronaMetaTable = getmetatable( display.getCurrentStage() )
220
+ --
221
+ local isDisplayObject = function( aDisplayObject )
222
+ return( type( aDisplayObject ) == "table" and getmetatable( aDisplayObject ) == coronaMetaTable )
223
+ end
224
+
225
+ --====================================================================--
226
+ -- PROTECTION
227
+ --====================================================================--
228
+
229
+ ------------------
230
+ -- Rectangle
231
+ ------------------
232
+
233
+ local protection = display.newRect( -_W, -_H, _W * 3, _H * 3 )
234
+ protection:setFillColor( 255, 255, 255 )
235
+ protection.alpha = 0.01
236
+ protection.isVisible = false
237
+ protectionView:insert( protection )
238
+
239
+ ------------------
240
+ -- Listener
241
+ ------------------
242
+
243
+ local fncProtection = function( event )
244
+ return true
245
+ end
246
+ protection:addEventListener( "touch", fncProtection )
247
+ protection:addEventListener( "tap", fncProtection )
248
+
249
+ --====================================================================--
250
+ -- CHANGE CONTROLS
251
+ --====================================================================--
252
+
253
+ ------------------
254
+ -- Effects Time
255
+ ------------------
256
+
257
+ function director:changeFxTime( newFxTime )
258
+ if type( newFxTime ) == "number" then
259
+ fxTime = newFxTime
260
+ end
261
+ end
262
+
263
+ ------------------
264
+ -- Safe Delay
265
+ ------------------
266
+
267
+ function director:changeSafeDelay( newSafeDelay )
268
+ if type( newSafeDelay ) == "number" then
269
+ safeDelay = newSafeDelay
270
+ end
271
+ end
272
+
273
+ --====================================================================--
274
+ -- GET SCENES
275
+ --====================================================================--
276
+
277
+ ------------------
278
+ -- Previous
279
+ ------------------
280
+
281
+ function director:getPrevScene()
282
+ return prevScene
283
+ end
284
+
285
+ ------------------
286
+ -- Current
287
+ ------------------
288
+
289
+ function director:getCurrScene()
290
+ return currScene
291
+ end
292
+
293
+ ------------------
294
+ -- Next
295
+ ------------------
296
+
297
+ function director:getNextScene()
298
+ return nextScene
299
+ end
300
+
301
+ --====================================================================--
302
+ -- REBUILD GROUP
303
+ --====================================================================--
304
+
305
+ local rebuildGroup = function( target )
306
+
307
+ ------------------
308
+ -- Verify which group
309
+ ------------------
310
+
311
+ -- Prev
312
+ if target == "prev" then
313
+
314
+ ------------------
315
+ -- Clean Group
316
+ ------------------
317
+
318
+ prevView:removeSelf()
319
+
320
+ ------------------
321
+ -- Search for the localGroup.clean() function
322
+ ------------------
323
+
324
+ if prevScreen then
325
+ if prevScreen.clean then
326
+
327
+ ------------------
328
+ -- Clean Object
329
+ ------------------
330
+
331
+ local handler, message = pcall( prevScreen.clean )
332
+ --
333
+ if not handler then
334
+ showError( "Failed to clean object '" .. prevScene .. "' - Please verify the localGroup.clean() function.", message )
335
+ return false
336
+ end
337
+
338
+ end
339
+
340
+ end
341
+
342
+ ------------------
343
+ -- Create Group
344
+ ------------------
345
+
346
+ prevView = display.newGroup()
347
+
348
+ -- Curr
349
+ elseif target == "curr" then
350
+
351
+ ------------------
352
+ -- Clean Group
353
+ ------------------
354
+
355
+ currView:removeSelf()
356
+
357
+ ------------------
358
+ -- Search for the localGroup.clean() function
359
+ ------------------
360
+
361
+ if currScreen then
362
+ if currScreen.clean then
363
+
364
+ ------------------
365
+ -- Clean Object
366
+ ------------------
367
+
368
+ local handler, message = pcall( currScreen.clean )
369
+ --
370
+ if not handler then
371
+ showError( "Failed to clean object '" .. currScene .. "' - Please verify the localGroup.clean() function.", message )
372
+ return false
373
+ end
374
+
375
+ end
376
+
377
+ end
378
+
379
+ ------------------
380
+ -- Create Group
381
+ ------------------
382
+
383
+ currView = display.newGroup()
384
+
385
+ -- Next
386
+ elseif target == "next" then
387
+
388
+ ------------------
389
+ -- Clean Group
390
+ ------------------
391
+
392
+ nextView:removeSelf()
393
+
394
+ ------------------
395
+ -- Search for the localGroup.clean() function
396
+ ------------------
397
+
398
+ if nextScreen then
399
+ if nextScreen.clean then
400
+
401
+ ------------------
402
+ -- Clean Object
403
+ ------------------
404
+
405
+ local handler, message = pcall( nextScreen.clean )
406
+ --
407
+ if not handler then
408
+ showError( "Failed to clean object '" .. nextScene .. "' - Please verify the localGroup.clean() function.", message )
409
+ return false
410
+ end
411
+
412
+ end
413
+
414
+ end
415
+
416
+ ------------------
417
+ -- Create Group
418
+ ------------------
419
+
420
+ nextView = display.newGroup()
421
+
422
+ -- Popup
423
+ elseif target == "popup" then
424
+
425
+ ------------------
426
+ -- Clean Group
427
+ ------------------
428
+
429
+ popupView:removeSelf()
430
+
431
+ ------------------
432
+ -- Search for the localGroup.clean() function
433
+ ------------------
434
+
435
+ if popupScreen then
436
+ if popupScreen.clean then
437
+
438
+ ------------------
439
+ -- Clean Object
440
+ ------------------
441
+
442
+ local handler, message = pcall( popupScreen.clean )
443
+ --
444
+ if not handler then
445
+ showError( "Failed to clean object '" .. popupScene .. "' - Please verify the localGroup.clean() function.", message )
446
+ return false
447
+ end
448
+
449
+ end
450
+
451
+ end
452
+
453
+ ------------------
454
+ -- Create Group
455
+ ------------------
456
+
457
+ popupView = display.newGroup()
458
+
459
+ end
460
+
461
+ ------------------
462
+ -- Init
463
+ ------------------
464
+
465
+ initViews()
466
+
467
+ end
468
+
469
+ --====================================================================--
470
+ -- INITIATE VARIABLES
471
+ --====================================================================--
472
+
473
+ local initVars = function( target )
474
+
475
+ ------------------
476
+ -- Verify which group
477
+ ------------------
478
+
479
+ -- Prev
480
+ if target == "prev" then
481
+
482
+ ------------------
483
+ -- Search for the localGroup.initVars() function
484
+ ------------------
485
+
486
+ if prevScreen then
487
+ if prevScreen.initVars then
488
+
489
+ ------------------
490
+ -- Init Vars
491
+ ------------------
492
+
493
+ local handler, message = pcall( prevScreen.initVars )
494
+ --
495
+ if not handler then
496
+ showError( "Failed to initiate variables of object '" .. prevScene .. "' - Please verify the localGroup.initVars() function.", message )
497
+ return false
498
+ end
499
+
500
+ end
501
+ end
502
+
503
+ -- Curr
504
+ elseif target == "curr" then
505
+
506
+ ------------------
507
+ -- Search for the localGroup.initVars() function
508
+ ------------------
509
+
510
+ if currScreen then
511
+ if currScreen.initVars then
512
+
513
+ ------------------
514
+ -- Init Vars
515
+ ------------------
516
+
517
+ local handler, message = pcall( currScreen.initVars )
518
+ --
519
+ if not handler then
520
+ showError( "Failed to initiate variables of object '" .. currScene .. "' - Please verify the localGroup.initVars() function.", message )
521
+ return false
522
+ end
523
+
524
+ end
525
+
526
+ end
527
+
528
+ -- Next
529
+ elseif target == "next" then
530
+
531
+ ------------------
532
+ -- Search for the localGroup.initVars() function
533
+ ------------------
534
+
535
+ if nextScreen then
536
+ if nextScreen.initVars then
537
+
538
+ ------------------
539
+ -- Init Vars
540
+ ------------------
541
+
542
+ local handler, message = pcall( nextScreen.initVars )
543
+ --
544
+ if not handler then
545
+ showError( "Failed to initiate variables of object '" .. nextScene .. "' - Please verify the localGroup.initVars() function.", message )
546
+ return false
547
+ end
548
+
549
+ end
550
+
551
+ end
552
+
553
+ -- Popup
554
+ elseif target == "popup" then
555
+
556
+ ------------------
557
+ -- Search for the localGroup.initVars() function
558
+ ------------------
559
+
560
+ if popupScreen then
561
+ if popupScreen.initVars then
562
+
563
+ ------------------
564
+ -- Init Vars
565
+ ------------------
566
+
567
+ local handler, message = pcall( popupScreen.initVars )
568
+ --
569
+ if not handler then
570
+ showError( "Failed to initiate variables of object '" .. popupScene .. "' - Please verify the localGroup.initVars() function.", message )
571
+ return false
572
+ end
573
+
574
+ end
575
+
576
+ end
577
+
578
+ end
579
+
580
+ end
581
+
582
+ --====================================================================--
583
+ -- UNLOAD SCENE
584
+ --====================================================================--
585
+
586
+ local unloadScene = function( moduleName )
587
+
588
+ ------------------
589
+ -- Test parameters
590
+ ------------------
591
+
592
+ if moduleName ~= "main" then
593
+
594
+ ------------------
595
+ -- Verify if it was loaded
596
+ ------------------
597
+
598
+ if type( package.loaded[ moduleName ] ) ~= "nil" then
599
+
600
+ ------------------
601
+ -- Search for the clean() function
602
+ ------------------
603
+
604
+ if package.loaded[ moduleName ].clean then
605
+
606
+ ------------------
607
+ -- Execute
608
+ ------------------
609
+
610
+ local handler, message = pcall( package.loaded[ moduleName ].clean )
611
+ --
612
+ if not handler then
613
+ showError( "Failed to clean module '" .. moduleName .. "' - Please verify the clean() function.", message )
614
+ return false
615
+ end
616
+
617
+ end
618
+
619
+ ------------------
620
+ -- Try to free memory
621
+ ------------------
622
+
623
+ package.loaded[ moduleName ] = nil
624
+ --
625
+ local function garbage( event )
626
+ garbageCollect()
627
+ end
628
+ --
629
+ timer.performWithDelay( fxTime, garbage )
630
+
631
+ end
632
+
633
+ end
634
+
635
+ end
636
+
637
+ --====================================================================--
638
+ -- LOAD SCENE
639
+ --====================================================================--
640
+
641
+ local loadScene = function( moduleName, target, params )
642
+
643
+ ------------------
644
+ -- Test parameters
645
+ ------------------
646
+
647
+ if type( moduleName ) ~= "string" then
648
+ showError( "Module name must be a string. moduleName = " .. tostring( moduleName ) )
649
+ return false
650
+ end
651
+
652
+ ------------------
653
+ -- Load Module
654
+ ------------------
655
+
656
+ if not package.loaded[ moduleName ] then
657
+ local handler, message = pcall( require, moduleName )
658
+ if not handler then
659
+ showError( "Failed to load module '" .. moduleName .. "' - Please check if the file exists and it is correct.", message )
660
+ return false
661
+ end
662
+ end
663
+
664
+ ------------------
665
+ -- Serach new() Function
666
+ ------------------
667
+
668
+ if not package.loaded[ moduleName ].new then
669
+ showError( "Module '" .. tostring( moduleName ) .. "' must have a new() function." )
670
+ return false
671
+ end
672
+ --
673
+ local functionName = package.loaded[ moduleName ].new
674
+
675
+ ------------------
676
+ -- Variables
677
+ ------------------
678
+
679
+ local handler
680
+
681
+ ------------------
682
+ -- Load choosed scene
683
+ ------------------
684
+
685
+ -- Prev
686
+ if string.lower( target ) == "prev" then
687
+
688
+ ------------------
689
+ -- Rebuild Group
690
+ ------------------
691
+
692
+ rebuildGroup( "prev" )
693
+
694
+ ------------------
695
+ -- Unload Scene
696
+ ------------------
697
+
698
+ if prevScene ~= currScene and prevScene ~= nextScene then
699
+ unloadScene( moduleName )
700
+ end
701
+
702
+ ------------------
703
+ -- Start Scene
704
+ ------------------
705
+
706
+ handler, prevScreen = pcall( functionName, params )
707
+ --
708
+ if not handler then
709
+ showError( "Failed to execute new( params ) function on '" .. tostring( moduleName ) .. "'.", prevScreen )
710
+ return false
711
+ end
712
+
713
+ ------------------
714
+ -- Check if it returned an object
715
+ ------------------
716
+
717
+ if not isDisplayObject( currScreen ) then
718
+ showError( "Module " .. moduleName .. " must return a display.newGroup()." )
719
+ return false
720
+ end
721
+
722
+ ------------------
723
+ -- Books Touch Area
724
+ ------------------
725
+
726
+ local bookBackground = display.newRect( 0, 0, _W, _H )
727
+ bookBackground.alpha = 0.01
728
+ bookBackground:addEventListener( "touch", moveBookPage )
729
+ prevView:insert( bookBackground )
730
+
731
+ ------------------
732
+ -- Finish
733
+ ------------------
734
+
735
+ prevView:insert( prevScreen )
736
+ prevScene = moduleName
737
+
738
+ ------------------
739
+ -- Initiate Variables
740
+ ------------------
741
+
742
+ initVars( "prev" )
743
+
744
+ -- Curr
745
+ elseif string.lower( target ) == "curr" then
746
+
747
+ ------------------
748
+ -- Rebuild Group
749
+ ------------------
750
+
751
+ rebuildGroup( "curr" )
752
+
753
+ ------------------
754
+ -- Unload Scene
755
+ ------------------
756
+
757
+ if prevScene ~= currScene and currScene ~= nextScene then
758
+ unloadScene( moduleName )
759
+ end
760
+
761
+ ------------------
762
+ -- Start Scene
763
+ ------------------
764
+
765
+ handler, currScreen = pcall( functionName, params )
766
+ --
767
+ if not handler then
768
+ showError( "Failed to execute new( params ) function on '" .. tostring( moduleName ) .. "'.", currScreen )
769
+ return false
770
+ end
771
+
772
+ ------------------
773
+ -- Check if it returned an object
774
+ ------------------
775
+
776
+ if not isDisplayObject( currScreen ) then
777
+ showError( "Module " .. moduleName .. " must return a display.newGroup()." )
778
+ return false
779
+ end
780
+
781
+ ------------------
782
+ -- Books Touch Area
783
+ ------------------
784
+
785
+ local bookBackground = display.newRect( 0, 0, _W, _H )
786
+ bookBackground.alpha = 0.01
787
+ bookBackground:addEventListener( "touch", moveBookPage )
788
+ currView:insert( bookBackground )
789
+
790
+ ------------------
791
+ -- Finish
792
+ ------------------
793
+
794
+ currView:insert( currScreen )
795
+ currScene = moduleName
796
+
797
+ ------------------
798
+ -- Initiate Variables
799
+ ------------------
800
+
801
+ initVars( "curr" )
802
+
803
+ -- Next
804
+ else
805
+
806
+ ------------------
807
+ -- Rebuild Group
808
+ ------------------
809
+
810
+ rebuildGroup( "next" )
811
+
812
+ ------------------
813
+ -- Unload Scene
814
+ ------------------
815
+
816
+ if prevScene ~= nextScene and currScene ~= nextScene then
817
+ unloadScene( moduleName )
818
+ end
819
+
820
+ ------------------
821
+ -- Start Scene
822
+ ------------------
823
+
824
+ handler, nextScreen = pcall( functionName, params )
825
+ --
826
+ if not handler then
827
+ showError( "Failed to execute new( params ) function on '" .. tostring( moduleName ) .. "'.", nextScreen )
828
+ return false
829
+ end
830
+
831
+ ------------------
832
+ -- Check if it returned an object
833
+ ------------------
834
+
835
+ if not isDisplayObject( nextScreen ) then
836
+ showError( "Module " .. moduleName .. " must return a display.newGroup()." )
837
+ return false
838
+ end
839
+
840
+ ------------------
841
+ -- Books Touch Area
842
+ ------------------
843
+
844
+ local bookBackground = display.newRect( 0, 0, _W, _H )
845
+ bookBackground.alpha = 0.01
846
+ bookBackground:addEventListener( "touch", moveBookPage )
847
+ nextView:insert( bookBackground )
848
+
849
+ ------------------
850
+ -- Finish
851
+ ------------------
852
+
853
+ nextView:insert( nextScreen )
854
+ nextScene = moduleName
855
+
856
+ ------------------
857
+ -- Initiate Variables
858
+ ------------------
859
+
860
+ initVars( "next" )
861
+
862
+ end
863
+
864
+ ------------------
865
+ -- Return
866
+ ------------------
867
+
868
+ return true
869
+
870
+ end
871
+
872
+ ------------------
873
+ -- Load prev screen
874
+ ------------------
875
+
876
+ local loadPrevScene = function( moduleName, params )
877
+ loadScene( moduleName, "prev", params )
878
+ prevView.x = -_W
879
+ end
880
+
881
+ ------------------
882
+ -- Load curr screen
883
+ ------------------
884
+
885
+ local loadCurrScene = function( moduleName, params )
886
+ loadScene( moduleName, "curr", params )
887
+ currView.x = 0
888
+ end
889
+
890
+ ------------------
891
+ -- Load next screen
892
+ ------------------
893
+
894
+ local loadNextScene = function( moduleName, params )
895
+ loadScene( moduleName, "next", params )
896
+ nextView.x = _W
897
+ end
898
+
899
+ --====================================================================--
900
+ -- EFFECT ENDED
901
+ --====================================================================--
902
+
903
+ local fxEnded = function( event )
904
+
905
+ ------------------
906
+ -- Reset current view
907
+ ------------------
908
+
909
+ currView.x = 0
910
+ currView.y = 0
911
+ currView.xScale = 1
912
+ currView.yScale = 1
913
+
914
+ ------------------
915
+ -- Rebuild Group
916
+ ------------------
917
+
918
+ rebuildGroup( "curr" )
919
+
920
+ ------------------
921
+ -- Unload scene
922
+ ------------------
923
+
924
+ if currScene ~= nextScene then
925
+ unloadScene( currScene )
926
+ end
927
+
928
+ ------------------
929
+ -- Next -> Current
930
+ ------------------
931
+
932
+ currScreen = nextScreen
933
+ currScene = newScene
934
+ currView:insert( currScreen )
935
+
936
+ ------------------
937
+ -- Reset next view
938
+ ------------------
939
+
940
+ nextView.x = _W
941
+ nextView.y = 0
942
+ nextView.xScale = 1
943
+ nextView.yScale = 1
944
+ nextScene = "main"
945
+ nextScreen = nil
946
+
947
+ ------------------
948
+ -- Finish
949
+ ------------------
950
+
951
+ isChangingScene = false
952
+ protection.isVisible = false
953
+
954
+ ------------------
955
+ -- Return
956
+ ------------------
957
+
958
+ return true
959
+
960
+ end
961
+
962
+ --====================================================================--
963
+ -- CHANGE SCENE
964
+ --====================================================================--
965
+
966
+ function director:changeScene( params,
967
+ nextLoadScene,
968
+ effect,
969
+ arg1,
970
+ arg2,
971
+ arg3 )
972
+
973
+ ------------------
974
+ -- If is changing scene, return without do anything
975
+ ------------------
976
+
977
+ if isChangingScene then
978
+ return true
979
+ else
980
+ isChangingScene = true
981
+ end
982
+
983
+ ------------------
984
+ -- If is book, return without do anything
985
+ ------------------
986
+
987
+ if isBook then
988
+ return true
989
+ end
990
+
991
+ ------------------
992
+ -- Test parameters
993
+ ------------------
994
+
995
+ if type( params ) ~= "table" then
996
+ arg3 = arg2
997
+ arg2 = arg1
998
+ arg1 = effect
999
+ effect = nextLoadScene
1000
+ nextLoadScene = params
1001
+ params = nil
1002
+ end
1003
+ --
1004
+ if type( nextLoadScene ) ~= "string" then
1005
+ showError( "The scene name must be a string. scene = " .. tostring( nextLoadScene ) )
1006
+ return false
1007
+ end
1008
+
1009
+ ------------------
1010
+ -- If is popup, don't change
1011
+ ------------------
1012
+
1013
+ if popupScene ~= "main" then
1014
+ showError( "Could not change scene inside a popup." )
1015
+ return false
1016
+ end
1017
+
1018
+ ------------------
1019
+ -- Verify objects on current stage
1020
+ ------------------
1021
+
1022
+ local currentStage = display.getCurrentStage()
1023
+ --
1024
+ for i = currentStage.numChildren, 1, -1 do
1025
+
1026
+ ------------------
1027
+ -- Verify directorId
1028
+ ------------------
1029
+
1030
+ if type( currentStage[i].directorId ) == "nil" then
1031
+ currentStage[i].directorId = currScene
1032
+ end
1033
+
1034
+ ------------------
1035
+ -- Insert into the CURR group if it's needed
1036
+ ------------------
1037
+
1038
+ if currentStage[i].directorId == currScene
1039
+ and currentStage[i].directorId ~= "main" then
1040
+ currScreen:insert( currentStage[i] )
1041
+ end
1042
+
1043
+ end
1044
+
1045
+ ------------------
1046
+ -- Prevent change to main
1047
+ ------------------
1048
+
1049
+ if nextLoadScene == "main" then
1050
+ return true
1051
+ end
1052
+
1053
+ ------------------
1054
+ -- Protection
1055
+ ------------------
1056
+
1057
+ protection.isVisible = true
1058
+
1059
+ ------------------
1060
+ -- Variables
1061
+ ------------------
1062
+
1063
+ newScene = nextLoadScene
1064
+ local showFx
1065
+
1066
+ ------------------
1067
+ -- Load Scene
1068
+ ------------------
1069
+
1070
+ loadNextScene( newScene, params )
1071
+
1072
+ ------------------
1073
+ -- Verify objects on current stage
1074
+ ------------------
1075
+
1076
+ for i = currentStage.numChildren, 1, -1 do
1077
+
1078
+ ------------------
1079
+ -- Verify directorId
1080
+ ------------------
1081
+
1082
+ if type( currentStage[i].directorId ) == "nil" then
1083
+ currentStage[i].directorId = newScene
1084
+ end
1085
+
1086
+ ------------------
1087
+ -- Insert into the NEXT group if it's needed
1088
+ ------------------
1089
+
1090
+ if currentStage[i].directorId == newScene
1091
+ and currentStage[i].directorId ~= "main" then
1092
+ nextScreen:insert( currentStage[i] )
1093
+ end
1094
+
1095
+ end
1096
+
1097
+ ------------------
1098
+ -- EFFECT: Move From Right
1099
+ ------------------
1100
+
1101
+ if effect == "moveFromRight" then
1102
+
1103
+ nextView.x = _W
1104
+ nextView.y = 0
1105
+ --
1106
+ showFx = transition.to( nextView, { x = 0, time = fxTime } )
1107
+ showFx = transition.to( currView, { x = -_W, time = fxTime, onComplete = fxEnded } )
1108
+
1109
+ ------------------
1110
+ -- EFFECT: Over From Right
1111
+ ------------------
1112
+
1113
+ elseif effect == "overFromRight" then
1114
+
1115
+ nextView.x = _W
1116
+ nextView.y = 0
1117
+ --
1118
+ showFx = transition.to( nextView, { x = 0, time = fxTime, onComplete = fxEnded } )
1119
+
1120
+ ------------------
1121
+ -- EFFECT: Move From Left
1122
+ ------------------
1123
+
1124
+ elseif effect == "moveFromLeft" then
1125
+
1126
+ nextView.x = -_W
1127
+ nextView.y = 0
1128
+ --
1129
+ showFx = transition.to( nextView, { x = 0, time = fxTime } )
1130
+ showFx = transition.to( currView, { x = _W, time = fxTime, onComplete = fxEnded } )
1131
+
1132
+ ------------------
1133
+ -- EFFECT: Over From Left
1134
+ ------------------
1135
+
1136
+ elseif effect == "overFromLeft" then
1137
+
1138
+ nextView.x = -_W
1139
+ nextView.y = 0
1140
+ --
1141
+ showFx = transition.to( nextView, { x = 0, time = fxTime, onComplete = fxEnded } )
1142
+
1143
+ ------------------
1144
+ -- EFFECT: Move From Top
1145
+ ------------------
1146
+
1147
+ elseif effect == "moveFromTop" then
1148
+
1149
+ nextView.x = 0
1150
+ nextView.y = -_H
1151
+ --
1152
+ showFx = transition.to( nextView, { y = 0, time = fxTime } )
1153
+ showFx = transition.to( currView, { y = _H, time = fxTime, onComplete = fxEnded } )
1154
+
1155
+ ------------------
1156
+ -- EFFECT: Over From Top
1157
+ ------------------
1158
+
1159
+ elseif effect == "overFromTop" then
1160
+
1161
+ nextView.x = 0
1162
+ nextView.y = -_H
1163
+ --
1164
+ showFx = transition.to( nextView, { y = 0, time = fxTime, onComplete = fxEnded } )
1165
+
1166
+ ------------------
1167
+ -- EFFECT: Move From Bottom
1168
+ ------------------
1169
+
1170
+ elseif effect == "moveFromBottom" then
1171
+
1172
+ nextView.x = 0
1173
+ nextView.y = _H
1174
+ --
1175
+ showFx = transition.to( nextView, { y = 0, time = fxTime } )
1176
+ showFx = transition.to( currView, { y = -_H, time = fxTime, onComplete = fxEnded } )
1177
+
1178
+ ------------------
1179
+ -- EFFECT: Over From Bottom
1180
+ ------------------
1181
+
1182
+ elseif effect == "overFromBottom" then
1183
+
1184
+ nextView.x = 0
1185
+ nextView.y = _H
1186
+ --
1187
+ showFx = transition.to( nextView, { y = 0, time = fxTime, onComplete = fxEnded } )
1188
+
1189
+ ------------------
1190
+ -- EFFECT: Crossfade
1191
+ ------------------
1192
+
1193
+ elseif effect == "crossfade" then
1194
+
1195
+ nextView.x = 0
1196
+ nextView.y = 0
1197
+ --
1198
+ nextView.alpha = 0
1199
+ --
1200
+ showFx = transition.to( currView, { alpha = 0, time = fxTime * 2 } )
1201
+ showFx = transition.to( nextView, { alpha = 1, time = fxTime * 2, onComplete = fxEnded } )
1202
+
1203
+ ------------------
1204
+ -- EFFECT: Fade
1205
+ ------------------
1206
+ -- ARG1 = color [ string ]
1207
+ ------------------
1208
+ -- ARG1 = red [ number ]
1209
+ -- ARG2 = green [ number ]
1210
+ -- ARG3 = blue [ number ]
1211
+ ------------------
1212
+
1213
+ elseif effect == "fade" then
1214
+
1215
+ nextView.x = _W
1216
+ nextView.y = 0
1217
+ --
1218
+ local fade = display.newRect( -_W, -_H, _W * 3, _H * 3 )
1219
+ fade.alpha = 0
1220
+ fade:setFillColor( getColor( arg1, arg2, arg3 ) )
1221
+ effectView:insert( fade )
1222
+ --
1223
+ local function returnFade( event )
1224
+ currView.x = _W
1225
+ nextView.x = 0
1226
+ --
1227
+ local function removeFade( event )
1228
+ fade:removeSelf()
1229
+ fxEnded()
1230
+ end
1231
+ --
1232
+ showFx = transition.to( fade, { alpha = 0, time = fxTime, onComplete = removeFade } )
1233
+ end
1234
+ --
1235
+ showFx = transition.to( fade, { alpha = 1.0, time = fxTime, onComplete = returnFade } )
1236
+
1237
+ ------------------
1238
+ -- EFFECT: Flip
1239
+ ------------------
1240
+
1241
+ elseif effect == "flip" then
1242
+
1243
+ nextView.xScale = 0.001
1244
+ --
1245
+ nextView.x = _W / 2
1246
+ --
1247
+ local phase1, phase2
1248
+ --
1249
+ showFx = transition.to( currView, { xScale = 0.001, time = fxTime } )
1250
+ showFx = transition.to( currView, { x = _W / 2, time = fxTime } )
1251
+ --
1252
+ phase1 = function( e )
1253
+ showFx = transition.to( nextView, { xScale = 0.001, x = _W / 2, time = fxTime, onComplete = phase2 } )
1254
+ end
1255
+ --
1256
+ phase2 = function( e )
1257
+ showFx = transition.to( nextView, { xScale = 1, x = 0, time = fxTime, onComplete = fxEnded } )
1258
+ end
1259
+ --
1260
+ showFx = transition.to( nextView, { time = 0, onComplete = phase1 } )
1261
+
1262
+ ------------------
1263
+ -- EFFECT: Down Flip
1264
+ ------------------
1265
+
1266
+ elseif effect == "downFlip" then
1267
+
1268
+ nextView.x = _W / 2
1269
+ nextView.y = _H * 0.15
1270
+ --
1271
+ nextView.xScale = 0.001
1272
+ nextView.yScale = 0.7
1273
+ --
1274
+ local phase1, phase2, phase3, phase4
1275
+ --
1276
+ phase1 = function( e )
1277
+ showFx = transition.to( currView, { xScale = 0.7, yScale = 0.7, x = _W * 0.15, y = _H * 0.15, time = fxTime, onComplete = phase2 } )
1278
+ end
1279
+ --
1280
+ phase2 = function( e )
1281
+ showFx = transition.to( currView, { xScale = 0.001, x = _W / 2, time = fxTime, onComplete = phase3 } )
1282
+ end
1283
+ --
1284
+ phase3 = function( e )
1285
+ showFx = transition.to( nextView, { x = _W * 0.15, xScale = 0.7, time = fxTime, onComplete = phase4 } )
1286
+ end
1287
+ --
1288
+ phase4 = function( e )
1289
+ showFx = transition.to( nextView, { xScale = 1, yScale = 1, x = 0, y = 0, time = fxTime, onComplete = fxEnded } )
1290
+ end
1291
+ --
1292
+ showFx = transition.to( currView, { time = 0, onComplete = phase1 } )
1293
+
1294
+ ------------------
1295
+ -- EFFECT: None
1296
+ ------------------
1297
+
1298
+ else
1299
+ timer.performWithDelay( safeDelay, fxEnded )
1300
+ end
1301
+
1302
+ ------------------
1303
+ -- Return
1304
+ ------------------
1305
+
1306
+ return true
1307
+
1308
+ end
1309
+
1310
+ --====================================================================--
1311
+ -- OPEN POPUP
1312
+ --====================================================================--
1313
+
1314
+ function director:openPopUp( params, newPopUpScene, onClose )
1315
+
1316
+ ------------------
1317
+ -- Test parameters
1318
+ ------------------
1319
+
1320
+ if type( params ) ~= "table" then
1321
+ onClose = newPopUpScene
1322
+ newPopUpScene = params
1323
+ params = nil
1324
+ end
1325
+ --
1326
+ if type( newPopUpScene ) ~= "string" then
1327
+ showError( "Module name must be a string. moduleName = " .. tostring( newPopUpScene ) )
1328
+ return false
1329
+ end
1330
+ --
1331
+ if type( onClose ) == "function" then
1332
+ popUpOnClose = onClose
1333
+ else
1334
+ popUpOnClose = nil
1335
+ end
1336
+
1337
+ ------------------
1338
+ -- Test scene name
1339
+ ------------------
1340
+
1341
+ if newPopUpScene == currScene
1342
+ or newPopUpScene == nextScene
1343
+ or newPopUpScene == "main"
1344
+ then
1345
+ return false
1346
+ end
1347
+
1348
+ ------------------
1349
+ -- If is inside a popup, don't load
1350
+ ------------------
1351
+
1352
+ if popupScene ~= "main" then
1353
+ showError( "Could not load more then 1 popup." )
1354
+ return false
1355
+ end
1356
+
1357
+ ------------------
1358
+ -- Rebuild Group
1359
+ ------------------
1360
+
1361
+ rebuildGroup( "popup" )
1362
+
1363
+ ------------------
1364
+ -- Unload Scene
1365
+ ------------------
1366
+
1367
+ unloadScene( newPopUpScene )
1368
+ popupScene = "main"
1369
+ popupScreen = nil
1370
+
1371
+ ------------------
1372
+ -- Load scene
1373
+ ------------------
1374
+
1375
+ local handler, message = pcall( require, newPopUpScene )
1376
+ --
1377
+ if not handler then
1378
+ showError( "Failed to load module '" .. newPopUpScene .. "' - Please check if the file exists and it is correct.", message )
1379
+ return false
1380
+ end
1381
+
1382
+ ------------------
1383
+ -- Serach for new() function
1384
+ ------------------
1385
+
1386
+ if not package.loaded[ newPopUpScene ].new then
1387
+ showError( "Module '" .. tostring( newPopUpScene ) .. "' must have a new() function." )
1388
+ return false
1389
+ end
1390
+
1391
+ ------------------
1392
+ -- Execute new() function
1393
+ ------------------
1394
+
1395
+ local functionName = package.loaded[ newPopUpScene ].new
1396
+ --
1397
+ handler, popupScreen = pcall( functionName, params )
1398
+ --
1399
+ if not handler then
1400
+ showError( "Failed to execute news( params ) function on '" .. tostring( moduleName ) .. "'.", popupScreen )
1401
+ return false
1402
+ end
1403
+
1404
+ ------------------
1405
+ -- Test if a group was returned
1406
+ ------------------
1407
+
1408
+ if not isDisplayObject( currScreen ) then
1409
+ showError( "Module " .. moduleName .. " must return a display.newGroup()." )
1410
+ return false
1411
+ end
1412
+ --
1413
+ popupView:insert( popupScreen )
1414
+ popupScene = newPopUpScene
1415
+
1416
+ ------------------
1417
+ -- Initiate Variables
1418
+ ------------------
1419
+
1420
+ initVars( "popup" )
1421
+
1422
+ ------------------
1423
+ -- Protection
1424
+ ------------------
1425
+
1426
+ protection.isVisible = true
1427
+
1428
+ ------------------
1429
+ -- Return
1430
+ ------------------
1431
+
1432
+ return true
1433
+
1434
+ end
1435
+
1436
+ --====================================================================--
1437
+ -- CLOSE POPUP
1438
+ --====================================================================--
1439
+
1440
+ function director:closePopUp()
1441
+
1442
+ ------------------
1443
+ -- If no popup is loaded, don't do anything
1444
+ ------------------
1445
+
1446
+ if popupScene == "main" then
1447
+ return true
1448
+ end
1449
+
1450
+ ------------------
1451
+ -- Rebuild Group
1452
+ ------------------
1453
+
1454
+ rebuildGroup( "popup" )
1455
+
1456
+ ------------------
1457
+ -- Unload Scene
1458
+ ------------------
1459
+
1460
+ unloadScene( newPopUpScene )
1461
+ popupScene = "main"
1462
+ popupScreen = nil
1463
+
1464
+ ------------------
1465
+ -- Protection
1466
+ ------------------
1467
+
1468
+ protection.isVisible = false
1469
+
1470
+ ------------------
1471
+ -- Call function
1472
+ ------------------
1473
+
1474
+ if type( popUpOnClose ) == "function" then
1475
+ popUpOnClose()
1476
+ end
1477
+
1478
+ ------------------
1479
+ -- Return
1480
+ ------------------
1481
+
1482
+ return true
1483
+
1484
+ end
1485
+
1486
+ --====================================================================--
1487
+ -- VERIFY IF IS BOOK
1488
+ --====================================================================--
1489
+
1490
+ function director:isBook()
1491
+ return isBook
1492
+ end
1493
+
1494
+ --====================================================================--
1495
+ -- GET CURRENT PAGE NAME
1496
+ --====================================================================--
1497
+
1498
+ getCurrBookPage = function()
1499
+ if bookPages[ currBookPage ] then
1500
+ return bookPages[ currBookPage ]
1501
+ end
1502
+ end
1503
+
1504
+ --====================================================================--
1505
+ -- GET CURRENT PAGE NUMBER
1506
+ --====================================================================--
1507
+
1508
+ getCurrBookPageNum = function()
1509
+ return currBookPage
1510
+ end
1511
+
1512
+ --====================================================================--
1513
+ -- GET PAGE COUNT
1514
+ --====================================================================--
1515
+
1516
+ getBookPageCount = function()
1517
+ return table.maxn( bookPages )
1518
+ end
1519
+
1520
+ --====================================================================--
1521
+ -- INSERT PAGES
1522
+ --====================================================================--
1523
+
1524
+ function director:newBookPages( pageList, fade )
1525
+
1526
+ ------------------
1527
+ -- If is not book, return without do anything
1528
+ ------------------
1529
+
1530
+ if not isBook then
1531
+ return true
1532
+ end
1533
+
1534
+ ------------------
1535
+ -- Test parameters
1536
+ ------------------
1537
+
1538
+ if type( pageList ) ~= "table" then
1539
+ return true
1540
+ end
1541
+
1542
+ ------------------
1543
+ -- Clean it
1544
+ ------------------
1545
+
1546
+ while getBookPageCount() > 0 do
1547
+ table.remove( bookPages )
1548
+ end
1549
+
1550
+ ------------------
1551
+ -- Mount
1552
+ ------------------
1553
+
1554
+ local i = 1
1555
+ while pageList[ i ] do
1556
+ if type( pageList[ i ] ) == "table" then
1557
+ bookPages[ i ] = pageList[ i ]
1558
+ i = i + 1
1559
+ end
1560
+ end
1561
+
1562
+ ------------------
1563
+ -- Fade
1564
+ ------------------
1565
+
1566
+ local fadeFx = display.newRect( 0, 0, _W, _H )
1567
+ fadeFx:setFillColor( 0, 0, 0 )
1568
+ fadeFx.alpha = 0
1569
+ fadeFx.isVisible = false
1570
+ effectView:insert( fadeFx )
1571
+ --
1572
+ local fxEnded = function( e )
1573
+
1574
+ ------------------
1575
+ -- Remove Fade
1576
+ ------------------
1577
+
1578
+ fadeFx:removeSelf()
1579
+ fadeFx = nil
1580
+
1581
+ end
1582
+
1583
+ ------------------
1584
+ -- Go to page
1585
+ ------------------
1586
+
1587
+ local change = function( e )
1588
+
1589
+ ------------------
1590
+ -- Rebuild Group
1591
+ ------------------
1592
+
1593
+ rebuildGroup( "prev" )
1594
+
1595
+ ------------------
1596
+ -- Unload Scene
1597
+ ------------------
1598
+
1599
+ if prevScene ~= bookPages[ 1 ] and prevScene ~= bookPages[ 2 ] then
1600
+ unloadScene( prevScene )
1601
+ end
1602
+ --
1603
+ prevScene = "main"
1604
+
1605
+ ------------------
1606
+ -- Rebuild Group
1607
+ ------------------
1608
+
1609
+ rebuildGroup( "next" )
1610
+
1611
+ ------------------
1612
+ -- Unload Scene
1613
+ ------------------
1614
+
1615
+ if nextScene ~= bookPages[ 1 ] and nextScene ~= bookPages[ 2 ] then
1616
+ unloadScene( nextScene )
1617
+ end
1618
+ --
1619
+ nextScene = "main"
1620
+
1621
+ ------------------
1622
+ -- Load pages 1 and 2
1623
+ ------------------
1624
+
1625
+ if bookPages[ 1 ] then
1626
+ loadCurrScene( bookPages[ 1 ].page, bookPages[ 1 ].params )
1627
+ currBookPage = 1
1628
+ end
1629
+ --
1630
+ if bookPages[ 2 ] then
1631
+ loadNextScene( bookPages[ 2 ].page, bookPages[ 2 ].params )
1632
+ end
1633
+
1634
+ ------------------
1635
+ -- Search for the localGroup.start() function
1636
+ ------------------
1637
+
1638
+ if currScreen then
1639
+ if currScreen.start then
1640
+
1641
+ ------------------
1642
+ -- Start Page
1643
+ ------------------
1644
+
1645
+ local handler, message = pcall( currScreen.start )
1646
+ --
1647
+ if not handler then
1648
+ showError( "Failed to start page of object '" .. currScene .. "' - Please verify the localGroup.start() function.", message )
1649
+ return false
1650
+ end
1651
+
1652
+ end
1653
+ end
1654
+
1655
+ ------------------
1656
+ -- Complete Fade
1657
+ ------------------
1658
+
1659
+ local showFx = transition.to( fadeFx, { alpha = 0, time = fxTime, onComplete = fxEnded } )
1660
+
1661
+ end
1662
+
1663
+ ------------------
1664
+ -- Execute Fade
1665
+ ------------------
1666
+
1667
+ if fade then
1668
+ fadeFx.isVisible = true
1669
+ local showFx = transition.to( fadeFx, { alpha = 1, time = fxTime, onComplete = change } )
1670
+ else
1671
+ change()
1672
+ end
1673
+
1674
+ end
1675
+
1676
+ --====================================================================--
1677
+ -- CHANGE BOOK PAGE
1678
+ --====================================================================--
1679
+
1680
+ function director:changeBookPage( target )
1681
+
1682
+ ------------------
1683
+ -- If is not book, return without do anything
1684
+ ------------------
1685
+
1686
+ if not isBook then
1687
+ return true
1688
+ end
1689
+
1690
+ ------------------
1691
+ -- If is changing scene, return without do anything
1692
+ ------------------
1693
+
1694
+ if isChangingScene then
1695
+ return true
1696
+ else
1697
+ isChangingScene = true
1698
+ end
1699
+
1700
+ ------------------
1701
+ -- Test parameters
1702
+ ------------------
1703
+
1704
+ if type( target ) == nil then
1705
+ return true
1706
+ end
1707
+ --
1708
+ if string.lower( target ) == "next" then
1709
+ if getCurrBookPageNum() + 1 < getBookPageCount() then
1710
+ target = getCurrBookPageNum() + 1
1711
+ else
1712
+ target = getBookPageCount()
1713
+ end
1714
+ elseif string.lower( target ) == "prev" then
1715
+ if getCurrBookPageNum() - 1 > 1 then
1716
+ target = getCurrBookPageNum() - 1
1717
+ else
1718
+ target = 1
1719
+ end
1720
+ end
1721
+ --
1722
+ if not type( target ) == "number" then
1723
+ return true
1724
+ end
1725
+ --
1726
+ local showFx
1727
+
1728
+ ------------------
1729
+ -- Prevent page -1
1730
+ ------------------
1731
+
1732
+ if target < 1 or
1733
+ target > getBookPageCount() or
1734
+ target == getCurrBookPageNum() then
1735
+
1736
+ local bookFxEnded = function( event )
1737
+ isChangingScene = false
1738
+ end
1739
+ --
1740
+ showFx = transition.to( prevView, { x = -_W, time = fxTime, transition = easing.outQuad } )
1741
+ showFx = transition.to( currView, { x = 0, time = fxTime, transition = easing.outQuad } )
1742
+ showFx = transition.to( nextView, { x = _W, time = fxTime, transition = easing.outQuad, onComplete = bookFxEnded } )
1743
+
1744
+ else
1745
+
1746
+ ------------------
1747
+ -- Animate to the correct side
1748
+ ------------------
1749
+
1750
+ -- Moved left
1751
+ if target > getCurrBookPageNum() then
1752
+
1753
+ local bookFxEnded = function( event )
1754
+
1755
+ ------------------
1756
+ -- Rebuild Group
1757
+ ------------------
1758
+
1759
+ rebuildGroup( "prev" )
1760
+
1761
+ ------------------
1762
+ -- Unload Scene
1763
+ ------------------
1764
+
1765
+ if prevScene ~= currScene and prevScene ~= nextScene then
1766
+ unloadScene( prevScene )
1767
+ end
1768
+
1769
+ ------------------
1770
+ -- Curr -> Prev
1771
+ ------------------
1772
+
1773
+ prevScreen = currScreen
1774
+ prevScene = currScene
1775
+ prevView:insert( prevScreen )
1776
+ prevView.x = -_W
1777
+
1778
+ ------------------
1779
+ -- Initiate Variables
1780
+ ------------------
1781
+
1782
+ initVars( "prev" )
1783
+
1784
+ ------------------
1785
+ -- Next -> Curr
1786
+ ------------------
1787
+
1788
+ currScreen = nextScreen
1789
+ currScene = nextScene
1790
+ currView:insert( currScreen )
1791
+ currView.x = 0
1792
+ --
1793
+ nextScreen = nil
1794
+
1795
+ ------------------
1796
+ -- Load Scene
1797
+ ------------------
1798
+
1799
+ if bookPages[ target + 1 ] then
1800
+ loadNextScene( bookPages[ target + 1 ].page, bookPages[ target + 1 ].params )
1801
+ end
1802
+ --
1803
+ nextView.x = _W
1804
+
1805
+ ------------------
1806
+ -- Finish
1807
+ ------------------
1808
+
1809
+ currBookPage = target
1810
+ isChangingScene = false
1811
+
1812
+ ------------------
1813
+ -- Search for the localGroup.start() function
1814
+ ------------------
1815
+
1816
+ if currScreen then
1817
+ if currScreen.start then
1818
+
1819
+ ------------------
1820
+ -- Start Page
1821
+ ------------------
1822
+
1823
+ local handler, message = pcall( currScreen.start )
1824
+ --
1825
+ if not handler then
1826
+ showError( "Failed to start page of object '" .. currScene .. "' - Please verify the localGroup.start() function.", message )
1827
+ return false
1828
+ end
1829
+
1830
+ end
1831
+ end
1832
+
1833
+ end
1834
+ --
1835
+ showFx = transition.to( prevView, { x = -_W, time = fxTime, transition = easing.outQuad } )
1836
+ showFx = transition.to( currView, { x = -_W, time = fxTime, transition = easing.outQuad } )
1837
+ showFx = transition.to( nextView, { x = 0, time = fxTime, transition = easing.outQuad, onComplete = bookFxEnded } )
1838
+
1839
+ -- Moved right
1840
+ else
1841
+
1842
+ local bookFxEnded = function( event )
1843
+
1844
+ ------------------
1845
+ -- Rebuild Group
1846
+ ------------------
1847
+
1848
+ rebuildGroup( "next" )
1849
+
1850
+ ------------------
1851
+ -- Unload Scene
1852
+ ------------------
1853
+
1854
+ if prevScene ~= nextScene and currScene ~= nextScene then
1855
+ unloadScene( nextScene )
1856
+ end
1857
+
1858
+ ------------------
1859
+ -- Curr -> Next
1860
+ ------------------
1861
+
1862
+ nextScreen = currScreen
1863
+ nextScene = currScene
1864
+ nextView:insert( nextScreen )
1865
+ nextView.x = _W
1866
+
1867
+ ------------------
1868
+ -- Initiate Variables
1869
+ ------------------
1870
+
1871
+ initVars( "next" )
1872
+
1873
+ ------------------
1874
+ -- Prev -> Curr
1875
+ ------------------
1876
+
1877
+ currScreen = prevScreen
1878
+ currScene = prevScene
1879
+ currView:insert( currScreen )
1880
+ currView.x = 0
1881
+ --
1882
+ prevScreen = nil
1883
+
1884
+ ------------------
1885
+ -- Load Scene
1886
+ ------------------
1887
+
1888
+ if bookPages[ target - 1 ] then
1889
+ loadPrevScene( bookPages[ target - 1 ].page, bookPages[ target - 1 ].params )
1890
+ end
1891
+ --
1892
+ prevView.x = -_W
1893
+
1894
+ ------------------
1895
+ -- Finish
1896
+ ------------------
1897
+
1898
+ currBookPage = target
1899
+ isChangingScene = false
1900
+
1901
+ ------------------
1902
+ -- Search for the localGroup.start() function
1903
+ ------------------
1904
+
1905
+ if currScreen then
1906
+ if currScreen.start then
1907
+
1908
+ ------------------
1909
+ -- Start Page
1910
+ ------------------
1911
+
1912
+ local handler, message = pcall( currScreen.start )
1913
+ --
1914
+ if not handler then
1915
+ showError( "Failed to start page of object '" .. currScene .. "' - Please verify the localGroup.start() function.", message )
1916
+ return false
1917
+ end
1918
+
1919
+ end
1920
+ end
1921
+
1922
+ end
1923
+ --
1924
+ showFx = transition.to( prevView, { x = 0, time = fxTime, transition = easing.outQuad } )
1925
+ showFx = transition.to( currView, { x = _W, time = fxTime, transition = easing.outQuad } )
1926
+ showFx = transition.to( nextView, { x = _W, time = fxTime, transition = easing.outQuad, onComplete = bookFxEnded } )
1927
+
1928
+ end
1929
+
1930
+ end
1931
+
1932
+ end
1933
+
1934
+ --====================================================================--
1935
+ -- MOVE TO CHANGE BOOK PAGE
1936
+ --====================================================================--
1937
+
1938
+ moveBookPage = function( event )
1939
+
1940
+ ------------------
1941
+ -- If is not book, return without do anything
1942
+ ------------------
1943
+
1944
+ if not isBook then
1945
+ return true
1946
+ end
1947
+
1948
+ ------------------
1949
+ -- If is changing scene, return without do anything
1950
+ ------------------
1951
+
1952
+ if isChangingScene then
1953
+ return true
1954
+ end
1955
+
1956
+ ------------------
1957
+ -- Verify event
1958
+ ------------------
1959
+
1960
+ if event.phase == "moved" then
1961
+
1962
+ ------------------
1963
+ -- Move pages while touching
1964
+ ------------------
1965
+
1966
+ if event.xStart > event.x then
1967
+ prevView.x = -_W - ( event.xStart - event.x )
1968
+ currView.x = 0 - ( event.xStart - event.x )
1969
+ nextView.x = _W - ( event.xStart - event.x )
1970
+ else
1971
+ prevView.x = -_W + ( event.x - event.xStart )
1972
+ currView.x = 0 + ( event.x - event.xStart )
1973
+ nextView.x = _W + ( event.x - event.xStart )
1974
+ end
1975
+
1976
+ elseif event.phase == "ended" then
1977
+
1978
+ ------------------
1979
+ -- If page reach limit then change
1980
+ ------------------
1981
+
1982
+ local limit = 0.2
1983
+ --
1984
+ if currView.x < -_W * limit then
1985
+ director:changeBookPage( "next" )
1986
+ elseif currView.x > _W * limit then
1987
+ director:changeBookPage( "prev" )
1988
+ else
1989
+ director:changeBookPage( getCurrBookPageNum() )
1990
+ end
1991
+
1992
+ end
1993
+
1994
+ end
1995
+
1996
+ --====================================================================--
1997
+ -- GO TO BOOK PAGE
1998
+ --====================================================================--
1999
+
2000
+ function director:goToPage( params, target, fade )
2001
+
2002
+ ------------------
2003
+ -- Test parameters
2004
+ ------------------
2005
+
2006
+ if type( params ) ~= "table" then
2007
+ fade = target
2008
+ target = params
2009
+ params = nil
2010
+ end
2011
+ --
2012
+ if type( target ) ~= "number" then
2013
+ showError( "The page name must be a number. page = " .. tostring( target ) )
2014
+ return false
2015
+ end
2016
+ --
2017
+ if target < 1 then
2018
+ showError( "Cannot change to page lower then 1. page = " .. tostring( target ) )
2019
+ return false
2020
+ end
2021
+
2022
+ ------------------
2023
+ -- Fade
2024
+ ------------------
2025
+
2026
+ local fadeFx = display.newRect( -_W, -_H, _W * 3, _H * 3 )
2027
+ fadeFx:setFillColor( 0, 0, 0 )
2028
+ fadeFx.alpha = 0
2029
+ fadeFx.isVisible = false
2030
+ effectView:insert( fadeFx )
2031
+ --
2032
+ local fxEnded = function( e )
2033
+
2034
+ ------------------
2035
+ -- Remove Fade
2036
+ ------------------
2037
+
2038
+ fadeFx:removeSelf()
2039
+ fadeFx = nil
2040
+
2041
+ end
2042
+
2043
+ ------------------
2044
+ -- Go to page
2045
+ ------------------
2046
+
2047
+ local change = function( e )
2048
+
2049
+ -- First page
2050
+ if target == 1 then
2051
+
2052
+ ------------------
2053
+ -- Rebuild Group
2054
+ ------------------
2055
+
2056
+ rebuildGroup( "prev" )
2057
+
2058
+ ------------------
2059
+ -- Unload Scene
2060
+ ------------------
2061
+
2062
+ if prevScene ~= currScene and prevScene ~= nextScene then
2063
+ unloadScene( prevScene )
2064
+ end
2065
+
2066
+ ------------------
2067
+ -- Load Scenes
2068
+ ------------------
2069
+
2070
+ if bookPages[ 1 ] then
2071
+ loadCurrScene( bookPages[ 1 ].page, params or bookPages[ 1 ].params )
2072
+ currBookPage = 1
2073
+ end
2074
+ --
2075
+ if bookPages[ 2 ] then
2076
+ loadNextScene( bookPages[ 2 ].page, bookPages[ 2 ].params )
2077
+ end
2078
+
2079
+ -- Last page
2080
+ elseif target == getBookPageCount() then
2081
+
2082
+ ------------------
2083
+ -- Load Scenes
2084
+ ------------------
2085
+
2086
+ if bookPages[ target ] then
2087
+ loadCurrScene( bookPages[ target ].page, params or bookPages[ target ].params )
2088
+ currBookPage = target
2089
+ end
2090
+ --
2091
+ if bookPages[ target - 1 ] then
2092
+ loadPrevScene( bookPages[ target - 1 ].page, bookPages[ target - 1 ].params )
2093
+ end
2094
+
2095
+ ------------------
2096
+ -- Rebuild Group
2097
+ ------------------
2098
+
2099
+ rebuildGroup( "next" )
2100
+
2101
+ ------------------
2102
+ -- Unload Scene
2103
+ ------------------
2104
+
2105
+ if prevScene ~= nextScene and currScene ~= nextScene then
2106
+ unloadScene( nextScene )
2107
+ end
2108
+
2109
+ -- Somewhere in the middle
2110
+ else
2111
+
2112
+ ------------------
2113
+ -- Load Scenes
2114
+ ------------------
2115
+
2116
+ if bookPages[ target-1 ] then
2117
+ loadPrevScene( bookPages[ target - 1 ].page, bookPages[ target - 1 ].params )
2118
+ end
2119
+ --
2120
+ if bookPages[ target ] then
2121
+ loadCurrScene( bookPages[ target ].page, params or bookPages[ target ].params )
2122
+ currBookPage = target
2123
+ end
2124
+ --
2125
+ if bookPages[ target + 1 ] then
2126
+ loadNextScene( bookPages[ target + 1 ].page, bookPages[ target + 1 ].params )
2127
+ end
2128
+
2129
+ end
2130
+
2131
+ ------------------
2132
+ -- Search for the localGroup.start() function
2133
+ ------------------
2134
+
2135
+ if currScreen then
2136
+ if currScreen.start then
2137
+
2138
+ ------------------
2139
+ -- Start Page
2140
+ ------------------
2141
+
2142
+ local handler, message = pcall( currScreen.start )
2143
+ --
2144
+ if not handler then
2145
+ showError( "Failed to start page of object '" .. currScene .. "' - Please verify the localGroup.start() function.", message )
2146
+ return false
2147
+ end
2148
+
2149
+ end
2150
+ end
2151
+
2152
+ ------------------
2153
+ -- Complete Fade
2154
+ ------------------
2155
+
2156
+ local showFx = transition.to( fadeFx, { alpha = 0, time = fxTime, onComplete = fxEnded } )
2157
+
2158
+ end
2159
+
2160
+ ------------------
2161
+ -- Execute Fade
2162
+ ------------------
2163
+
2164
+ if fade then
2165
+ fadeFx.isVisible = true
2166
+ local showFx = transition.to( fadeFx, { alpha = 1, time = fxTime, onComplete = change } )
2167
+ else
2168
+ change()
2169
+ end
2170
+
2171
+ end
2172
+
2173
+ --====================================================================--
2174
+ -- TURN TO BOOK OR SCENES
2175
+ --====================================================================--
2176
+
2177
+ function director:turnToBook()
2178
+ isBook = true
2179
+ end
2180
+ --
2181
+ function director:turnToScenes()
2182
+
2183
+ ------------------
2184
+ -- Toggle Books
2185
+ ------------------
2186
+
2187
+ isBook = false
2188
+
2189
+ ------------------
2190
+ -- Rebuild Group
2191
+ ------------------
2192
+
2193
+ rebuildGroup( "prev" )
2194
+
2195
+ ------------------
2196
+ -- Unload Scene
2197
+ ------------------
2198
+
2199
+ if prevScene ~= currScene and prevScene ~= nextScene then
2200
+ unloadScene( prevScene )
2201
+ end
2202
+
2203
+ ------------------
2204
+ -- Rebuild Group
2205
+ ------------------
2206
+
2207
+ rebuildGroup( "next" )
2208
+
2209
+ ------------------
2210
+ -- Unload Scene
2211
+ ------------------
2212
+
2213
+ if prevScene ~= nextScene and currScene ~= nextScene then
2214
+ unloadScene( nextScene )
2215
+ end
2216
+
2217
+ ------------------
2218
+ -- Finish
2219
+ ------------------
2220
+
2221
+ prevScreen = nil
2222
+ nextScreen = nil
2223
+ prevScene = "main"
2224
+ nextScene = "main"
2225
+
2226
+ end