diff --git a/cocos2d/core/components/CCLayout.js b/cocos2d/core/components/CCLayout.js index 96941cd8d7..c9747ef0d2 100644 --- a/cocos2d/core/components/CCLayout.js +++ b/cocos2d/core/components/CCLayout.js @@ -29,9 +29,9 @@ */ var Type = cc.Enum({ /** - *@property {Number} BASIC + *@property {Number} NONE */ - BASIC: 0, + NONE: 0, /** * @property {Number} HORIZONTAL */ @@ -99,14 +99,27 @@ var Layout = cc.Class({ /** * The layout type. * @property {Layout.Type} layoutType - * @default Layout.Type.BASIC + * @default Layout.Type.NONE */ layoutType: { - default: Type.BASIC, + default: Type.NONE, type: Type, notify: function() { this._doLayoutDirty(); - } + }, + tooltip: 'i18n:COMPONENT.layout.layout_type' + }, + + /** + * Whether allow layout to adjust size. + * @property {Boolean} autoResize + * @default true + * @readonly + */ + autoResize: { + default: true, + readonly: true, + tooltip: 'i18n:COMPONENT.layout.auto_resize' }, /** @@ -117,18 +130,32 @@ var Layout = cc.Class({ default: 0, notify: function() { this._doLayoutDirty(); - } + }, + tooltip: 'i18n:COMPONENT.layout.margin' }, /** - * The distance between each element in layout. - * @property {Number} spacing + * The distance in x-axis between each element in layout. + * @property {Number} spacingX */ - spacing: { + spacingX: { default: 0, notify: function() { this._doLayoutDirty(); - } + }, + tooltip: 'i18n:COMPONENT.layout.space_x' + }, + + /** + * The distance in y-axis between each element in layout. + * @property {Number} spacingY + */ + spacingY: { + default: 0, + notify: function() { + this._doLayoutDirty(); + }, + tooltip: 'i18n:COMPONENT.layout.space_y' }, /** @@ -141,7 +168,8 @@ var Layout = cc.Class({ type: VerticalDirection, notify: function() { this._doLayoutDirty(); - } + }, + tooltip: 'i18n:COMPONENT.layout.vertical_direction' }, /** @@ -154,7 +182,8 @@ var Layout = cc.Class({ type: HorizontalDirection, notify: function() { this._doLayoutDirty(); - } + }, + tooltip: 'i18n:COMPONENT.layout.horizontal_direction' }, }, @@ -183,6 +212,7 @@ var Layout = cc.Class({ children.forEach(function(child) { child.on('size-changed', this._doLayoutDirty, this); child.on('position-changed', this._doLayoutDirty, this); + child.on('anchor-changed', this._doLayoutDirty, this); }.bind(this)); }, @@ -204,7 +234,7 @@ var Layout = cc.Class({ newWidth += child.width; }); - newWidth += (children.length - 1) * this.spacing + 2 * this.margin; + newWidth += (children.length - 1) * this.spacingX + 2 * this.margin; this.node.setContentSize(newWidth, layoutSize.height); var leftBoundaryOfLayout = -layoutAnchor.x * newWidth; @@ -213,14 +243,14 @@ var Layout = cc.Class({ leftBoundaryOfLayout = (1 - layoutAnchor.x) * newWidth; } - var nextX = leftBoundaryOfLayout + sign * this.margin - sign * this.spacing; + var nextX = leftBoundaryOfLayout + sign * this.margin - sign * this.spacingX; children.forEach(function(child) { var anchorX = child.anchorX; if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) { anchorX = 1 - child.anchorX; } - nextX = nextX + sign * anchorX * child.width + sign * this.spacing; + nextX = nextX + sign * anchorX * child.width + sign * this.spacingX; child.setPosition(cc.p(nextX, child.y)); @@ -236,7 +266,7 @@ var Layout = cc.Class({ newHeight += child.height; }); - newHeight += (children.length - 1) * this.spacing + 2 * this.margin; + newHeight += (children.length - 1) * this.spacingY + 2 * this.margin; this.node.setContentSize(layoutSize.width, newHeight); var bottomBoundaryOfLayout = -layoutAnchor.y * newHeight; @@ -245,14 +275,14 @@ var Layout = cc.Class({ bottomBoundaryOfLayout = (1 - layoutAnchor.y) * newHeight; } - var nextY = bottomBoundaryOfLayout + sign * this.margin - sign * this.spacing; + var nextY = bottomBoundaryOfLayout + sign * this.margin - sign * this.spacingY; children.forEach(function(child) { var anchorY = child.anchorY; if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) { anchorY = 1 - child.anchorY; } - nextY = nextY + sign * anchorY * child.height + sign * this.spacing; + nextY = nextY + sign * anchorY * child.height + sign * this.spacingY; child.setPosition(cc.p(child.x, nextY)); @@ -260,6 +290,34 @@ var Layout = cc.Class({ }.bind(this)); }, + _doLayoutBasic: function(layoutAnchor, layoutSize, children) { + var allChildrenBoundingBox = null; + + children.forEach(function(child){ + if(!allChildrenBoundingBox){ + allChildrenBoundingBox = child.getBoundingBoxToWorld(); + } else { + allChildrenBoundingBox = cc.rectUnion(allChildrenBoundingBox, child.getBoundingBoxToWorld()); + } + }); + + if(allChildrenBoundingBox) { + var leftBottomInParentSpace = this.node.parent.convertToNodeSpaceAR(cc.p(allChildrenBoundingBox.x, allChildrenBoundingBox.y)); + var rightTopInParentSpace = this.node.parent.convertToNodeSpaceAR(cc.p(allChildrenBoundingBox.x + allChildrenBoundingBox.width, + allChildrenBoundingBox.y + allChildrenBoundingBox.height)); + + var newSize = cc.size(rightTopInParentSpace.x - leftBottomInParentSpace.x, + rightTopInParentSpace.y - leftBottomInParentSpace.y); + + var layoutPosition = this.node.getPosition(); + var newAnchor = cc.p((layoutPosition.x - leftBottomInParentSpace.x) / newSize.width, + (layoutPosition.y - leftBottomInParentSpace.y) / newSize.height); + + this.node.setAnchorPoint(newAnchor); + this.node.setContentSize(newSize); + } + }, + _doLayout: function() { var children = this.node.children; var layoutAnchor = this.node.getAnchorPoint(); @@ -269,11 +327,13 @@ var Layout = cc.Class({ this._doLayoutHorizontally(layoutAnchor, layoutSize, children); } else if (this.layoutType === Type.VERTICAL) { this._doLayoutVertically(layoutAnchor, layoutSize, children); + } else if (this.layoutType === Type.NONE) { + this._doLayoutBasic(layoutAnchor, layoutSize, children); } }, lateUpdate: function() { - if (this._layoutDirty) { + if (this._layoutDirty && this.node.children.length > 0) { this._doLayout(); this._layoutDirty = false; } diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index b0b44ad0eb..95b2e440de 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -72,7 +72,7 @@ var Sprite = cc.Class({ var lastSprite = this._spriteFrame; this._spriteFrame = value; if (this._sgNode) { - this._applySprite(this._sgNode, lastSprite); + this._applySpriteFrame(this._sgNode, lastSprite); // color cleared after reset texture, should reapply color this._sgNode.setColor(this.node._color); this._sgNode.setOpacity(this.node._opacity); @@ -315,7 +315,7 @@ var Sprite = cc.Class({ }, _applyCapInset: function (sgNode) { - if (this._type === SpriteType.SLICED) { + if (this._type === SpriteType.SLICED && this._spriteFrame) { sgNode = sgNode || this._sgNode; sgNode.setInsetTop(this._spriteFrame.insetTop); sgNode.setInsetBottom(this._spriteFrame.insetBottom); @@ -326,7 +326,7 @@ var Sprite = cc.Class({ _applySpriteSize: function (sgNode) { sgNode = sgNode || this._sgNode; - if (this._useOriginalSize) { + if (this._useOriginalSize && this._spriteFrame) { var rect = this._spriteFrame.getRect(); this.node.setContentSize(cc.size(rect.width, rect.height)); } @@ -335,30 +335,28 @@ var Sprite = cc.Class({ } }, - _applySprite: function (sgNode, oldSprite) { - if (oldSprite && oldSprite.off) { - oldSprite.off('load', this._applyCapInset, this); + _onSpriteFrameLoaded: function (event, sgNode) { + var self = this; + sgNode = sgNode || this._sgNode; + sgNode.setSpriteFrame(self._spriteFrame); + self._applyCapInset(sgNode); + self._applySpriteSize(); + if ( !sgNode.isVisible() ) { + sgNode.setVisible(true); + } + }, + + _applySpriteFrame: function (sgNode, oldFrame) { + if (oldFrame && oldFrame.off) { + oldFrame.off('load', this._onSpriteFrameLoaded, this); } if (this._spriteFrame) { - if (!sgNode.isVisible()) { - sgNode.setVisible(true); - } - - sgNode.setSpriteFrame(this._spriteFrame); - var locLoaded = this._spriteFrame.textureLoaded(); - if (!locLoaded) { - if (!this._useOriginalSize) { - sgNode.setContentSize(this.node.getContentSize(true)); - } - this._spriteFrame.once('load', function () { - this._applyCapInset(); - this._applySpriteSize(); - }, this); + if (this._spriteFrame.textureLoaded()) { + this._onSpriteFrameLoaded(null, sgNode); } else { - this._applyCapInset(sgNode); - this._applySpriteSize(sgNode); + this._spriteFrame.once('load', this._onSpriteFrameLoaded, this); } } else { @@ -373,13 +371,28 @@ var Sprite = cc.Class({ _createSgNode: function () { var sgNode = new cc.Scale9Sprite(); + + // should keep the size of the sg node the same as entity, + // otherwise setContentSize may not take effect + sgNode.setContentSize(this.node.getContentSize(true)); + + this._applySpriteSize(sgNode); sgNode.setRenderingType(this._type); - this._applySprite(sgNode, null); + this._applySpriteFrame(sgNode, null); + return sgNode; }, _resized: function () { - this.useOriginalSize = false; + if (this._useOriginalSize && this._spriteFrame) { + var rect = this._spriteFrame.getRect(); + var expectedW = rect.width; + var expectedH = rect.height; + var actualSize = this.node.getContentSize(); + if (expectedW !== actualSize.width || expectedH !== actualSize.height) { + this.useOriginalSize = false; + } + } }, }); diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index 78a5a5d5a1..3d0ab9ef4d 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -887,7 +887,8 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ * @return {Rect} The calculated bounding box of the node */ getBoundingBox: function () { - return this._sgNode.getBoundingBox(); + var rect = cc.rect(0, 0, this._contentSize.width, this._contentSize.height); + return cc._rectApplyAffineTransformIn(rect, this.getNodeToParentTransform()); }, /** diff --git a/cocos2d/labels/CCSGLabel.js b/cocos2d/labels/CCSGLabel.js index 5263388e85..4c0dca2acc 100644 --- a/cocos2d/labels/CCSGLabel.js +++ b/cocos2d/labels/CCSGLabel.js @@ -217,6 +217,11 @@ _ccsg.Label = _ccsg.Node.extend({ }, setString: function(string) { + //convert param to string + if (typeof string !== 'string') { + string = '' + string; + } + if (this._string === string) return; this._string = string; this._notifyLabelSkinDirty(); diff --git a/cocos2d/labels/CCSGLabelCanvasRenderCmd.js b/cocos2d/labels/CCSGLabelCanvasRenderCmd.js index 8f2cc0dd1c..4453ce2a70 100644 --- a/cocos2d/labels/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/labels/CCSGLabelCanvasRenderCmd.js @@ -131,6 +131,7 @@ if (label_symbolRex.test(sLine || tmpText)) { result = label_lastWordRex.exec(sText); fuzzyLen -= result ? result[0].length : 0; + if (fuzzyLen === 0) fuzzyLen = 1; sLine = text.substr(fuzzyLen); sText = text.substr(0, fuzzyLen);