This repository was archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathactor.js
More file actions
3256 lines (2898 loc) · 102 KB
/
actor.js
File metadata and controls
3256 lines (2898 loc) · 102 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
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
/**
* See LICENSE file.
*
* Classes to define animable elements.
* Actor is the superclass of every animable element in the scene graph. It handles the whole
* affine transformation MatrixStack, rotation, translation, globalAlpha and Behaviours. It also
* defines input methods.
* TODO: add text presentation/animation effects.
**/
(function() {
var __index= 0;
/**
* This class is the base for all animable entities in CAAT.
* It defines an entity able to:
*
* <ul>
* <li>Position itself on screen.
* <li>Able to modify its presentation aspect via affine transforms.
* <li>Take control of parent/child relationship.
* <li>Take track of behaviors (@see CAAT.Behavior).
* <li>Define a region on screen.
* <li>Define alpha composition scope.
* <li>Expose lifecycle.
* <li>Manage itself in/out scene time.
* <li>etc.
* </ul>
*
* @constructor
*/
CAAT.Actor = function() {
this.behaviorList= [];
// this.keyframesList= [];
this.lifecycleListenerList= [];
this.AABB= new CAAT.Rectangle();
this.viewVertices= [
new CAAT.Point(0,0,0),
new CAAT.Point(0,0,0),
new CAAT.Point(0,0,0),
new CAAT.Point(0,0,0)
];
this.scaleAnchor= this.ANCHOR_CENTER;
this.modelViewMatrix= new CAAT.Matrix();
this.worldModelViewMatrix= new CAAT.Matrix();
this.resetTransform();
this.setScale(1,1);
this.setRotation(0);
this.id= __index++;
return this;
};
/**
* Reflection information needed to use the inspector.
* Each key defined identifies an object field. For each field, it could be specified:
* + get : accessor function or field name. if ended with () a function will be assumed.
* + set : mutator function or field name. if ended with () a function will be assumed.
* + type : field or accessor function return type.
*
* If not get or set method is defined, the inspector will assume either the field can't be read and/or set.
* If neither get and set are defined, the property will be avoided.
*
* The key can contain a set of comma separated values. This means these properties must be set/modified
* at once in the inspector editor field (if any). The way these functions will be set will be by calling
* the set method (must be a method) as previously defined.
*/
CAAT.Actor.__reflectionInfo= {
"x" : "set:setX(), get:x, type:number",
"cached" : "get:isCached(), type:boolean",
"scaleX,scaleY" : "set:setScale(), type:number"
/*
"y" : "setY,w",
"width" : "setWidth,w",
"height" : "setHeight,w",
"start_time" : "setStartTime,w",
"duration" : "setDuration,w",
"clip" : "setClip,w",
"rotationAngle" : "setRotation,w",
"alpha" : "setAlpha,w",
"isGlobalAlpha" : "isGlobalAlpha,w",
"visible" : "isVisible",
"id" : "getId",
"backgroundImage" : ""*/
};
CAAT.Actor.ANCHOR_CENTER= 0; // constant values to determine different affine transform
CAAT.Actor.ANCHOR_TOP= 1; // anchors.
CAAT.Actor.ANCHOR_BOTTOM= 2;
CAAT.Actor.ANCHOR_LEFT= 3;
CAAT.Actor.ANCHOR_RIGHT= 4;
CAAT.Actor.ANCHOR_TOP_LEFT= 5;
CAAT.Actor.ANCHOR_TOP_RIGHT= 6;
CAAT.Actor.ANCHOR_BOTTOM_LEFT= 7;
CAAT.Actor.ANCHOR_BOTTOM_RIGHT= 8;
CAAT.Actor.ANCHOR_CUSTOM= 9;
CAAT.Actor.CACHE_SIMPLE= 1;
CAAT.Actor.CACHE_DEEP= 2;
CAAT.Actor.prototype= {
lifecycleListenerList: null, // Array of life cycle listener
behaviorList: null, // Array of behaviors to apply to the Actor
parent: null, // Parent of this Actor. May be Scene.
x: 0, // x position on parent. In parent's local coord. system.
y: 0, // y position on parent. In parent's local coord. system.
width: 0, // Actor's width. In parent's local coord. system.
height: 0, // Actor's height. In parent's local coord. system.
start_time: 0, // Start time in Scene time.
duration: Number.MAX_VALUE, // Actor duration in Scene time
clip: false, // should clip the Actor's content against its contour.
clipPath: null,
tAnchorX : 0,
tAnchorY : 0,
scaleX: 0, // transformation. width scale parameter
scaleY: 0, // transformation. height scale parameter
scaleTX: .50, // transformation. scale anchor x position
scaleTY: .50, // transformation. scale anchor y position
scaleAnchor: 0, // transformation. scale anchor
rotationAngle: 0, // transformation. rotation angle in radians
rotationY: .50, // transformation. rotation center y
alpha: 1, // alpha transparency value
rotationX: .50, // transformation. rotation center x
isGlobalAlpha: false, // is this a global alpha
frameAlpha: 1, // hierarchically calculated alpha for this Actor.
expired: false, // set when the actor has been expired
discardable: false, // set when you want this actor to be removed if expired
pointed: false, // is the mouse pointer inside this actor
mouseEnabled: true, // events enabled ?
visible: true,
ANCHOR_CENTER: 0, // constant values to determine different affine transform
ANCHOR_TOP: 1, // anchors.
ANCHOR_BOTTOM: 2,
ANCHOR_LEFT: 3,
ANCHOR_RIGHT: 4,
ANCHOR_TOP_LEFT: 5,
ANCHOR_TOP_RIGHT: 6,
ANCHOR_BOTTOM_LEFT: 7,
ANCHOR_BOTTOM_RIGHT: 8,
ANCHOR_CUSTOM: 9,
fillStyle: null, // any canvas rendering valid fill style.
strokeStyle: null, // any canvas rendering valid stroke style.
time: 0, // Cache Scene time.
AABB: null, // CAAT.Rectangle
viewVertices: null, // model to view transformed vertices.
inFrame: false, // boolean indicating whether this Actor was present on last frame.
dirty: true, // model view is dirty ?
wdirty: true, // world model view is dirty ?
oldX: -1,
oldY: -1,
modelViewMatrix: null, // model view matrix.
worldModelViewMatrix: null, // world model view matrix.
modelViewMatrixI: null, // model view matrix.
worldModelViewMatrixI: null, // world model view matrix.
glEnabled: false,
backgroundImage: null,
id: null,
size_active: 1, // number of animated children
size_total: 1,
__next: null,
__d_ax: -1, // for drag-enabled actors.
__d_ay: -1,
gestureEnabled: false,
invalid : true,
cached : 0, // 0 no, CACHE_SIMPLE | CACHE_DEEP
collides : false,
collidesAsRect : true,
isAA : true, // is this actor/container Axis aligned ? if so, much faster inverse matrices
// can be calculated.
/**
* Touch Start only received when CAAT.TOUCH_BEHAVIOR= CAAT.TOUCH_AS_MULTITOUCH
* @param e <CAAT.TouchEvent>
*/
touchStart : function(e) {
},
touchMove : function(e) {
},
touchEnd : function(e) {
},
gestureStart : function(rotation, scaleX, scaleY) {
},
gestureChange : function( rotation, scaleX, scaleY ) {
if ( this.gestureEnabled ) {
this.setRotation( rotation );
this.setScale( scaleX, scaleY );
}
return this;
},
gestureEnd : function( rotation, scaleX, scaleY ) {
},
isVisible : function() {
return this.isVisible;
},
setupCollission : function( collides, isCircular ) {
this.collides= collides;
this.collidesAsRect= !isCircular;
},
invalidate : function() {
this.invalid= true;
},
setGestureEnabled : function( enable ) {
this.gestureEnabled= !!enable;
return this;
},
isGestureEnabled : function() {
return this.gestureEnabled;
},
getId : function() {
return this.id;
},
setId : function(id) {
this.id= id;
return this;
},
/**
* Set this actor's parent.
* @param parent {CAAT.ActorContainer}
* @return this
*/
setParent : function(parent) {
this.parent= parent;
return this;
},
/**
* Set this actor's background image.
* The need of a background image is to kept compatibility with the new CSSDirector class.
* The image parameter can be either an Image/Canvas or a CAAT.SpriteImage instance. If an image
* is supplied, it will be wrapped into a CAAT.SriteImage instance of 1 row by 1 column.
* If the actor has set an image in the background, the paint method will draw the image, otherwise
* and if set, will fill its background with a solid color.
* If adjust_size_to_image is true, the host actor will be redimensioned to the size of one
* single image from the SpriteImage (either supplied or generated because of passing an Image or
* Canvas to the function). That means the size will be set to [width:SpriteImage.singleWidth,
* height:singleHeight].
*
* WARN: if using a CSS renderer, the image supplied MUST be a HTMLImageElement instance.
*
* @see CAAT.SpriteImage
*
* @param image {Image|HTMLCanvasElement|CAAT.SpriteImage}
* @param adjust_size_to_image {boolean} whether to set this actor's size based on image parameter.
*
* @return this
*/
setBackgroundImage : function(image, adjust_size_to_image ) {
if ( image ) {
if ( !(image instanceof CAAT.SpriteImage) ) {
image= new CAAT.SpriteImage().initialize(image,1,1);
}
image.setOwner(this);
this.backgroundImage= image;
if ( typeof adjust_size_to_image==='undefined' || adjust_size_to_image ) {
this.width= image.getWidth();
this.height= image.getHeight();
}
this.glEnabled= true;
} else {
this.backgroundImage= null;
}
return this;
},
/**
* Set the actor's SpriteImage index from animation sheet.
* @see CAAT.SpriteImage
* @param index {number}
*
* @return this
*/
setSpriteIndex: function(index) {
if ( this.backgroundImage ) {
this.backgroundImage.setSpriteIndex(index);
this.invalidate();
}
return this;
},
/**
* Set this actor's background SpriteImage offset displacement.
* The values can be either positive or negative meaning the texture space of this background
* image does not start at (0,0) but at the desired position.
* @see CAAT.SpriteImage
* @param ox {number} horizontal offset
* @param oy {number} vertical offset
*
* @return this
*/
setBackgroundImageOffset : function( ox, oy ) {
if ( this.backgroundImage ) {
this.backgroundImage.setOffset(ox,oy);
}
return this;
},
/**
* Set this actor's background SpriteImage its animation sequence.
* In its simplet's form a SpriteImage treats a given image as an array of rows by columns
* subimages. If you define d Sprite Image of 2x2, you'll be able to draw any of the 4 subimages.
* This method defines the animation sequence so that it could be set [0,2,1,3,2,1] as the
* animation sequence
* @param ii {Array<number>} an array of integers.
*/
setAnimationImageIndex : function( ii ) {
if ( this.backgroundImage ) {
this.backgroundImage.setAnimationImageIndex(ii);
}
return this;
},
resetAnimationTime : function() {
if ( this.backgroundImage ) {
this.backgroundImage.resetAnimationTime();
}
return this;
},
setChangeFPS : function(time) {
if ( this.backgroundImage ) {
this.backgroundImage.setChangeFPS(time);
}
return this;
},
/**
* Set this background image transformation.
* If GL is enabled, this parameter has no effect.
* @param it any value from CAAT.SpriteImage.TR_*
* @return this
*/
setImageTransformation : function( it ) {
if ( this.backgroundImage ) {
this.backgroundImage.setSpriteTransformation(it);
}
return this;
},
/**
* Center this actor at position (x,y).
* @param x {number} x position
* @param y {number} y position
*
* @return this
* @deprecated
*/
centerOn : function( x,y ) {
this.setLocation( x-this.width/2, y-this.height/2 );
return this;
},
/**
* Center this actor at position (x,y).
* @param x {number} x position
* @param y {number} y position
*
* @return this
*/
centerAt : function(x,y) {
return this.centerOn(x,y);
},
/**
* If GL is enables, get this background image's texture page, otherwise it will fail.
* @return {CAAT.GLTexturePage}
*/
getTextureGLPage : function() {
return this.backgroundImage.image.__texturePage;
},
/**
* Set this actor invisible.
* The actor is animated but not visible.
* A container won't show any of its children if set visible to false.
*
* @param visible {boolean} set this actor visible or not.
* @return this
*/
setVisible : function(visible) {
this.visible= visible;
return this;
},
/**
* Puts an Actor out of time line, that is, won't be transformed nor rendered.
* @return this
*/
setOutOfFrameTime : function() {
this.setFrameTime(-1,0);
return this;
},
/**
* Adds an Actor's life cycle listener.
* The developer must ensure the actorListener is not already a listener, otherwise
* it will notified more than once.
* @param actorListener {object} an object with at least a method of the form:
* <code>actorLyfeCycleEvent( actor, string_event_type, long_time )</code>
*/
addListener : function( actorListener ) {
this.lifecycleListenerList.push(actorListener);
return this;
},
/**
* Removes an Actor's life cycle listener.
* It will only remove the first occurrence of the given actorListener.
* @param actorListener {object} an Actor's life cycle listener.
*/
removeListener : function( actorListener ) {
var n= this.lifecycleListenerList.length;
while(n--) {
if ( this.lifecycleListenerList[n]===actorListener ) {
// remove the nth element.
this.lifecycleListenerList.splice(n,1);
return;
}
}
},
/**
* Set alpha composition scope. global will mean this alpha value will be its children maximum.
* If set to false, only this actor will have this alpha value.
* @param global {boolean} whether the alpha value should be propagated to children.
*/
setGlobalAlpha : function( global ) {
this.isGlobalAlpha= global;
return this;
},
/**
* Notifies the registered Actor's life cycle listener about some event.
* @param sEventType an string indicating the type of event being notified.
* @param time an integer indicating the time related to Scene's timeline when the event
* is being notified.
*/
fireEvent : function(sEventType, time) {
for( var i=0; i<this.lifecycleListenerList.length; i++ ) {
this.lifecycleListenerList[i].actorLifeCycleEvent(this, sEventType, time);
}
},
/**
* Sets this Actor as Expired.
* If this is a Container, all the contained Actors won't be nor drawn nor will receive
* any event. That is, expiring an Actor means totally taking it out the Scene's timeline.
* @param time {number} an integer indicating the time the Actor was expired at.
* @return this.
*/
setExpired : function(time) {
this.expired= true;
this.fireEvent('expired',time);
return this;
},
/**
* Enable or disable the event bubbling for this Actor.
* @param enable {boolean} a boolean indicating whether the event bubbling is enabled.
* @return this
*/
enableEvents : function( enable ) {
this.mouseEnabled= enable;
return this;
},
/**
* Removes all behaviors from an Actor.
* @return this
*/
emptyBehaviorList : function() {
this.behaviorList=[];
return this;
},
/**
* Caches a fillStyle in the Actor.
* @param style a valid Canvas rendering context fillStyle.
* @return this
*/
setFillStyle : function( style ) {
this.fillStyle= style;
return this;
},
/**
* Caches a stroke style in the Actor.
* @param style a valid canvas rendering context stroke style.
* @return this
*/
setStrokeStyle : function( style ) {
this.strokeStyle= style;
return this;
},
/**
* @deprecated
* @param paint
*/
setPaint : function( paint ) {
return this.setFillStyle(paint);
},
/**
* Stablishes the Alpha transparency for the Actor.
* If it globalAlpha enabled, this alpha will the maximum alpha for every contained actors.
* The alpha must be between 0 and 1.
* @param alpha a float indicating the alpha value.
* @return this
*/
setAlpha : function( alpha ) {
this.alpha= alpha;
this.invalidate();
return this;
},
/**
* Remove all transformation values for the Actor.
* @return this
*/
resetTransform : function () {
this.rotationAngle=0;
this.rotationX=.5;
this.rotationY=.5;
this.scaleX=1;
this.scaleY=1;
this.scaleTX=.5;
this.scaleTY=.5;
this.scaleAnchor=0;
this.oldX=-1;
this.oldY=-1;
this.dirty= true;
return this;
},
/**
* Sets the time life cycle for an Actor.
* These values are related to Scene time.
* @param startTime an integer indicating the time until which the Actor won't be visible on the Scene.
* @param duration an integer indicating how much the Actor will last once visible.
* @return this
*/
setFrameTime : function( startTime, duration ) {
this.start_time= startTime;
this.duration= duration;
this.expired= false;
this.dirty= true;
return this;
},
/**
* This method should me overriden by every custom Actor.
* It will be the drawing routine called by the Director to show every Actor.
* @param director the CAAT.Director instance that contains the Scene the Actor is in.
* @param time an integer indicating the Scene time in which the drawing is performed.
*/
paint : function(director, time) {
if ( this.backgroundImage ) {
this.backgroundImage.paint(director,time,0,0);
} else if ( this.fillStyle ) {
var ctx= director.crc;
ctx.fillStyle= this.fillStyle;
ctx.fillRect(0,0,this.width,this.height );
}
},
/**
* A helper method to setScaleAnchored with an anchor of ANCHOR_CENTER
*
* @see setScaleAnchored
*
* @param sx a float indicating a width size multiplier.
* @param sy a float indicating a height size multiplier.
* @return this
*/
setScale : function( sx, sy ) {
this.scaleX=sx;
this.scaleY=sy;
this.dirty= true;
return this;
},
flipX : function() {
this.setScale(-this.scaleX,this.scaleY);
},
getAnchorPercent : function( anchor ) {
var anchors=[
.50,.50, .50,0, .50,1.00,
0,.50, 1.00,.50, 0,0,
1.00,0, 0,1.00, 1.00,1.00
];
return { x: anchors[anchor*2], y: anchors[anchor*2+1] };
},
/**
* Private.
* Gets a given anchor position referred to the Actor.
* @param anchor
* @return an object of the form { x: float, y: float }
*/
getAnchor : function( anchor ) {
var tx=0, ty=0;
switch( anchor ) {
case this.ANCHOR_CENTER:
tx= .5;
ty= .5;
break;
case this.ANCHOR_TOP:
tx= .5;
ty= 0;
break;
case this.ANCHOR_BOTTOM:
tx= .5;
ty= 1;
break;
case this.ANCHOR_LEFT:
tx= 0;
ty= .5;
break;
case this.ANCHOR_RIGHT:
tx= 1;
ty= .5;
break;
case this.ANCHOR_TOP_RIGHT:
tx= 1;
ty= 0;
break;
case this.ANCHOR_BOTTOM_LEFT:
tx= 0;
ty= 1;
break;
case this.ANCHOR_BOTTOM_RIGHT:
tx= 1;
ty= 1;
break;
case this.ANCHOR_TOP_LEFT:
tx= 0;
ty= 0;
break;
}
return {x: tx, y: ty};
},
setGlobalAnchor : function( ax, ay ) {
this.tAnchorX= ax;
this.rotationX= ax;
this.scaleTX= ax;
this.tAnchorY= ay;
this.rotationY= ay;
this.scaleTY= ay;
this.dirty= true;
return this;
},
setScaleAnchor : function( sax, say ) {
this.scaleTX= sax;
this.scaleTY= say;
this.dirty= true;
return this;
},
/**
* Modify the dimensions on an Actor.
* The dimension will not affect the local coordinates system in opposition
* to setSize or setBounds.
*
* @param sx {number} width scale.
* @param sy {number} height scale.
* @param anchorx {number} x anchor to perform the Scale operation.
* @param anchory {number} y anchor to perform the Scale operation.
*
* @return this;
*/
setScaleAnchored : function( sx, sy, anchorx, anchory ) {
this.scaleTX= anchorx;
this.scaleTY= anchory;
this.scaleX=sx;
this.scaleY=sy;
this.dirty= true;
return this;
},
setRotationAnchor : function( rax, ray ) {
this.rotationX= ray;
this.rotationY= rax;
this.dirty= true;
return this;
},
/**
* A helper method for setRotationAnchored. This methods stablishes the center
* of rotation to be the center of the Actor.
*
* @param angle a float indicating the angle in radians to rotate the Actor.
* @return this
*/
setRotation : function( angle ) {
this.rotationAngle= angle;
this.dirty= true;
return this;
},
/**
* This method sets Actor rotation around a given position.
* @param angle {number} indicating the angle in radians to rotate the Actor.
* @param rx {number} value in the range 0..1
* @param ry {number} value in the range 0..1
* @return this;
*/
setRotationAnchored : function( angle, rx, ry ) {
this.rotationAngle= angle;
this.rotationX= rx;
this.rotationY= ry;
this.dirty= true;
return this;
},
/**
* Sets an Actor's dimension
* @param w a float indicating Actor's width.
* @param h a float indicating Actor's height.
* @return this
*/
setSize : function( w, h ) {
this.width= w|0;
this.height= h|0;
this.dirty= true;
return this;
},
/**
* Set location and dimension of an Actor at once.
*
* @param x{number} a float indicating Actor's x position.
* @param y{number} a float indicating Actor's y position
* @param w{number} a float indicating Actor's width
* @param h{number} a float indicating Actor's height
* @return this
*/
setBounds : function(x, y, w, h) {
/*
this.x= x|0;
this.y= y|0;
this.width= w|0;
this.height= h|0;
*/
this.x= x;
this.y= y;
this.width= w;
this.height= h;
this.dirty= true;
return this;
},
setFullBounds : function(director) {
this.x = 0;
this.y = 0;
this.width = director.width;
this.height = director.height;
this.dirty = true;
return this;
},
/**
* This method sets the position of an Actor inside its parent.
*
* @param x{number} a float indicating Actor's x position
* @param y{number} a float indicating Actor's y position
* @return this
*
* @deprecated
*/
setLocation : function( x, y ) {
this.x= x;
this.y= y;
this.oldX= x;
this.oldY= y;
this.dirty= true;
return this;
},
setPosition : function( x,y ) {
return this.setLocation( x,y );
},
setPositionAnchor : function( pax, pay ) {
this.tAnchorX= pax;
this.tAnchorY= pay;
return this;
},
setPositionAnchored : function( x,y,pax,pay ) {
this.setLocation( x,y );
this.tAnchorX= pax;
this.tAnchorY= pay;
return this;
},
/**
* This method is called by the Director to know whether the actor is on Scene time.
* In case it was necessary, this method will notify any life cycle behaviors about
* an Actor expiration.
* @param time {number} time indicating the Scene time.
*
* @private
*
*/
isInAnimationFrame : function(time) {
if ( this.expired ) {
return false;
}
if ( this.duration===Number.MAX_VALUE ) {
return this.start_time<=time;
}
if ( time>=this.start_time+this.duration ) {
if ( !this.expired ) {
this.setExpired(time);
}
return false;
}
return this.start_time<=time && time<this.start_time+this.duration;
},
/**
* Checks whether a coordinate is inside the Actor's bounding box.
* @param x {number} a float
* @param y {number} a float
*
* @return boolean indicating whether it is inside.
*/
contains : function(x, y) {
return x>=0 && y>=0 && x<this.width && y<this.height;
},
/**
* This method must be called explicitly by every CAAT Actor.
* Making the life cycle explicitly initiated has always been a good idea.
*
* @return this
* @deprecated no longer needed.
*/
create : function() {
return this;
},
/**
* Add a Behavior to the Actor.
* An Actor accepts an undefined number of Behaviors.
*
* @param behavior {CAAT.Behavior} a CAAT.Behavior instance
* @return this
*
* @deprecated
*/
addBehavior : function( behavior ) {
this.behaviorList.push(behavior);
return this;
},
/**
* Remove a Behavior from the Actor.
* If the Behavior is not present at the actor behavior collection nothing happends.
*
* @param behavior {CAAT.Behavior} a CAAT.Behavior instance.
*/
removeBehaviour : function( behavior ) {
var c= this.behaviorList;
var n= c.length-1;
while(n) {
if ( c[n]===behavior ) {
c.splice(n,1);
return this;
}
}
return this;
},
/**
* Remove a Behavior with id param as behavior identifier from this actor.
* This function will remove ALL behavior instances with the given id.
*
* @param id {number} an integer.
* return this;
*/
removeBehaviorById : function( id ) {
var c= this.behaviorList;
for( var n=0; n<c.length; n++ ) {
if ( c[n].id===id) {
c.splice(n,1);
}
}
return this;
},
getBehavior : function(id) {
var c= this.behaviorList;
for( var n=0; n<c.length; n++ ) {
var cc= c[n];
if ( cc.id===id) {
return cc;
}
}
return null;
},
/**
* Set discardable property. If an actor is discardable, upon expiration will be removed from
* scene graph and hence deleted.
* @param discardable {boolean} a boolean indicating whether the Actor is discardable.
* @return this
*/
setDiscardable : function( discardable ) {
this.discardable= discardable;
return this;
},
/**
* This method will be called internally by CAAT when an Actor is expired, and at the
* same time, is flagged as discardable.
* It notifies the Actor life cycle listeners about the destruction event.
*
* @param time an integer indicating the time at wich the Actor has been destroyed.
*
* @private
*
*/
destroy : function(time) {
if ( this.parent ) {
this.parent.removeChild(this);
}
this.fireEvent('destroyed',time);
},
/**
* Transform a point or array of points in model space to view space.
*
* @param point {CAAT.Point|Array} an object of the form {x : float, y: float}
*
* @return the source transformed elements.
*
* @private
*
*/
modelToView : function(point) {
if ( this.dirty ) {
this.setModelViewMatrix();
}
var tm= this.worldModelViewMatrix.matrix;
if ( point instanceof Array ) {
for( var i=0; i<point.length; i++ ) {
//this.worldModelViewMatrix.transformCoord(point[i]);
var pt= point[i];
var x= pt.x;
var y= pt.y;
pt.x= x*tm[0] + y*tm[1] + tm[2];
pt.y= x*tm[3] + y*tm[4] + tm[5];
}
}
else {
// this.worldModelViewMatrix.transformCoord(point);
var x= point.x;
var y= point.y;
point.x= x*tm[0] + y*tm[1] + tm[2];
point.y= x*tm[3] + y*tm[4] + tm[5];
}
return point;
},
/**
* Transform a local coordinate point on this Actor's coordinate system into
* another point in otherActor's coordinate system.
* @param point {CAAT.Point}
* @param otherActor {CAAT.Actor}
*/
modelToModel : function( point, otherActor ) {
if ( this.dirty ) {
this.setModelViewMatrix();
}
return otherActor.viewToModel( this.modelToView( point ) );
},