forked from cocos2d/cocos2d-html5
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCCSprite.js
2216 lines (1928 loc) · 80.9 KB
/
CCSprite.js
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 (c) 2010-2012 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011 Zynga Inc.
http://www.cocos2d-x.org
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.
****************************************************************************/
/**
* cc.Sprite invalid index on the cc.SpriteBatchNode
* @constant
* @type Number
*/
cc.SPRITE_INDEX_NOT_INITIALIZED = -1;
/**
* generate texture's cache for texture tint
* @function
* @param {HTMLImageElement} texture
* @return {Array}
*/
cc.generateTextureCacheForColor = function (texture) {
if (texture.hasOwnProperty('channelCache')) {
return texture.channelCache;
}
var textureCache = [
document.createElement("canvas"),
document.createElement("canvas"),
document.createElement("canvas"),
document.createElement("canvas")
];
function renderToCache() {
var ref = cc.generateTextureCacheForColor;
var w = texture.width;
var h = texture.height;
textureCache[0].width = w;
textureCache[0].height = h;
textureCache[1].width = w;
textureCache[1].height = h;
textureCache[2].width = w;
textureCache[2].height = h;
textureCache[3].width = w;
textureCache[3].height = h;
ref.canvas.width = w;
ref.canvas.height = h;
var ctx = ref.canvas.getContext("2d");
ctx.drawImage(texture, 0, 0);
ref.tempCanvas.width = w;
ref.tempCanvas.height = h;
var pixels = ctx.getImageData(0, 0, w, h).data;
for (var rgbI = 0; rgbI < 4; rgbI++) {
var cacheCtx = textureCache[rgbI].getContext('2d');
cacheCtx.getImageData(0, 0, w, h).data;
ref.tempCtx.drawImage(texture, 0, 0);
var to = ref.tempCtx.getImageData(0, 0, w, h);
var toData = to.data;
for (var i = 0; i < pixels.length; i += 4) {
toData[i ] = (rgbI === 0) ? pixels[i ] : 0;
toData[i + 1] = (rgbI === 1) ? pixels[i + 1] : 0;
toData[i + 2] = (rgbI === 2) ? pixels[i + 2] : 0;
toData[i + 3] = pixels[i + 3];
}
cacheCtx.putImageData(to, 0, 0);
}
texture.onload = null;
}
try {
renderToCache();
} catch (e) {
texture.onload = renderToCache;
}
texture.channelCache = textureCache;
return textureCache;
};
cc.generateTextureCacheForColor.canvas = document.createElement('canvas');
cc.generateTextureCacheForColor.tempCanvas = document.createElement('canvas');
cc.generateTextureCacheForColor.tempCtx = cc.generateTextureCacheForColor.tempCanvas.getContext('2d');
/**
* generate tinted texture
* source-in: Where source and destination overlaps and both are opaque, the source is displayed.
* Everywhere else transparency is displayed.
* @function
* @param {HTMLImageElement} texture
* @param {cc.Color3B|cc.Color4F} color
* @param {cc.Rect} rect
* @return {HTMLCanvasElement}
*/
cc.generateTintImage2 = function (texture, color, rect) {
if (!rect) {
rect = cc.rect(0, 0, texture.width, texture.height);
rect = cc.RECT_PIXELS_TO_POINTS(rect);
}
var selColor;
if (color instanceof cc.Color4F) {
selColor = cc.c4b(color.r * 255, color.g * 255, color.b * 255, color.a * 255);
} else {
selColor = cc.c4b(color.r, color.g, color.b, 50);//color;
}
var buff = document.createElement("canvas");
var ctx = buff.getContext("2d");
if (buff.width != rect.width) buff.width = rect.width;
if (buff.height != rect.height) buff.height = rect.height;
ctx.save();
ctx.drawImage(texture, rect.x, rect.y, rect.width, rect.height, 0, 0, rect.width, rect.height);
ctx.globalCompositeOperation = "source-in";
ctx.globalAlpha = selColor.a / 255.0;
ctx.fillStyle = "rgb(" + selColor.r + "," + selColor.g + "," + selColor.b + ")";
ctx.fillRect(0, 0, rect.width, rect.height);
ctx.restore();
return buff;
};
/**
* generate tinted texture
* lighter: The source and destination colors are added to each other, resulting in brighter colors,
* moving towards color values of 1 (maximum brightness for that color).
* @function
* @param {HTMLImageElement} texture
* @param {Array} tintedImgCache
* @param {cc.Color3B|cc.Color4F} color
* @param {cc.Rect} rect
* @param {HTMLCanvasElement} [renderCanvas]
* @return {HTMLCanvasElement}
*/
cc.generateTintImage = function (texture, tintedImgCache, color, rect, renderCanvas) {
if (!rect)
rect = cc.rect(0, 0, texture.width, texture.height);
var selColor;
if (color.a == null) {
// Optimization for the particle system which mainly uses c4f colors
selColor = cc.c4f(color.r / 255.0, color.g / 255.0, color.b / 255, 1);
} else {
selColor = color;
}
var w = Math.min(rect.width, tintedImgCache[0].width);
var h = Math.min(rect.height, tintedImgCache[0].height);
var buff = renderCanvas;
var ctx;
// Create a new buffer if required
if (!buff) {
buff = document.createElement("canvas");
buff.width = w;
buff.height = h;
ctx = buff.getContext("2d");
} else {
ctx = buff.getContext("2d");
ctx.clearRect(0, 0, w, h);
}
ctx.save();
ctx.globalCompositeOperation = 'lighter';
// Make sure to keep the renderCanvas alpha in mind in case of overdraw
var a = ctx.globalAlpha;
if (selColor.r > 0) {
ctx.globalAlpha = selColor.r * a;
ctx.drawImage(tintedImgCache[0], rect.x, rect.y, w, h, 0, 0, w, h);
}
if (selColor.g > 0) {
ctx.globalAlpha = selColor.g * a;
ctx.drawImage(tintedImgCache[1], rect.x, rect.y, w, h, 0, 0, w, h);
}
if (selColor.b > 0) {
ctx.globalAlpha = selColor.b * a;
ctx.drawImage(tintedImgCache[2], rect.x, rect.y, w, h, 0, 0, w, h);
}
if ((selColor.r === 0) && (selColor.g === 0) && (selColor.b === 0)) {
ctx.globalAlpha = a;
ctx.drawImage(tintedImgCache[3], rect.x, rect.y, w, h, 0, 0, w, h);
}
ctx.restore();
return buff;
};
cc.cutRotateImageToCanvas = function (texture, rect) {
if (!texture)
return null;
if (!rect)
return texture;
var nCanvas = document.createElement("canvas");
nCanvas.width = rect.width;
nCanvas.height = rect.height;
var ctx = nCanvas.getContext("2d");
ctx.translate(nCanvas.width / 2, nCanvas.height / 2);
ctx.rotate(-1.5707963267948966);
ctx.drawImage(texture, rect.x, rect.y, rect.height, rect.width, -rect.height / 2, -rect.width / 2, rect.height, rect.width);
return nCanvas;
};
/**
* a Values object for transform
* @Class
* @Construct
* @param {cc.Point} pos position x and y
* @param {cc.Point} scale scale x and y
* @param {Number} rotation
* @param {cc.Point} skew skew x and y
* @param {cc.Point} ap anchor point in pixels
* @param {Boolean} visible
*/
cc.TransformValues = function (pos, scale, rotation, skew, ap, visible) {
this.pos = pos; // position x and y
this.scale = scale; // scale x and y
this.rotation = rotation;
this.skew = skew; // skew x and y
this.ap = ap; // anchor point in pixels
this.visible = visible;
};
cc.RENDER_IN_SUBPIXEL = function (A) {
return (0 | A);
};
if (cc.SPRITEBATCHNODE_RENDER_SUBPIXEL) {
cc.RENDER_IN_SUBPIXEL = function (A) {
return A;
};
}
/**
* <p>cc.Sprite is a 2d image ( http://en.wikipedia.org/wiki/Sprite_(computer_graphics) ) <br/>
*
* cc.Sprite can be created with an image, or with a sub-rectangle of an image. <br/>
*
* If the parent or any of its ancestors is a cc.SpriteBatchNode then the following features/limitations are valid <br/>
* - Features when the parent is a cc.BatchNode: <br/>
* - MUCH faster rendering, specially if the cc.SpriteBatchNode has many children. All the children will be drawn in a single batch. <br/>
*
* - Limitations <br/>
* - Camera is not supported yet (eg: CCOrbitCamera action doesn't work) <br/>
* - GridBase actions are not supported (eg: CCLens, CCRipple, CCTwirl) <br/>
* - The Alias/Antialias property belongs to CCSpriteBatchNode, so you can't individually set the aliased property. <br/>
* - The Blending function property belongs to CCSpriteBatchNode, so you can't individually set the blending function property. <br/>
* - Parallax scroller is not supported, but can be simulated with a "proxy" sprite. <br/>
*
* If the parent is an standard cc.Node, then cc.Sprite behaves like any other cc.Node: <br/>
* - It supports blending functions <br/>
* - It supports aliasing / antialiasing <br/>
* - But the rendering will be slower: 1 draw per children. <br/>
*
* The default anchorPoint in cc.Sprite is (0.5, 0.5). </p>
* @class
* @extends cc.NodeRGBA
*
* @example
* var aSprite = new cc.Sprite();
* aSprite.initWithFile("HelloHTML5World.png",cc.rect(0,0,480,320));
*/
cc.Sprite = cc.NodeRGBA.extend(/** @lends cc.Sprite# */{
RGBAProtocol:true,
//
// Data used when the sprite is rendered using a CCSpriteSheet
//
_textureAtlas:null, //cc.SpriteBatchNode texture atlas
_atlasIndex:0,
_batchNode:null,
_dirty:false, //Whether the sprite needs to be updated
_recursiveDirty:null, //Whether all of the sprite's children needs to be updated
_hasChildren:null, //Whether the sprite contains children
_shouldBeHidden:false, //should not be drawn because one of the ancestors is not visible
_transformToBatch:null,
//
// Data used when the sprite is self-rendered
//
_blendFunc:null, //It's required for CCTextureProtocol inheritance
_texture:null, //cc.Texture2D object that is used to render the sprite
//
// Shared data
//
// texture
_rect:cc.rect(0, 0, 0, 0), //Retangle of cc.Texture2D
_rectRotated:false, //Whether the texture is rotated
// Offset Position (used by Zwoptex)
_offsetPosition:null, // absolute
_unflippedOffsetPositionFromCenter:null,
_opacityModifyRGB:false,
// image is flipped
_flippedX:false, //Whether the sprite is flipped horizontally or not.
_flippedY:false, //Whether the sprite is flipped vertically or not.
_textureLoaded:false,
_loadedEventListeners: null,
_newTextureWhenChangeColor: null, //hack property for LabelBMFont
textureLoaded:function(){
return this._textureLoaded;
},
addLoadedEventListener:function(callback, target){
this._loadedEventListeners.push({eventCallback:callback, eventTarget:target});
},
_callLoadedEventCallbacks:function(){
var locListeners = this._loadedEventListeners;
for(var i = 0, len = locListeners.length; i < len; i++){
var selCallback = locListeners[i];
selCallback.eventCallback.call(selCallback.eventTarget, this);
}
locListeners.length = 0;
},
/**
* Whether or not the Sprite needs to be updated in the Atlas
* @return {Boolean} true if the sprite needs to be updated in the Atlas, false otherwise.
*/
isDirty:function () {
return this._dirty;
},
/**
* Makes the Sprite to be updated in the Atlas.
* @param {Boolean} bDirty
*/
setDirty:function (bDirty) {
this._dirty = bDirty;
},
/**
* Returns whether or not the texture rectangle is rotated.
* @return {Boolean}
*/
isTextureRectRotated:function () {
return this._rectRotated;
},
/**
* Returns the index used on the TextureAtlas.
* @return {Number}
*/
getAtlasIndex:function () {
return this._atlasIndex;
},
/**
* Set the index used on the TextureAtlas.
* @warning Don't modify this value unless you know what you are doing
* @param {Number} atlasIndex
*/
setAtlasIndex:function (atlasIndex) {
this._atlasIndex = atlasIndex;
},
/**
* returns the rect of the cc.Sprite in points
* @return {cc.Rect}
*/
getTextureRect:function () {
return cc.rect(this._rect.x, this._rect.y, this._rect.width, this._rect.height);
},
/**
* Gets the weak reference of the cc.TextureAtlas when the sprite is rendered using via cc.SpriteBatchNode
* @return {cc.TextureAtlas}
*/
getTextureAtlas:function () {
return this._textureAtlas;
},
/**
* Sets the weak reference of the cc.TextureAtlas when the sprite is rendered using via cc.SpriteBatchNode
* @param {cc.TextureAtlas} textureAtlas
*/
setTextureAtlas:function (textureAtlas) {
this._textureAtlas = textureAtlas;
},
/**
* return the SpriteBatchNode of the cc.Sprite
* @return {cc.SpriteBatchNode}
*/
getSpriteBatchNode:function () {
return this._batchNode;
},
/**
* set the SpriteBatchNode of the cc.Sprite
* @param {cc.SpriteBatchNode} spriteBatchNode
*/
setSpriteBatchNode:function (spriteBatchNode) {
this._batchNode = spriteBatchNode;
},
/**
* Gets the offset position of the sprite. Calculated automatically by editors like Zwoptex.
* @return {cc.Point}
*/
getOffsetPosition:function () {
return cc.p(this._offsetPosition.x, this._offsetPosition.y);
},
/**
* conforms to cc.TextureProtocol protocol
* @return {cc.BlendFunc}
*/
getBlendFunc:function () {
return this._blendFunc;
},
/**
* Initializes a sprite with an SpriteFrame. The texture and rect in SpriteFrame will be applied on this sprite
* @param {cc.SpriteFrame} spriteFrame A CCSpriteFrame object. It should includes a valid texture and a rect
* @return {Boolean} true if the sprite is initialized properly, false otherwise.
* @example
* var spriteFrame = cc.SpriteFrameCache.getInstance().getSpriteFrame("grossini_dance_01.png");
* var sprite = new cc.Sprite();
* sprite.initWithSpriteFrame(spriteFrame);
*/
initWithSpriteFrame:function (spriteFrame) {
cc.Assert(spriteFrame != null, "");
if(!spriteFrame.textureLoaded()){
//add event listener
this._textureLoaded = false;
spriteFrame.addLoadedEventListener(this._spriteFrameLoadedCallback, this);
}
var ret = this.initWithTexture(spriteFrame.getTexture(), spriteFrame.getRect());
this.setDisplayFrame(spriteFrame);
return ret;
},
_spriteFrameLoadedCallback:null,
_spriteFrameLoadedCallbackForWebGL:function(spriteFrame){
this.setNodeDirty();
this.setTextureRect(spriteFrame.getRect(), spriteFrame.isRotated(), spriteFrame.getOriginalSize());
this._callLoadedEventCallbacks();
},
_spriteFrameLoadedCallbackForCanvas:function(spriteFrame){
this.setNodeDirty();
this.setTextureRect(spriteFrame.getRect(), spriteFrame.isRotated(), spriteFrame.getOriginalSize());
var curColor = this.getColor();
if (curColor.r !== 255 || curColor.g !== 255 || curColor.b !== 255)
this._changeTextureColor();
this._callLoadedEventCallbacks();
},
/**
* Initializes a sprite with a sprite frame name. <br/>
* A cc.SpriteFrame will be fetched from the cc.SpriteFrameCache by name. <br/>
* If the cc.SpriteFrame doesn't exist it will raise an exception. <br/>
* @param {String} spriteFrameName A key string that can fected a volid cc.SpriteFrame from cc.SpriteFrameCache
* @return {Boolean} true if the sprite is initialized properly, false otherwise.
* @example
* var sprite = new cc.Sprite();
* sprite.initWithSpriteFrameName("grossini_dance_01.png");
*/
initWithSpriteFrameName:function (spriteFrameName) {
cc.Assert(spriteFrameName != null, "");
var frame = cc.SpriteFrameCache.getInstance().getSpriteFrame(spriteFrameName);
return this.initWithSpriteFrame(frame);
},
/**
* tell the sprite to use batch node render.
* @param {cc.SpriteBatchNode} batchNode
*/
useBatchNode:function (batchNode) {
this._textureAtlas = batchNode.getTextureAtlas(); // weak ref
this._batchNode = batchNode;
},
/**
* <p>
* set the vertex rect.<br/>
* It will be called internally by setTextureRect. <br/>
* Useful if you want to create 2x images from SD images in Retina Display. <br/>
* Do not call it manually. Use setTextureRect instead. <br/>
* (override this method to generate "double scale" sprites)
* </p>
* @param {cc.Rect} rect
*/
setVertexRect:function (rect) {
this._rect = rect;
},
sortAllChildren:function () {
if (this._reorderChildDirty) {
var j, tempItem, locChildren = this._children, tempChild;
for (var i = 1; i < locChildren.length; i++) {
tempItem = locChildren[i];
j = i - 1;
tempChild = locChildren[j];
//continue moving element downwards while zOrder is smaller or when zOrder is the same but mutatedIndex is smaller
while (j >= 0 && ( tempItem._zOrder < tempChild._zOrder ||
( tempItem._zOrder == tempChild._zOrder && tempItem._orderOfArrival < tempChild._orderOfArrival ))) {
locChildren[j + 1] = tempChild;
j = j - 1;
tempChild = locChildren[j];
}
locChildren[j + 1] = tempItem;
}
if (this._batchNode) {
this._arrayMakeObjectsPerformSelector(locChildren, cc.Node.StateCallbackType.sortAllChildren);
}
this._reorderChildDirty = false;
}
},
/**
* Reorders a child according to a new z value. (override cc.Node )
* @param {cc.Node} child
* @param {Number} zOrder
* @override
*/
reorderChild:function (child, zOrder) {
cc.Assert(child != null, "child is null");
cc.Assert(this._children.indexOf(child) > -1, "this child is not in children list");
if (zOrder === child.getZOrder())
return;
if (this._batchNode && !this._reorderChildDirty) {
this._setReorderChildDirtyRecursively();
this._batchNode.reorderBatch(true);
}
cc.Node.prototype.reorderChild.call(this, child, zOrder);
},
/**
* Removes a child from the sprite. (override cc.Node )
* @param child
* @param cleanup whether or not cleanup all running actions
* @override
*/
removeChild:function (child, cleanup) {
if (this._batchNode)
this._batchNode.removeSpriteFromAtlas(child);
cc.Node.prototype.removeChild.call(this, child, cleanup);
},
/**
* Removes all children from the container (override cc.Node )
* @param cleanup whether or not cleanup all running actions
* @override
*/
removeAllChildren:function (cleanup) {
var locChildren = this._children, locBatchNode = this._batchNode;
if (locBatchNode && locChildren != null) {
for (var i = 0, len = locChildren.length; i < len; i++)
locBatchNode.removeSpriteFromAtlas(locChildren[i]);
}
cc.Node.prototype.removeAllChildren.call(this, cleanup);
this._hasChildren = false;
},
//
// cc.Node property overloads
//
/**
* set Recursively is or isn't Dirty
* used only when parent is cc.SpriteBatchNode
* @param {Boolean} value
*/
setDirtyRecursively:function (value) {
this._recursiveDirty = value;
this.setDirty(value);
// recursively set dirty
var locChildren = this._children;
if (locChildren != null) {
for (var i = 0; i < locChildren.length; i++) {
if (locChildren[i] instanceof cc.Sprite)
locChildren[i].setDirtyRecursively(true);
}
}
},
/**
* HACK: optimization
*/
SET_DIRTY_RECURSIVELY:function () {
if (this._batchNode && !this._recursiveDirty) {
this._recursiveDirty = true;
this._dirty = true;
if (this._hasChildren)
this.setDirtyRecursively(true);
}
},
/**
* position setter (override cc.Node )
* @param {cc.Point} pos
* @override
*/
setPosition:function (pos) {
if (arguments.length >= 2)
cc.Node.prototype.setPosition.call(this, pos, arguments[1]);
else
cc.Node.prototype.setPosition.call(this, pos);
this.SET_DIRTY_RECURSIVELY();
},
/**
* Rotation setter (override cc.Node )
* @param {Number} rotation
* @override
*/
setRotation:function (rotation) {
cc.Node.prototype.setRotation.call(this, rotation);
this.SET_DIRTY_RECURSIVELY();
},
setRotationX:function (rotationX) {
cc.Node.prototype.setRotationX.call(this, rotationX);
this.SET_DIRTY_RECURSIVELY();
},
setRotationY:function (rotationY) {
cc.Node.prototype.setRotationY.call(this, rotationY);
this.SET_DIRTY_RECURSIVELY();
},
/**
* SkewX setter (override cc.Node )
* @param {Number} sx SkewX value
* @override
*/
setSkewX:function (sx) {
cc.Node.prototype.setSkewX.call(this, sx);
this.SET_DIRTY_RECURSIVELY();
},
/**
* SkewY setter (override cc.Node )
* @param {Number} sy SkewY value
* @override
*/
setSkewY:function (sy) {
cc.Node.prototype.setSkewY.call(this, sy);
this.SET_DIRTY_RECURSIVELY();
},
/**
* ScaleX setter (override cc.Node )
* @param {Number} scaleX
* @override
*/
setScaleX:function (scaleX) {
cc.Node.prototype.setScaleX.call(this, scaleX);
this.SET_DIRTY_RECURSIVELY();
},
/**
* ScaleY setter (override cc.Node )
* @param {Number} scaleY
* @override
*/
setScaleY:function (scaleY) {
cc.Node.prototype.setScaleY.call(this, scaleY);
this.SET_DIRTY_RECURSIVELY();
},
/**
* <p>The scale factor of the node. 1.0 is the default scale factor. <br/>
* It modifies the X and Y scale at the same time. (override cc.Node ) <p/>
* @param {Number} scale
* @param {Number|null} [scaleY=]
* @override
*/
setScale:function (scale, scaleY) {
cc.Node.prototype.setScale.call(this, scale, scaleY);
this.SET_DIRTY_RECURSIVELY();
},
/**
* VertexZ setter (override cc.Node )
* @param {Number} vertexZ
* @override
*/
setVertexZ:function (vertexZ) {
cc.Node.prototype.setVertexZ.call(this, vertexZ);
this.SET_DIRTY_RECURSIVELY();
},
/**
* AnchorPoint setter (override cc.Node )
* @param {cc.Point} anchor
* @override
*/
setAnchorPoint:function (anchor) {
cc.Node.prototype.setAnchorPoint.call(this, anchor);
this.SET_DIRTY_RECURSIVELY();
},
/**
* visible setter (override cc.Node )
* @param {Boolean} visible
* @override
*/
setVisible:function (visible) {
cc.Node.prototype.setVisible.call(this, visible);
this.SET_DIRTY_RECURSIVELY();
},
/**
* IsRelativeAnchorPoint setter (override cc.Node )
* @param {Boolean} relative
* @override
*/
ignoreAnchorPointForPosition:function (relative) {
cc.Assert(!this._batchNode, "ignoreAnchorPointForPosition is invalid in cc.Sprite");
cc.Node.prototype.ignoreAnchorPointForPosition.call(this, relative);
},
/**
* Sets whether the sprite should be flipped horizontally or not.
* @param {Boolean} flippedX true if the sprite should be flipped horizontally, false otherwise.
*/
setFlippedX:function (flippedX) {
if (this._flippedX != flippedX) {
this._flippedX = flippedX;
this.setTextureRect(this._rect, this._rectRotated, this._contentSize);
this.setNodeDirty();
}
},
/**
* Sets whether the sprite should be flipped vertically or not.
* @param {Boolean} flippedY true if the sprite should be flipped vertically, false otherwise.
*/
setFlippedY:function (flippedY) {
if (this._flippedY != flippedY) {
this._flippedY = flippedY;
this.setTextureRect(this._rect, this._rectRotated, this._contentSize);
this.setNodeDirty();
}
},
/**
* <p>
* Returns the flag which indicates whether the sprite is flipped horizontally or not. <br/>
* <br/>
* It only flips the texture of the sprite, and not the texture of the sprite's children. <br/>
* Also, flipping the texture doesn't alter the anchorPoint. <br/>
* If you want to flip the anchorPoint too, and/or to flip the children too use: <br/>
* sprite->setScaleX(sprite->getScaleX() * -1); <p/>
* @return {Boolean} true if the sprite is flipped horizaontally, false otherwise.
*/
isFlippedX:function () {
return this._flippedX;
},
/**
* <p>
* Return the flag which indicates whether the sprite is flipped vertically or not. <br/>
* <br/>
* It only flips the texture of the sprite, and not the texture of the sprite's children. <br/>
* Also, flipping the texture doesn't alter the anchorPoint. <br/>
* If you want to flip the anchorPoint too, and/or to flip the children too use: <br/>
* sprite->setScaleY(sprite->getScaleY() * -1); <p/>
* @return {Boolean} true if the sprite is flipped vertically, flase otherwise.
*/
isFlippedY:function () {
return this._flippedY;
},
//
// RGBA protocol
//
/**
* opacity: conforms to CCRGBAProtocol protocol
* @param {Boolean} modify
*/
setOpacityModifyRGB:null,
_setOpacityModifyRGBForWebGL: function (modify) {
if (this._opacityModifyRGB !== modify) {
this._opacityModifyRGB = modify;
this.updateColor();
}
},
_setOpacityModifyRGBForCanvas: function (modify) {
if (this._opacityModifyRGB !== modify) {
this._opacityModifyRGB = modify;
this.setNodeDirty();
}
},
/**
* return IsOpacityModifyRGB value
* @return {Boolean}
*/
isOpacityModifyRGB:function () {
return this._opacityModifyRGB;
},
updateDisplayedOpacity: null,
_updateDisplayedOpacityForWebGL:function (parentOpacity) {
cc.NodeRGBA.prototype.updateDisplayedOpacity.call(this, parentOpacity);
this.updateColor();
},
_updateDisplayedOpacityForCanvas:function (parentOpacity) {
cc.NodeRGBA.prototype.updateDisplayedOpacity.call(this, parentOpacity);
this._changeTextureColor();
this.setNodeDirty();
},
// Animation
/**
* changes the display frame with animation name and index.<br/>
* The animation name will be get from the CCAnimationCache
* @param animationName
* @param frameIndex
*/
setDisplayFrameWithAnimationName:function (animationName, frameIndex) {
cc.Assert(animationName, "cc.Sprite#setDisplayFrameWithAnimationName. animationName must not be null");
var cache = cc.AnimationCache.getInstance().getAnimation(animationName);
cc.Assert(cache, "cc.Sprite#setDisplayFrameWithAnimationName: Frame not found");
var animFrame = cache.getFrames()[frameIndex];
cc.Assert(animFrame, "cc.Sprite#setDisplayFrame. Invalid frame");
this.setDisplayFrame(animFrame.getSpriteFrame());
},
/**
* Returns the batch node object if this sprite is rendered by cc.SpriteBatchNode
* @returns {cc.SpriteBatchNode|null} The cc.SpriteBatchNode object if this sprite is rendered by cc.SpriteBatchNode, null if the sprite isn't used batch node.
*/
getBatchNode:function () {
return this._batchNode;
},
_setReorderChildDirtyRecursively:function () {
//only set parents flag the first time
if (!this._reorderChildDirty) {
this._reorderChildDirty = true;
var pNode = this._parent;
while (pNode && pNode != this._batchNode) {
pNode._setReorderChildDirtyRecursively();
pNode = pNode.getParent();
}
}
},
// CCTextureProtocol
getTexture:function () {
return this._texture;
},
_quad:null, // vertex coords, texture coords and color info
_quadWebBuffer:null,
_quadDirty:false,
_colorized:false,
_isLighterMode:false,
_originalTexture:null,
/**
* Constructor
* @param {String|cc.SpriteFrame|cc.SpriteBatchNode|HTMLImageElement|cc.Texture2D} fileName sprite construct parameter
*/
ctor: null,
_ctorForWebGL: function (fileName) {
cc.NodeRGBA.prototype.ctor.call(this);
this._shouldBeHidden = false;
this._offsetPosition = cc.p(0, 0);
this._unflippedOffsetPositionFromCenter = cc.p(0, 0);
this._blendFunc = {src: cc.BLEND_SRC, dst: cc.BLEND_DST};
this._quad = new cc.V3F_C4B_T2F_Quad();
this._quadWebBuffer = cc.renderContext.createBuffer();
this._quadDirty = true;
this._textureLoaded = true;
this._loadedEventListeners = [];
if (fileName) {
if (typeof(fileName) === "string") {
var frame = cc.SpriteFrameCache.getInstance().getSpriteFrame(fileName);
this.initWithSpriteFrame(frame);
} else if (typeof(fileName) === "object") {
if (fileName instanceof cc.SpriteFrame) {
this.initWithSpriteFrame(fileName);
} else if ((fileName instanceof HTMLImageElement) || (fileName instanceof HTMLCanvasElement)) {
var texture2d = new cc.Texture2D();
texture2d.initWithElement(fileName);
texture2d.handleLoadedTexture();
this.initWithTexture(texture2d);
} else if (fileName instanceof cc.Texture2D) {
this.initWithTexture(fileName);
}
}
}
},
_ctorForCanvas: function (fileName) {
cc.NodeRGBA.prototype.ctor.call(this);
this._shouldBeHidden = false;
this._offsetPosition = cc.p(0, 0);
this._unflippedOffsetPositionFromCenter = cc.p(0, 0);
this._blendFunc = {src: cc.BLEND_SRC, dst: cc.BLEND_DST};
this._newTextureWhenChangeColor = false;
this._textureLoaded = true;
this._loadedEventListeners = [];
if (fileName) {
if (typeof(fileName) === "string") {
var frame = cc.SpriteFrameCache.getInstance().getSpriteFrame(fileName);
this.initWithSpriteFrame(frame);
} else if (typeof(fileName) === "object") {
if (fileName instanceof cc.SpriteFrame) {
this.initWithSpriteFrame(fileName);
} else if ((fileName instanceof HTMLImageElement) || (fileName instanceof HTMLCanvasElement)) {
var texture2d = new cc.Texture2D();
texture2d.initWithElement(fileName);
texture2d.handleLoadedTexture();
this.initWithTexture(texture2d);
} else if (fileName instanceof cc.Texture2D) {
this.initWithTexture(fileName);
}
}
}
},
/**
* Returns the quad (tex coords, vertex coords and color) information.
* @return {cc.V3F_C4B_T2F_Quad}
*/
getQuad:function () {
return this._quad;
},
/**
* conforms to cc.TextureProtocol protocol
* @param {Number|cc.BlendFunc} src
* @param {Number} dst
*/
setBlendFunc: null,
_setBlendFuncForWebGL: function (src, dst) {
if (arguments.length == 1)
this._blendFunc = src;
else
this._blendFunc = {src: src, dst: dst};
},
_setBlendFuncForCanvas: function (src, dst) {
if (arguments.length == 1)
this._blendFunc = src;
else