forked from dmccuskey/DMC-Corona-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmc_objects.lua
More file actions
862 lines (718 loc) · 17.9 KB
/
Copy pathdmc_objects.lua
File metadata and controls
862 lines (718 loc) · 17.9 KB
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
--====================================================================--
-- dmc_objects.lua
--
-- by David McCuskey
-- Documentation: http://docs.davidmccuskey.com/display/docs/dmc_objects.lua
--====================================================================--
--[[
Copyright (C) 2011-2013 David McCuskey. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in the
Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
--]]
-- Semantic Versioning Specification: http://semver.org/
local VERSION = "1.0.0"
-- =========================================================
-- Imports
-- =========================================================
local Utils = require( "dmc_utils" )
-- =========================================================
-- Class Support Functions
-- =========================================================
--
-- indexFunc()
-- override the normal Lua lookup functionality to allow
-- property getter functions
--
local function indexFunc( t, k )
local gt, f, val, par
-- look for key in getters table
gt = rawget( t, '__getters' ) or {}
f = gt[ k ]
if f then
-- found getter function, let's call it
val = f( t )
else
-- not in getter, so check for key directly on object
val = rawget( t, k )
if val == nil then
-- not found on object, so let's check parent hierarchy
par = rawget( t, '__parent' )
if par then val = par[ k ] end
end
end
return val
end
-- newindexFunc()
-- override the normal Lua lookup functionality to allow
-- property setter functions
--
local function newindexFunc( t, k, v )
local st, f
-- look for key in setters table
st = rawget( t, '__setters' ) or {}
f = st[ k ]
if f then
-- found setter function, let's call it
f( t, v )
else
-- not in setter, so place key/value directly on object
rawset( t, k, v )
end
end
-- _bless()
-- sets up the inheritance chain via metatables
-- creates object to bless if one isn't provided
--
-- @param base base class object (optional)
-- @param obj object to bless (optional)
-- @return a blessed object
--
local function bless( base, obj )
local o = obj or {}
local mt = {
__index = indexFunc,
__newindex = newindexFunc
}
setmetatable( o, mt )
-- create lookup tables - parent, setter, getter
o.__parent = base
o.__setters = {}
o.__getters = {}
if base then
-- we have a parent, so let's copy down all its getters/setters
o.__getters = Utils.extend( base.__getters, o.__getters )
o.__setters = Utils.extend( base.__setters, o.__setters )
end
return o
end
local function inheritsFrom( baseClass, options, constructor )
local constructor = constructor
local o
-- get default constructor
if baseClass and constructor == nil then
constructor = baseClass['new'] -- assuming new
end
-- create our class object
if baseClass == nil or constructor == nil then
o = bless( baseClass )
else
o = constructor( baseClass, options )
end
-- Setup some class type functions
-- Return the class object of the instance
function o:class()
return o
end
-- Return the super class object of the instance
function o:superClass()
return baseClass
end
-- Return true if the caller is an instance of theClass
function o:isa( theClass )
local b_isa = false
local cur_class = o
while ( nil ~= cur_class ) and ( false == b_isa ) do
if cur_class == theClass then
b_isa = true
else
cur_class = cur_class:superClass()
end
end
return b_isa
end
return o
end
-- =========================================================
-- Object Class
-- =========================================================
local Object = inheritsFrom( nil )
Object.NAME = "Object Base"
Object._PRINT_INCLUDE = {}
Object._PRINT_EXCLUDE = { '__dmc_super' }
-- new()
-- class constructor
--
function Object:new( options )
return self:_bless()
end
-- _bless()
-- interface to generic bless()
--
function Object:_bless( obj )
return bless( self, obj )
end
-- superCall( name, ... )
-- call a method on an object's parent
--
function Object:superCall( name, ... )
local c, s -- class, super
local found -- flag, if property is found
local result
local self_dmc_super = self.__dmc_super
local super_flag = self_dmc_super
-- structure in which to save our place
-- in case supercall is invoked again
if self_dmc_super == nil then
self.__dmc_super = {} -- a stack
self_dmc_super = self.__dmc_super
table.insert( self_dmc_super, self )
end
-- loop setup
c = self_dmc_super[ # self_dmc_super ]
s = c:superClass()
found = false
-- loop through class supers and their properties
-- until we find first match
while s and not found do
found = rawget( s, name )
if found then
-- push the stack, call the method, pop the stack
table.insert( self_dmc_super, s )
result = s[name]( self, unpack( arg ) )
table.remove( self_dmc_super, # self_dmc_super )
end
s = s:superClass()
end
-- we were the first and last, so clean up
if super_flag == nil then
table.remove( self_dmc_super, # self_dmc_super )
self.__dmc_super = nil
end
return result
end
-- print
-- convenience method to interface with Utils.print
--
function Object:print( include, exclude )
local include = include or self._PRINT_INCLUDE
local exclude = exclude or self._PRINT_EXCLUDE
Utils.print( self, include, exclude )
end
function Object:optimize()
function _optimize( obj, class )
-- climb up the hierarchy
if not class then return end
_optimize( obj, class:superClass() )
-- make local references to all functions
for k,v in pairs( class ) do
if type( v ) == "function" then
obj[ k ] = v
end
end
end
_optimize( self, self:class() )
end
function Object:deoptimize()
for k,v in pairs( self ) do
if type( v ) == "function" then
self[ k ] = nil
end
end
end
-- =========================================================
-- CoronaBase Class
-- =========================================================
local CoronaBase = inheritsFrom( Object )
CoronaBase.NAME = "Corona Base"
-- new()
-- class constructor
--
function CoronaBase:new( options )
local o = self:_bless()
o:_init( options )
o:_createView()
o:_initComplete()
return o
end
-- _init()
-- initialize the object - setting the display
--
function CoronaBase:_init( options )
-- == Create Properties ==
-- has-a - Corona display object
self.display = nil
-- create our class display container
self:_setDisplay( display.newGroup() )
end
-- _undoInit()
-- remove items added during _init()
--
function CoronaBase:_undoInit( options )
-- OVERRIDE THIS
end
-- _createView()
-- create any visual items specific to object
-- they are then put into the Corona display object
--
function CoronaBase:_createView()
-- OVERRIDE THIS
end
-- _undoCreateView()
-- remove any items added during _createView()
--
function CoronaBase:_undoCreateView()
-- OVERRIDE THIS
end
-- _initComplete()
-- any setup after object is done being created
--
function CoronaBase:_initComplete()
-- OVERRIDE THIS
end
-- _undoInitComplete()
-- remove any items added during _initComplete()
--
function CoronaBase:_undoInitComplete()
-- OVERRIDE THIS
end
-- _setDisplay( displayObject )
-- set the display property to incoming display object
-- remove current if already set, only check direct property, not hierarchy
--
function CoronaBase:_setDisplay( displayObject )
if rawget( self, 'display' ) ~= nil then
if self.display.__dmc_ref then self.display.__dmc_ref = nil end
self.display:removeSelf()
end
self.display = displayObject
self.display.__dmc_ref = self
end
-- destroy()
-- remove the display object from the stage
--
function CoronaBase:destroy()
self.display:removeSelf()
self.display = nil
end
function CoronaBase:show()
self.display.isVisible = true
end
function CoronaBase:hide()
self.display.isVisible = false
end
--== Corona Specific Properties and Methods ==--
--= DISPLAY GROUP =--
-- Properties --
-- numChildren
--
function CoronaBase.__getters:numChildren()
return self.display.numChildren
end
-- Methods --
-- insert( [index,] child, [, resetTransform] )
--
function CoronaBase:insert( ... )
self.display:insert( ... )
end
-- remove( indexOrChild )
--
function CoronaBase:remove( ... )
self.display:remove( ... )
end
--= CORONA OBJECT =--
-- Properties
-- alpha
--
function CoronaBase.__getters:alpha()
return self.display.alpha
end
function CoronaBase.__setters:alpha( value )
self.display.alpha = value
end
-- contentBounds
--
function CoronaBase.__getters:contentBounds()
return self.display.contentBounds
end
-- contentHeight
--
function CoronaBase.__getters:contentHeight()
return self.display.contentHeight
end
-- contentWidth
--
function CoronaBase.__getters:contentWidth()
return self.display.contentWidth
end
-- height
--
function CoronaBase.__getters:height()
return self.display.height
end
function CoronaBase.__setters:height( value )
self.display.height = value
end
-- isHitTestMasked
--
function CoronaBase.__getters:isHitTestMasked()
return self.display.isHitTestMasked
end
function CoronaBase.__setters:isHitTestMasked( value )
self.display.isHitTestMasked = value
end
-- isHitTestable
--
function CoronaBase.__getters:isHitTestable()
return self.display.isHitTestable
end
function CoronaBase.__setters:isHitTestable( value )
self.display.isHitTestable = value
end
-- isVisible
--
function CoronaBase.__getters:isVisible()
return self.display.isVisible
end
function CoronaBase.__setters:isVisible( value )
self.display.isVisible = value
end
-- maskRotation
--
function CoronaBase.__getters:maskRotation()
return self.display.maskRotation
end
function CoronaBase.__setters:maskRotation( value )
self.display.maskRotation = value
end
-- maskScaleX
--
function CoronaBase.__getters:maskScaleX()
return self.display.maskScaleX
end
function CoronaBase.__setters:maskScaleX( value )
self.display.maskScaleX = value
end
-- maskScaleY
--
function CoronaBase.__getters:maskScaleY()
return self.display.maskScaleY
end
function CoronaBase.__setters:maskScaleY( value )
self.display.maskScaleY = value
end
-- maskX
--
function CoronaBase.__getters:maskX()
return self.display.maskX
end
function CoronaBase.__setters:maskX( value )
self.display.maskX = value
end
-- maskY
--
function CoronaBase.__getters:maskY()
return self.display.maskY
end
function CoronaBase.__setters:maskY( value )
self.display.maskY = value
end
-- parent
--
function CoronaBase.__getters:parent()
return self.display.parent
end
-- rotation
--
function CoronaBase.__getters:rotation()
return self.display.rotation
end
function CoronaBase.__setters:rotation( value )
self.display.rotation = value
end
-- stageBounds
--
function CoronaBase.__getters:stageBounds()
print( "\nDEPRECATED: object.stageBounds - use object.contentBounds\n" )
return self.display.stageBounds
end
-- width
--
function CoronaBase.__getters:width()
return self.display.width
end
function CoronaBase.__setters:width( value )
self.display.width = value
end
-- x
--
function CoronaBase.__getters:x()
return self.display.x
end
function CoronaBase.__setters:x( value )
self.display.x = value
end
-- xOrigin
--
function CoronaBase.__getters:xOrigin()
return self.display.xOrigin
end
function CoronaBase.__setters:xOrigin( value )
self.display.xOrigin = value
end
-- xReference
--
function CoronaBase.__getters:xReference()
return self.display.xReference
end
function CoronaBase.__setters:xReference( value )
self.display.xReference = value
end
-- xScale
--
function CoronaBase.__getters:xScale()
return self.display.xScale
end
function CoronaBase.__setters:xScale( value )
self.display.xScale = value
end
-- y
--
function CoronaBase.__getters:y()
return self.display.y
end
function CoronaBase.__setters:y( value )
self.display.y = value
end
-- yOrigin
--
function CoronaBase.__getters:yOrigin()
return self.display.yOrigin
end
function CoronaBase.__setters:yOrigin( value )
self.display.yOrigin = value
end
-- yReference
--
function CoronaBase.__getters:yReference()
return self.display.yReference
end
function CoronaBase.__setters:yReference( value )
self.display.yReference = value
end
-- yScale
--
function CoronaBase.__getters:yScale()
return self.display.yScale
end
function CoronaBase.__setters:yScale( value )
self.display.yScale = value
end
-- Methods --
-- addEventListener( eventName, listener )
--
function CoronaBase:addEventListener( ... )
self.display:addEventListener( ... )
end
-- contentToLocal( x_content, y_content )
--
function CoronaBase:contentToLocal( ... )
self.display:contentToLocal( ... )
end
-- dispatchEvent( event )
--
function CoronaBase:dispatchEvent( ... )
self.display:dispatchEvent( ... )
end
-- localToContent( x, y )
--
function CoronaBase:localToContent( ... )
self.display:localToContent( ... )
end
-- removeEventListener( eventName, listener )
--
function CoronaBase:removeEventListener( ... )
self.display:removeEventListener( ... )
end
-- removeSelf()
--
function CoronaBase:removeSelf()
--print( "\nOVERRIDE: removeSelf()\n" );
if self.display.__dmc_ref then
self.display.__dmc_ref = nil
end
self:_undoInitComplete()
self:_undoCreateView()
self:_undoInit()
if self.display ~= nil then
self.display:removeSelf()
self.display = nil
end
end
-- rotate( deltaAngle )
--
function CoronaBase:rotate( ... )
self.display:rotate( ... )
end
-- scale( sx, sy )
--
function CoronaBase:scale( ... )
self.display:scale( ... )
end
function CoronaBase:setMask( ... )
print( "\nWARNING: setMask( mask ) not tested \n" );
self.display:setMask( ... )
end
-- setReferencePoint( referencePoint )
--
function CoronaBase:setReferencePoint( ... )
self.display:setReferencePoint( ... )
end
-- toBack()
--
function CoronaBase:toBack()
self.display:toBack()
end
-- toFront()
--
function CoronaBase:toFront()
self.display:toFront()
end
-- translate( deltaX, deltaY )
--
function CoronaBase:translate( ... )
self.display:translate( ... )
end
-- =========================================================
-- CoronaPhysics Class
-- =========================================================
local CoronaPhysics = inheritsFrom( CoronaBase )
CoronaPhysics.NAME = "Corona Physics"
-- Properties --
-- angularDamping()
--
function CoronaPhysics.__getters:angularDamping()
return self.display.angularDamping
end
function CoronaPhysics.__setters:angularDamping( value )
self.display.angularDamping = value
end
-- angularVelocity()
--
function CoronaPhysics.__getters:angularVelocity()
return self.display.angularVelocity
end
function CoronaPhysics.__setters:angularVelocity( value )
self.display.angularVelocity = value
end
-- bodyType()
--
function CoronaPhysics.__getters:bodyType()
return self.display.bodyType
end
function CoronaPhysics.__setters:bodyType( value )
self.display.bodyType = value
end
-- isAwake()
--
function CoronaPhysics.__getters:isAwake()
return self.display.isAwake
end
function CoronaPhysics.__setters:isAwake( value )
self.display.isAwake = value
end
-- isBodyActive()
--
function CoronaPhysics.__getters:isBodyActive()
return self.display.isBodyActive
end
function CoronaPhysics.__setters:isBodyActive( value )
self.display.isBodyActive = value
end
-- isBullet()
--
function CoronaPhysics.__getters:isBullet()
return self.display.isBullet
end
function CoronaPhysics.__setters:isBullet( value )
self.display.isBullet = value
end
-- isFixedRotation()
--
function CoronaPhysics.__getters:isFixedRotation()
return self.display.isFixedRotation
end
function CoronaPhysics.__setters:isFixedRotation( value )
self.display.isFixedRotation = value
end
-- isSensor()
--
function CoronaPhysics.__getters:isSensor()
return self.display.isSensor
end
function CoronaPhysics.__setters:isSensor( value )
self.display.isSensor = value
end
-- isSleepingAllowed()
--
function CoronaPhysics.__getters:isSleepingAllowed()
return self.display.isSleepingAllowed
end
function CoronaPhysics.__setters:isSleepingAllowed( value )
self.display.isSleepingAllowed = value
end
-- linearDamping()
--
function CoronaPhysics.__getters:linearDamping()
return self.display.linearDamping
end
function CoronaPhysics.__setters:linearDamping( value )
self.display.linearDamping = value
end
-- Methods --
-- applyAngularImpulse( appliedForce )
--
function CoronaPhysics:applyAngularImpulse( ... )
self.display:applyAngularImpulse( ... )
end
-- applyForce( xForce, yForce, bodyX, bodyY )
--
function CoronaPhysics:applyForce( ... )
self.display:applyForce( ... )
end
-- applyLinearImpulse( xForce, yForce, bodyX, bodyY )
--
function CoronaPhysics:applyLinearImpulse( ... )
self.display:applyLinearImpulse( ... )
end
-- applyTorque( appliedForce )
--
function CoronaPhysics:applyTorque( ... )
self.display:applyTorque( ... )
end
-- getLinearVelocity()
--
function CoronaPhysics:getLinearVelocity()
return self.display:getLinearVelocity()
end
-- resetMassData()
--
function CoronaPhysics:resetMassData()
self.display:resetMassData()
end
-- setLinearVelocity( xVelocity, yVelocity )
--
function CoronaPhysics:setLinearVelocity( ... )
self.display:setLinearVelocity( ... )
end
-- =========================================================
-- DMC Objects Exports
-- =========================================================
local Objects = {
inheritsFrom = inheritsFrom,
Object = Object,
CoronaBase = CoronaBase,
CoronaPhysics = CoronaPhysics,
}
return Objects