From 7e90c1618649e44153cef9c1bbf3967bbe8ad9df Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 11 Jan 2016 18:09:02 +0800 Subject: [PATCH 1/3] simplify setContentSize logic --- cocos2d/core/label/CCSGLabel.js | 67 ++++++++++++--------------------- 1 file changed, 25 insertions(+), 42 deletions(-) diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index 81c1eed3d0..c37591fef4 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -382,37 +382,7 @@ _ccsg.Label = _ccsg.Node.extend({ return; } - var oldWidth = this._contentSize.width; - var oldHeight = this._contentSize.height; - if (this._labelType === _ccsg.Label.Type.TTF || - this._labelType === _ccsg.Label.Type.SystemFont) { - _ccsg.Node.prototype.setContentSize.call(this, size, height); - if (oldWidth === this._contentSize.width && oldHeight === this._contentSize.height) { - return; - } - var newWidth = size.width || size; - var newHeight = size.height || height; - this._labelWidth = newWidth; - this._labelHeight = newHeight; - this._labelDimensions.width = newWidth; - this._labelDimensions.height = newHeight; - - this._maxLineWidth = newWidth; - - this._notifyLabelSkinDirty(); - } else if (this._labelType === _ccsg.Label.Type.BMFont) { - if (!height) { - if (oldWidth === size.width && oldHeight === size.height) { - return; - } - this._setDimensions(size.width, size.height); - } else { - if (oldWidth === size && oldHeight === height) { - return; - } - this._setDimensions(size, height); - } - } + this._setDimensions(size, height); }, setBlendFunc: function(src, dst) { @@ -625,18 +595,31 @@ cc.BMFontHelper = { this._lettersInfo[letterIndex]._positionY = letterPosition.y; }, - _setDimensions: function(width, height) { - if (this._overFlow === _ccsg.Label.Overflow.RESIZE_HEIGHT) { - height = 0; - } - if (height !== this._labelHeight || width !== this._labelWidth) { - this._labelWidth = width; - this._labelHeight = height; - this._labelDimensions.width = width; - this._labelDimensions.height = height; + _setDimensions: function(size, height) { + var newWidth = size.width || size; + var newHeight = size.height || height; - this._maxLineWidth = width; - if (this._overFlow === _ccsg.Label.Overflow.SHRINK) { + if (this._overFlow === _ccsg.Label.Overflow.RESIZE_HEIGHT) { + newHeight = 0; + } + + if (this._overFlow === _ccsg.Label.Overflow.NONE) { + newWidth = 0; + newHeight = 0; + } + + if (newHeight !== this._labelHeight || newWidth !== this._labelWidth) { + _ccsg.Node.prototype.setContentSize.call(this, size, height); + + this._labelWidth = newWidth; + this._labelHeight = newHeight; + this._labelDimensions.width = newWidth; + this._labelDimensions.height = newHeight; + + this._maxLineWidth = newWidth; + + if (this._labelType === _ccsg.Label.Type.BMFont + && this._overFlow === _ccsg.Label.Overflow.SHRINK) { if (this._bmFontSize > 0) { this._restoreFontSize(); } From bc962b11699ba04f910ed5fd24f9cc47a81903bc Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 11 Jan 2016 18:24:25 +0800 Subject: [PATCH 2/3] fix ttf NONE content size error --- cocos2d/core/label/CCSGLabel.js | 45 +++++++++++-------- .../core/label/CCSGLabelCanvasRenderCmd.js | 8 ++-- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index c37591fef4..0fcd3c4004 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -258,16 +258,22 @@ _ccsg.Label = _ccsg.Node.extend({ setOverflow: function(overflow) { if (this._overFlow === overflow) return; + this._overFlow = overflow; - if (this._overFlow === _ccsg.Label.Overflow.RESIZE_HEIGHT) { - this._setDimensions(this._labelDimensions.width, 0); - this._isWrapText = true; + + if (this._labelType === _ccsg.Label.Type.BMFont) { + + if ( this._overFlow === _ccsg.Label.Overflow.RESIZE_HEIGHT) { + this._setDimensions(this._labelDimensions.width, 0); + this._isWrapText = true; + } + + if (this._overFlow === _ccsg.Label.Overflow.NONE) { + this._isWrapText = false; + this._setDimensions(0, 0); + } } - if (this._overFlow === _ccsg.Label.Overflow.NONE) { - this._isWrapText = false; - this._setDimensions(0, 0); - } this._rescaleWithOriginalFontSize(); this._notifyLabelSkinDirty(); @@ -325,9 +331,6 @@ _ccsg.Label = _ccsg.Node.extend({ //todo add bmfont here this._labelType = _ccsg.Label.Type.BMFont; this._initBMFontWithString(this._string, fontHandle); - if(this._overFlow === _ccsg.Label.Overflow.RESIZE_HEIGHT) { - this._setDimensions(this._labelDimensions.width, 0); - } } }, @@ -599,17 +602,22 @@ cc.BMFontHelper = { var newWidth = size.width || size; var newHeight = size.height || height; - if (this._overFlow === _ccsg.Label.Overflow.RESIZE_HEIGHT) { - newHeight = 0; + _ccsg.Node.prototype.setContentSize.call(this, size, height); + + if (this._labelType === _ccsg.Label.Type.BMFont) { + + if (this._overFlow === _ccsg.Label.Overflow.RESIZE_HEIGHT) { + newHeight = 0; + } + + if (this._overFlow === _ccsg.Label.Overflow.NONE) { + newWidth = 0; + newHeight = 0; + } } - if (this._overFlow === _ccsg.Label.Overflow.NONE) { - newWidth = 0; - newHeight = 0; - } if (newHeight !== this._labelHeight || newWidth !== this._labelWidth) { - _ccsg.Node.prototype.setContentSize.call(this, size, height); this._labelWidth = newWidth; this._labelHeight = newHeight; @@ -618,8 +626,7 @@ cc.BMFontHelper = { this._maxLineWidth = newWidth; - if (this._labelType === _ccsg.Label.Type.BMFont - && this._overFlow === _ccsg.Label.Overflow.SHRINK) { + if (this._labelType === _ccsg.Label.Type.BMFont && this._overFlow === _ccsg.Label.Overflow.SHRINK) { if (this._bmFontSize > 0) { this._restoreFontSize(); } diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index 2a889f1c2c..7b8de1232f 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -214,11 +214,11 @@ this._splitedStrings = this._splitedStrings.concat(this._fragmentText(paragraphedStrings[i], canvasSizeX, ctx)); } canvasSizeY = this._splitedStrings.length * this._getLineHeight(); - node.setContentSize(cc.size(canvasSizeX, canvasSizeY)); + node._setDimensions(cc.size(canvasSizeX, canvasSizeY)); } else { this._splitedStrings = paragraphedStrings; canvasSizeY = this._splitedStrings.length * this._getLineHeight(); - node.setContentSize(cc.size(canvasSizeX, canvasSizeY)); + node._setDimensions(cc.size(canvasSizeX, canvasSizeY)); } } else if(_ccsg.Label.Overflow.SHRINK === node._overFlow) { this._splitedStrings = paragraphedStrings; @@ -252,12 +252,14 @@ } } else if(_ccsg.Label.Overflow.NONE == node._overFlow) { this._splitedStrings = paragraphedStrings; + canvasSizeX = 0; + canvasSizeY = 0; for (var i = 0; i < paragraphedStrings.length; ++i) { var paraLength = ctx.measureText(paragraphedStrings[i]).width; canvasSizeX = canvasSizeX > paraLength ? canvasSizeX : paraLength; } canvasSizeY = this._splitedStrings.length * this._getLineHeight(); - node.setContentSize(cc.size(canvasSizeX, canvasSizeY)); + node._setDimensions(cc.size(canvasSizeX, canvasSizeY)); } else { //others treat it as clamp if (node._isWrapText) { From 035e94bd51b8192e2453ca666403f15ef66b54cb Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 11 Jan 2016 20:13:52 +0800 Subject: [PATCH 3/3] make code more readable --- cocos2d/core/label/CCSGLabel.js | 41 ++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index 0fcd3c4004..65dd0fe6f1 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -256,26 +256,41 @@ _ccsg.Label = _ccsg.Node.extend({ return this._fontSize; }, + _updateWrapText: function(overflow){ + if ( overflow === _ccsg.Label.Overflow.RESIZE_HEIGHT) { + this._isWrapText = true; + } + + if (overflow === _ccsg.Label.Overflow.NONE) { + this._isWrapText = false; + } + }, + + + _setOverflowBMFont: function () { + if (this._labelType === _ccsg.Label.Type.BMFont) { + + if ( this._overFlow === _ccsg.Label.Overflow.RESIZE_HEIGHT) { + this._setDimensions(this._labelDimensions.width, 0); + } + + if (this._overFlow === _ccsg.Label.Overflow.NONE) { + this._setDimensions(0, 0); + } + + this._rescaleWithOriginalFontSize(); + } + }, + setOverflow: function(overflow) { if (this._overFlow === overflow) return; this._overFlow = overflow; - if (this._labelType === _ccsg.Label.Type.BMFont) { + this._updateWrapText(this._overFlow); - if ( this._overFlow === _ccsg.Label.Overflow.RESIZE_HEIGHT) { - this._setDimensions(this._labelDimensions.width, 0); - this._isWrapText = true; - } + this._setOverflowBMFont(); - if (this._overFlow === _ccsg.Label.Overflow.NONE) { - this._isWrapText = false; - this._setDimensions(0, 0); - } - } - - - this._rescaleWithOriginalFontSize(); this._notifyLabelSkinDirty(); },