Merge pull request #120 from zilongshanren/addLayout

Panel组件开发
This commit is contained in:
Jare Guo
2015-12-24 15:29:39 +08:00
5 changed files with 301 additions and 10 deletions
+3 -1
View File
@@ -330,7 +330,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();
+279
View File
@@ -0,0 +1,279 @@
/****************************************************************************
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,
});
/**
* 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, use it to arrange child elements easily.
*
* @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),
_layoutDirty: {
default: true,
serializable: false,
},
/**
* The layout type.
* @property {LayoutType} layoutType
* @default Layout.LayoutType.BASIC
*/
layoutType: {
default: LayoutType.BASIC,
type: LayoutType,
notify: function() {
this._doLayoutDirty();
}
},
/**
* The margin of layout, it only effect the layout in one direction.
* @property {Number} margin
*/
margin: {
default: 0,
notify: function() {
this._doLayoutDirty();
}
},
/**
* The distance between each element in layout.
* @property {Number} spacing
*/
spacing: {
default: 0,
notify: function() {
this._doLayoutDirty();
}
},
/**
* 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,
notify: function() {
this._doLayoutDirty();
}
},
/**
* 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,
notify: function() {
this._doLayoutDirty();
}
},
},
onLoad: function() {
this.node.setContentSize(this._layoutSize);
this.node.on('size-changed', this._resized, 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();
},
_doLayoutDirty : function() {
this._layoutDirty = true;
},
_updateChildrenEventListener: function() {
var children = this.node.children;
children.forEach(function(child) {
child.on('size-changed', this._doLayoutDirty, this);
child.on('position-changed', this._doLayoutDirty, this);
}.bind(this));
},
_childrenAddOrDeleted: function(event) {
this._updateChildrenEventListener();
this._doLayoutDirty();
},
_resized: function() {
this._layoutSize = this.node.getContentSize();
this._doLayoutDirty();
},
_doLayoutHorizontally: function(layoutAnchor, layoutSize, children) {
var newWidth = 0;
var sign = 1;
children.forEach(function(child) {
newWidth += child.width;
});
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 anchorX = child.anchorX;
if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) {
anchorX = 1 - child.anchorX;
}
nextX = nextX + sign * anchorX * child.width + sign * this.spacing;
child.setPosition(cc.p(nextX, child.y));
nextX += sign * (1 - anchorX) * child.width;
}.bind(this));
},
_doLayoutVertically: function(layoutAnchor, layoutSize, children) {
var newHeight = 0;
var sign = 1;
children.forEach(function(child) {
newHeight += child.height;
});
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 anchorY = child.anchorY;
if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) {
anchorY = 1 - child.anchorY;
}
nextY = nextY + sign * anchorY * child.height + sign * this.spacing;
child.setPosition(cc.p(child.x, nextY));
nextY += sign * (1 - anchorY) * child.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);
}
},
lateUpdate: function() {
if (this._layoutDirty) {
this._doLayout();
this._layoutDirty = false;
}
}
});
cc.Layout = module.exports = Layout;
+13 -9
View File
@@ -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) {
+1
View File
@@ -13,4 +13,5 @@ module.exports = [
require('./CCMask'),
require('./CCScrollBar'),
require('./CCScrollView'),
require('./CCLayout'),
];
+5
View File
@@ -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(){