27
27
/**
28
28
* using image file to print text label on the screen, might be a bit slower than cc.Label, similar to cc.LabelBMFont
29
29
* @class
30
- * @extends cc.AtlasNode
30
+ * @extends cc.LabelBMFont
31
31
*
32
32
* @property {String } string - Content string of label
33
33
*
43
43
* //creates the cc.LabelAtlas with a string, a fnt file
44
44
* var myLabel = new cc.LabelAtlas('Text to display', 'CharMapFile.plist‘);
45
45
*/
46
- cc . LabelAtlas = cc . AtlasNode . extend ( /** @lends cc.LabelAtlas# */ {
47
- //property String is Getter and Setter
48
- // string to render
49
- _string : null ,
50
- // the first char in the charmap
51
- _mapStartChar : null ,
52
-
53
- _textureLoaded : false ,
46
+ cc . LabelAtlas = cc . LabelBMFont . extend ( /** @lends cc.LabelBMFont# */ {
54
47
_className : "LabelAtlas" ,
55
48
56
49
/**
@@ -68,35 +61,47 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{
68
61
* @param {Number } [startCharMap=""]
69
62
*/
70
63
ctor : function ( strText , charMapFile , itemWidth , itemHeight , startCharMap ) {
71
- cc . AtlasNode . prototype . ctor . call ( this ) ;
64
+ cc . SpriteBatchNode . prototype . ctor . call ( this ) ;
65
+ this . _imageOffset = cc . p ( 0 , 0 ) ;
66
+ this . _cascadeColorEnabled = true ;
67
+ this . _cascadeOpacityEnabled = true ;
72
68
73
- this . _renderCmd . setCascade ( ) ;
74
69
charMapFile && cc . LabelAtlas . prototype . initWithString . call ( this , strText , charMapFile , itemWidth , itemHeight , startCharMap ) ;
75
70
} ,
76
71
77
- _createRenderCmd : function ( ) {
78
- if ( cc . _renderType === cc . game . RENDER_TYPE_WEBGL )
79
- return new cc . LabelAtlas . WebGLRenderCmd ( this ) ;
72
+ _createRenderCmd : function ( ) {
73
+ if ( cc . _renderType === cc . game . RENDER_TYPE_WEBGL )
74
+ return new cc . LabelBMFont . WebGLRenderCmd ( this ) ;
80
75
else
81
- return new cc . LabelAtlas . CanvasRenderCmd ( this ) ;
76
+ return new cc . LabelBMFont . CanvasRenderCmd ( this ) ;
82
77
} ,
83
78
84
- /**
85
- * Return texture is loaded.
86
- * @returns {boolean }
87
- */
88
- textureLoaded : function ( ) {
89
- return this . _textureLoaded ;
90
- } ,
79
+ _createFntConfig : function ( texture , itemWidth , itemHeight , startCharMap ) {
80
+ var fnt = { } ;
81
+ fnt . commonHeight = itemHeight ;
82
+
83
+ var fontDefDictionary = fnt . fontDefDictionary = { } ;
84
+
85
+ var textureWidth = texture . pixelsWidth ;
86
+ var textureHeight = texture . pixelsHeight ;
87
+
88
+ var startCharCode = startCharMap . charCodeAt ( 0 ) ;
89
+ var i = 0 ;
90
+ for ( var col = itemHeight ; col <= textureHeight ; col += itemHeight ) {
91
+ for ( var row = 0 ; row < textureWidth ; row += itemWidth ) {
92
+ fontDefDictionary [ startCharCode + i ] = {
93
+ rect : { x : row , y : col - itemHeight , width :itemWidth , height : itemHeight } ,
94
+ xOffset : 0 ,
95
+ yOffset : 0 ,
96
+ xAdvance : itemWidth
97
+ } ;
98
+ ++ i ;
99
+ }
100
+ }
91
101
92
- /**
93
- * Add texture loaded event listener.
94
- * @param {Function } callback
95
- * @param {cc.Node } target
96
- * @deprecated since 3.1, please use addEventListener instead
97
- */
98
- addLoadedEventListener : function ( callback , target ) {
99
- this . addEventListener ( "load" , callback , target ) ;
102
+ fnt . kerningDict = { } ;
103
+
104
+ return fnt ;
100
105
} ,
101
106
102
107
/**
@@ -116,6 +121,10 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{
116
121
*/
117
122
initWithString : function ( strText , charMapFile , itemWidth , itemHeight , startCharMap ) {
118
123
var label = strText + "" , textureFilename , width , height , startChar ;
124
+ var self = this , theString = label || "" ;
125
+ this . _initialString = theString ;
126
+ self . _string = theString ;
127
+
119
128
if ( itemWidth === undefined ) {
120
129
var dict = cc . loader . getRes ( charMapFile ) ;
121
130
if ( parseInt ( dict [ "version" ] , 10 ) !== 1 ) {
@@ -135,88 +144,60 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{
135
144
startChar = startCharMap || " " ;
136
145
}
137
146
138
- var texture = null ;
139
- if ( textureFilename instanceof cc . Texture2D )
140
- texture = textureFilename ;
141
- else
142
- texture = cc . textureCache . addImage ( textureFilename ) ;
143
- var locLoaded = texture . isLoaded ( ) ;
144
- this . _textureLoaded = locLoaded ;
145
- if ( ! locLoaded ) {
146
- this . _string = label ;
147
- texture . addEventListener ( "load" , function ( sender ) {
148
- this . initWithTexture ( texture , width , height , label . length ) ;
149
- this . string = this . _string ;
150
- this . setColor ( this . _renderCmd . _displayedColor ) ;
151
- this . dispatchEvent ( "load" ) ;
152
- } , this ) ;
153
- }
154
- if ( this . initWithTexture ( texture , width , height , label . length ) ) {
155
- this . _mapStartChar = startChar ;
156
- this . string = label ;
157
- return true ;
147
+ var texture ;
148
+ if ( charMapFile ) {
149
+ self . _fntFile = "dummy_fnt_file:" + textureFilename ;
150
+ var spriteFrameBaseName = textureFilename ;
151
+ var spriteFrame = cc . spriteFrameCache . getSpriteFrame ( spriteFrameBaseName ) || cc . spriteFrameCache . getSpriteFrame ( cc . path . basename ( spriteFrameBaseName ) ) ;
152
+ if ( spriteFrame ) {
153
+ texture = spriteFrame . getTexture ( ) ;
154
+ this . _spriteFrame = spriteFrame ;
155
+ } else {
156
+ texture = cc . textureCache . addImage ( textureFilename ) ;
157
+ }
158
+
159
+ var newConf = this . _createFntConfig ( texture , width , height , startChar ) ;
160
+ newConf . atlasName = textureFilename ;
161
+ self . _config = newConf ;
162
+
163
+ var locIsLoaded = texture . isLoaded ( ) ;
164
+ self . _textureLoaded = locIsLoaded ;
165
+ if ( ! locIsLoaded ) {
166
+ texture . addEventListener ( "load" , function ( ) {
167
+ var self1 = this ;
168
+ self1 . _textureLoaded = true ;
169
+ //reset the LabelBMFont
170
+ self1 . setString ( self1 . _initialString , true ) ;
171
+ self1 . dispatchEvent ( "load" ) ;
172
+ } , self ) ;
173
+ }
174
+ } else {
175
+ texture = new cc . Texture2D ( ) ;
176
+ var image = new Image ( ) ;
177
+ texture . initWithElement ( image ) ;
178
+ self . _textureLoaded = false ;
158
179
}
159
- return false ;
160
- } ,
180
+ this . _texture = texture ;
161
181
162
- /**
163
- * Set the color.
164
- * @param {cc.Color } color3
165
- */
166
- setColor : function ( color3 ) {
167
- cc . AtlasNode . prototype . setColor . call ( this , color3 ) ;
168
- this . _renderCmd . updateAtlasValues ( ) ;
169
- } ,
182
+ self . _alignment = cc . TEXT_ALIGNMENT_LEFT ;
183
+ self . _imageOffset = cc . p ( 0 , 0 ) ;
184
+ self . _width = - 1 ;
170
185
171
- /**
172
- * return the text of this label
173
- * @return {String }
174
- */
175
- getString : function ( ) {
176
- return this . _string ;
177
- } ,
186
+ self . _realOpacity = 255 ;
187
+ self . _realColor = cc . color ( 255 , 255 , 255 , 255 ) ;
178
188
179
- addChild : function ( child , localZOrder , tag ) {
180
- this . _renderCmd . _addChild ( child ) ;
181
- cc . Node . prototype . addChild . call ( this , child , localZOrder , tag ) ;
182
- } ,
189
+ self . _contentSize . width = 0 ;
190
+ self . _contentSize . height = 0 ;
183
191
184
- /**
185
- * Atlas generation
186
- * @function
187
- */
188
- updateAtlasValues : function ( ) {
189
- this . _renderCmd . updateAtlasValues ( ) ;
192
+ self . setString ( theString , true ) ;
193
+ return true ;
190
194
} ,
191
195
192
- /**
193
- * set the display string
194
- * @function
195
- * @param {String } label
196
- */
197
- setString : function ( label ) {
198
- label = String ( label ) ;
199
- var len = label . length ;
200
- this . _string = label ;
201
- this . setContentSize ( len * this . _itemWidth , this . _itemHeight ) ;
202
- this . _renderCmd . setString ( label ) ;
203
-
204
- this . _renderCmd . updateAtlasValues ( ) ;
205
- this . quadsToDraw = len ;
196
+ setFntFile : function ( ) {
197
+ cc . warn ( "setFntFile doesn't support with LabelAtlas." ) ;
206
198
}
207
- } ) ;
208
199
209
- ( function ( ) {
210
- var proto = cc . LabelAtlas . prototype ;
211
- // Override properties
212
- cc . defineGetterSetter ( proto , "opacity" , proto . getOpacity , proto . setOpacity ) ;
213
- cc . defineGetterSetter ( proto , "color" , proto . getColor , proto . setColor ) ;
214
-
215
- // Extended properties
216
- /** @expose */
217
- proto . string ;
218
- cc . defineGetterSetter ( proto , "string" , proto . getString , proto . setString ) ;
219
- } ) ( ) ;
200
+ } ) ;
220
201
221
202
/**
222
203
* <p>
0 commit comments