Skip to content

Commit afcffc7

Browse files
thuydx55minggo
authored and
minggo
committed
Handle auto layout with scaled widget (#17822)
* handle auto layout with scaled widget * add test for UILayout for Scaled Widget * add test for UILayout - Scaled Widget - JS
1 parent 92e713b commit afcffc7

File tree

6 files changed

+127
-12
lines changed

6 files changed

+127
-12
lines changed

Diff for: cocos/ui/UILayoutManager.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void LinearHorizontalLayoutManager::doLayout(LayoutProtocol* layout)
5757
{
5858
LinearLayoutParameter::LinearGravity childGravity = layoutParameter->getGravity();
5959
Vec2 ap = child->getAnchorPoint();
60-
Size cs = child->getContentSize();
60+
Size cs = child->getBoundingBox().size;
6161
float finalPosX = leftBoundary + (ap.x * cs.width);
6262
float finalPosY = layoutSize.height - (1.0f - ap.y) * cs.height;
6363
switch (childGravity)
@@ -115,7 +115,7 @@ void LinearVerticalLayoutManager::doLayout(LayoutProtocol* layout)
115115
{
116116
LinearLayoutParameter::LinearGravity childGravity = layoutParameter->getGravity();
117117
Vec2 ap = subWidget->getAnchorPoint();
118-
Size cs = subWidget->getContentSize();
118+
Size cs = subWidget->getBoundingBox().size;
119119
float finalPosX = ap.x * cs.width;
120120
float finalPosY = topBoundary - ((1.0f-ap.y) * cs.height);
121121
switch (childGravity)
@@ -136,7 +136,7 @@ void LinearVerticalLayoutManager::doLayout(LayoutProtocol* layout)
136136
finalPosX += mg.left;
137137
finalPosY -= mg.top;
138138
subWidget->setPosition(finalPosX, finalPosY);
139-
topBoundary = subWidget->getPosition().y - subWidget->getAnchorPoint().y * subWidget->getContentSize().height - mg.bottom;
139+
topBoundary = subWidget->getPosition().y - subWidget->getAnchorPoint().y * subWidget->getBoundingBox().size.height - mg.bottom;
140140
}
141141
}
142142
}
@@ -206,7 +206,7 @@ Widget* RelativeLayoutManager::getRelativeWidget(Widget* widget)
206206
bool RelativeLayoutManager::calculateFinalPositionWithRelativeWidget(LayoutProtocol *layout)
207207
{
208208
Vec2 ap = _widget->getAnchorPoint();
209-
Size cs = _widget->getContentSize();
209+
Size cs = _widget->getBoundingBox().size;
210210

211211
_finalPositionX = 0.0f;
212212
_finalPositionY = 0.0f;
@@ -280,7 +280,7 @@ bool RelativeLayoutManager::calculateFinalPositionWithRelativeWidget(LayoutProto
280280
{
281281
return false;
282282
}
283-
Size rbs = relativeWidget->getContentSize();
283+
Size rbs = relativeWidget->getBoundingBox().size;
284284
float locationTop = relativeWidget->getTopBoundary();
285285

286286
_finalPositionY = locationTop + ap.y * cs.height;
@@ -320,7 +320,7 @@ bool RelativeLayoutManager::calculateFinalPositionWithRelativeWidget(LayoutProto
320320
{
321321
return false;
322322
}
323-
Size rbs = relativeWidget->getContentSize();
323+
Size rbs = relativeWidget->getBoundingBox().size;
324324
float locationLeft = relativeWidget->getLeftBoundary();
325325
_finalPositionX = locationLeft - (1.0f - ap.x) * cs.width;
326326

@@ -360,7 +360,7 @@ bool RelativeLayoutManager::calculateFinalPositionWithRelativeWidget(LayoutProto
360360
{
361361
return false;
362362
}
363-
Size rbs = relativeWidget->getContentSize();
363+
Size rbs = relativeWidget->getBoundingBox().size;
364364
float locationRight = relativeWidget->getRightBoundary();
365365
_finalPositionX = locationRight + ap.x * cs.width;
366366

@@ -400,7 +400,7 @@ bool RelativeLayoutManager::calculateFinalPositionWithRelativeWidget(LayoutProto
400400
{
401401
return false;
402402
}
403-
Size rbs = relativeWidget->getContentSize();
403+
Size rbs = relativeWidget->getBoundingBox().size;
404404
float locationBottom = relativeWidget->getBottomBoundary();
405405

406406
_finalPositionY = locationBottom - (1.0f - ap.y) * cs.height;

Diff for: cocos/ui/UIWidget.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1125,22 +1125,22 @@ bool Widget::isEnabled() const
11251125

11261126
float Widget::getLeftBoundary() const
11271127
{
1128-
return getPosition().x - getAnchorPoint().x * _contentSize.width;
1128+
return getBoundingBox().origin.x;
11291129
}
11301130

11311131
float Widget::getBottomBoundary() const
11321132
{
1133-
return getPosition().y - getAnchorPoint().y * _contentSize.height;
1133+
return getBoundingBox().origin.y;
11341134
}
11351135

11361136
float Widget::getRightBoundary() const
11371137
{
1138-
return getLeftBoundary() + _contentSize.width;
1138+
return getLeftBoundary() + getBoundingBox().size.width;
11391139
}
11401140

11411141
float Widget::getTopBoundary() const
11421142
{
1143-
return getBottomBoundary() + _contentSize.height;
1143+
return getBottomBoundary() + getBoundingBox().size.height;
11441144
}
11451145

11461146
const Vec2& Widget::getTouchBeganPosition()const

Diff for: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ UILayoutTests::UILayoutTests()
1414
ADD_TEST_CASE(UILayoutTest_Layout_Linear_Horizontal);
1515
ADD_TEST_CASE(UILayoutTest_Layout_Relative_Align_Parent);
1616
ADD_TEST_CASE(UILayoutTest_Layout_Relative_Location);
17+
ADD_TEST_CASE(UILayoutTest_Layout_Scaled_Widget);
1718
ADD_TEST_CASE(UILayoutComponentTest);
1819
ADD_TEST_CASE(UILayoutComponent_Berth_Test);
1920
ADD_TEST_CASE(UILayoutComponent_Berth_Stretch_Test);
@@ -726,6 +727,62 @@ bool UILayoutTest_Layout_Relative_Location::init()
726727
return false;
727728
}
728729

730+
// UILayoutTest_Layout_Relative_Location
731+
732+
UILayoutTest_Layout_Scaled_Widget::UILayoutTest_Layout_Scaled_Widget()
733+
{
734+
}
735+
736+
UILayoutTest_Layout_Scaled_Widget::~UILayoutTest_Layout_Scaled_Widget()
737+
{
738+
}
739+
740+
bool UILayoutTest_Layout_Scaled_Widget::init()
741+
{
742+
if (UIScene::init())
743+
{
744+
Size widgetSize = _widget->getContentSize();
745+
746+
// Add the alert
747+
Text* alert = Text::create("Layout Scaled Widget", "fonts/Marker Felt.ttf", 20);
748+
alert->setColor(Color3B(159, 168, 176));
749+
alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 4.5f));
750+
_uiLayer->addChild(alert);
751+
752+
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
753+
754+
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
755+
756+
// Create the layout
757+
Layout* layout = Layout::create();
758+
layout->setLayoutType(Layout::Type::HORIZONTAL);
759+
layout->setContentSize(Size(280, 150));
760+
Size backgroundSize = background->getContentSize();
761+
layout->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
762+
(backgroundSize.width - layout->getContentSize().width) / 2.0f,
763+
(widgetSize.height - backgroundSize.height) / 2.0f +
764+
(backgroundSize.height - layout->getContentSize().height) / 2.0f));
765+
_uiLayer->addChild(layout);
766+
767+
ImageView* imageView_Center1 = ImageView::create("cocosui/scrollviewbg.png");
768+
imageView_Center1->setScale(0.5);
769+
layout->addChild(imageView_Center1);
770+
771+
ImageView* imageView_Center2 = ImageView::create("cocosui/scrollviewbg.png");
772+
imageView_Center2->setScale(1.2);
773+
layout->addChild(imageView_Center2);
774+
775+
ImageView* imageView_Center3 = ImageView::create("cocosui/scrollviewbg.png");
776+
imageView_Center3->setScale(0.8);
777+
layout->addChild(imageView_Center3);
778+
779+
return true;
780+
}
781+
782+
return false;
783+
}
784+
785+
729786
bool UILayoutComponentTest::init()
730787
{
731788
if (UIScene::init())

Diff for: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.h

+10
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ class UILayoutTest_Layout_Relative_Location : public UIScene
123123
CREATE_FUNC(UILayoutTest_Layout_Relative_Location);
124124
};
125125

