Skip to content

Commit 22a4dff

Browse files
authored
Merge pull request #3417 from zilongshanren/add-bmfont-batch-v2
add bmfont and atlas batch
2 parents 6404318 + afe94ba commit 22a4dff

File tree

6 files changed

+462
-556
lines changed

6 files changed

+462
-556
lines changed

Diff for: cocos2d/labels/CCLabelAtlas.js

+84-103
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,47 @@ 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 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+
}
91101

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;
100105
},
101106

102107
/**
@@ -116,6 +121,10 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{
116121
*/
117122
initWithString: function (strText, charMapFile, itemWidth, itemHeight, startCharMap) {
118123
var label = strText + "", textureFilename, width, height, startChar;
124+
var self = this, theString = label || "";
125+
this._initialString = theString;
126+
self._string = theString;
127+
119128
if (itemWidth === undefined) {
120129
var dict = cc.loader.getRes(charMapFile);
121130
if (parseInt(dict["version"], 10) !== 1) {
@@ -135,88 +144,60 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{
135144
startChar = startCharMap || " ";
136145
}
137146

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;
158179
}
159-
return false;
160-
},
180+
this._texture = texture;
161181

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

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

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

184-
/**
185-
* Atlas generation
186-
* @function
187-
*/
188-
updateAtlasValues: function(){
189-
this._renderCmd.updateAtlasValues();
192+
self.setString(theString, true);
193+
return true;
190194
},
191195

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.");
206198
}
207-
});
208199

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+
});
220201

221202
/**
222203
* <p>

0 commit comments

Comments
 (0)