From afda122d299466f42b305c07afc2e1f1e7c78b2b Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Fri, 12 Aug 2016 15:08:03 +0800 Subject: [PATCH 1/2] add widget horizontal center and vertical center offset for fireball/issues/3967 --- cocos2d/core/base-ui/CCWidgetManager.js | 18 +++---- cocos2d/core/components/CCWidget.js | 72 +++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/cocos2d/core/base-ui/CCWidgetManager.js b/cocos2d/core/base-ui/CCWidgetManager.js index 7cbcc15638..1136dfba9e 100644 --- a/cocos2d/core/base-ui/CCWidgetManager.js +++ b/cocos2d/core/base-ui/CCWidgetManager.js @@ -91,8 +91,9 @@ function alignToParent (node, widget) { else { width = node.width * scaleX; if (widget.isAlignHorizontalCenter) { - var parentCenter = (0.5 - parentAnchor.x) * parentWidth; // no offset - x = parentCenter + (anchorX - 0.5) * width; + var horizontalCenter = widget._isAbsHorizontalCenter ? widget._horizontalCenter : widget._horizontalCenter * parentWidth; + var parentCenter = (0.5 - parentAnchor.x) * parentWidth; + x = parentCenter + (anchorX - 0.5) * width + horizontalCenter; } else if (widget.isAlignLeft) { x = localLeft + anchorX * width; @@ -137,8 +138,9 @@ function alignToParent (node, widget) { else { height = node.height * scaleY; if (widget.isAlignVerticalCenter) { - var parentMiddle = (0.5 - parentAnchor.y) * parentHeight; // no offset - y = parentMiddle + (anchorY - 0.5) * height; + var verticalCenter = widget._isAbsVerticalCenter ? widget._verticalCenter : widget._verticalCenter * parentHeight; + var parentMiddle = (0.5 - parentAnchor.y) * parentHeight; + y = parentMiddle + (anchorY - 0.5) * height + verticalCenter; } else if (widget.isAlignBottom) { y = localBottom + anchorY * height; @@ -268,14 +270,10 @@ var adjustWidgetToAllowMovingInEditor = CC_EDITOR && function (event) { this.right -= (this.isAbsoluteRight ? delta.x : deltaInPercent.x); } if (this.isAlignHorizontalCenter) { - if (oldPos.x !== newPos.x) { - this.isAlignHorizontalCenter = false; - } + this.horizontalCenter += (this.isAbsoluteHorizontalCenter ? delta.x : deltaInPercent.x); } if (this.isAlignVerticalCenter) { - if (oldPos.y !== newPos.y) { - this.isAlignVerticalCenter = false; - } + this.verticalCenter += (this.isAbsoluteVerticalCenter ? delta.y : deltaInPercent.y); } }; diff --git a/cocos2d/core/components/CCWidget.js b/cocos2d/core/components/CCWidget.js index 4c4c739bcf..6fdaec20c1 100644 --- a/cocos2d/core/components/CCWidget.js +++ b/cocos2d/core/components/CCWidget.js @@ -301,8 +301,76 @@ var Widget = cc.Class({ tooltip: 'i18n:COMPONENT.widget.right', }, + /** + * !#en TODO + * !#zh 水平居中的偏移值,可填写负值,只有在 isAlignHorizontalCenter 开启时才有作用。 + * @property horizontalCenter + * @type {Number} + * @default 0 + */ + horizontalCenter: { + get: function () { + return this._horizontalCenter; + }, + set: function (value) { + this._horizontalCenter = value; + }, + tooltip: 'i18n:COMPONENT.widget.horizontal_center', + }, + + /** + * !#en TODO + * !#zh 垂直居中的偏移值,可填写负值,只有在 isAlignVerticalCenter 开启时才有作用。 + * @property verticalCenter + * @type {Number} + * @default 0 + */ + verticalCenter: { + get: function () { + return this._verticalCenter; + }, + set: function (value) { + this._verticalCenter = value; + }, + tooltip: 'i18n:COMPONENT.widget.vertical_center', + }, + // PARCENTAGE OR ABSOLUTE + /** + * !#en TODO + * !#zh 如果为 true,"horizontalCenter" 将会以像素作为偏移值,反之为百分比。 + * @property isAbsoluteHorizontalCenter + * @type {Boolean} + * @default true + */ + isAbsoluteHorizontalCenter: { + get: function () { + return this._isAbsHorizontalCenter; + }, + set: function (value) { + this._isAbsHorizontalCenter = value; + }, + animatable: false + }, + + /** + * !#en TODO + * !#zh 如果为 true,"verticalCenter" 将会以像素作为偏移值,反之为百分比。 + * @property isAbsoluteHorizontalCenter + * @type {Boolean} + * @default true + */ + isAbsoluteVerticalCenter: { + get: function () { + return this._isAbsVerticalCenter; + }, + set: function (value) { + this._isAbsVerticalCenter = value; + }, + animatable: false + }, + /** * !#en * If true, top is pixel margin, otherwise is percentage (0 - 1) margin relative to the parent's height. @@ -411,10 +479,14 @@ var Widget = cc.Class({ _right: 0, _top: 0, _bottom: 0, + _verticalCenter: 0, + _horizontalCenter: 0, _isAbsLeft: true, _isAbsRight: true, _isAbsTop: true, _isAbsBottom: true, + _isAbsHorizontalCenter: true, + _isAbsVerticalCenter: true, // original size before align _originalWidth: 0, From f4a0cfba9369c154628aee65c807320ccc5d05a8 Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Fri, 12 Aug 2016 16:24:08 +0800 Subject: [PATCH 2/2] update widget horizontal center and vertical center offset for fireball/issues/3967 --- cocos2d/core/base-ui/CCWidgetManager.js | 16 ++++++---------- cocos2d/core/components/CCWidget.js | 16 ++++++++++------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/cocos2d/core/base-ui/CCWidgetManager.js b/cocos2d/core/base-ui/CCWidgetManager.js index 1136dfba9e..7264eb18cb 100644 --- a/cocos2d/core/base-ui/CCWidgetManager.js +++ b/cocos2d/core/base-ui/CCWidgetManager.js @@ -91,9 +91,9 @@ function alignToParent (node, widget) { else { width = node.width * scaleX; if (widget.isAlignHorizontalCenter) { - var horizontalCenter = widget._isAbsHorizontalCenter ? widget._horizontalCenter : widget._horizontalCenter * parentWidth; + var localHorizontalCenter = widget._isAbsHorizontalCenter ? widget._horizontalCenter : widget._horizontalCenter * parentWidth; var parentCenter = (0.5 - parentAnchor.x) * parentWidth; - x = parentCenter + (anchorX - 0.5) * width + horizontalCenter; + x = parentCenter + (anchorX - 0.5) * width + localHorizontalCenter; } else if (widget.isAlignLeft) { x = localLeft + anchorX * width; @@ -138,9 +138,9 @@ function alignToParent (node, widget) { else { height = node.height * scaleY; if (widget.isAlignVerticalCenter) { - var verticalCenter = widget._isAbsVerticalCenter ? widget._verticalCenter : widget._verticalCenter * parentHeight; + var localVerticalCenter = widget._isAbsVerticalCenter ? widget._verticalCenter : widget._verticalCenter * parentHeight; var parentMiddle = (0.5 - parentAnchor.y) * parentHeight; - y = parentMiddle + (anchorY - 0.5) * height + verticalCenter; + y = parentMiddle + (anchorY - 0.5) * height + localVerticalCenter; } else if (widget.isAlignBottom) { y = localBottom + anchorY * height; @@ -309,14 +309,10 @@ var adjustWidgetToAllowResizingInEditor = CC_EDITOR && function (event) { this.right -= (this.isAbsoluteRight ? delta.x : deltaInPercent.x) * (1 - anchor.x); } if (this.isAlignHorizontalCenter) { - if (delta.x !== 0 && anchor.x !== 0.5) { - this.isAlignHorizontalCenter = false; - } + this.horizontalCenter -= (this.isAbsoluteHorizontalCenter ? delta.x : deltaInPercent.x); } if (this.isAlignVerticalCenter) { - if (delta.y !== 0 && anchor.y !== 0.5) { - this.isAlignVerticalCenter = false; - } + this.verticalCenter -= (this.isAbsoluteVerticalCenter ? delta.y : deltaInPercent.y); } }; diff --git a/cocos2d/core/components/CCWidget.js b/cocos2d/core/components/CCWidget.js index 6fdaec20c1..6063ec7088 100644 --- a/cocos2d/core/components/CCWidget.js +++ b/cocos2d/core/components/CCWidget.js @@ -302,7 +302,9 @@ var Widget = cc.Class({ }, /** - * !#en TODO + * !#en + * Horizontal aligns the midpoint offset value, + * the value can be negative, Only available in 'isAlignHorizontalCenter' open. * !#zh 水平居中的偏移值,可填写负值,只有在 isAlignHorizontalCenter 开启时才有作用。 * @property horizontalCenter * @type {Number} @@ -319,7 +321,9 @@ var Widget = cc.Class({ }, /** - * !#en TODO + * !#en + * Vertical aligns the midpoint offset value, + * the value can be negative, Only available in 'isAlignVerticalCenter' open. * !#zh 垂直居中的偏移值,可填写负值,只有在 isAlignVerticalCenter 开启时才有作用。 * @property verticalCenter * @type {Number} @@ -338,8 +342,8 @@ var Widget = cc.Class({ // PARCENTAGE OR ABSOLUTE /** - * !#en TODO - * !#zh 如果为 true,"horizontalCenter" 将会以像素作为偏移值,反之为百分比。 + * !#en If true, horizontalCenter is pixel margin, otherwise is percentage (0 - 1) margin. + * !#zh 如果为 true,"horizontalCenter" 将会以像素作为偏移值,反之为百分比(0 到 1)。 * @property isAbsoluteHorizontalCenter * @type {Boolean} * @default true @@ -355,8 +359,8 @@ var Widget = cc.Class({ }, /** - * !#en TODO - * !#zh 如果为 true,"verticalCenter" 将会以像素作为偏移值,反之为百分比。 + * !#en If true, verticalCenter is pixel margin, otherwise is percentage (0 - 1) margin. + * !#zh 如果为 true,"verticalCenter" 将会以像素作为偏移值,反之为百分比(0 到 1)。 * @property isAbsoluteHorizontalCenter * @type {Boolean} * @default true