126+
class UILayoutTest_Layout_Scaled_Widget : public UIScene
127+
{
128+
public:
129+
UILayoutTest_Layout_Scaled_Widget();
130+
~UILayoutTest_Layout_Scaled_Widget();
131+
virtual bool init() override;
132+
133+
CREATE_FUNC(UILayoutTest_Layout_Scaled_Widget);
134+
};
135+
126136
class UILayoutComponentTest : public UIScene
127137
{
128138
public:

Diff for: tests/js-tests/src/GUITest/UILayoutTest/UILayoutTest.js

+42
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,48 @@ var UILayoutTest_Layout_Relative_Location = UIMainLayer.extend({
406406
}
407407
});
408408

409+
var UILayoutTest_Layout_Scaled_Widget = UIMainLayer.extend({
410+
init: function(){
411+
if (this._super()) {
412+
var widgetSize = this._widget.getContentSize();
413+
414+
// Add the alert
415+
var alert = new ccui.Text("Layout Scaled Widget", "Marker Felt", 20);
416+
alert.setColor(cc.color(159, 168, 176));
417+
alert.setPosition(widgetSize.width / 2.0, widgetSize.height / 2.0 - alert.height * 4.5);
418+
this._mainNode.addChild(alert);
419+
420+
var root = this._mainNode.getChildByTag(81);
421+
var background = root.getChildByName("background_Panel");
422+
423+
// Create the layout
424+
var layout = new ccui.Layout();
425+
layout.setLayoutType(ccui.Layout.LINEAR_HORIZONTAL);
426+
layout.setContentSize(280, 150);
427+
var backgroundSize = background.getContentSize();
428+
layout.setPosition((widgetSize.width - backgroundSize.width) / 2.0 + (backgroundSize.width - layout.width) / 2.0,
429+
(widgetSize.height - backgroundSize.height) / 2.0 + (backgroundSize.height - layout.height) / 2.0 );
430+
this._mainNode.addChild(layout);
431+
432+
// center
433+
var imageView_Center1 = new ccui.ImageView("ccs-res/cocosui/scrollviewbg.png");
434+
imageView_Center1.scale = 0.5;
435+
layout.addChild(imageView_Center1);
436+
437+
var imageView_Center2 = new ccui.ImageView("ccs-res/cocosui/scrollviewbg.png");
438+
imageView_Center2.scale = 1.2;
439+
layout.addChild(imageView_Center2);
440+
441+
var imageView_Center3 = new ccui.ImageView("ccs-res/cocosui/scrollviewbg.png");
442+
imageView_Center3.scale = 0.8;
443+
layout.addChild(imageView_Center3);
444+
445+
return true;
446+
}
447+
return false;
448+
}
449+
});
450+
409451
var UILayoutComponentTest = UIMainLayer.extend({
410452
_baseLayer: null,
411453
init: function(){

Diff for: tests/js-tests/src/GUITest/UISceneManager.js

+6
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@
410410
return new UILayoutTest_Layout_Relative_Location();
411411
}
412412
},
413+
{
414+
title: "UILayoutTest_Layout_Scaled_Widget",
415+
func: function () {
416+
return new UILayoutTest_Layout_Scaled_Widget();
417+
}
418+
},
413419
{
414420
title: "UILayoutComponent_Berth_Test",
415421
func: function () {

0 commit comments

Comments
 (0)