Skip to content

Commit 053739c

Browse files
committed
refactor labelAtlas to extend bmfont
1 parent 443cf83 commit 053739c

File tree

2 files changed

+141
-149
lines changed

2 files changed

+141
-149
lines changed

Diff for: cocos2d/labels/CCLabelAtlas.js

+89-92
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* using image file to print text label on the screen, might be a bit slower than cc.Label, similar to cc.LabelBMFont
2929
* @class
30-
* @extends cc.AtlasNode
30+
* @extends cc.LabelBMFont
3131
*
3232
* @property {String} string - Content string of label
3333
*
@@ -43,14 +43,7 @@
4343
* //creates the cc.LabelAtlas with a string, a fnt file
4444
* var myLabel = new cc.LabelAtlas('Text to display', 'CharMapFile.plist‘);
4545
*/
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# */{
5447
_className: "LabelAtlas",
5548

5649
/**
@@ -68,35 +61,50 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{
6861
* @param {Number} [startCharMap=""]
6962
*/
7063
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;
7268

73-
this._renderCmd.setCascade();
7469
charMapFile && cc.LabelAtlas.prototype.initWithString.call(this, strText, charMapFile, itemWidth, itemHeight, startCharMap);
7570
},
7671

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);
8075
else
81-
return new cc.LabelAtlas.CanvasRenderCmd(this);
76+
return new cc.LabelBMFont.CanvasRenderCmd(this);
8277
},
8378

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+
}
91104

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;
100108
},
101109

102110
/**
@@ -116,6 +124,10 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{
116124
*/
117125
initWithString: function (strText, charMapFile, itemWidth, itemHeight, startCharMap) {
118126
var label = strText + "", textureFilename, width, height, startChar;
127+
var self = this, theString = label || "";
128+
this._initialString = theString;
129+
self._string = theString;
130+
119131
if (itemWidth === undefined) {
120132
var dict = cc.loader.getRes(charMapFile);
121133
if (parseInt(dict["version"], 10) !== 1) {
@@ -135,78 +147,63 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{
135147
startChar = startCharMap || " ";
136148
}
137149

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;
158183
}
159-
return false;
160-
},
184+
this._texture = texture;
161185

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;
170189

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);
178192

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;
183195

184-
/**
185-
* Atlas generation
186-
* @function
187-
*/
188-
updateAtlasValues: function(){
189-
this._renderCmd.updateAtlasValues();
196+
self.setString(theString, true);
197+
return true;
190198
},
191199

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.");
206202
}
203+
207204
});
208205

209-
(function(){
206+
(function () {
210207
var proto = cc.LabelAtlas.prototype;
211208
// Override properties
212209
cc.defineGetterSetter(proto, "opacity", proto.getOpacity, proto.setOpacity);

0 commit comments

Comments
 (0)