Skip to content

Commit f9d0a5c

Browse files
committed
Issue #2416: refactor cc.Sprite and cc.LabelTTF
2 parents 8ed792c + b67bb36 commit f9d0a5c

File tree

13 files changed

+476
-809
lines changed

13 files changed

+476
-809
lines changed

cocos2d/core/labelttf/CCLabelTTF.js

+7-119
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,25 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
7272
_string: "",
7373
_originalText: null,
7474
_isMultiLine: false,
75-
_fontStyleStr: null,
75+
_fontStyleStr: null, //TODO move to render cmd
7676

7777
// font shadow
7878
_shadowEnabled: false,
7979
_shadowOffset: null,
8080
_shadowOpacity: 0,
8181
_shadowBlur: 0,
82-
_shadowColorStr: null,
82+
_shadowColorStr: null, //TODO move to render cmd
8383
_shadowColor: null,
8484

8585
// font stroke
8686
_strokeEnabled: false,
8787
_strokeColor: null,
8888
_strokeSize: 0,
89-
_strokeColorStr: null,
89+
_strokeColorStr: null, // TODO move to render cmd
9090

9191
// font tint
9292
_textFillColor: null,
93-
_fillColorStr: null,
93+
_fillColorStr: null, // TODO move to render cmd
9494

9595
_strokeShadowOffsetX: 0,
9696
_strokeShadowOffsetY: 0,
@@ -130,7 +130,6 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
130130
this._hAlignment = hAlignment;
131131
this._vAlignment = vAlignment;
132132

133-
//this._fontSize = (cc._renderType === cc._RENDER_TYPE_CANVAS) ? fontSize : fontSize * cc.contentScaleFactor();
134133
this._fontSize = fontSize;
135134
this._fontStyleStr = this._fontSize + "px '" + fontName + "'";
136135
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontName, this._fontSize);
@@ -1002,85 +1001,9 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
10021001
cc.Sprite.prototype.visit.call(this, context);
10031002
},
10041003

1005-
draw: null,
1006-
1007-
_setTextureCoords: function (rect) {
1008-
var tex = this._batchNode ? this.textureAtlas.texture : this._texture;
1009-
if (!tex)
1010-
return;
1011-
1012-
var atlasWidth = tex.pixelsWidth;
1013-
var atlasHeight = tex.pixelsHeight;
1014-
1015-
var left, right, top, bottom, tempSwap, locQuad = this._quad;
1016-
if (this._rectRotated) {
1017-
if (cc.FIX_ARTIFACTS_BY_STRECHING_TEXEL) {
1018-
left = (2 * rect.x + 1) / (2 * atlasWidth);
1019-
right = left + (rect.height * 2 - 2) / (2 * atlasWidth);
1020-
top = (2 * rect.y + 1) / (2 * atlasHeight);
1021-
bottom = top + (rect.width * 2 - 2) / (2 * atlasHeight);
1022-
} else {
1023-
left = rect.x / atlasWidth;
1024-
right = (rect.x + rect.height) / atlasWidth;
1025-
top = rect.y / atlasHeight;
1026-
bottom = (rect.y + rect.width) / atlasHeight;
1027-
}// CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
1028-
1029-
if (this._flippedX) {
1030-
tempSwap = top;
1031-
top = bottom;
1032-
bottom = tempSwap;
1033-
}
1034-
1035-
if (this._flippedY) {
1036-
tempSwap = left;
1037-
left = right;
1038-
right = tempSwap;
1039-
}
1040-
1041-
locQuad.bl.texCoords.u = left;
1042-
locQuad.bl.texCoords.v = top;
1043-
locQuad.br.texCoords.u = left;
1044-
locQuad.br.texCoords.v = bottom;
1045-
locQuad.tl.texCoords.u = right;
1046-
locQuad.tl.texCoords.v = top;
1047-
locQuad.tr.texCoords.u = right;
1048-
locQuad.tr.texCoords.v = bottom;
1049-
} else {
1050-
if (cc.FIX_ARTIFACTS_BY_STRECHING_TEXEL) {
1051-
left = (2 * rect.x + 1) / (2 * atlasWidth);
1052-
right = left + (rect.width * 2 - 2) / (2 * atlasWidth);
1053-
top = (2 * rect.y + 1) / (2 * atlasHeight);
1054-
bottom = top + (rect.height * 2 - 2) / (2 * atlasHeight);
1055-
} else {
1056-
left = rect.x / atlasWidth;
1057-
right = (rect.x + rect.width) / atlasWidth;
1058-
top = rect.y / atlasHeight;
1059-
bottom = (rect.y + rect.height) / atlasHeight;
1060-
} // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
1061-
1062-
if (this._flippedX) {
1063-
tempSwap = left;
1064-
left = right;
1065-
right = tempSwap;
1066-
}
1067-
1068-
if (this._flippedY) {
1069-
tempSwap = top;
1070-
top = bottom;
1071-
bottom = tempSwap;
1072-
}
1073-
1074-
locQuad.bl.texCoords.u = left;
1075-
locQuad.bl.texCoords.v = bottom;
1076-
locQuad.br.texCoords.u = right;
1077-
locQuad.br.texCoords.v = bottom;
1078-
locQuad.tl.texCoords.u = left;
1079-
locQuad.tl.texCoords.v = top;
1080-
locQuad.tr.texCoords.u = right;
1081-
locQuad.tr.texCoords.v = top;
1082-
}
1083-
this._quadDirty = true;
1004+
setTextureRect: function (rect, rotated, untrimmedSize) {
1005+
//set needConvert to false
1006+
cc.Sprite.prototype.setTextureRect.call(this, rect, rotated, untrimmedSize, false);
10841007
}
10851008
});
10861009

