-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoryboard.lua
1789 lines (1510 loc) · 49.8 KB
/
storyboard.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Copyright © 2013 - 2014 Corona Labs Inc. All Rights Reserved.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
--
-- * Redistributions of source code must retain the above copyright
-- notice, this list of conditions and the following disclaimer.
-- * Redistributions in binary form must reproduce the above copyright
-- notice, this list of conditions and the following disclaimer in the
-- documentation and/or other materials provided with the distribution.
-- * Neither the name of the company nor the names of its contributors
-- may be used to endorse or promote products derived from this software
-- without specific prior written permission.
-- * Redistributions in any form whatsoever must retain the following
-- acknowledgment visually in the program (e.g. the credits of the program):
-- 'This product includes software developed by Corona Labs Inc. (http://www.coronalabs.com).'
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-- DISCLAIMED. IN NO EVENT SHALL CORONA LABS INC. BE LIABLE FOR ANY
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-----------------------------------------------------------------------------------------
--
-- storyboard.lua
--
-----------------------------------------------------------------------------------------
local storyboard = {}
-----------------------------------------------------------------------------------------
local stage = display.newGroup() -- top-level group which scene views are inserted
local currentModule -- reference to currently loaded scene's module name (string)
local currentScene -- reference to currently loaded scene (display group)
local previousScene -- string that keeps track of the previously loaded scene
local currentOverlay -- reference to currently shown overlay/popup scene
local touchOverlay -- forward declaration for touch-disabling overlay (created once)
local modalRect -- forward declaration for touch-disabling rect that goes behind overlay scenes
storyboard.loadedSceneMods = {} -- this will hold a history of most recently used scenes
storyboard.scenes = {} -- table to replace use of package.loaded
storyboard.stage = stage -- allows external access to storyboard's display group
storyboard.disableAutoPurge = false -- if true, no scenes will auto-purge on low memory
storyboard.purgeOnSceneChange = false -- if true, will automatically purge non-active scenes on scene change
storyboard.isDebug = false -- if true, will print useful info to the terminal in some situations
--Localize variables to prevent table lookups
local _tonumber = tonumber
local _pairs = pairs
local _ioOpen = io.open
local _ioClose = io.close
local _ioWrite = io.write
local _toString = tostring
local _stringSub = string.sub
local _stringFind = string.find
local _type = type
local _collectGarbage = collectgarbage
local _stringFormat = string.format
local _getInfo = system.getInfo
local displayW = display.contentWidth
local displayH = display.contentHeight
local isGraphicsV1 = false
if system.getInfo( "graphicsPipelineVersion" ) ~= "1.0" then
isGraphicsV1 = ( 1 == display.getDefault( "graphicsCompatibility" ) )
end
-- Constants for screen size calculation
local SCREEN_WIDTH = display.contentWidth - ( display.screenOriginX * 2 )
local SCREEN_HEIGHT = display.contentHeight - ( display.screenOriginY * 2 )
local SCREEN_TOP = display.screenOriginY
local SCREEN_RIGHT = display.contentWidth - display.screenOriginX
local SCREEN_BOTTOM = display.contentHeight - display.screenOriginY
local SCREEN_LEFT = display.screenOriginX
local SCREEN_CENTERX = display.contentWidth / 2
local SCREEN_CENTERY = display.contentHeight / 2
-----------------------------------------------------------------------------------------
-- TRANSITION EFFECTS
local effectList = {
["fade"] =
{
["from"] =
{
alphaStart = 1.0,
alphaEnd = 0,
},
["to"] =
{
alphaStart = 0,
alphaEnd = 1.0
}
},
["zoomOutIn"] =
{
["from"] =
{
xEnd = displayW*0.5,
yEnd = displayH*0.5,
xScaleEnd = 0.001,
yScaleEnd = 0.001
},
["to"] =
{
xScaleStart = 0.001,
yScaleStart = 0.001,
xScaleEnd = 1.0,
yScaleEnd = 1.0,
xStart = displayW*0.5,
yStart = displayH*0.5,
xEnd = 0,
yEnd = 0
},
hideOnOut = true
},
["zoomOutInFade"] =
{
["from"] =
{
xEnd = displayW*0.5,
yEnd = displayH*0.5,
xScaleEnd = 0.001,
yScaleEnd = 0.001,
alphaStart = 1.0,
alphaEnd = 0
},
["to"] =
{
xScaleStart = 0.001,
yScaleStart = 0.001,
xScaleEnd = 1.0,
yScaleEnd = 1.0,
xStart = displayW*0.5,
yStart = displayH*0.5,
xEnd = 0,
yEnd = 0,
alphaStart = 0,
alphaEnd = 1.0
},
hideOnOut = true
},
["zoomInOut"] =
{
["from"] =
{
xEnd = -displayW*0.5,
yEnd = -displayH*0.5,
xScaleEnd = 2.0,
yScaleEnd = 2.0
},
["to"] =
{
xScaleStart = 2.0,
yScaleStart = 2.0,
xScaleEnd = 1.0,
yScaleEnd = 1.0,
xStart = -displayW*0.5,
yStart = -displayH*0.5,
xEnd = 0,
yEnd = 0
},
hideOnOut = true
},
["zoomInOutFade"] =
{
["from"] =
{
xEnd = -displayW*0.5,
yEnd = -displayH*0.5,
xScaleEnd = 2.0,
yScaleEnd = 2.0,
alphaStart = 1.0,
alphaEnd = 0
},
["to"] =
{
xScaleStart = 2.0,
yScaleStart = 2.0,
xScaleEnd = 1.0,
yScaleEnd = 1.0,
xStart = -displayW*0.5,
yStart = -displayH*0.5,
xEnd = 0,
yEnd = 0,
alphaStart = 0,
alphaEnd = 1.0
},
hideOnOut = true
},
["flip"] =
{
["from"] =
{
xEnd = displayW*0.5,
xScaleEnd = 0.001
},
["to"] =
{
xScaleStart = 0.001,
xScaleEnd = 1.0,
xStart = displayW*0.5,
xEnd = 0
}
},
["flipFadeOutIn"] =
{
["from"] =
{
xEnd = displayW*0.5,
xScaleEnd = 0.001,
alphaStart = 1.0,
alphaEnd = 0
},
["to"] =
{
xScaleStart = 0.001,
xScaleEnd = 1.0,
xStart = displayW*0.5,
xEnd = 0,
alphaStart = 0,
alphaEnd = 1.0
}
},
["zoomOutInRotate"] =
{
["from"] =
{
xEnd = displayW*0.5,
yEnd = displayH*0.5,
xScaleEnd = 0.001,
yScaleEnd = 0.001,
rotationStart = 0,
rotationEnd = -360
},
["to"] =
{
xScaleStart = 0.001,
yScaleStart = 0.001,
xScaleEnd = 1.0,
yScaleEnd = 1.0,
xStart = displayW*0.5,
yStart = displayH*0.5,
xEnd = 0,
yEnd = 0,
rotationStart = -360,
rotationEnd = 0
},
hideOnOut = true
},
["zoomOutInFadeRotate"] =
{
["from"] =
{
xEnd = displayW*0.5,
yEnd = displayH*0.5,
xScaleEnd = 0.001,
yScaleEnd = 0.001,
rotationStart = 0,
rotationEnd = -360,
alphaStart = 1.0,
alphaEnd = 0
},
["to"] =
{
xScaleStart = 0.001,
yScaleStart = 0.001,
xScaleEnd = 1.0,
yScaleEnd = 1.0,
xStart = displayW*0.5,
yStart = displayH*0.5,
xEnd = 0,
yEnd = 0,
rotationStart = -360,
rotationEnd = 0,
alphaStart = 0,
alphaEnd = 1.0
},
hideOnOut = true
},
["zoomInOutRotate"] =
{
["from"] =
{
xEnd = displayW*0.5,
yEnd = displayH*0.5,
xScaleEnd = 2.0,
yScaleEnd = 2.0,
rotationStart = 0,
rotationEnd = -360
},
["to"] =
{
xScaleStart = 2.0,
yScaleStart = 2.0,
xScaleEnd = 1.0,
yScaleEnd = 1.0,
xStart = displayW*0.5,
yStart = displayH*0.5,
xEnd = 0,
yEnd = 0,
rotationStart = -360,
rotationEnd = 0
},
hideOnOut = true
},
["zoomInOutFadeRotate"] =
{
["from"] =
{
xEnd = displayW*0.5,
yEnd = displayH*0.5,
xScaleEnd = 2.0,
yScaleEnd = 2.0,
rotationStart = 0,
rotationEnd = -360,
alphaStart = 1.0,
alphaEnd = 0
},
["to"] =
{
xScaleStart = 2.0,
yScaleStart = 2.0,
xScaleEnd = 1.0,
yScaleEnd = 1.0,
xStart = displayW*0.5,
yStart = displayH*0.5,
xEnd = 0,
yEnd = 0,
rotationStart = -360,
rotationEnd = 0,
alphaStart = 0,
alphaEnd = 1.0
},
hideOnOut = true
},
["fromRight"] =
{
["from"] =
{
xStart = 0,
yStart = 0,
xEnd = 0,
yEnd = 0,
transition = easing.outQuad
},
["to"] =
{
xStart = displayW,
yStart = 0,
xEnd = 0,
yEnd = 0,
transition = easing.outQuad
},
concurrent = true,
sceneAbove = true
},
["fromLeft"] =
{
["from"] =
{
xStart = 0,
yStart = 0,
xEnd = 0,
yEnd = 0,
transition = easing.outQuad
},
["to"] =
{
xStart = -displayW,
yStart = 0,
xEnd = 0,
yEnd = 0,
transition = easing.outQuad
},
concurrent = true,
sceneAbove = true
},
["fromTop"] =
{
["from"] =
{
xStart = 0,
yStart = 0,
xEnd = 0,
yEnd = 0,
transition = easing.outQuad
},
["to"] =
{
xStart = 0,
yStart = -displayH,
xEnd = 0,
yEnd = 0,
transition = easing.outQuad
},
concurrent = true,
sceneAbove = true
},
["fromBottom"] =
{
["from"] =
{
xStart = 0,
yStart = 0,
xEnd = 0,
yEnd = 0,
transition = easing.outQuad
},
["to"] =
{
xStart = 0,
yStart = displayH,
xEnd = 0,
yEnd = 0,
transition = easing.outQuad
},
concurrent = true,
sceneAbove = true
},
["slideLeft"] =
{
["from"] =
{
xStart = 0,
yStart = 0,
xEnd = -displayW,
yEnd = 0,
transition = easing.outQuad
},
["to"] =
{
xStart = displayW,
yStart = 0,
xEnd = 0,
yEnd = 0,
transition = easing.outQuad
},
concurrent = true,
sceneAbove = true
},
["slideRight"] =
{
["from"] =
{
xStart = 0,
yStart = 0,
xEnd = displayW,
yEnd = 0,
transition = easing.outQuad
},
["to"] =
{
xStart = -displayW,
yStart = 0,
xEnd = 0,
yEnd = 0,
transition = easing.outQuad
},
concurrent = true,
sceneAbove = true
},
["slideDown"] =
{
["from"] =
{
xStart = 0,
yStart = 0,
xEnd = 0,
yEnd = displayH,
transition = easing.outQuad
},
["to"] =
{
xStart = 0,
yStart = -displayH,
xEnd = 0,
yEnd = 0,
transition = easing.outQuad
},
concurrent = true,
sceneAbove = true
},
["slideUp"] =
{
["from"] =
{
xStart = 0,
yStart = 0,
xEnd = 0,
yEnd = -displayH,
transition = easing.outQuad
},
["to"] =
{
xStart = 0,
yStart = displayH,
xEnd = 0,
yEnd = 0,
transition = easing.outQuad
},
concurrent = true,
sceneAbove = true
},
["crossFade"] =
{
["from"] =
{
alphaStart = 1.0,
alphaEnd = 0,
},
["to"] =
{
alphaStart = 0,
alphaEnd = 1.0
},
concurrent = true
}
}
storyboard.effectList = effectList
-----------------------------------------------------------------------------------------
local function debug_print( ... )
_ioWrite( "STORYBOARD > " )
print( ... )
print( "" )
end
-----------------------------------------------------------------------------------------
local function findSceneIndex( sceneName )
for i=1,#storyboard.loadedSceneMods do
if storyboard.loadedSceneMods[i] == sceneName then
return i
end
end
end
-----------------------------------------------------------------------------------------
local function removeFromSceneHistory( sceneName )
local index = findSceneIndex( sceneName )
if index then
table.remove( storyboard.loadedSceneMods, index )
end
end
-----------------------------------------------------------------------------------------
local function addToSceneHistory( sceneName )
removeFromSceneHistory( sceneName )
storyboard.loadedSceneMods[#storyboard.loadedSceneMods+1] = sceneName
end
-----------------------------------------------------------------------------------------
local function saveSceneAndHide( currentScene, newModule, noEffect )
if not currentScene then return; end
local screenshot
if currentScene and currentScene.numChildren and currentScene.numChildren > 0 and not noEffect then
--screenshot = display.capture( currentScene )
screenshot = currentScene
elseif noEffect and currentScene then
currentScene.isVisible = false
end
-- Since display.capture() only captures the group as far as content width/height,
-- we must make calculations to account for groups that are both less than the total width/height
-- of the screen, as well as groups that are offset have elements that are not on the screen:
local bounds = currentScene.contentBounds
local xMin, xMax = bounds.xMin, bounds.xMax
local yMin, yMax = bounds.yMin, bounds.yMax
local objectsOutsideLeft = xMin < display.screenOriginX
local objectsOutsideRight = xMax > displayW+(-display.screenOriginX)
local objectsAboveTop = yMin < display.screenOriginY
local objectsBelowBottom = yMax > displayH+(-display.screenOriginY)
-- Calculate xMin and xMax
if xMin < 0 then xMin = 0; end
if xMax > displayW then
xMax = displayW
end
-- Caluclate yMin and yMax
if yMin < 0 then yMin = 0; end
if yMax > displayH then
yMax = displayH
end
-- Calculate actual width/height of screen capture
local width = xMax - xMin
local height = yMax - yMin
-- loop through current scene and remove potential Runtime table listeners
for i=currentScene.numChildren,1,-1 do
if currentScene[i].enterFrame then Runtime:removeEventListener( "enterFrame", currentScene[i] ); end
end
-- dispatch current scene's exitScene event
if currentModule and storyboard.scenes[currentModule] then
local event = {}
event.name = "exitScene"
storyboard.scenes[currentModule]:dispatchEvent( event )
end
-- set new currentModule
currentModule = newModule
-- display screenshot of previous scene
if screenshot then
stage:insert( screenshot )
return screenshot
end
end
-----------------------------------------------------------------------------------------
local function createTouchOverlay()
local function disableTouches(event) return true; end -- don't allow touches on scene
local overlayRect = display.newRect( SCREEN_LEFT , SCREEN_TOP, SCREEN_WIDTH, SCREEN_HEIGHT )
overlayRect:setFillColor( 0 )
overlayRect.isVisible = false
overlayRect.isHitTestable = true -- allow touches when invisible
overlayRect:addEventListener( "touch", disableTouches )
overlayRect:addEventListener( "tap", disableTouches )
if not isGraphicsV1 then
overlayRect.anchorX = 0
overlayRect.anchorY = 0
end
return overlayRect
end
-----------------------------------------------------------------------------------------
function storyboard.removeScene( sceneName )
-- remove scene, its display group, and global reference in storyboard.scenes table
storyboard.purgeScene( sceneName )
-- remove global reference
storyboard.scenes[sceneName] = nil
package.loaded[sceneName] = nil
end
-----------------------------------------------------------------------------------------
function storyboard.removeAll()
storyboard.hideOverlay()
-- removes all scenes (except for the one that is currently showing)
for i=#storyboard.loadedSceneMods,1,-1 do
local sceneToUnload = storyboard.loadedSceneMods[i]
if sceneToUnload ~= currentModule then
storyboard.removeScene( sceneToUnload )
end
end
end
-----------------------------------------------------------------------------------------
function storyboard.purgeScene( sceneName )
-- Unload a scene and remove its display group
-- NOTE: global reference in storyboard.scenes is kept
local scene = storyboard.scenes[sceneName]
if scene and scene.view then
local event = {}
event.name = "destroyScene"
scene:dispatchEvent( event )
-- remove module from scene history table
removeFromSceneHistory( sceneName )
if scene.view then
display.remove( scene.view )
scene.view = nil
_collectGarbage( "collect" )
end
elseif storyboard.isDebug then
if not scene then
debug_print( sceneName .. " was not purged because it does not exist. Use storyboard.loadScene() or storyboard.gotoScene()." )
elseif scene and not scene.view then
debug_print( sceneName .. " was not purged because it's view (display group) does not exist. This means it has already been purged or the view was never created." )
end
end
end
-----------------------------------------------------------------------------------------
function storyboard.purgeAll()
local purge_count = 0
-- Purges all scenes (except for the one that is currently showing)
for i=#storyboard.loadedSceneMods,1,-1 do
local sceneToUnload = storyboard.loadedSceneMods[i]
if sceneToUnload ~= currentModule then
purge_count = purge_count + 1
storyboard.purgeScene( sceneToUnload )
end
end
if storyboard.isDebug then
local msg = "A total of [" .. purge_count .. "] scene(s) have been purged."
if purge_count == 0 then
msg = "No scenes were purged."
end
debug_print( msg )
end
end
-----------------------------------------------------------------------------------------
function storyboard.getPrevious()
-- Returns the name (string) of the previous scene (or nil if active scene is first)
return previousScene
end
-----------------------------------------------------------------------------------------
function storyboard.getScene( sceneName )
-- Returns a reference (scene object) to the specified sceneName
local scene = storyboard.scenes[sceneName]
if storyboard.isDebug and not scene then
debug_print( "The specified scene, " .. sceneName .. ", does not exist." )
end
return scene
end
-----------------------------------------------------------------------------------------
function storyboard.getCurrentSceneName()
return currentModule
end
-----------------------------------------------------------------------------------------
local function loadObjects( self, options ) -- scene method
local options = options or {}
local file = options.file
local baseDirectory = options.baseDir or system.ResourceDirectory
local assetDirectory = options.assetDir or "assets"
local physicsDisabled = options.excludePhysics
local listener = options.listener
if not file then
error( "You must specify a scene data file via the 'file' option when calling scene:createViewFromData()." )
end
if listener then
self:addEventListener( "createObject", listener )
end
local physics
if not physicsDisabled then
physics = physics or require "physics"
physics.start()
physics.pause()
end
-- load from scene data file
local json = require "json"
local _jsonDecode = json.decode
local sceneDataFile = file
local sceneData
-- strip slashes from assetDirectory string (e.g. "/assets", "/assets/", or "assets/" becomes just "assets")
if _stringSub( assetDirectory, 1, 1 ) == "/" then
assetDirectory = _stringSub( assetDirectory, 2 )
end
if _stringSub( assetDirectory, -1 ) == "/" then
assetDirectory = _stringSub( assetDirectory, 1, #assetDirectory-1 )
end
-- load scene data from file; or from storyboard.loadedSceneDataFiles table (if file was previously loaded)
if (not storyboard.loadedSceneDataFiles) or (not storyboard.loadedSceneDataFiles[sceneDataFile]) then
storyboard.loadedSceneDataFiles = {}
-- load external scene data file
local path = system.pathForFile( sceneDataFile, baseDirectory )
local fh = _ioOpen( path, "r" )
if fh then
sceneData = _jsonDecode( fh:read("*a") )
_ioClose( fh ); fh = nil
storyboard.loadedSceneDataFiles[sceneDataFile] = sceneData -- storing loaded table into storyboard.loadedSceneDataFiles (to prevent unnecessary filesystem access for future loads)
end
else
sceneData = storyboard.loadedSceneDataFiles[sceneDataFile]
end
if sceneData and sceneData.objects then
self.objects = {} -- table to get specific objects by id (e.g. local obj = scene.objects["Object-Id"]; )
-- creates group/object hierarchy based on table structure
local function createGroupStructure( groupTable, isTopLevel )
local g = self.view
if not isTopLevel then g =display.newGroup(); end
if groupTable.children then
for i=1,#groupTable.children do
local o = groupTable.children[i]
local obj
-- setup event table w/ keys common to all objects
local event = {}
event.name = "createObject"
event.phase = "began"
event.objectId = o.id
event.objectProperties = o
local function add_physics( o, obj )
if o.physicsEnabled and not physicsDisabled then
local bodyShape, radius
if o.radius and o.radius ~= 0 then
radius = o.radius
else
-- construct physics bodyShape from vector points
if o.bodyShape then
bodyShape = {}
for i=1,#o.bodyShape do
bodyShape[#bodyShape+1] = o.bodyShape[i].x * o.xScale
bodyShape[#bodyShape+1] = o.bodyShape[i].y * o.yScale
end
end
end
physics.addBody( obj, o.bodyType, { bounce=o.bounce, density=o.density, friction=o.friction, shape=bodyShape, radius=radius } )
end
end
-- create objects, depending on their type
if o.isGroup then
-- GROUPS
-- dispatch "createObject" event with "began" phase
event.objectIsGroup = true
obj = self:dispatchEvent( event ) -- user's event listener should return a display object (group in this case)
if not obj or (obj.numChildren ~= nil) then -- returned object must be a group
obj = createGroupStructure( o )
end
-- add physics
add_physics( o, obj )
elseif o.isText then
-- TEXT OBJECTS
if o.font == "native.systemFont" then o.font = native.systemFont; end
if o.font == "native.systemFontBold" then o.font = native.systemFontBold; end
-- dispatch "createObject" event ("began" phase)
-- potential uses: override object creation to replace object with sprite object, for instance
event.objectIsText = true
obj = self:dispatchEvent( event ) -- user's event listener should return a display object
if not obj then
obj = display.newText( o.text, 0, 0, o.font, o.size )
obj:setTextColor( o.textColor.r, o.textColor.g, o.textColor.b )
end
obj:setReferencePoint( display.CenterReferencePoint )
-- set object properties
for k,v in _pairs( o ) do
if k ~= "text" and k ~= "size" then -- properties already set
obj[k] = v
end
end
elseif o.isRect then
-- RECTANGLE OBJECTS
-- dispatch "createObject" event ("began" phase)
-- potential uses: override object creation to replace object with sprite object, for instance
event.objectIsRect = true
obj = self:dispatchEvent( event ) -- user's event listener should return a display object
if not obj then
obj = display.newRect( 0, 0, o.rectWidth, o.rectHeight )
obj:setFillColor( o.fillColor.r, o.fillColor.g, o.fillColor.b, 255 )
obj:setStrokeColor( o.strokeColor.r, o.strokeColor.g, o.strokeColor.b, 255 )
end
-- add physics
add_physics( o, obj )
-- set object properties
for k,v in _pairs( o ) do
if k ~= "fillColor" and k ~= "strokeColor" and k ~= "rectWidth" and k ~= "rectHeight" then -- properties already set
obj[k] = v
end
end
elseif o.isCircle then
-- CIRCLE OBJECTS
-- dispatch "createObject" event ("began" phase)
-- potential uses: override object creation to replace object with sprite object, for instance
event.objectIsCircle = true
obj = self:dispatchEvent( event ) -- user's event listener should return a display object
if not obj then
obj = display.newCircle( 0, 0, o.circleRadius )
obj:setFillColor( o.fillColor.r, o.fillColor.g, o.fillColor.b, 255 )
obj:setStrokeColor( o.strokeColor.r, o.strokeColor.g, o.strokeColor.b, 255 )
end
-- add physics
add_physics( o, obj )
-- set object properties
for k,v in _pairs( o ) do
if k ~= "fillColor" and k ~= "strokeColor" and k ~= "circleRadius" then -- properties already set
obj[k] = v
end
end
elseif o.isLine then
-- LINE OBJECTS
-- dispatch "createObject" event ("began" phase)
-- potential uses: override object creation to replace object with sprite object, for instance
event.objectIsLine = true
obj = self:dispatchEvent( event ) -- user's event listener should return a display object
if not obj then
obj = display.newLine( o.x1, o.y1, o.x2, o.y2 )
obj:setColor( o.lineColor[1], o.lineColor[2], o.lineColor[3], o.lineColor[4] )
obj.width = o.lineWidth
end
-- set object properties
for k,v in _pairs( o ) do
if k ~= "lineColor" and k ~= "lineWidth" and k ~= "contentWidth" and k ~= "contentHeight" then -- properties already set
obj[k] = v
end
end
obj:translate( -o.contentWidth*0.5, -o.contentHeight*0.5 )
else
-- NORMAL DISPLAY OBJECTS (non-groups and non-text)