From 97af241136e2e1f368fdcaa2105c2ddaabc0b283 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 22 Dec 2015 21:06:56 +0800 Subject: [PATCH 1/4] Finish layout --- cocos2d/core/components/CCLayout.js | 114 ++++++++++++++++++++++++++++ cocos2d/core/components/index.js | 1 + 2 files changed, 115 insertions(+) create mode 100644 cocos2d/core/components/CCLayout.js diff --git a/cocos2d/core/components/CCLayout.js b/cocos2d/core/components/CCLayout.js new file mode 100644 index 0000000000..6a58414de4 --- /dev/null +++ b/cocos2d/core/components/CCLayout.js @@ -0,0 +1,114 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + + +/** + * Enum for Layout type + * @enum Layout.LayoutType + */ +var LayoutType = cc.Enum({ + /** + *@property {Number} BASIC + */ + BASIC: 0, + /** + * @property {Number} HORIZONTAL + */ + HORIZONTAL: 1, + + /** + * @property {Number} VERTICAL + */ + VERTICAL: 2, +}); + +var VerticalDirection = cc.Enum({ + BOTTOM_TO_TOP: 0, + TOP_TO_BOTTOM: 1, +}); + +var HorizontalDirection = cc.Enum({ + LEFT_TO_RIGHT: 0, + RIGHT_TO_LEFT: 1, +}); + +/** + * The Layout is a container component, it could arrange all its children conveniently. + * + * @class Layout + * @extends Component + */ +var Layout = cc.Class({ + name: 'cc.Layout', + extends: require('./CCComponent'), + + editor: CC_EDITOR && { + menu: 'i18n:MAIN_MENU.component.ui/Layout', + inspector: 'app://editor/page/inspector/cclayout.html', + executeInEditMode: true, + }, + + properties: { + _layoutSize: cc.size(300, 200), + + layoutType: { + default: LayoutType.BASIC, + type: LayoutType + }, + + margin: { + default: 0, + }, + + spacing: { + default: 0, + }, + + verticalDirection: { + default: VerticalDirection.TOP_TO_BOTTOM, + type: VerticalDirection + }, + + horizontalDirection: { + default: HorizontalDirection.LEFT_TO_RIGHT, + type: HorizontalDirection + }, + }, + + onLoad: function () { + this.node.setContentSize(this._layoutSize); + this.node.on('size-changed', this._resized, this); + }, + + onDestroy: function () { + this.node.off('size-changed', this._resized, this); + }, + _resized: function(){ + this._layoutSize = this.node.getContentSize(); + } + +}); + + +cc.Layout = module.exports = Layout; diff --git a/cocos2d/core/components/index.js b/cocos2d/core/components/index.js index 23dc40bbd7..d110c9cfbf 100644 --- a/cocos2d/core/components/index.js +++ b/cocos2d/core/components/index.js @@ -13,4 +13,5 @@ module.exports = [ require('./CCMask'), require('./CCScrollBar'), require('./CCScrollView'), + require('./CCLayout'), ]; From 8fbc0e94c4e57f778d45dd973ee95d2a03b5b0c1 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 23 Dec 2015 13:48:30 +0800 Subject: [PATCH 2/4] add defensive technical to scrollview --- cocos2d/core/components/CCScrollView.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index 220533564f..3b5cd29177 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -478,15 +478,17 @@ var ScrollView = cc.Class({ }, _calculateBoundary: function() { - var scrollViewSize = this.node.getContentSize(); + if(this.content){ + var scrollViewSize = this.node.getContentSize(); - var leftBottomPosition = this._convertToContentParentSpace(cc.p(0, 0)); - this._leftBoundary = leftBottomPosition.x; - this._bottomBoundary = leftBottomPosition.y; + var leftBottomPosition = this._convertToContentParentSpace(cc.p(0, 0)); + this._leftBoundary = leftBottomPosition.x; + this._bottomBoundary = leftBottomPosition.y; - var topRightPosition = this._convertToContentParentSpace(cc.p(scrollViewSize.width, scrollViewSize.height)); - this._rightBoundary = topRightPosition.x; - this._topBoundary = topRightPosition.y; + var topRightPosition = this._convertToContentParentSpace(cc.p(scrollViewSize.width, scrollViewSize.height)); + this._rightBoundary = topRightPosition.x; + this._topBoundary = topRightPosition.y; + } }, _convertToContentParentSpace: function(position) { @@ -511,7 +513,7 @@ var ScrollView = cc.Class({ // touch event handler _onTouchBegan: function(touch) { var hit = this._hitTest(touch.getLocation()); - if (hit) { + if (hit && this.content) { this._handlePressLogic(touch); } @@ -525,7 +527,9 @@ var ScrollView = cc.Class({ }, _onTouchEnded: function(touch) { - this._handleReleaseLogic(touch); + if(this.content){ + this._handleReleaseLogic(touch); + } }, _onTouchCancelled: function(touch) { From 3ba13cfe3809fd48adc2b3253bc33e3918d7df00 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 23 Dec 2015 14:49:50 +0800 Subject: [PATCH 3/4] Finish layout add children change event add child-added and child-remove event to basenode add layout dirty skeleton finish layout in one direction add layout doc string. --- cocos2d/core/CCNode.js | 4 +- cocos2d/core/components/CCLayout.js | 198 ++++++++++++++++++++++++++-- cocos2d/core/utils/base-node.js | 5 + 3 files changed, 198 insertions(+), 9 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 7eff54e223..a742c2d75d 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -327,7 +327,9 @@ var Node = cc.Class({ if ( !destroyByParent ) { // remove from parent if (parent) { - parent._children.splice(parent._children.indexOf(this), 1); + var childIndex = parent._children.indexOf(this); + parent._children.splice(childIndex, 1); + parent.emit('child-removed', this); } this._removeSgNode(); diff --git a/cocos2d/core/components/CCLayout.js b/cocos2d/core/components/CCLayout.js index 6a58414de4..963eef0757 100644 --- a/cocos2d/core/components/CCLayout.js +++ b/cocos2d/core/components/CCLayout.js @@ -43,18 +43,38 @@ var LayoutType = cc.Enum({ VERTICAL: 2, }); +/** + * Enum for vertical layout direction. + * @enum Layout.VerticalDirection + */ var VerticalDirection = cc.Enum({ + /** + * @property {Number} BOTTOM_TO_TOP + */ BOTTOM_TO_TOP: 0, + /** + * @property {Number} TOP_TO_BOTTOM + */ TOP_TO_BOTTOM: 1, }); +/** + * Enum for horizontal layout direction. + * @enum Layout.HorizontalDirection + */ var HorizontalDirection = cc.Enum({ + /** + * @property {Number} LEFT_TO_RIGHT + */ LEFT_TO_RIGHT: 0, + /** + *@property {Number} RIGHT_TO_LEFT + */ RIGHT_TO_LEFT: 1, }); /** - * The Layout is a container component, it could arrange all its children conveniently. + * The Layout is a container component, use it to arrange child elements easily. * * @class Layout * @extends Component @@ -71,41 +91,203 @@ var Layout = cc.Class({ properties: { _layoutSize: cc.size(300, 200), + _layoutDirty: { + default: true, + serializable: false, + }, + /** + * The layout type. + * @property {LayoutType} layoutType + * @default Layout.LayoutType.BASIC + */ layoutType: { default: LayoutType.BASIC, - type: LayoutType + type: LayoutType, + notify: function() { + this._layoutDirty = true; + } }, + /** + * The margin of layout, it only effect the layout in one direction. + * @property {Number} margin + */ margin: { default: 0, + notify: function() { + this._layoutDirty = true; + } }, + /** + * The distance between each element in layout. + * @property {Number} spacing + */ spacing: { default: 0, + notify: function() { + this._layoutDirty = true; + } }, + /** + * Only take effect in Vertical layout mode. + * This option changes the start element's positioning. + * @property {VerticalDirection} verticalDirection + */ verticalDirection: { default: VerticalDirection.TOP_TO_BOTTOM, - type: VerticalDirection + type: VerticalDirection, + notify: function() { + this._layoutDirty = true; + } }, + /** + * Only take effect in Horizontal layout mode. + * This option changes the start element's positioning. + * @property {HorizontalDirection} horizontalDirection + */ horizontalDirection: { default: HorizontalDirection.LEFT_TO_RIGHT, - type: HorizontalDirection + type: HorizontalDirection, + notify: function() { + this._layoutDirty = true; + } }, }, - onLoad: function () { + onLoad: function() { this.node.setContentSize(this._layoutSize); this.node.on('size-changed', this._resized, this); + this.node.on('anchor-changed', function() { + this._layoutDirty = true; + }.bind(this), this); + this.node.on('child-added', this._childrenAddOrDeleted, this); + this.node.on('child-removed', this._childrenAddOrDeleted, this);; + this._updateChildrenEventListener(); }, - onDestroy: function () { - this.node.off('size-changed', this._resized, this); + _registerChildrenSizeAndPositionChangeEvent: function(child) { + var layoutDirtyFunc = function(event) { + this._layoutDirty = true; + }.bind(this); + + child.on('size-changed', layoutDirtyFunc, this); + + child.on('position-changed', layoutDirtyFunc, this); }, - _resized: function(){ + + _updateChildrenEventListener: function() { + var children = this.node.children; + children.forEach(function(child) { + this._registerChildrenSizeAndPositionChangeEvent(child); + }.bind(this)); + }, + + _childrenAddOrDeleted: function(event) { + this._updateChildrenEventListener(); + this._layoutDirty = true; + }, + + _resized: function() { this._layoutSize = this.node.getContentSize(); + this._layoutDirty = true; + }, + + _doLayoutHorizontally: function(layoutAnchor, layoutSize, children) { + var newWidth = 0; + var sign = 1; + + children.forEach(function(child) { + var childSize = child.getContentSize(); + newWidth += childSize.width; + }.bind(this)); + + newWidth += (children.length - 1) * this.spacing + 2 * this.margin; + this.node.setContentSize(newWidth, layoutSize.height); + + var leftBoundaryOfLayout = -layoutAnchor.x * newWidth; + if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) { + sign = -1; + leftBoundaryOfLayout = (1 - layoutAnchor.x) * newWidth; + } + + var nextX = leftBoundaryOfLayout + sign * this.margin - sign * this.spacing; + + children.forEach(function(child) { + var childAnchor = child.getAnchorPoint(); + var childSize = child.getContentSize(); + var childPosition = child.getPosition(); + + var anchorX = childAnchor.x; + if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) { + anchorX = 1 - childAnchor.x; + } + nextX = nextX + sign * anchorX * childSize.width + sign * this.spacing; + + child.setPosition(cc.p(nextX, childPosition.y)); + + nextX += sign * (1 - anchorX) * childSize.width; + }.bind(this)); + }, + + _doLayoutVertically: function(layoutAnchor, layoutSize, children) { + var newHeight = 0; + var sign = 1; + + children.forEach(function(child) { + var childSize = child.getContentSize(); + newHeight += childSize.height; + }.bind(this)); + + newHeight += (children.length - 1) * this.spacing + 2 * this.margin; + this.node.setContentSize(layoutSize.width, newHeight); + + var bottomBoundaryOfLayout = -layoutAnchor.y * newHeight; + if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) { + sign = -1; + bottomBoundaryOfLayout = (1 - layoutAnchor.y) * newHeight; + } + + var nextY = bottomBoundaryOfLayout + sign * this.margin - sign * this.spacing; + + children.forEach(function(child) { + var childAnchor = child.getAnchorPoint(); + var childSize = child.getContentSize(); + var childPosition = child.getPosition(); + + var anchorY = childAnchor.y; + if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) { + anchorY = 1 - childAnchor.y; + } + nextY = nextY + sign * anchorY * childSize.height + sign * this.spacing; + + child.setPosition(cc.p(childPosition.x, nextY)); + + nextY += sign * (1 - anchorY) * childSize.height; + }.bind(this)); + }, + + _doLayout: function() { + var children = this.node.children; + var layoutAnchor = this.node.getAnchorPoint(); + var layoutSize = this.node.getContentSize(); + + if (this.layoutType === LayoutType.HORIZONTAL) { + this._doLayoutHorizontally(layoutAnchor, layoutSize, children); + } else if (this.layoutType === LayoutType.VERTICAL) { + this._doLayoutVertically(layoutAnchor, layoutSize, children); + } + }, + + update: function() { + + if (this._layoutDirty) { + this._doLayout(); + this._layoutDirty = false; + } } }); diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index 949b470882..9acdc74278 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -46,6 +46,8 @@ var SIZE_CHANGED = 'size-changed'; var ANCHOR_CHANGED = 'anchor-changed'; var COLOR_CHANGED = 'color-changed'; var OPACITY_CHANGED = 'opacity-changed'; +var CHILD_ADDED = 'child-added'; +var CHILD_REMOVED = 'child-removed'; /** * A base node for CCNode and CCEScene, it will: @@ -129,6 +131,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ parent.addChild(node); setMaxZOrder(node); value._children.push(this); + value.emit(CHILD_ADDED, this); } // var oldParent = this._parent; @@ -140,6 +143,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ return cc.error('Internal error, should not remove unknown node from parent.'); } oldParent._children.splice(removeAt, 1); + oldParent.emit(CHILD_REMOVED, removeAt); this._onHierarchyChanged(oldParent); } } @@ -1091,6 +1095,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ child.parent = null; cc.js.array.remove(this._children, child); + this.emit(CHILD_REMOVED, child); }, setNodeDirty: function(){ From fd185047be5c7977065c488def29dfdd8add7b5f Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 24 Dec 2015 15:20:40 +0800 Subject: [PATCH 4/4] refine the coding style --- cocos2d/core/components/CCLayout.js | 73 +++++++++++------------------ 1 file changed, 28 insertions(+), 45 deletions(-) diff --git a/cocos2d/core/components/CCLayout.js b/cocos2d/core/components/CCLayout.js index 963eef0757..8f6f96aaf0 100644 --- a/cocos2d/core/components/CCLayout.js +++ b/cocos2d/core/components/CCLayout.js @@ -105,7 +105,7 @@ var Layout = cc.Class({ default: LayoutType.BASIC, type: LayoutType, notify: function() { - this._layoutDirty = true; + this._doLayoutDirty(); } }, @@ -116,7 +116,7 @@ var Layout = cc.Class({ margin: { default: 0, notify: function() { - this._layoutDirty = true; + this._doLayoutDirty(); } }, @@ -127,7 +127,7 @@ var Layout = cc.Class({ spacing: { default: 0, notify: function() { - this._layoutDirty = true; + this._doLayoutDirty(); } }, @@ -140,7 +140,7 @@ var Layout = cc.Class({ default: VerticalDirection.TOP_TO_BOTTOM, type: VerticalDirection, notify: function() { - this._layoutDirty = true; + this._doLayoutDirty(); } }, @@ -153,47 +153,41 @@ var Layout = cc.Class({ default: HorizontalDirection.LEFT_TO_RIGHT, type: HorizontalDirection, notify: function() { - this._layoutDirty = true; + this._doLayoutDirty(); } }, }, onLoad: function() { this.node.setContentSize(this._layoutSize); + this.node.on('size-changed', this._resized, this); - this.node.on('anchor-changed', function() { - this._layoutDirty = true; - }.bind(this), this); + this.node.on('anchor-changed', this._doLayoutDirty, this); this.node.on('child-added', this._childrenAddOrDeleted, this); this.node.on('child-removed', this._childrenAddOrDeleted, this);; this._updateChildrenEventListener(); }, - _registerChildrenSizeAndPositionChangeEvent: function(child) { - var layoutDirtyFunc = function(event) { - this._layoutDirty = true; - }.bind(this); - - child.on('size-changed', layoutDirtyFunc, this); - - child.on('position-changed', layoutDirtyFunc, this); + _doLayoutDirty : function() { + this._layoutDirty = true; }, _updateChildrenEventListener: function() { var children = this.node.children; children.forEach(function(child) { - this._registerChildrenSizeAndPositionChangeEvent(child); + child.on('size-changed', this._doLayoutDirty, this); + child.on('position-changed', this._doLayoutDirty, this); }.bind(this)); }, _childrenAddOrDeleted: function(event) { this._updateChildrenEventListener(); - this._layoutDirty = true; + this._doLayoutDirty(); }, _resized: function() { this._layoutSize = this.node.getContentSize(); - this._layoutDirty = true; + this._doLayoutDirty(); }, _doLayoutHorizontally: function(layoutAnchor, layoutSize, children) { @@ -201,9 +195,8 @@ var Layout = cc.Class({ var sign = 1; children.forEach(function(child) { - var childSize = child.getContentSize(); - newWidth += childSize.width; - }.bind(this)); + newWidth += child.width; + }); newWidth += (children.length - 1) * this.spacing + 2 * this.margin; this.node.setContentSize(newWidth, layoutSize.height); @@ -217,19 +210,15 @@ var Layout = cc.Class({ var nextX = leftBoundaryOfLayout + sign * this.margin - sign * this.spacing; children.forEach(function(child) { - var childAnchor = child.getAnchorPoint(); - var childSize = child.getContentSize(); - var childPosition = child.getPosition(); - - var anchorX = childAnchor.x; + var anchorX = child.anchorX; if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) { - anchorX = 1 - childAnchor.x; + anchorX = 1 - child.anchorX; } - nextX = nextX + sign * anchorX * childSize.width + sign * this.spacing; + nextX = nextX + sign * anchorX * child.width + sign * this.spacing; - child.setPosition(cc.p(nextX, childPosition.y)); + child.setPosition(cc.p(nextX, child.y)); - nextX += sign * (1 - anchorX) * childSize.width; + nextX += sign * (1 - anchorX) * child.width; }.bind(this)); }, @@ -238,9 +227,8 @@ var Layout = cc.Class({ var sign = 1; children.forEach(function(child) { - var childSize = child.getContentSize(); - newHeight += childSize.height; - }.bind(this)); + newHeight += child.height; + }); newHeight += (children.length - 1) * this.spacing + 2 * this.margin; this.node.setContentSize(layoutSize.width, newHeight); @@ -254,19 +242,15 @@ var Layout = cc.Class({ var nextY = bottomBoundaryOfLayout + sign * this.margin - sign * this.spacing; children.forEach(function(child) { - var childAnchor = child.getAnchorPoint(); - var childSize = child.getContentSize(); - var childPosition = child.getPosition(); - - var anchorY = childAnchor.y; + var anchorY = child.anchorY; if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) { - anchorY = 1 - childAnchor.y; + anchorY = 1 - child.anchorY; } - nextY = nextY + sign * anchorY * childSize.height + sign * this.spacing; + nextY = nextY + sign * anchorY * child.height + sign * this.spacing; - child.setPosition(cc.p(childPosition.x, nextY)); + child.setPosition(cc.p(child.x, nextY)); - nextY += sign * (1 - anchorY) * childSize.height; + nextY += sign * (1 - anchorY) * child.height; }.bind(this)); }, @@ -282,8 +266,7 @@ var Layout = cc.Class({ } }, - update: function() { - + lateUpdate: function() { if (this._layoutDirty) { this._doLayout(); this._layoutDirty = false;