@@ -1154,40 +1077,7 @@ if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
11541077
}
11551078
};
11561079

1157-
_p.draw = cc.Sprite.prototype.draw;
1158-
1159-
_p.setTextureRect = function (rect, rotated, untrimmedSize) {
1160-
this._rectRotated = rotated || false;
1161-
untrimmedSize = untrimmedSize || rect;
1162-
1163-
this.setContentSize(untrimmedSize);
1164-
this.setVertexRect(rect);
1165-
1166-
var locTextureCoordRect = this._renderCmd._textureCoord;
1167-
locTextureCoordRect.x = rect.x;
1168-
locTextureCoordRect.y = rect.y;
1169-
locTextureCoordRect.renderX = rect.x;
1170-
locTextureCoordRect.renderY = rect.y;
1171-
locTextureCoordRect.width = rect.width;
1172-
locTextureCoordRect.height = rect.height;
1173-
locTextureCoordRect.validRect = !(locTextureCoordRect.width === 0 || locTextureCoordRect.height === 0
1174-
|| locTextureCoordRect.x < 0 || locTextureCoordRect.y < 0);
1175-
1176-
var relativeOffset = this._unflippedOffsetPositionFromCenter;
1177-
if (this._flippedX)
1178-
relativeOffset.x = -relativeOffset.x;
1179-
if (this._flippedY)
1180-
relativeOffset.y = -relativeOffset.y;
1181-
this._offsetPosition.x = relativeOffset.x + (this._contentSize.width - this._rect.width) / 2;
1182-
this._offsetPosition.y = relativeOffset.y + (this._contentSize.height - this._rect.height) / 2;
1183-
1184-
// rendering using batch node
1185-
if (this._batchNode) {
1186-
this.dirty = true;
1187-
}
1188-
};
11891080
_p = null;
1190-
11911081
} else {
11921082
cc.assert(cc.isFunction(cc._tmp.WebGLLabelTTF), cc._LogInfos.MissingFile, "LabelTTFWebGL.js");
11931083
cc._tmp.WebGLLabelTTF();
@@ -1199,7 +1089,6 @@ cc._tmp.PrototypeLabelTTF();
11991089
delete cc._tmp.PrototypeLabelTTF;
12001090

12011091
cc.LabelTTF._textAlign = ["left", "center", "right"];
1202-
12031092
cc.LabelTTF._textBaseline = ["top", "middle", "bottom"];
12041093

12051094
//check the first character
@@ -1259,7 +1148,6 @@ document.body ?
12591148
document.body.appendChild(cc.LabelTTF.__labelHeightDiv);
12601149
}, false);
12611150

