33
33
* <p> All features from cc.Node are valid, plus the following features: <br/>
34
34
* - opacity and RGB colors </p>
35
35
* @class
36
- * @extends cc.Node
36
+ * @extends cc.NodeRGBA
37
37
*/
38
- cc . AtlasNodeCanvas = cc . Node . extend ( /** @lends cc.AtlasNode# */ {
38
+ cc . AtlasNodeCanvas = cc . NodeRGBA . extend ( /** @lends cc.AtlasNode# */ {
39
39
/// ---- common properties start ----
40
40
RGBAProtocol :true ,
41
41
//! chars per row
@@ -54,18 +54,15 @@ cc.AtlasNodeCanvas = cc.Node.extend(/** @lends cc.AtlasNode# */{
54
54
_opacityModifyRGB :false ,
55
55
_blendFunc :null ,
56
56
57
- _opacity :0 ,
58
- _color :null ,
59
-
60
57
// quads to draw
61
58
_quadsToDraw :0 ,
59
+ _ignoreContentScaleFactor :false , // This variable is only used for CCLabelAtlas FPS display. So plz don't modify its value.
62
60
63
61
ctor :function ( ) {
64
62
this . _super ( ) ;
65
- this . _colorUnmodified = cc . WHITE ;
66
- this . _color = cc . white ( ) ;
63
+ this . _colorUnmodified = cc . white ( ) ;
67
64
this . _blendFunc = { src :cc . BLEND_SRC , dst :cc . BLEND_DST } ;
68
- this . _opacity = 255 ;
65
+ this . _ignoreContentScaleFactor = false ;
69
66
} ,
70
67
71
68
/** updates the Atlas (indexed vertex array).
@@ -81,14 +78,7 @@ cc.AtlasNodeCanvas = cc.Node.extend(/** @lends cc.AtlasNode# */{
81
78
getColor :function ( ) {
82
79
if ( this . _opacityModifyRGB )
83
80
return this . _colorUnmodified ;
84
- return this . _color ;
85
- } ,
86
-
87
- /**
88
- * @return {Number }
89
- */
90
- getOpacity :function ( ) {
91
- return this . _opacity ;
81
+ return cc . NodeRGBA . prototype . getColor . call ( this ) ;
92
82
} ,
93
83
94
84
/**
@@ -153,11 +143,10 @@ cc.AtlasNodeCanvas = cc.Node.extend(/** @lends cc.AtlasNode# */{
153
143
setQuadsToDraw :function ( quadsToDraw ) {
154
144
this . _quadsToDraw = quadsToDraw ;
155
145
} ,
156
- /// ---- common properties end ----
157
146
147
+ /// ---- common properties end ----
158
148
_textureForCanvas :null ,
159
149
_originalTexture :null ,
160
- _colorF32Array :null ,
161
150
162
151
/** initializes an cc.AtlasNode with an Atlas file the width and height of each item and the quantity of items to render
163
152
* @param {String } tile
@@ -168,11 +157,24 @@ cc.AtlasNodeCanvas = cc.Node.extend(/** @lends cc.AtlasNode# */{
168
157
*/
169
158
initWithTileFile :function ( tile , tileWidth , tileHeight , itemsToRender ) {
170
159
cc . Assert ( tile != null , "title should not be null" ) ;
160
+ var texture = cc . TextureCache . getInstance ( ) . addImage ( tile ) ;
161
+ return this . initWithTexture ( texture , tileWidth , tileHeight , itemsToRender ) ;
162
+ } ,
163
+
164
+ /**
165
+ * initializes an CCAtlasNode with a texture the width and height of each item measured in points and the quantity of items to render
166
+ * @param {HTMLImageElement|HTMLCanvasElement } texture
167
+ * @param {Number } tileWidth
168
+ * @param {Number } tileHeight
169
+ * @param {Number } itemsToRender
170
+ * @returen {Boolean}
171
+ */
172
+ initWithTexture :function ( texture , tileWidth , tileHeight , itemsToRender ) {
171
173
this . _itemWidth = tileWidth ;
172
174
this . _itemHeight = tileHeight ;
173
175
174
176
this . _opacityModifyRGB = true ;
175
- this . _originalTexture = cc . TextureCache . getInstance ( ) . addImage ( tile ) ;
177
+ this . _originalTexture = texture ;
176
178
if ( ! this . _originalTexture ) {
177
179
cc . log ( "cocos2d: Could not initialize cc.AtlasNode. Invalid Texture." ) ;
178
180
return false ;
@@ -188,31 +190,34 @@ cc.AtlasNodeCanvas = cc.Node.extend(/** @lends cc.AtlasNode# */{
188
190
* @param {cc.Color3B } color3
189
191
*/
190
192
setColor :function ( color3 ) {
191
- if ( ( this . _color . r == color3 . r ) && ( this . _color . g == color3 . g ) && ( this . _color . b == color3 . b ) )
193
+ if ( ( this . _realColor . r == color3 . r ) && ( this . _realColor . g == color3 . g ) && ( this . _realColor . b == color3 . b ) )
192
194
return ;
193
- this . _color = this . _colorUnmodified = color3 ;
195
+ var temp = new cc . Color3B ( color3 . r , color3 . g , color3 . b ) ;
196
+ this . _colorUnmodified = color3 ;
197
+
198
+ if ( this . _opacityModifyRGB ) {
199
+ temp . r = temp . r * this . _displayedOpacity / 255 ;
200
+ temp . g = temp . g * this . _displayedOpacity / 255 ;
201
+ temp . b = temp . b * this . _displayedOpacity / 255 ;
202
+ }
203
+ cc . NodeRGBA . prototype . setColor . call ( this , color3 ) ;
194
204
if ( this . getTexture ( ) ) {
195
205
var cacheTextureForColor = cc . TextureCache . getInstance ( ) . getTextureColors ( this . _originalTexture ) ;
196
206
if ( cacheTextureForColor ) {
197
207
var tx = this . _originalTexture ;
198
208
var textureRect = cc . rect ( 0 , 0 , tx . width , tx . height ) ;
199
- var colorTexture = cc . generateTintImage ( tx , cacheTextureForColor , this . _color , textureRect ) ;
209
+ var colorTexture = cc . generateTintImage ( tx , cacheTextureForColor , this . _realColor , textureRect ) ;
200
210
this . setTexture ( colorTexture ) ;
201
211
}
202
212
}
203
-
204
- if ( this . _opacityModifyRGB ) {
205
- this . _color . r = color3 . r * this . _opacity / 255 ;
206
- this . _color . g = color3 . g * this . _opacity / 255 ;
207
- this . _color . b = color3 . b * this . _opacity / 255 ;
208
- }
209
213
} ,
210
214
211
215
/**
212
216
* @param {Number } opacity
213
217
*/
214
218
setOpacity :function ( opacity ) {
215
- this . _opacity = opacity ;
219
+ cc . NodeRGBA . prototype . setOpacity . call ( this , opacity ) ;
220
+
216
221
// special opacity for premultiplied textures
217
222
if ( this . _opacityModifyRGB ) {
218
223
this . setColor ( this . _colorUnmodified ) ;
@@ -241,26 +246,13 @@ cc.AtlasNodeCanvas = cc.Node.extend(/** @lends cc.AtlasNode# */{
241
246
242
247
this . _itemsPerColumn = 0 | ( size . height / this . _itemHeight ) ;
243
248
this . _itemsPerRow = 0 | ( size . width / this . _itemWidth ) ;
249
+ } ,
250
+
251
+ _setIgnoreContentScaleFactor :function ( ignoreContentScaleFactor ) {
252
+ this . _ignoreContentScaleFactor = ignoreContentScaleFactor ;
244
253
}
245
254
} ) ;
246
255
247
- /** creates a cc.AtlasNodeCanvas with an Atlas file the width and height of each item and the quantity of items to render
248
- * @param {String } tile
249
- * @param {Number } tileWidth
250
- * @param {Number } tileHeight
251
- * @param {Number } itemsToRender
252
- * @return {cc.AtlasNode }
253
- * @example
254
- * // example
255
- * var node = cc.AtlasNode.create("pathOfTile", 16, 16, 1);
256
- */
257
- cc . AtlasNodeCanvas . create = function ( tile , tileWidth , tileHeight , itemsToRender ) {
258
- var ret = new cc . AtlasNodeCanvas ( ) ;
259
- if ( ret . initWithTileFile ( tile , tileWidth , tileHeight , itemsToRender ) )
260
- return ret ;
261
- return null ;
262
- } ;
263
-
264
256
/** <p> cc.AtlasNode is a subclass of cc.Node that implements the cc.RGBAProtocol and<br/>
265
257
* cc.TextureProtocol protocol (WebGL implement)</p>
266
258
*
@@ -272,7 +264,7 @@ cc.AtlasNodeCanvas.create = function (tile, tileWidth, tileHeight, itemsToRender
272
264
* @class
273
265
* @extends cc.Node
274
266
*/
275
- cc . AtlasNodeWebGL = cc . Node . extend ( {
267
+ cc . AtlasNodeWebGL = cc . NodeRGBA . extend ( {
276
268
/// ---- common properties start ----
277
269
RGBAProtocol :true ,
278
270
//! chars per row
@@ -291,18 +283,15 @@ cc.AtlasNodeWebGL = cc.Node.extend({
291
283
_opacityModifyRGB :false ,
292
284
_blendFunc :null ,
293
285
294
- _opacity :0 ,
295
- _color :null ,
296
-
297
286
// quads to draw
298
287
_quadsToDraw :0 ,
288
+ _ignoreContentScaleFactor :false , // This variable is only used for CCLabelAtlas FPS display. So plz don't modify its value.
299
289
300
290
ctor :function ( ) {
301
291
this . _super ( ) ;
302
- this . _colorUnmodified = cc . WHITE ;
303
- this . _color = cc . white ( ) ;
292
+ this . _colorUnmodified = cc . white ( ) ;
304
293
this . _blendFunc = { src :cc . BLEND_SRC , dst :cc . BLEND_DST } ;
305
- this . _opacity = 255 ;
294
+ this . _ignoreContentScaleFactor = false ;
306
295
} ,
307
296
308
297
/** updates the Atlas (indexed vertex array).
@@ -318,14 +307,7 @@ cc.AtlasNodeWebGL = cc.Node.extend({
318
307
getColor :function ( ) {
319
308
if ( this . _opacityModifyRGB )
320
309
return this . _colorUnmodified ;
321
- return this . _color ;
322
- } ,
323
-
324
- /**
325
- * @return {Number }
326
- */
327
- getOpacity :function ( ) {
328
- return this . _opacity ;
310
+ return cc . NodeRGBA . prototype . getColor . call ( this ) ;
329
311
} ,
330
312
331
313
/**
@@ -403,14 +385,32 @@ cc.AtlasNodeWebGL = cc.Node.extend({
403
385
*/
404
386
initWithTileFile :function ( tile , tileWidth , tileHeight , itemsToRender ) {
405
387
cc . Assert ( tile != null , "title should not be null" ) ;
388
+ var texture = cc . TextureCache . getInstance ( ) . addImage ( tile ) ;
389
+ return this . initWithTexture ( texture , tileWidth , tileHeight , itemsToRender ) ;
390
+ } ,
391
+
392
+ /**
393
+ * initializes an CCAtlasNode with a texture the width and height of each item measured in points and the quantity of items to render
394
+ * @param {cc.Texture2D } texture
395
+ * @param {Number } tileWidth
396
+ * @param {Number } tileHeight
397
+ * @param {Number } itemsToRender
398
+ * @returen {Boolean}
399
+ */
400
+ initWithTexture :function ( texture , tileWidth , tileHeight , itemsToRender ) {
406
401
this . _itemWidth = tileWidth ;
407
402
this . _itemHeight = tileHeight ;
403
+ this . _colorUnmodified = cc . WHITE ;
408
404
this . _opacityModifyRGB = true ;
409
405
410
- this . _colorF32Array = new Float32Array ( [ this . _color . r / 255.0 , this . _color . g / 255.0 , this . _color . b / 255.0 , this . _opacity / 255.0 ] ) ;
411
- var newAtlas = new cc . TextureAtlas ( ) ;
412
- newAtlas . initWithFile ( tile , itemsToRender ) ;
413
- this . setTextureAtlas ( newAtlas ) ;
406
+ this . _blendFunc . src = cc . BLEND_SRC ;
407
+ this . _blendFunc . dst = cc . BLEND_DST ;
408
+
409
+ var locRealColor = this . _realColor ;
410
+ this . _colorF32Array = new Float32Array ( [ locRealColor . r / 255.0 , locRealColor . g / 255.0 , locRealColor . b / 255.0 , this . _realOpacity / 255.0 ] ) ;
411
+ this . _textureAtlas = new cc . TextureAtlas ( ) ;
412
+ this . _textureAtlas . initWithTexture ( texture , itemsToRender ) ;
413
+
414
414
if ( ! this . _textureAtlas ) {
415
415
cc . log ( "cocos2d: Could not initialize cc.AtlasNode. Invalid Texture." ) ;
416
416
return false ;
@@ -428,7 +428,7 @@ cc.AtlasNodeWebGL = cc.Node.extend({
428
428
} ,
429
429
430
430
/**
431
- * @param {WebGLRenderingContext } ctx CanvasContext
431
+ * @param {WebGLRenderingContext } ctx renderContext
432
432
*/
433
433
draw :function ( ctx ) {
434
434
var context = ctx || cc . renderContext ;
@@ -442,28 +442,30 @@ cc.AtlasNodeWebGL = cc.Node.extend({
442
442
* @param {cc.Color3B } color3
443
443
*/
444
444
setColor :function ( color3 ) {
445
- if ( ( this . _color . r == color3 . r ) && ( this . _color . g == color3 . g ) && ( this . _color . b == color3 . b ) )
446
- return ;
447
- this . _color = this . _colorUnmodified = color3 ;
445
+ var temp = cc . Color3B ( color3 . r , color3 . g , color3 . b ) ;
446
+ this . _colorUnmodified = color3 ;
448
447
449
448
if ( this . _opacityModifyRGB ) {
450
- this . _color . r = color3 . r * this . _opacity / 255 ;
451
- this . _color . g = color3 . g * this . _opacity / 255 ;
452
- this . _color . b = color3 . b * this . _opacity / 255 ;
449
+ temp . r = temp . r * this . _displayedOpacity / 255 ;
450
+ temp . g = temp . g * this . _displayedOpacity / 255 ;
451
+ temp . b = temp . b * this . _displayedOpacity / 255 ;
453
452
}
454
- this . _colorF32Array = new Float32Array ( [ this . _color . r / 255.0 , this . _color . g / 255.0 , this . _color . b / 255.0 , this . _opacity / 255.0 ] ) ;
453
+ cc . NodeRGBA . prototype . setColor . call ( this , color3 ) ;
454
+ this . _colorF32Array = new Float32Array ( [ this . _displayedColor . r / 255.0 , this . _displayedColor . g / 255.0 ,
455
+ this . _displayedColor . b / 255.0 , this . _displayedOpacity / 255.0 ] ) ;
455
456
} ,
456
457
457
458
/**
458
459
* @param {Number } opacity
459
460
*/
460
461
setOpacity :function ( opacity ) {
461
- this . _opacity = opacity ;
462
+ cc . NodeRGBA . prototype . setOpacity . call ( this , opacity ) ;
462
463
// special opacity for premultiplied textures
463
464
if ( this . _opacityModifyRGB ) {
464
465
this . setColor ( this . _colorUnmodified ) ;
465
466
} else {
466
- this . _colorF32Array = new Float32Array ( [ this . _color . r / 255.0 , this . _color . g / 255.0 , this . _color . b / 255.0 , this . _opacity / 255.0 ] ) ;
467
+ var locDisplayedColor = this . _displayedColor ;
468
+ this . _colorF32Array = new Float32Array ( [ locDisplayedColor . r / 255.0 , locDisplayedColor . g / 255.0 , locDisplayedColor . b / 255.0 , this . _displayedOpacity / 255.0 ] ) ;
467
469
}
468
470
} ,
469
471
@@ -488,6 +490,9 @@ cc.AtlasNodeWebGL = cc.Node.extend({
488
490
_calculateMaxItems :function ( ) {
489
491
var selTexture = this . getTexture ( ) ;
490
492
var size = selTexture . getContentSize ( ) ;
493
+ if ( this . _ignoreContentScaleFactor ) {
494
+ size = selTexture . getContentSizeInPixels ( ) ;
495
+ }
491
496
492
497
this . _itemsPerColumn = 0 | ( size . height / this . _itemHeight ) ;
493
498
this . _itemsPerRow = 0 | ( size . width / this . _itemWidth ) ;
@@ -502,10 +507,16 @@ cc.AtlasNodeWebGL = cc.Node.extend({
502
507
503
508
_updateOpacityModifyRGB :function ( ) {
504
509
this . _opacityModifyRGB = this . _textureAtlas . getTexture ( ) . hasPremultipliedAlpha ( ) ;
510
+ } ,
511
+
512
+ _setIgnoreContentScaleFactor :function ( ignoreContentScaleFactor ) {
513
+ this . _ignoreContentScaleFactor = ignoreContentScaleFactor ;
505
514
}
506
515
} ) ;
507
516
508
- /** creates a cc.AtlasNodeWebGL with an Atlas file the width and height of each item and the quantity of items to render
517
+ cc . AtlasNode = cc . Browser . supportWebGL ? cc . AtlasNodeWebGL : cc . AtlasNodeCanvas ;
518
+
519
+ /** creates a cc.AtlasNode with an Atlas file the width and height of each item and the quantity of items to render
509
520
* @param {String } tile
510
521
* @param {Number } tileWidth
511
522
* @param {Number } tileHeight
@@ -515,13 +526,10 @@ cc.AtlasNodeWebGL = cc.Node.extend({
515
526
* // example
516
527
* var node = cc.AtlasNode.create("pathOfTile", 16, 16, 1);
517
528
*/
518
- cc . AtlasNodeWebGL . create = function ( tile , tileWidth , tileHeight , itemsToRender ) {
519
- var ret = new cc . AtlasNodeWebGL ( ) ;
529
+ cc . AtlasNode . create = function ( tile , tileWidth , tileHeight , itemsToRender ) {
530
+ var ret = new cc . AtlasNode ( ) ;
520
531
if ( ret . initWithTileFile ( tile , tileWidth , tileHeight , itemsToRender ) )
521
532
return ret ;
522
533
return null ;
523
534
} ;
524
535
525
- cc . AtlasNode = cc . Browser . supportWebGL ? cc . AtlasNodeWebGL : cc . AtlasNodeCanvas ;
526
-
527
-
0 commit comments