forked from coronalabs/framework-composer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
composer_scene.lua
836 lines (646 loc) · 19.1 KB
/
composer_scene.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
-----------------------------------------------------------------------------------------
--
-- Corona Labs
--
-- composer_scene.lua
--
-- Code is MIT licensed; see https://www.coronalabs.com/links/code/license
-----------------------------------------------------------------------------------------
-- still cheating. Which object is the Runtime super? EventListener?
local Super = Runtime._super
-- the scene object
local Scene = Super:new()
-- libraries we need
local json = require( "json" )
local physics = require( "physics" )
local function print_r(object, mesg)
if mesg then
tag = mesg .. ": "
else
tag = ""
end
--print(tag .. json.encode(object, { indent = true }))
end
-- Needed because the table order gets scrambled by JSON roundtrip
local function unpack_color(rgbTable)
local r = rgbTable.r
local g = rgbTable.g
local b = rgbTable.b
return r, g, b
end
-----------------------------------------------------------------------------------------
-- initialize()
-- class constructor
-----------------------------------------------------------------------------------------
function Scene:initialize()
Super.initialize( self )
self.view = Super.view
self._objects = {}
self._hasPhysics = false
end
function Scene:setComposerSceneName( file )
self._composerFileName = file
end
function Scene:getComposerSceneName()
return self._composerFileName
end
-------
-- graphics object handling
-------
-- splits a string by a pattern
local function split(string, pat)
pat = pat or '%s+'
local st, g = 1, string:gmatch("()("..pat..")")
local function getter(segs, seps, sep, cap1, ...)
st = sep and seps + #sep
return string:sub(segs, (seps or 0) - 1), cap1 or sep, ...
end
return function() if st then return getter(st, g()) end end
end
-- create a display group
local function _newGroup( params )
local group = display.newGroup()
if params.tag then
group.tag = params.tag
end
return group
end
local function getGroupByTag ( group, groupTag )
local returnGroup
for i = 1, group.numChildren do
local child = group[ i ]
-- if we have a tag, return it
if groupTag == child.tag then
returnGroup = child
end
if child.numChildren and child.numChildren > 0 then
getGroupByTag( child, groupTag )
end
end
return returnGroup
end
-- create a image
local function _newImage( params )
local methodArgs = {}
if params.parentGroup then
local group = getGroupByTag( display.getCurrentStage(), params.parentGroup )
if group then
table.insert ( methodArgs, group )
end
end
if params.baseDir then
table.insert( methodArgs, params.baseDir )
end
if params.imageFile then
table.insert( methodArgs, params.imageFile )
end
if params.x then
table.insert( methodArgs, params.x )
end
if params.y then
table.insert( methodArgs, params.y )
end
table.insert( methodArgs, true )
newImage = display.newImage( unpack( methodArgs ) )
-- if params.physicsEnabled then
-- newImage.width = newImage.width * params.xScale
-- newImage.height = newImage.height * params.yScale
-- end
if params.strokeColor then
--[[
local strokeTable = {}
for token in split( params.strokeColor, "," ) do
table.insert( strokeTable, tonumber( token ) )
end
newRect:setStrokeColor( unpack( strokeTable ) )
--]]
newImage:setStrokeColor( unpack_color(params.strokeColor) )
end
if params.strokeWidth then
newImage.strokeWidth = params.strokeWidth
end
return newImage
end
-- create a rectangle
local function _newRect ( params )
local methodArgs = {}
if params.parentGroup then
local group = newDisplay.getGroupByTag( display.getCurrentStage(), params.parentGroup )
if group then
table.insert ( methodArgs, group )
end
end
if params.x then
table.insert( methodArgs, params.x )
end
if params.y then
table.insert( methodArgs, params.y )
end
if params.rectWidth then
table.insert( methodArgs, params.rectWidth )
end
if params.rectHeight then
table.insert( methodArgs, params.rectHeight )
end
local newRect = display.newRect( unpack ( methodArgs ) )
if params.fillColor then
--[[
local fillTable = {}
for token in split( params.fillColor, "," ) do
table.insert( fillTable, tonumber( token ) )
end
newRect:setFillColor( unpack( fillTable ) )
--]]
newRect:setFillColor( unpack_color(params.fillColor) )
end
if params.strokeColor then
--[[
local strokeTable = {}
for token in split( params.strokeColor, "," ) do
table.insert( strokeTable, tonumber( token ) )
end
newRect:setStrokeColor( unpack( strokeTable ) )
--]]
newRect:setStrokeColor( unpack_color(params.strokeColor) )
end
if params.strokeWidth then
newRect.strokeWidth = params.strokeWidth
end
return newRect
end
-- create a circle
local function _newCircle ( params )
local methodArgs = {}
if params.parentGroup then
local group = newDisplay.getGroupByTag( display.getCurrentStage(), params.parentGroup )
if group then
table.insert ( methodArgs, group )
end
end
if params.x then
table.insert( methodArgs, params.x )
end
if params.y then
table.insert( methodArgs, params.y )
end
if params.circleRadius then
table.insert( methodArgs, params.circleRadius )
end
local newCircle = display.newCircle( unpack ( methodArgs ) )
--newCircle.width = newCircle.width * params.xScale
--newCircle.height = newCircle.height * params.yScale
newCircle.width = newCircle.width
newCircle.height = newCircle.height
if params.fillColor then
--[[
local fillTable = {}
for token in split( params.fillColor, "," ) do
table.insert( fillTable, tonumber( token ) )
end
newCircle:setFillColor( unpack( fillTable ) )
--]]
newCircle:setFillColor( unpack_color(params.fillColor) )
end
if params.strokeColor then
--[[
local strokeTable = {}
for token in split( params.strokeColor, "," ) do
table.insert( strokeTable, tonumber( token ) )
end
newCircle:setStrokeColor( unpack( strokeTable ) )
--]]
newCircle:setStrokeColor( unpack_color(params.strokeColor) )
end
if params.strokeWidth then
newCircle.strokeWidth = params.strokeWidth
end
return newCircle
end
-- create a line
local function _newLine ( params )
local methodArgs = {}
if params.parentGroup then
local group = newDisplay.getGroupByTag( display.getCurrentStage(), params.parentGroup )
if group then
table.insert ( methodArgs, group )
end
end
if params.x1 then
table.insert( methodArgs, params.x1 )
end
if params.y1 then
table.insert( methodArgs, params.y1 )
end
if params.x2 then
table.insert( methodArgs, params.x2 )
end
if params.y2 then
table.insert( methodArgs, params.y2 )
end
local newLine = display.newLine( params.x1, params.y1, params.x2, params.y2 )
if params.lineColor then
newLine:setStrokeColor( unpack_color(params.lineColor) )
end
if params.lineWidth then
newLine.strokeWidth = params.lineWidth
end
newLine.x = params.x
newLine.y = params.y
return newLine
end
-- create a text
local function _newText ( params )
local methodArgs = {}
if params.parentGroup then
local group = newDisplay.getGroupByTag( display.getCurrentStage(), params.parentGroup )
if group then
table.insert ( methodArgs, group )
end
end
if params.text then
table.insert( methodArgs, params.text )
end
if params.x then
table.insert( methodArgs, params.x )
end
if params.y then
table.insert( methodArgs, params.y )
end
if params.font and params.font ~= "" then
-- Note we have a string and we want the internal representation
if params.font == "native.systemFont" then
table.insert( methodArgs, native.systemFont )
elseif params.font == "native.systemFontBold" then
table.insert( methodArgs, native.systemFontBold )
else
table.insert( methodArgs, params.font )
end
else
table.insert( methodArgs, native.systemFont )
end
if params.size then
table.insert( methodArgs, params.size )
end
-- We could also just pass "params" to newText() (almost)
local newText = display.newText( unpack ( methodArgs ) )
if params.textColor then
--[[
local colorTable = {}
for token in split( params.textColor, "," ) do
table.insert( colorTable, tonumber( token ) )
end
--]]
-- there seems to be a disagreement about the limit
newText:setFillColor( unpack_color(params.textColor) )
end
if params.physicsEnabled then
if params.xScale then
--newText.xScale = params.xScale
end
if params.yScale then
--newText.yScale = params.yScale
end
end
if params.width then
newText.width = params.width
end
return newText
end
-- transition handling
function Scene:computeTransitions( object, transitionTable )
local function createTransitionParams( object, objectModel )
if not object.isVisible then
return
end
if objectModel then
local timeline = objectModel
-- timeline contains the keyframes, that have:
-- index = the object index, position = the keyframe position in the timeline, time = the effective time of the keyframe,
-- params = all the tween params
-- first, we need a sorting of the timeline table based on keyframe positions
local tranTable = timeline
local function compare( a, b )
return a.position < b.position
end
table.sort(tranTable, compare)
local copyTable = tranTable
-- if we have at least a keyframe
if #copyTable > 0 then
local initialDelay = copyTable[ 1 ].time
-- then we iterate the keyframes
-- if only one keyframe, we just show the object at the params contained in the keyframe
local transitionParams = copyTable[ 1 ].params
transitionParams.time = initialDelay
-- OBSOLETE: instead of transitioning here, we just place the object properties
--transition.to( object, transitionParams )
for k, v in pairs( transitionParams ) do
object[ k ] = v
end
-- if more keyframes
if #copyTable > 1 then
local delayCount = initialDelay
for i = 2, #timeline do
-- setup the params
local transitionParams = copyTable[ i ].params
transitionParams.delay = delayCount
transitionParams.time = copyTable[ i ].time - copyTable[ i - 1 ].time
transition.to( object, transitionParams )
delayCount = delayCount + transitionParams.time
end
end
end
end
end
createTransitionParams( object, transitionTable )
end
function Scene:newObject ( params )
local factoryMethods = {
image = _newImage,
rect = _newRect,
circle = _newCircle,
line = _newLine,
text = _newText,
group = _newGroup
}
local returnedObject
local objectType = params.type
local factory = factoryMethods[ objectType ]
if nil ~= factory then
returnedObject = factory( params )
end
return returnedObject
end
local function dragBody ( event, params )
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()
if "began" == phase then
stage:setFocus( body, event.id )
body.isFocus = true
-- Create a temporary touch joint and store it in the object for later reference
if params and params.center then
-- drag the body from its center point
body.tempJoint = physics.newJoint( "touch", body, body.x, body.y )
else
-- drag the body from the point where it was touched
body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )
end
-- Apply optional joint parameters
if params then
local maxForce, frequency, dampingRatio
if params.maxForce then
-- Internal default is (1000 * mass), so set this fairly high if setting manually
body.tempJoint.maxForce = params.maxForce
end
if params.frequency then
-- This is the response speed of the elastic joint: higher numbers = less lag/bounce
body.tempJoint.frequency = params.frequency
end
if params.dampingRatio then
-- Possible values: 0 (no damping) to 1.0 (critical damping)
body.tempJoint.dampingRatio = params.dampingRatio
end
end
elseif body.isFocus then
if "moved" == phase then
-- Update the joint to track the touch
body.tempJoint:setTarget( event.x, event.y )
elseif "ended" == phase or "cancelled" == phase then
stage:setFocus( body, nil )
body.isFocus = false
-- Remove the joint when the touch ends
body.tempJoint:removeSelf()
end
end
-- Stop further propagation of touch event
return true
end
-- scene rendering methods
function Scene:loadFile ( filename )
-- set default base dir if none specified
local base = system.ResourceDirectory
-- create a file path for corona i/o
local path = system.pathForFile( filename, base )
-- will hold contents of file
local contents
-- io.open opens a file at path. returns nil if no file found
local file = io.open( path, "r" )
if file then
-- read all contents of file into a string
contents = file:read( "*a" )
io.close( file ) -- close the file after using it
--print(contents)
--return decoded json string
return json.decode( contents )
else
--or return nil if file didn't ex
return nil
end
end
function Scene:startPhysics()
local physics = physics or require "physics"
physics.stop()
physics.start()
end
function Scene:createObject( objData )
local v = objData
if not v.type then
v.type = "image"
end
display.setDefault( "background", 1, 1, 1 )
if v.children and v.sceneName then
local background = display.newRect( self.view, display.contentWidth * 0.5, display.contentHeight * 0.5, display.contentWidth, display.contentHeight )
if v.bgColor then
background:setFillColor( v.bgColor.r, v.bgColor.g, v.bgColor.b, v.bgColor.a )
end
background:toBack()
-- global physics properties
if v.xGravity and v.yGravity then
if not self._hasPhysics then
self._hasPhysics = true
physics.start()
end
physics.setGravity( tonumber( v.xGravity ), tonumber( v.yGravity ) )
end
else
local object = self:newObject( v )
-- properties and positioning
if object then
-- position
if not object.numChildren then
object.x = v.x
object.y = v.y
end
-- rotation
if v.rotation then
object.rotation = v.rotation
end
-- tint color
if v.bgColor and v.type ~= "group" then
object:setFillColor( unpack_color( v.bgColor ) )
end
-- tint color for image objects
if v.fillColor and v.fillColor.r and v.fillColor.g and v.fillColor.b then
object:setFillColor( v.fillColor.r, v.fillColor.g, v.fillColor.b )
end
if v.alpha then
object.alpha = v.alpha
end
if v.fillEffect then
object.fill.effect = v.fillEffect
end
-- mirrors code in CCSceneMethods.lua
if v.physicsEnabled then
-- if at least one object has physics enabled, we enable physics
if not self._hasPhysics then
self._hasPhysics = true
physics.start()
end
local bodyShape, radius
if v.radius and v.radius ~= 0 then
radius = v.radius
else
if v.bodyShape then
bodyShape = {}
for i=1,#v.bodyShape do
bodyShape[#bodyShape+1] = v.bodyShape[i].x * v.xScale
bodyShape[#bodyShape+1] = v.bodyShape[i].y * v.yScale
end
end
end
physics.addBody( object, v.bodyType, { bounce=v.bounce, density=v.density, friction=v.friction, shape=bodyShape, radius=radius } )
if v.hasJoint == true then
object:addEventListener( "touch", dragBody )
end
--further physics properties
if v.isSensor then
object.isSensor = v.isSensor
end
if v.isBullet then
object.isBullet = v.isBullet
end
if v.isFixedRotation then
object.isFixedRotation = v.isFixedRotation
end
if v.isBodyActive then
object.isBodyActive = v.isBodyActive
end
if v.isBodyActive then
object.isBodyActive = v.isBodyActive
end
if v.linearDamping then
object.linearDamping = v.linearDamping
end
if v.angularDamping then
object.angularDamping = v.angularDamping
end
if v.isSleepingAllowed then
object.isSleepingAllowed = v.isSleepingAllowed
end
if v.isAwake then
object.isAwake = v.isAwake
end
end
object.isVisible = v.isVisible
object.id = v.id
object.tag = v.tag
if v.isImage and v.width or v.height then
if v.width then
object.width = v.width
end
if v.height then
object.height = v.height
end
end
if v.xScale then
object.xScale = v.xScale
end
if v.yScale then
object.yScale = v.yScale
end
table.insert( self._objects, object )
return object
end
end
end
function Scene:load( fileName )
self._objects = {}
self._hasPhysics = false
local objects = self:loadFile( fileName )
local root = objects["objects"]
-- create the bg
local objData = root.id1
self:createObject( objData )
-- then all the objects
local function showObjects( group, parentGroup )
for i = 1, #group do
local objData = root[ group[ i ] ]
local obj = self:createObject( objData )
parentGroup:insert( obj )
if obj.numChildren then
showObjects( objData.children, obj )
end
end
end
showObjects( root.id1.children, self.view )
end
function Scene:getObjectsByTag(...)
local objectsTable = {}
for i = 1, #self._objects do
local currentObject = self._objects[ i ]
for i, v in ipairs( arg ) do
if currentObject.tag == v then
table.insert( objectsTable, currentObject )
end
end
end
if #objectsTable > 0 then
return objectsTable
else
return nil
end
end
function Scene:getObjectByTag( searchTag )
for i = 1, #self._objects do
local currentObject = self._objects[ i ]
if currentObject.tag == searchTag then
return currentObject
end
end
return nil
end
function Scene:getObjectsByName(...)
local objectsTable = {}
for i = 1, #self._objects do
local currentObject = self._objects[ i ]
for i, v in ipairs( arg ) do
if currentObject.id == v then
table.insert( objectsTable, currentObject )
end
end
end
if #objectsTable > 0 then
return objectsTable
else
return nil
end
end
function Scene:getObjectByName( searchName )
for i = 1, #self._objects do
local currentObject = self._objects[ i ]
if currentObject.id == searchName then
return currentObject
end
end
return nil
end
function Scene:getGroupByName( searchName )
for i = 1, #self._objects do
local currentObject = self._objects[ i ]
if currentObject.id == searchName and currentObject.numChildren then
return currentObject
end
end
return nil
end
return Scene