1262-
12631151
cc.LabelTTF.__getFontHeightByDiv = function (fontName, fontSize) {
12641152
var clientHeight = cc.LabelTTF.__fontHeightCache[fontName + "." + fontSize];
12651153
if (clientHeight > 0) return clientHeight;

cocos2d/core/labelttf/CCLabelTTFRenderCmd.js

+19
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,23 @@
2222
THE SOFTWARE.
2323
****************************************************************************/
2424

25+
cc.LabelTTF.CanvasRenderCmd = function(renderable){
26+
cc.Sprite.CanvasRenderCmd.call(this, renderable);
27+
28+
this._fontStyleStr = "";
29+
this._shadowColorStr = "rgba(128, 128, 128, 0.5)";
30+
this._strokeColorStr = "";
31+
this._fillColorStr = "rgba(255,255,255,1)";
32+
};
33+
34+
cc.LabelTTF.CanvasRenderCmd.prototype = Object.create(cc.Sprite.CanvasRenderCmd.prototype);
35+
36+
// ----------------------------------- LabelTTF WebGL render cmd ----------------------------
37+
38+
cc.LabelTTF.WebGLRenderCmd = function(renderable){
39+
cc.Sprite.WebGLRenderCmd.call(this, renderable);
40+
41+
};
42+
43+
cc.LabelTFF.WebGLRenderCmd.prototype = Object.create(cc.Sprite.WebGLRenderCmd.prototype);
2544

cocos2d/core/labelttf/LabelTTFWebGL.js

-51
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
****************************************************************************/
2626

2727
cc._tmp.WebGLLabelTTF = function () {
28-
2928
var _p = cc.LabelTTF.prototype;
3029

3130
_p.setColor = cc.Sprite.prototype.setColor;
@@ -78,54 +77,4 @@ cc._tmp.WebGLLabelTTF = function () {
7877
this._needUpdateTexture = true;
7978
}
8079
};
81-
82-
_p.draw = function (ctx) {
83-
if (!this._string || this._string == "")
84-
return;
85-
86-
var gl = ctx || cc._renderContext, locTexture = this._texture;
87-
88-
if (locTexture && locTexture._isLoaded) {
89-
this._shaderProgram.use();
90-
this._shaderProgram.setUniformForModelViewAndProjectionMatrixWithMat4();
91-
92-
cc.glBlendFunc(this._blendFunc.src, this._blendFunc.dst);
93-
cc.glBindTexture2D(locTexture);
94-
95-
cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
96-
97-
gl.bindBuffer(gl.ARRAY_BUFFER, this._quadWebBuffer);
98-
if (this._quadDirty) {
99-
gl.bufferData(gl.ARRAY_BUFFER, this._quad.arrayBuffer, gl.STATIC_DRAW);
100-
this._quadDirty = false;
101-
}
102-
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 24, 0);
103-
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 24, 16);
104-
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, gl.UNSIGNED_BYTE, true, 24, 12);
105-
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
106-
}
107-
108-
if (cc.SPRITE_DEBUG_DRAW === 1) {
109-
// draw bounding box
110-
var locQuad = this._quad;
111-
var verticesG1 = [
112-
cc.p(locQuad.tl.vertices.x, locQuad.tl.vertices.y),
113-
cc.p(locQuad.bl.vertices.x, locQuad.bl.vertices.y),
114-
cc.p(locQuad.br.vertices.x, locQuad.br.vertices.y),
115-
cc.p(locQuad.tr.vertices.x, locQuad.tr.vertices.y)
116-
];
117-
cc._drawingUtil.drawPoly(verticesG1, 4, true);
118-
} else if (cc.SPRITE_DEBUG_DRAW === 2) {
119-
// draw texture box
120-
var drawSizeG2 = this.getTextureRect();
121-
var offsetPixG2X = this.offsetX, offsetPixG2Y = this.offsetY;
122-
var verticesG2 = [cc.p(offsetPixG2X, offsetPixG2Y), cc.p(offsetPixG2X + drawSizeG2.width, offsetPixG2Y),
123-
cc.p(offsetPixG2X + drawSizeG2.width, offsetPixG2Y + drawSizeG2.height), cc.p(offsetPixG2X, offsetPixG2Y + drawSizeG2.height)];
124-
cc._drawingUtil.drawPoly(verticesG2, 4, true);
125-
} // CC_SPRITE_DEBUG_DRAW
126-
cc.g_NumberOfDraws++;
127-
};
128-
129-
//TODO: cc.Sprite.prototype._setTextureRectForWebGL
130-
_p.setTextureRect = cc.Sprite.prototype.setTextureRect;
13180
};

cocos2d/core/renderer/RendererCanvas.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,18 @@ cc.rendererCanvas = {
5858
* drawing all renderer command to cache canvas' context
5959
* @param {CanvasRenderingContext2D} ctx
6060
* @param {Number} [instanceID]
61+
* @param {Number} [scaleX]
62+
* @param {Number} [scaleY]
6163
*/
62-
_renderingToCacheCanvas: function (ctx, instanceID) {
64+
_renderingToCacheCanvas: function (ctx, instanceID, scaleX, scaleY) {
6365
if (!ctx)
6466
cc.log("The context of RenderTexture is invalid.");
65-
67+
scaleX = cc.isUndefined(scaleX) ? 1 : scaleX;
68+
scaleY = cc.isUndefined(scaleY) ? 1 : scaleY;
6669
instanceID = instanceID || this._currentID;
6770
var locCmds = this._cacheToCanvasCmds[instanceID], i, len;
6871
for (i = 0, len = locCmds.length; i < len; i++) {
69-
locCmds[i].rendering(ctx, 1, 1);
72+
locCmds[i].rendering(ctx, scaleX, scaleY);
7073
}
7174
locCmds.length = 0;
7275
var locIDs = this._cacheInstanceIds;

0 commit comments

Comments
 (0)