Skip to content

Commit

Permalink
Merge pull request cocos#1234 from jareguo/master
Browse files Browse the repository at this point in the history
add widget.updateAlignment
  • Loading branch information
jareguo authored Nov 2, 2016
2 parents af30cbf + 85ea595 commit d661039
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
15 changes: 14 additions & 1 deletion cocos2d/core/base-ui/CCWidgetManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ var adjustWidgetToAllowResizingInEditor = CC_EDITOR && function (event) {

var activeWidgets = [];

// updateAlignment from scene to node recursively
function updateAlignment (node) {
var parent = node._parent;
if (parent) {
updateAlignment(parent);
}
var widget = node._widget;
if (widget) {
alignToParent(node, widget);
}
}

var widgetManager = cc._widgetManager = module.exports = {
_AlignFlags: {
TOP: TOP,
Expand Down Expand Up @@ -356,5 +368,6 @@ var widgetManager = cc._widgetManager = module.exports = {
widget.node.off('size-changed', adjustWidgetToAllowResizingInEditor, widget);
}
},
_getParentSize: getParentSize
_getParentSize: getParentSize,
updateAlignment: updateAlignment
};
22 changes: 21 additions & 1 deletion cocos2d/core/components/CCWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,27 @@ var Widget = cc.Class({

this._alignFlags &= ~flag;
}
}
},

/**
* !#en
* Immediately perform the widget alignment. You need to manually call this method only if
* you need to get the latest results after the alignment before the end of current frame.
* !#zh
* 立刻执行 widget 对齐操作。这个接口一般不需要手工调用。
* 只有当你需要在当前帧结束前获得 widget 对齐后的最新结果时才需要手动调用这个方法。
*
* @method updateAlignment
*
* @example
* widget.top = 10; // change top margin
* cc.log(widget.node.y); // not yet changed
* widget.updateAlignment();
* cc.log(widget.node.y); // changed
*/
updateAlignment: function () {
WidgetManager.updateAlignment(this.node);
},
});


Expand Down
28 changes: 28 additions & 0 deletions test/qunit/unit/test-widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module('cc.Widget', SetupEngine);

test('updateAlignment', function () {
var scene = cc.director.getScene();
var dummyCanvas = new cc.Node();
dummyCanvas.width = 960;
dummyCanvas.parent = scene;

var parent = new cc.Node();
var parentWidget = parent.addComponent(cc.Widget);
parent.parent = dummyCanvas;

var child = new cc.Node();
var childWidget = child.addComponent(cc.Widget);
child.parent = parent;

parentWidget.isAlignLeft = true;
parentWidget.isAlignRight = true;
parentWidget.left = 0;
parentWidget.right = 0;
childWidget.isAlignLeft = true;
childWidget.isAlignRight = true;
childWidget.left = 0;
childWidget.right = 0;

childWidget.updateAlignment();
strictEqual(child.width, 960, 'child size should be updated even if parent size also diry');
});

0 comments on commit d661039

Please sign in to comment.