Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion extensions/ccui/layouts/UILayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

/**
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
},
Expand Down Expand Up @@ -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;
53 changes: 50 additions & 3 deletions extensions/ccui/layouts/UILayoutManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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();
Expand Down