diff --git a/extensions/ccui/layouts/UILayout.js b/extensions/ccui/layouts/UILayout.js index 695f7e40f4..9077db64b3 100644 --- a/extensions/ccui/layouts/UILayout.js +++ b/extensions/ccui/layouts/UILayout.js @@ -101,6 +101,7 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ this._clippingRect = cc.rect(0, 0, 0, 0); this._backGroundImageColor = cc.color(255, 255, 255, 255); + this._alignment = ccui.Layout.ALIGN_LEFT; }, /** @@ -146,7 +147,7 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ var stencilClipping = this._clippingEnabled && this._clippingType === ccui.Layout.CLIPPING_STENCIL; var scissorClipping = this._clippingEnabled && this._clippingType === ccui.Layout.CLIPPING_SCISSOR; - + if (stencilClipping) { cmd.stencilClippingVisit(parentCmd); } @@ -1424,6 +1425,28 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ return "Layout"; }, + /** + * Set the alignment. Used by linear layout. + * + * By default left alignment is used in horizontal layouts and top alignment + * in vertical layouts. + * + * @param {ccui.Layout.ALIGN_LEFT|ccui.Layout.ALIGN_TOP|ccui.Layout.ALIGN_CENTER|ccui.Layout.ALIGN_RIGHT|ccui.Layout.ALIGN_BOTTOM|number} alignment + */ + setAlignment: function (alignment) { + this._alignment = alignment; + this._doLayoutDirty = true; + }, + + /** + * Returns the alignment used by linear layout. + * + * @return {ccui.Layout.ALIGN_LEFT|ccui.Layout.ALIGN_TOP|ccui.Layout.ALIGN_CENTER|ccui.Layout.ALIGN_RIGHT|ccui.Layout.ALIGN_BOTTOM} + */ + getAlignment: function () { + return this._alignment; + }, + _createCloneInstance: function () { return new ccui.Layout(); }, @@ -1565,3 +1588,23 @@ ccui.Layout.BACKGROUND_IMAGE_ZORDER = -1; * @constant */ ccui.Layout.BACKGROUND_RENDERER_ZORDER = -2; + +//Layout alignment +/** + * Place items on the left. + * @type {number} + * @constant + */ +ccui.Layout.ALIGN_LEFT = ccui.Layout.ALIGN_TOP = 0; +/** + * Place items in the center. + * @type {number} + * @constant + */ +ccui.Layout.ALIGN_CENTER = 1; +/** + * Place items on the right. + * @type {number} + * @constant + */ +ccui.Layout.ALIGN_RIGHT = ccui.Layout.ALIGN_BOTTOM = 2; diff --git a/extensions/ccui/layouts/UILayoutManager.js b/extensions/ccui/layouts/UILayoutManager.js index 9f89d13e75..ecb666151e 100644 --- a/extensions/ccui/layouts/UILayoutManager.js +++ b/extensions/ccui/layouts/UILayoutManager.js @@ -48,7 +48,30 @@ ccui.linearVerticalLayoutManager = /** @lends ccui.linearVerticalLayoutManager# _doLayout: function (layout) { var layoutSize = layout._getLayoutContentSize(); var container = layout._getLayoutElements(); - var topBoundary = layoutSize.height; + + var topBoundary; + if (layout.getAlignment() == ccui.Layout.ALIGN_TOP) { + topBoundary = layoutSize.height; + } else { + var usedHeight = 0; + for (var i = 0, len = container.length; i < len; i++) { + var child = container[i]; + if (child) { + var layoutParameter = child.getLayoutParameter(); + if (layoutParameter) { + var cs = child.getContentSize(); + var mg = layoutParameter.getMargin(); + usedHeight += cs.height + mg.top + mg.bottom; + } + } + } + + if (layout.getAlignment() == ccui.Layout.ALIGN_CENTER) { + topBoundary = (layoutSize.height + usedHeight) / 2; + } else { + topBoundary = usedHeight; + } + } for (var i = 0, len = container.length; i < len; i++) { var child = container[i]; @@ -94,8 +117,32 @@ ccui.linearHorizontalLayoutManager = /** @lends ccui.linearHorizontalLayoutManag _doLayout: function (layout) { var layoutSize = layout._getLayoutContentSize(); var container = layout._getLayoutElements(); - var leftBoundary = 0.0; - for (var i = 0, len = container.length; i < len; i++) { + + var leftBoundary; + if (layout.getAlignment() == ccui.Layout.ALIGN_LEFT) { + leftBoundary = 0.0; + } else { + var usedWidth = 0; + for (var i = 0, len = container.length; i < len; i++) { + var child = container[i]; + if (child) { + var layoutParameter = child.getLayoutParameter(); + if (layoutParameter) { + var cs = child.getContentSize(); + var mg = layoutParameter.getMargin(); + usedWidth += cs.width + mg.left + mg.right; + } + } + } + + if (layout.getAlignment() == ccui.Layout.ALIGN_CENTER) { + leftBoundary = (layoutSize.width - usedWidth) / 2; + } else { + leftBoundary = layoutSize.width - usedWidth; + } + } + + for (var i = 0, len = container.length; i < len; i++) { var child = container[i]; if (child) { var layoutParameter = child.getLayoutParameter();