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,50 @@ 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 offset = 0 ;
86
+ if ( startCharMap && startCharMap !== "0" ) {
87
+ fontDefDictionary [ startCharMap . charCodeAt ( 0 ) ] = {
88
+ rect : { x : 0 , y :0 , width :itemWidth , height : itemHeight } ,
89
+ xOffset : 0 ,
90
+ yOffset : 0 ,
91
+ xAdvance : itemWidth
92
+ } ;
93
+ offset = 1 ;
94
+ }
95
+ var startCharCode = 48 ;
96
+ for ( var i = 0 ; i < 10 ; ++ i ) {
97
+ fontDefDictionary [ startCharCode + i ] = {
98
+ rect : { x : ( i + offset ) * itemWidth , y :0 , width :itemWidth , height : itemHeight } ,
99
+ xOffset : 0 ,
100
+ yOffset : 0 ,
101
+ xAdvance : itemWidth
102
+ } ;
103
+ }
91
104
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 ) ;
105
+ fnt . kerningDict = { } ;
106
+
107
+ return fnt ;
100
108
} ,
101
109
102
110
/**
@@ -116,6 +124,10 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{
116
124
*/
117
125
initWithString : function ( strText , charMapFile , itemWidth , itemHeight , startCharMap ) {
118
126
var label = strText + "" , textureFilename , width , height , startChar ;
127
+ var self = this , theString = label || "" ;
128
+ this . _initialString = theString ;
129
+ self . _string = theString ;
130
+
119
131
if ( itemWidth === undefined ) {
120
132
var dict = cc . loader . getRes ( charMapFile ) ;
121
133
if ( parseInt ( dict [ "version" ] , 10 ) !== 1 ) {
@@ -135,78 +147,63 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{
135
147
startChar = startCharMap || " " ;
136
148
}
137
149
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 ;
150
+
151
+ var texture ;
152
+ if ( charMapFile ) {
153
+ self . _fntFile = "dummy_fnt_file:" + textureFilename ;
154
+ var spriteFrameBaseName = textureFilename ;
155
+ var spriteFrame = cc . spriteFrameCache . getSpriteFrame ( spriteFrameBaseName ) || cc . spriteFrameCache . getSpriteFrame ( cc . path . basename ( spriteFrameBaseName ) ) ;
156
+ if ( spriteFrame ) {
157
+ texture = spriteFrame . getTexture ( ) ;
158
+ this . _spriteFrame = spriteFrame ;
159
+ } else {
160
+ texture = cc . textureCache . addImage ( textureFilename ) ;
161
+ }
162
+
163
+ var newConf = this . _createFntConfig ( texture , width , height , startChar ) ;
164
+ newConf . atlasName = textureFilename ;
165
+ self . _config = newConf ;
166
+
167
+ var locIsLoaded = texture . isLoaded ( ) ;
168
+ self . _textureLoaded = locIsLoaded ;
169
+ if ( ! locIsLoaded ) {
170
+ texture . addEventListener ( "load" , function ( ) {
171
+ var self1 = this ;
172
+ self1 . _textureLoaded = true ;
173
+ //reset the LabelBMFont
174
+ self1 . setString ( self1 . _initialString , true ) ;
175
+ self1 . dispatchEvent ( "load" ) ;
176
+ } , self ) ;
177
+ }
178
+ } else {
179
+ texture = new cc . Texture2D ( ) ;
180
+ var image = new Image ( ) ;
181
+ texture . initWithElement ( image ) ;
182
+ self . _textureLoaded = false ;
158
183
}
159
- return false ;
160
- } ,
184
+ this . _texture = texture ;
161
185
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
- } ,
186
+ self . _alignment = cc . TEXT_ALIGNMENT_LEFT ;
187
+ self . _imageOffset = cc . p ( 0 , 0 ) ;
188
+ self . _width = - 1 ;
170
189
171
- /**
172
- * return the text of this label
173
- * @return {String }
174
- */
175
- getString : function ( ) {
176
- return this . _string ;
177
- } ,
190
+ self . _realOpacity = 255 ;
191
+ self . _realColor = cc . color ( 255 , 255 , 255 , 255 ) ;
178
192
179
- addChild : function ( child , localZOrder , tag ) {
180
- this . _renderCmd . _addChild ( child ) ;
181
- cc . Node . prototype . addChild . call ( this , child , localZOrder , tag ) ;
182
- } ,
193
+ self . _contentSize . width = 0 ;
194
+ self . _contentSize . height = 0 ;
183
195
184
- /**
185
- * Atlas generation
186
- * @function
187
- */
188
- updateAtlasValues : function ( ) {
189
- this . _renderCmd . updateAtlasValues ( ) ;
196
+ self . setString ( theString , true ) ;
197
+ return true ;
190
198
} ,
191
199
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 ;
200
+ setFntFile : function ( ) {
201
+ cc . warn ( "setFntFile doesn't support with LabelAtlas." ) ;
206
202
}
203
+
207
204
} ) ;
208
205
209
- ( function ( ) {
206
+ ( function ( ) {
210
207
var proto = cc . LabelAtlas . prototype ;
211
208
// Override properties
212
209
cc . defineGetterSetter ( proto , "opacity" , proto . getOpacity , proto . setOpacity ) ;
0 commit comments