Merge pull request #2622 from knoxHuang/temp

fix layout newChildWidth bug for cocos-creator/engine/issues/2383
This commit is contained in:
pandamicro
2018-05-04 10:09:07 +08:00
committed by GitHub
+92 -73
View File
@@ -192,10 +192,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) {
@@ -223,10 +223,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;
}
@@ -252,7 +252,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();
},
},
@@ -268,7 +268,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) {
@@ -342,7 +342,7 @@ var Layout = cc.Class({
*/
spacingX: {
default: 0,
notify: function() {
notify: function () {
this._doLayoutDirty();
},
tooltip: CC_DEV && 'i18n:COMPONENT.layout.space_x'
@@ -355,7 +355,7 @@ var Layout = cc.Class({
*/
spacingY: {
default: 0,
notify: function() {
notify: function () {
this._doLayoutDirty();
},
tooltip: CC_DEV && 'i18n:COMPONENT.layout.space_y'
@@ -371,7 +371,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',
@@ -388,7 +388,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',
@@ -412,14 +412,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();
}
@@ -430,7 +430,7 @@ var Layout = cc.Class({
this._removeEventListeners();
},
_doLayoutDirty : function () {
_doLayoutDirty: function () {
this._layoutDirty = true;
},
@@ -454,27 +454,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);
@@ -484,7 +486,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);
@@ -494,12 +496,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,14 +523,23 @@ var Layout = cc.Class({
var maxHeightChildAnchorY = 0;
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;
var activeChildCount = 0;
for (var i = 0; i < children.length; ++i) {
var child = children[i];
if (child.activeInHierarchy) {
activeChildCount++;
}
}
children.forEach(function(child) {
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;
}
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) {
@@ -575,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));
}
@@ -594,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;
},
@@ -617,13 +628,13 @@ var Layout = cc.Class({
var newHeight = 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) {
activeChildCount++;
newHeight += child.height;
}
activeChildCount++;
newHeight += child.height;
});
}
newHeight += (activeChildCount - 1) * this.spacingY + this.paddingBottom + this.paddingTop;
}
@@ -633,7 +644,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;
@@ -654,14 +665,23 @@ var Layout = cc.Class({
var containerResizeBoundary = 0;
var maxWidthChildAnchorX = 0;
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;
var activeChildCount = 0;
for (var i = 0; i < children.length; ++i) {
var child = children[i];
if (child.activeInHierarchy) {
activeChildCount++;
}
}
children.forEach(function(child) {
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;
}
for (var i = 0; i < children.length; ++i) {
var child = children[i];
if (!child.activeInHierarchy) {
return;
continue;
}
//for resizing children
@@ -743,27 +763,26 @@ 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){
if (!child.activeInHierarchy) {
return;
for (var i = 0; i < children.length; ++i) {
var child = children[i];
if (child.activeInHierarchy) {
if (!allChildrenBoundingBox) {
allChildrenBoundingBox = child.getBoundingBoxToWorld();
} else {
allChildrenBoundingBox = cc.rectUnion(allChildrenBoundingBox, child.getBoundingBoxToWorld());
}
}
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));
@@ -798,7 +817,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);
@@ -839,7 +858,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);
@@ -866,7 +885,7 @@ var Layout = cc.Class({
}
},
_doLayoutGrid: function() {
_doLayoutGrid: function () {
var layoutAnchor = this.node.getAnchorPoint();
var layoutSize = this.node.getContentSize();
@@ -874,7 +893,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);
}
@@ -884,13 +903,13 @@ 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) {
activeChildCount++;
newWidth += child.width;
}
activeChildCount++;
newWidth += child.width;
});
}
newWidth += (activeChildCount - 1) * this.spacingX + this.paddingLeft + this.paddingRight;
}
else {
@@ -899,23 +918,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;
};
@@ -928,7 +947,7 @@ var Layout = cc.Class({
this._doLayoutBasic();
}
}
else if(this.type === Type.GRID) {
else if (this.type === Type.GRID) {
this._doLayoutGrid();
}
},
@@ -946,7 +965,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;