From 70bf3e325bcfdbe35219303b81e8eb4bb6347a16 Mon Sep 17 00:00:00 2001 From: knox Date: Wed, 2 May 2018 17:45:35 +0800 Subject: [PATCH 1/3] fix layout newChildWidth bug for cocos-creator/engine/issues/2383 --- cocos2d/core/components/CCLayout.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/components/CCLayout.js b/cocos2d/core/components/CCLayout.js index 7315b320d4..be07bcfdb0 100644 --- a/cocos2d/core/components/CCLayout.js +++ b/cocos2d/core/components/CCLayout.js @@ -520,9 +520,17 @@ var Layout = cc.Class({ var maxHeightChildAnchorY = 0; + var activeChildCount = 0; + children.forEach(function(child) { + if (!child.activeInHierarchy) { + return; + } + activeChildCount++; + }); + var newChildWidth = this.cellSize.width; if (this.type !== Type.GRID && this.resizeMode === ResizeMode.CHILDREN) { - newChildWidth = (baseWidth - (this.paddingLeft + this.paddingRight) - (children.length - 1) * this.spacingX) / children.length; + newChildWidth = (baseWidth - (this.paddingLeft + this.paddingRight) - (activeChildCount - 1) * this.spacingX) / activeChildCount; } children.forEach(function(child) { @@ -653,9 +661,17 @@ var Layout = cc.Class({ var containerResizeBoundary = 0; var maxWidthChildAnchorX = 0; + var activeChildCount = 0; + children.forEach(function(child) { + if (!child.activeInHierarchy) { + return; + } + activeChildCount++; + }); + var newChildHeight = this.cellSize.height; if (this.type !== Type.GRID && this.resizeMode === ResizeMode.CHILDREN) { - newChildHeight = (baseHeight - (this.paddingTop + this.paddingBottom) - (children.length - 1) * this.spacingY) / children.length; + newChildHeight = (baseHeight - (this.paddingTop + this.paddingBottom) - (activeChildCount - 1) * this.spacingY) / activeChildCount; } children.forEach(function(child) { From 06cc115b767870c184847b6080be4de881bdd8e5 Mon Sep 17 00:00:00 2001 From: knox Date: Thu, 3 May 2018 09:44:06 +0800 Subject: [PATCH 2/3] improve layout code --- cocos2d/core/components/CCLayout.js | 134 +++++++++++++++------------- 1 file changed, 71 insertions(+), 63 deletions(-) diff --git a/cocos2d/core/components/CCLayout.js b/cocos2d/core/components/CCLayout.js index be07bcfdb0..7d6e2ffa3b 100644 --- a/cocos2d/core/components/CCLayout.js +++ b/cocos2d/core/components/CCLayout.js @@ -191,10 +191,10 @@ var Layout = cc.Class({ */ type: { type: Type, - get: function() { + get: function () { return this._N$layoutType; }, - set: function(value) { + set: function (value) { this._N$layoutType = value; if (CC_EDITOR && this.type !== Type.NONE && this._resize === ResizeMode.CONTAINER && !cc.engine.isPlaying) { @@ -222,10 +222,10 @@ var Layout = cc.Class({ type: ResizeMode, tooltip: CC_DEV && 'i18n:COMPONENT.layout.resize_mode', animatable: false, - get: function() { + get: function () { return this._resize; }, - set: function(value) { + set: function (value) { if (this.type === Type.NONE && value === ResizeMode.CHILDREN) { return; } @@ -251,7 +251,7 @@ var Layout = cc.Class({ default: cc.size(40, 40), tooltip: CC_DEV && 'i18n:COMPONENT.layout.cell_size', type: cc.Size, - notify: function() { + notify: function () { this._doLayoutDirty(); }, }, @@ -267,7 +267,7 @@ var Layout = cc.Class({ default: AxisDirection.HORIZONTAL, tooltip: CC_DEV && 'i18n:COMPONENT.layout.start_axis', type: AxisDirection, - notify: function() { + notify: function () { if (CC_EDITOR && this._resize === ResizeMode.CONTAINER && !cc.engine.isPlaying) { var reLayouted = _Scene.DetectConflict.checkConflict_Layout(this); if (reLayouted) { @@ -341,7 +341,7 @@ var Layout = cc.Class({ */ spacingX: { default: 0, - notify: function() { + notify: function () { this._doLayoutDirty(); }, tooltip: CC_DEV && 'i18n:COMPONENT.layout.space_x' @@ -354,7 +354,7 @@ var Layout = cc.Class({ */ spacingY: { default: 0, - notify: function() { + notify: function () { this._doLayoutDirty(); }, tooltip: CC_DEV && 'i18n:COMPONENT.layout.space_y' @@ -370,7 +370,7 @@ var Layout = cc.Class({ verticalDirection: { default: VerticalDirection.TOP_TO_BOTTOM, type: VerticalDirection, - notify: function() { + notify: function () { this._doLayoutDirty(); }, tooltip: CC_DEV && 'i18n:COMPONENT.layout.vertical_direction', @@ -387,7 +387,7 @@ var Layout = cc.Class({ horizontalDirection: { default: HorizontalDirection.LEFT_TO_RIGHT, type: HorizontalDirection, - notify: function() { + notify: function () { this._doLayoutDirty(); }, tooltip: CC_DEV && 'i18n:COMPONENT.layout.horizontal_direction', @@ -411,14 +411,14 @@ var Layout = cc.Class({ this._N$padding = 0; }, - onEnable: function() { + onEnable: function () { this._addEventListeners(); - if(cc.sizeEqualToSize(this.node.getContentSize(), cc.size(0, 0))) { + if (cc.sizeEqualToSize(this.node.getContentSize(), cc.size(0, 0))) { this.node.setContentSize(this._layoutSize); } - if(this._N$padding !== 0) { + if (this._N$padding !== 0) { this._migratePaddingData(); } @@ -429,7 +429,7 @@ var Layout = cc.Class({ this._removeEventListeners(); }, - _doLayoutDirty : function () { + _doLayoutDirty: function () { this._layoutDirty = true; }, @@ -453,27 +453,29 @@ var Layout = cc.Class({ this._removeChildrenEventListeners(); }, - _addChildrenEventListeners: function() { + _addChildrenEventListeners: function () { var children = this.node.children; - children.forEach(function (child) { + for (var i = 0; i < children.length; ++i) { + var child = children[i]; child.on('size-changed', this._doLayoutDirty, this); child.on('position-changed', this._doLayoutDirty, this); child.on('anchor-changed', this._doLayoutDirty, this); child.on('active-in-hierarchy-changed', this._doLayoutDirty, this); - }.bind(this)); + } }, _removeChildrenEventListeners: function () { var children = this.node.children; - children.forEach(function (child) { + for (var i = 0; i < children.length; ++i) { + var child = children[i]; child.off('size-changed', this._doLayoutDirty, this); child.off('position-changed', this._doLayoutDirty, this); child.off('anchor-changed', this._doLayoutDirty, this); child.off('active-in-hierarchy-changed', this._doLayoutDirty, this); - }.bind(this)); + } }, - _childAdded: function(event) { + _childAdded: function (event) { var child = event.detail; child.on('size-changed', this._doLayoutDirty, this); child.on('position-changed', this._doLayoutDirty, this); @@ -483,7 +485,7 @@ var Layout = cc.Class({ this._doLayoutDirty(); }, - _childRemoved: function(event) { + _childRemoved: function (event) { var child = event.detail; child.off('size-changed', this._doLayoutDirty, this); child.off('position-changed', this._doLayoutDirty, this); @@ -493,12 +495,12 @@ var Layout = cc.Class({ this._doLayoutDirty(); }, - _resized: function() { + _resized: function () { this._layoutSize = this.node.getContentSize(); this._doLayoutDirty(); }, - _doLayoutHorizontally: function(baseWidth, rowBreak, fnPositionY, applyChildren) { + _doLayoutHorizontally: function (baseWidth, rowBreak, fnPositionY, applyChildren) { var layoutAnchor = this.node.getAnchorPoint(); var children = this.node.children; @@ -521,21 +523,23 @@ var Layout = cc.Class({ var maxHeightChildAnchorY = 0; var activeChildCount = 0; - children.forEach(function(child) { + for (var i = 0; i < children.length; ++i) { + var child = children[i]; if (!child.activeInHierarchy) { - return; + continue; } activeChildCount++; - }); + } var newChildWidth = this.cellSize.width; if (this.type !== Type.GRID && this.resizeMode === ResizeMode.CHILDREN) { newChildWidth = (baseWidth - (this.paddingLeft + this.paddingRight) - (activeChildCount - 1) * this.spacingX) / activeChildCount; } - children.forEach(function(child) { + for (var i = 0; i < children.length; ++i) { + var child = children[i]; if (!child.activeInHierarchy) { - return; + continue; } //for resizing children if (this._resize === ResizeMode.CHILDREN) { @@ -582,13 +586,13 @@ var Layout = cc.Class({ secondMaxHeight = child.height; tempMaxHeight = 0; } - nextX = leftBoundaryOfLayout + sign * (paddingX + anchorX * child.width); + nextX = leftBoundaryOfLayout + sign * (paddingX + anchorX * child.width); row++; } } var finalPositionY = fnPositionY(child, rowMaxHeight, row); - if(baseWidth >= (child.width + this.paddingLeft + this.paddingRight)) { + if (baseWidth >= (child.width + this.paddingLeft + this.paddingRight)) { if (applyChildren) { child.setPosition(cc.p(nextX, finalPositionY)); } @@ -601,21 +605,21 @@ var Layout = cc.Class({ if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) { containerResizeBoundary = containerResizeBoundary || this.node._contentSize.height; signX = -1; - tempFinalPositionY = finalPositionY + signX * (topMarign * maxHeightChildAnchorY + this.paddingBottom); + tempFinalPositionY = finalPositionY + signX * (topMarign * maxHeightChildAnchorY + this.paddingBottom); if (tempFinalPositionY < containerResizeBoundary) { containerResizeBoundary = tempFinalPositionY; } } else { containerResizeBoundary = containerResizeBoundary || -this.node._contentSize.height; - tempFinalPositionY = finalPositionY + signX * (topMarign * maxHeightChildAnchorY + this.paddingTop); + tempFinalPositionY = finalPositionY + signX * (topMarign * maxHeightChildAnchorY + this.paddingTop); if (tempFinalPositionY > containerResizeBoundary) { containerResizeBoundary = tempFinalPositionY; } } nextX += rightBoundaryOfChild; - }.bind(this)); + } return containerResizeBoundary; }, @@ -624,13 +628,14 @@ var Layout = cc.Class({ var newHeight = 0; var activeChildCount = 0; if (this.resizeMode === ResizeMode.CONTAINER) { - children.forEach(function(child) { + for (var i = 0; i < children.length; ++i) { + var child = children[i]; if (!child.activeInHierarchy) { - return; + continue; } activeChildCount++; newHeight += child.height; - }); + } newHeight += (activeChildCount - 1) * this.spacingY + this.paddingBottom + this.paddingTop; } @@ -640,7 +645,7 @@ var Layout = cc.Class({ return newHeight; }, - _doLayoutVertically: function(baseHeight, columnBreak, fnPositionX, applyChildren) { + _doLayoutVertically: function (baseHeight, columnBreak, fnPositionX, applyChildren) { var layoutAnchor = this.node.getAnchorPoint(); var children = this.node.children; @@ -662,21 +667,23 @@ var Layout = cc.Class({ var maxWidthChildAnchorX = 0; var activeChildCount = 0; - children.forEach(function(child) { + for (var i = 0; i < children.length; ++i) { + var child = children[i]; if (!child.activeInHierarchy) { - return; + continue; } activeChildCount++; - }); + } var newChildHeight = this.cellSize.height; if (this.type !== Type.GRID && this.resizeMode === ResizeMode.CHILDREN) { newChildHeight = (baseHeight - (this.paddingTop + this.paddingBottom) - (activeChildCount - 1) * this.spacingY) / activeChildCount; } - children.forEach(function(child) { + for (var i = 0; i < children.length; ++i) { + var child = children[i]; if (!child.activeInHierarchy) { - return; + continue; } //for resizing children @@ -758,27 +765,27 @@ var Layout = cc.Class({ } nextY += topBoundaryOfChild; - }.bind(this)); + } return containerResizeBoundary; }, - _doLayoutBasic: function() { + _doLayoutBasic: function () { var children = this.node.children; var allChildrenBoundingBox = null; - children.forEach(function(child){ + for (var i = 0; i < children.length; ++i) { + var child = children[i]; if (!child.activeInHierarchy) { - return; + continue; } - - if(!allChildrenBoundingBox){ + 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)); @@ -813,7 +820,7 @@ var Layout = cc.Class({ paddingY = this.paddingTop; } - var fnPositionY = function(child, topOffset, row) { + var fnPositionY = function (child, topOffset, row) { return bottomBoundaryOfLayout + sign * (topOffset + child.anchorY * child.height + paddingY + row * this.spacingY); }.bind(this); @@ -854,7 +861,7 @@ var Layout = cc.Class({ paddingX = this.paddingRight; } - var fnPositionX = function(child, leftOffset, column) { + var fnPositionX = function (child, leftOffset, column) { return leftBoundaryOfLayout + sign * (leftOffset + child.anchorX * child.width + paddingX + column * this.spacingX); }.bind(this); @@ -881,7 +888,7 @@ var Layout = cc.Class({ } }, - _doLayoutGrid: function() { + _doLayoutGrid: function () { var layoutAnchor = this.node.getAnchorPoint(); var layoutSize = this.node.getContentSize(); @@ -889,7 +896,7 @@ var Layout = cc.Class({ this._doLayoutGridAxisHorizontal(layoutAnchor, layoutSize); } - else if(this.startAxis === AxisDirection.VERTICAL) { + else if (this.startAxis === AxisDirection.VERTICAL) { this._doLayoutGridAxisVertical(layoutAnchor, layoutSize); } @@ -899,13 +906,14 @@ var Layout = cc.Class({ var newWidth = 0; var activeChildCount = 0; if (this.resizeMode === ResizeMode.CONTAINER) { - children.forEach(function(child) { - if(!child.activeInHierarchy) { - return; + for (var i = 0; i < children.length; ++i) { + var child = children[i]; + if (!child.activeInHierarchy) { + continue; } activeChildCount++; newWidth += child.width; - }); + } newWidth += (activeChildCount - 1) * this.spacingX + this.paddingLeft + this.paddingRight; } else { @@ -914,23 +922,23 @@ var Layout = cc.Class({ return newWidth; }, - _doLayout: function() { + _doLayout: function () { if (this.type === Type.HORIZONTAL) { var newWidth = this._getHorizontalBaseWidth(this.node.children); - var fnPositionY = function(child) { + var fnPositionY = function (child) { return child.y; }; - this._doLayoutHorizontally( newWidth, false, fnPositionY, true); + this._doLayoutHorizontally(newWidth, false, fnPositionY, true); this.node.width = newWidth; } else if (this.type === Type.VERTICAL) { var newHeight = this._getVerticalBaseHeight(this.node.children); - var fnPositionX = function(child) { + var fnPositionX = function (child) { return child.x; }; @@ -943,7 +951,7 @@ var Layout = cc.Class({ this._doLayoutBasic(); } } - else if(this.type === Type.GRID) { + else if (this.type === Type.GRID) { this._doLayoutGrid(); } }, @@ -961,7 +969,7 @@ var Layout = cc.Class({ * layout.updateLayout(); * cc.log(childNode.x); // changed */ - updateLayout: function() { + updateLayout: function () { if (this._layoutDirty && this.node.children.length > 0) { this._doLayout(); this._layoutDirty = false; From e45c5cd1a92b7f86ee89d542e89494fd6ece6c56 Mon Sep 17 00:00:00 2001 From: knox Date: Fri, 4 May 2018 09:57:43 +0800 Subject: [PATCH 3/3] improve code --- cocos2d/core/components/CCLayout.js | 37 +++++++++++++---------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/cocos2d/core/components/CCLayout.js b/cocos2d/core/components/CCLayout.js index 7d6e2ffa3b..37440838e0 100644 --- a/cocos2d/core/components/CCLayout.js +++ b/cocos2d/core/components/CCLayout.js @@ -525,10 +525,9 @@ var Layout = cc.Class({ var activeChildCount = 0; for (var i = 0; i < children.length; ++i) { var child = children[i]; - if (!child.activeInHierarchy) { - continue; + if (child.activeInHierarchy) { + activeChildCount++; } - activeChildCount++; } var newChildWidth = this.cellSize.width; @@ -630,11 +629,10 @@ var Layout = cc.Class({ if (this.resizeMode === ResizeMode.CONTAINER) { for (var i = 0; i < children.length; ++i) { var child = children[i]; - if (!child.activeInHierarchy) { - continue; + if (child.activeInHierarchy) { + activeChildCount++; + newHeight += child.height; } - activeChildCount++; - newHeight += child.height; } newHeight += (activeChildCount - 1) * this.spacingY + this.paddingBottom + this.paddingTop; @@ -669,10 +667,9 @@ var Layout = cc.Class({ var activeChildCount = 0; for (var i = 0; i < children.length; ++i) { var child = children[i]; - if (!child.activeInHierarchy) { - continue; + if (child.activeInHierarchy) { + activeChildCount++; } - activeChildCount++; } var newChildHeight = this.cellSize.height; @@ -777,13 +774,12 @@ var Layout = cc.Class({ for (var i = 0; i < children.length; ++i) { var child = children[i]; - if (!child.activeInHierarchy) { - continue; - } - if (!allChildrenBoundingBox) { - allChildrenBoundingBox = child.getBoundingBoxToWorld(); - } else { - allChildrenBoundingBox = cc.rectUnion(allChildrenBoundingBox, child.getBoundingBoxToWorld()); + if (child.activeInHierarchy) { + if (!allChildrenBoundingBox) { + allChildrenBoundingBox = child.getBoundingBoxToWorld(); + } else { + allChildrenBoundingBox = cc.rectUnion(allChildrenBoundingBox, child.getBoundingBoxToWorld()); + } } } @@ -908,11 +904,10 @@ var Layout = cc.Class({ if (this.resizeMode === ResizeMode.CONTAINER) { for (var i = 0; i < children.length; ++i) { var child = children[i]; - if (!child.activeInHierarchy) { - continue; + if (child.activeInHierarchy) { + activeChildCount++; + newWidth += child.width; } - activeChildCount++; - newWidth += child.width; } newWidth += (activeChildCount - 1) * this.spacingX + this.paddingLeft + this.paddingRight; }