Skip to content

Commit 1220e31

Browse files
committed
Issue cocos2d#2416: correct some mistakes, let tests running.
2 parents 7a48613 + 64f1bc7 commit 1220e31

11 files changed

+227
-246
lines changed

CCDebugger.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,14 @@ cc._LogInfos = {
144144
Sprite_setTexture_2: "Invalid argument: cc.Sprite.texture setter expects a CCTexture2D.",
145145
Sprite_updateQuadFromSprite_2: "cc.SpriteBatchNode.updateQuadFromSprite(): sprite should be non-null",
146146
Sprite_insertQuadFromSprite_2: "cc.SpriteBatchNode.insertQuadFromSprite(): sprite should be non-null",
147-
Sprite_addChild_6: "cc.SpriteBatchNode.addChild(): child should be non-null",
148147

149148
SpriteBatchNode_addSpriteWithoutQuad: "cc.SpriteBatchNode.addQuadFromSprite(): SpriteBatchNode only supports cc.Sprites as children",
150149
SpriteBatchNode_increaseAtlasCapacity: "cocos2d: CCSpriteBatchNode: resizing TextureAtlas capacity from %s to %s.",
151150
SpriteBatchNode_increaseAtlasCapacity_2: "cocos2d: WARNING: Not enough memory to resize the atlas",
152151
SpriteBatchNode_reorderChild: "cc.SpriteBatchNode.addChild(): Child doesn't belong to Sprite",
153152
SpriteBatchNode_removeChild: "cc.SpriteBatchNode.addChild(): sprite batch node should contain the child",
154153
SpriteBatchNode_addSpriteWithoutQuad_2: "cc.SpriteBatchNode.addQuadFromSprite(): child should be non-null",
155-
SpriteBatchNode_reorderChild_2: "cc.SpriteBatchNode.addChild():child should be non-null",
154+
SpriteBatchNode_reorderChild_2: "cc.SpriteBatchNode.addChild(): child should be non-null",
156155

157156
spriteFrameCache__getFrameConfig: "cocos2d: WARNING: originalWidth/Height not found on the cc.SpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist",
158157
spriteFrameCache_addSpriteFrames: "cocos2d: WARNING: an alias with name %s already exists",

cocos2d/core/base-nodes/CCNodeRenderCmd.js

+54-11
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ cc.Node.RenderCmd.prototype = {
7777
},
7878

7979
detachFromParent: function(){
80-
8180
},
8281

8382
_updateAnchorPointInPoint: function() {
@@ -143,20 +142,21 @@ cc.Node.CanvasRenderCmd.prototype.transform = function(parentCmd, recursive){
143142
return;
144143
var i, len;
145144
for(i = 0, len = locChildren.length; i< len; i++){
146-
locChildren[i].transform(this, recursive);
145+
locChildren[i]._renderCmd.transform(this, recursive);
147146
}
148147
}
149148
};
150149

151150
cc.Node.CanvasRenderCmd.prototype.getNodeToParentTransform = function(){
152-
var node = this._node;
151+
var node = this._node, normalizeDirty = false;
153152
if(node._usingNormalizedPosition && node._parent){ //TODO need refactor
154153
var conSize = node._parent._contentSize;
155154
node._position.x = node._normalizedPosition.x * conSize.width;
156155
node._position.y = node._normalizedPosition.y * conSize.height;
157156
node._normalizedPositionDirty = false;
157+
normalizeDirty = true;
158158
}
159-
if (this._dirtyFlag & cc.Node._dirtyFlags.transformDirty) {
159+
if (normalizeDirty || (this._dirtyFlag & cc.Node._dirtyFlags.transformDirty)) {
160160
var t = this._transform;// quick reference
161161

162162
// base position
@@ -242,7 +242,7 @@ cc.Node.CanvasRenderCmd.prototype.visit = function(parentCmd){
242242

243243
//visit for canvas
244244
var i, children = node._children, child;
245-
_t.transform(parentCmd);
245+
_t._syncStatus(parentCmd);
246246
var len = children.length;
247247
if (len > 0) {
248248
node.sortAllChildren();
@@ -275,14 +275,57 @@ cc.Node.CanvasRenderCmd.prototype.updateStatus = function(){
275275

276276
if(this._dirtyFlag & cc.Node._dirtyFlags.transformDirty){
277277
//update the transform
278-
this.transform(null, true);
278+
this.transform(this.getParentRenderCmd(), true);
279279
}
280280
};
281281

282-
cc.Node.CanvasRenderCmd.prototype._updateDisplayColor = function(parentColor){
283-
this._dirtyFlag = this._dirtyFlag ^ cc.Node._dirtyFlags.colorDirty;
282+
cc.Node.CanvasRenderCmd.prototype._syncStatus = function(parentCmd){
283+
if(this._dirtyFlag & cc.Node._dirtyFlags.colorDirty){
284+
//update the color
285+
this._syncDisplayColor()
286+
}
287+
288+
if(this._dirtyFlag & cc.Node._dirtyFlags.opacityDirty){
289+
//update the opacity
290+
this._syncDisplayOpacity();
291+
}
292+
293+
if(this._dirtyFlag & cc.Node._dirtyFlags.transformDirty){
294+
//update the transform
295+
this.transform(parentCmd);
296+
}
297+
};
298+
299+
cc.Node.CanvasRenderCmd.prototype._syncDisplayColor = function(parentColor){
300+
var node = this._node, locDispColor = this._displayedColor, locRealColor = node._realColor;
301+
if(parentColor === undefined){
302+
var locParent = node._parent;
303+
if (locParent && locParent._cascadeColorEnabled)
304+
parentColor = locParent.getDisplayedColor();
305+
else
306+
parentColor = cc.color.WHITE;
307+
}
308+
locDispColor.r = 0 | (locRealColor.r * parentColor.r / 255.0);
309+
locDispColor.g = 0 | (locRealColor.g * parentColor.g / 255.0);
310+
locDispColor.b = 0 | (locRealColor.b * parentColor.b / 255.0);
311+
this._dirtyFlag ^= cc.Node._dirtyFlags.colorDirty;
312+
};
284313

285-
var locDispColor = this._displayedColor, locRealColor = this._realColor, node = this._node;
314+
cc.Node.CanvasRenderCmd.prototype._syncDisplayOpacity = function(parentOpacity){
315+
var node = this._node;
316+
if(parentOpacity === undefined){
317+
var locParent = node._parent;
318+
parentOpacity = 255;
319+
if (locParent && locParent._cascadeOpacityEnabled)
320+
parentOpacity = locParent.getDisplayedOpacity();
321+
}
322+
this._displayedOpacity = node._realOpacity * parentOpacity / 255.0;
323+
this._dirtyFlag ^= cc.Node._dirtyFlags.opacityDirty;
324+
};
325+
326+
cc.Node.CanvasRenderCmd.prototype._updateDisplayColor = function(parentColor){
327+
var node = this._node;
328+
var locDispColor = this._displayedColor, locRealColor = node._realColor;
286329
var i, len, selChildren, item;
287330
if(this._cascadeColorEnabledDirty && !node._cascadeColorEnabled){
288331
locDispColor.r = locRealColor.r;
@@ -316,11 +359,10 @@ cc.Node.CanvasRenderCmd.prototype._updateDisplayColor = function(parentColor){
316359
}
317360
}
318361
this._cascadeColorEnabledDirty = false;
362+
this._dirtyFlag ^= cc.Node._dirtyFlags.colorDirty;
319363
};
320364

321365
cc.Node.CanvasRenderCmd.prototype._updateDisplayOpacity = function(parentOpacity){
322-
this._dirtyFlag ^= cc.Node._dirtyFlags.opacityDirty;
323-
324366
var node = this._node;
325367
var i, len, selChildren, item;
326368
if(this._cascadeOpacityEnabledDirty && !node._cascadeOpacityEnabled){
@@ -349,6 +391,7 @@ cc.Node.CanvasRenderCmd.prototype._updateDisplayOpacity = function(parentOpacity
349391
}
350392
}
351393
this._cascadeOpacityEnabledDirty = false;
394+
this._dirtyFlag ^= cc.Node._dirtyFlags.opacityDirty;
352395
};
353396

354397
cc.Node.CanvasRenderCmd.prototype.setDirtyFlag = function(dirtyFlag){

cocos2d/core/labelttf/CCLabelTTF.js

+10-25
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,20 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
7272
_string: "",
7373
_originalText: null,
7474

75-
_fontStyleStr: null, //TODO move to render cmd
76-
_fontClientHeight: 18,
77-
7875
// font shadow
7976
_shadowEnabled: false,
8077
_shadowOffset: null,
8178
_shadowOpacity: 0,
8279
_shadowBlur: 0,
83-
_shadowColorStr: null, //TODO move to render cmd
8480
_shadowColor: null,
8581

8682
// font stroke
8783
_strokeEnabled: false,
8884
_strokeColor: null,
8985
_strokeSize: 0,
90-
_strokeColorStr: null, // TODO move to render cmd
9186

9287
// font tint
9388
_textFillColor: null,
94-
_fillColorStr: null, // TODO move to render cmd
9589

9690
_strokeShadowOffsetX: 0,
9791
_strokeShadowOffsetY: 0,
@@ -132,8 +126,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
132126
this._vAlignment = vAlignment;
133127

134128
this._fontSize = fontSize;
135-
this._fontStyleStr = this._fontSize + "px '" + fontName + "'";
136-
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontName, this._fontSize);
129+
this._renderCmd._setFontStyle(this._fontName, fontSize);
137130
this.string = strInfo;
138131
this._renderCmd._setColorsString();
139132
this._renderCmd._updateTexture();
@@ -154,28 +147,23 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
154147
this._hAlignment = cc.TEXT_ALIGNMENT_LEFT;
155148
this._vAlignment = cc.VERTICAL_TEXT_ALIGNMENT_TOP;
156149
this._opacityModifyRGB = false;
157-
this._fontStyleStr = "";
158150
this._fontName = "Arial";
159151

160152
this._shadowEnabled = false;
161153
this._shadowOffset = cc.p(0, 0);
162154
this._shadowOpacity = 0;
163155
this._shadowBlur = 0;
164-
this._shadowColorStr = "rgba(128, 128, 128, 0.5)";
165156

166157
this._strokeEnabled = false;
167158
this._strokeColor = cc.color(255, 255, 255, 255);
168159
this._strokeSize = 0;
169-
this._strokeColorStr = "";
170160

171161
this._textFillColor = cc.color(255, 255, 255, 255);
172-
this._fillColorStr = "rgba(255,255,255,1)";
173162
this._strokeShadowOffsetX = 0;
174163
this._strokeShadowOffsetY = 0;
175164
this._needUpdateTexture = false;
176165

177166
this._lineWidths = [];
178-
179167
this._renderCmd._setColorsString();
180168

181169
if (fontName && fontName instanceof cc.FontDefinition) {
@@ -193,8 +181,8 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
193181
return "<cc.LabelTTF | FontName =" + this._fontName + " FontSize = " + this._fontSize.toFixed(1) + ">";
194182
},
195183

196-
getLineHiehgt: function () {
197-
return this._lineHeight || this._fontClientHeight;
184+
getLineHeight: function () {
185+
return this._lineHeight || this._renderCmd._getFontClientHeight();
198186
},
199187

200188
setLineHeight: function (lineHeight) {
@@ -258,7 +246,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
258246
*/
259247
initWithStringAndTextDefinition: function (text, textDefinition) {
260248
// shader program
261-
this.setShaderProgram(cc.shaderCache.programForKey(cc.LabelTTF._SHADER_PROGRAM));
249+
this.setShaderProgram(cc.shaderCache.programForKey(cc.LabelTTF._SHADER_PROGRAM)); //TODO
262250
// prepare everything needed to render the label
263251
this._updateWithTextDefinition(textDefinition, false);
264252
// set the string
@@ -514,8 +502,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
514502

515503
this._fontName = textDefinition.fontName;
516504
this._fontSize = textDefinition.fontSize || 12;
517-
this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";
518-
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName, this._fontSize);
505+
this._renderCmd._setFontStyle(this._fontName, this._fontSize)
519506

520507
// shadow
521508
if (textDefinition.shadowEnabled)
@@ -678,8 +665,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
678665
setFontSize: function (fontSize) {
679666
if (this._fontSize !== fontSize) {
680667
this._fontSize = fontSize;
681-
this._fontStyleStr = fontSize + "px '" + this._fontName + "'";
682-
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName, fontSize);
668+
this._renderCmd._setFontStyle(this._fontName, this._fontSize);
683669
// Force update
684670
this._setUpdateTextureDirty();
685671
}
@@ -692,23 +678,22 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
692678
setFontName: function (fontName) {
693679
if (this._fontName && this._fontName != fontName) {
694680
this._fontName = fontName;
695-
this._fontStyleStr = this._fontSize + "px '" + fontName + "'";
696-
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontName, this._fontSize);
681+
this._renderCmd._setFontStyle(this._fontName, this._fontSize);
697682
// Force update
698683
this._setUpdateTextureDirty();
699684
}
700685
},
701686

702687
_getFont: function () {
703-
return this._fontStyleStr;
688+
return this._renderCmd._getFontStyle();
704689
},
705690
_setFont: function (fontStyle) {
706691
var res = cc.LabelTTF._fontStyleRE.exec(fontStyle);
707692
if (res) {
708693
this._fontSize = parseInt(res[1]);
709694
this._fontName = res[2];
710-
this._fontStyleStr = fontStyle;
711-
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName, this._fontSize);
695+
this._renderCmd._setFontStyle(this._fontName, this._fontSize);
696+
712697
// Force update
713698
this._setUpdateTextureDirty();
714699
}

cocos2d/core/labelttf/CCLabelTTFRenderCmd.js

+28-15
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ cc.LabelTTF.RenderCmd.prototype._getLabelContext = function () {
6868
return this._labelContext;
6969
};
7070

71+
cc.LabelTTF.RenderCmd.prototype._setFontStyle = function(fontName, fontSize){
72+
this._fontStyleStr = fontSize + "px '" + fontName + "'";
73+
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontName, fontSize);
74+
};
75+
76+
cc.LabelTTF.RenderCmd.prototype._getFontStyle = function () {
77+
return this._fontStyleStr;
78+
};
79+
80+
cc.LabelTTF.RenderCmd.prototype._getFontClientHeight = function(){
81+
return this._fontClientHeight;
82+
};
83+
7184
cc.LabelTTF.RenderCmd.prototype._updateTexture = function () {
7285
var node = this._node;
7386
var locContext = this._getLabelContext(), locLabelCanvas = this._labelCanvas;
@@ -82,7 +95,7 @@ cc.LabelTTF.RenderCmd.prototype._updateTexture = function () {
8295
}
8396

8497
//set size for labelCanvas
85-
locContext.font = node._fontStyleStr;
98+
locContext.font = this._fontStyleStr;
8699
this._updateTTF();
87100
var width = locContentSize.width, height = locContentSize.height;
88101
var flag = locLabelCanvas.width == width && locLabelCanvas.height == height;
@@ -145,15 +158,15 @@ cc.LabelTTF.RenderCmd.prototype._updateTTF = function(){
145158
if (locDimensionsWidth === 0) {
146159
if (this._isMultiLine)
147160
locSize = cc.size(0 | (Math.max.apply(Math, locLineWidth) + locStrokeShadowOffsetX),
148-
0 | ((node._fontClientHeight * this._strings.length) + locStrokeShadowOffsetY));
161+
0 | ((this._fontClientHeight * this._strings.length) + locStrokeShadowOffsetY));
149162
else
150-
locSize = cc.size(0 | (this._measure(node._string) + locStrokeShadowOffsetX), 0 | (node._fontClientHeight + locStrokeShadowOffsetY));
163+
locSize = cc.size(0 | (this._measure(node._string) + locStrokeShadowOffsetX), 0 | (this._fontClientHeight + locStrokeShadowOffsetY));
151164
} else {
152165
if (node._dimensions.height === 0) {
153166
if (this._isMultiLine)
154-
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | ((node.getLineHiehgt() * this._strings.length) + locStrokeShadowOffsetY));
167+
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | ((node.getLineHeight() * this._strings.length) + locStrokeShadowOffsetY));
155168
else
156-
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | (node.getLineHiehgt() + locStrokeShadowOffsetY));
169+
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | (node.getLineHeight() + locStrokeShadowOffsetY));
157170
} else {
158171
//dimension is already set, contentSize must be same as dimension
159172
locSize = cc.size(0 | (locDimensionsWidth + locStrokeShadowOffsetX), 0 | (node._dimensions.height + locStrokeShadowOffsetY));
@@ -174,28 +187,28 @@ cc.LabelTTF.RenderCmd.prototype._drawTTFInCanvas = function (context) {
174187
return;
175188
var node = this._node;
176189
var locStrokeShadowOffsetX = node._strokeShadowOffsetX, locStrokeShadowOffsetY = node._strokeShadowOffsetY;
177-
var locContentSizeHeight = node._contentSize.height - locStrokeShadowOffsetY, locVAlignment = node._vAlignment, locHAlignment = node._hAlignment,
178-
locFontHeight = node._fontClientHeight, locStrokeSize = node._strokeSize;
190+
var locContentSizeHeight = node._contentSize.height - locStrokeShadowOffsetY, locVAlignment = node._vAlignment,
191+
locHAlignment = node._hAlignment, locStrokeSize = node._strokeSize;
179192

180193
context.setTransform(1, 0, 0, 1, 0 + locStrokeShadowOffsetX * 0.5, locContentSizeHeight + locStrokeShadowOffsetY * 0.5);
181194

182195
//this is fillText for canvas
183-
if (context.font != node._fontStyleStr)
184-
context.font = node._fontStyleStr;
185-
context.fillStyle = node._fillColorStr;
196+
if (context.font != this._fontStyleStr)
197+
context.font = this._fontStyleStr;
198+
context.fillStyle = this._fillColorStr;
186199

187200
var xOffset = 0, yOffset = 0;
188201
//stroke style setup
189202
var locStrokeEnabled = node._strokeEnabled;
190203
if (locStrokeEnabled) {
191204
context.lineWidth = locStrokeSize * 2;
192-
context.strokeStyle = node._strokeColorStr;
205+
context.strokeStyle = this._strokeColorStr;
193206
}
194207

195208
//shadow style setup
196209
if (node._shadowEnabled) {
197210
var locShadowOffset = node._shadowOffset;
198-
context.shadowColor = node._shadowColorStr;
211+
context.shadowColor = this._shadowColorStr;
199212
context.shadowOffsetX = locShadowOffset.x;
200213
context.shadowOffsetY = -locShadowOffset.y;
201214
context.shadowBlur = node._shadowBlur;
@@ -206,9 +219,9 @@ cc.LabelTTF.RenderCmd.prototype._drawTTFInCanvas = function (context) {
206219

207220
var locContentWidth = node._contentSize.width - locStrokeShadowOffsetX;
208221

209-
//lineHiehgt
210-
var lineHeight = node.getLineHiehgt();
211-
var transformTop = (lineHeight - node._fontClientHeight) / 2;
222+
//lineHeight
223+
var lineHeight = node.getLineHeight();
224+
var transformTop = (lineHeight - this._fontClientHeight) / 2;
212225

213226
if (locHAlignment === cc.TEXT_ALIGNMENT_RIGHT)
214227
xOffset += locContentWidth;

0 commit comments

Comments
 (0)