diff --git a/cocos2d/core/base-ui/CCWidgetManager.js b/cocos2d/core/base-ui/CCWidgetManager.js index 7cbcc15638..7264eb18cb 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 localHorizontalCenter = widget._isAbsHorizontalCenter ? widget._horizontalCenter : widget._horizontalCenter * parentWidth; + var parentCenter = (0.5 - parentAnchor.x) * parentWidth; + x = parentCenter + (anchorX - 0.5) * width + localHorizontalCenter; } 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 localVerticalCenter = widget._isAbsVerticalCenter ? widget._verticalCenter : widget._verticalCenter * parentHeight; + var parentMiddle = (0.5 - parentAnchor.y) * parentHeight; + y = parentMiddle + (anchorY - 0.5) * height + localVerticalCenter; } 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); } }; @@ -311,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 4c4c739bcf..6063ec7088 100644 --- a/cocos2d/core/components/CCWidget.js +++ b/cocos2d/core/components/CCWidget.js @@ -301,8 +301,80 @@ var Widget = cc.Class({ tooltip: 'i18n:COMPONENT.widget.right', }, + /** + * !#en + * Horizontal aligns the midpoint offset value, + * the value can be negative, Only available in 'isAlignHorizontalCenter' open. + * !#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 + * Vertical aligns the midpoint offset value, + * the value can be negative, Only available in 'isAlignVerticalCenter' open. + * !#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 If true, horizontalCenter is pixel margin, otherwise is percentage (0 - 1) margin. + * !#zh 如果为 true,"horizontalCenter" 将会以像素作为偏移值,反之为百分比(0 到 1)。 + * @property isAbsoluteHorizontalCenter + * @type {Boolean} + * @default true + */ + isAbsoluteHorizontalCenter: { + get: function () { + return this._isAbsHorizontalCenter; + }, + set: function (value) { + this._isAbsHorizontalCenter = value; + }, + animatable: false + }, + + /** + * !#en If true, verticalCenter is pixel margin, otherwise is percentage (0 - 1) margin. + * !#zh 如果为 true,"verticalCenter" 将会以像素作为偏移值,反之为百分比(0 到 1)。 + * @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 +483,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,