Skip to content

Commit 9b2a2db

Browse files
rh101minggo
authored andcommitted
Fix for issue #19890 (#20407)
1 parent 8214f27 commit 9b2a2db

File tree

3 files changed

+81
-14
lines changed

3 files changed

+81
-14
lines changed

cocos/ui/UILayout.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,10 @@ const Rect& Layout::getClippingRect()
475475
{
476476
if (_clippingRectDirty)
477477
{
478-
Vec2 worldPos = convertToWorldSpace(Vec2::ZERO);
479-
AffineTransform t = getNodeToWorldAffineTransform();
480-
float scissorWidth = _contentSize.width*t.a;
481-
float scissorHeight = _contentSize.height*t.d;
482-
Rect parentClippingRect;
478+
const Vec2 worldPos = convertToWorldSpace(Vec2::ZERO);
479+
const AffineTransform t = getNodeToWorldAffineTransform();
480+
const float scissorWidth = _contentSize.width * t.a;
481+
const float scissorHeight = _contentSize.height * t.d;
483482
Layout* parent = this;
484483

485484
while (parent)
@@ -497,29 +496,29 @@ const Rect& Layout::getClippingRect()
497496

498497
if (_clippingParent)
499498
{
500-
parentClippingRect = _clippingParent->getClippingRect();
501-
float finalX = worldPos.x - (scissorWidth * _anchorPoint.x);
502-
float finalY = worldPos.y - (scissorHeight * _anchorPoint.y);
499+
const Rect& parentClippingRect = _clippingParent->getClippingRect();
500+
float finalX = worldPos.x;
501+
float finalY = worldPos.y;
503502
float finalWidth = scissorWidth;
504503
float finalHeight = scissorHeight;
505504

506-
float leftOffset = worldPos.x - parentClippingRect.origin.x;
505+
const float leftOffset = worldPos.x - parentClippingRect.origin.x;
507506
if (leftOffset < 0.0f)
508507
{
509508
finalX = parentClippingRect.origin.x;
510509
finalWidth += leftOffset;
511510
}
512-
float rightOffset = (worldPos.x + scissorWidth) - (parentClippingRect.origin.x + parentClippingRect.size.width);
511+
const float rightOffset = (worldPos.x + scissorWidth) - (parentClippingRect.origin.x + parentClippingRect.size.width);
513512
if (rightOffset > 0.0f)
514513
{
515514
finalWidth -= rightOffset;
516515
}
517-
float topOffset = (worldPos.y + scissorHeight) - (parentClippingRect.origin.y + parentClippingRect.size.height);
516+
const float topOffset = (worldPos.y + scissorHeight) - (parentClippingRect.origin.y + parentClippingRect.size.height);
518517
if (topOffset > 0.0f)
519518
{
520519
finalHeight -= topOffset;
521520
}
522-
float bottomOffset = worldPos.y - parentClippingRect.origin.y;
521+
const float bottomOffset = worldPos.y - parentClippingRect.origin.y;
523522
if (bottomOffset < 0.0f)
524523
{
525524
finalY = parentClippingRect.origin.y;
@@ -540,8 +539,8 @@ const Rect& Layout::getClippingRect()
540539
}
541540
else
542541
{
543-
_clippingRect.origin.x = worldPos.x - (scissorWidth * _anchorPoint.x);
544-
_clippingRect.origin.y = worldPos.y - (scissorHeight * _anchorPoint.y);
542+
_clippingRect.origin.x = worldPos.x;
543+
_clippingRect.origin.y = worldPos.y;
545544
_clippingRect.size.width = scissorWidth;
546545
_clippingRect.size.height = scissorHeight;
547546
}

tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ UILayoutTests::UILayoutTests()
4242
ADD_TEST_CASE(UILayoutComponentTest);
4343
ADD_TEST_CASE(UILayoutComponent_Berth_Test);
4444
ADD_TEST_CASE(UILayoutComponent_Berth_Stretch_Test);
45+
ADD_TEST_CASE(UILayoutTest_Issue19890);
4546
}
4647

4748
// UILayoutTest
@@ -956,3 +957,62 @@ bool UILayoutComponent_Berth_Stretch_Test::init()
956957
}
957958
return false;
958959
}
960+
961+
bool UILayoutTest_Issue19890::init()
962+
{
963+
if (!UIScene::init())
964+
{
965+
return false;
966+
}
967+
968+
const Size widgetSize = _widget->getContentSize();
969+
970+
auto label = Text::create("Issue 19890", "fonts/Marker Felt.ttf", 32);
971+
label->setAnchorPoint(Vec2(0.5f, -1.0f));
972+
label->setPosition(Vec2(widgetSize.width / 2.0f,
973+
widgetSize.height / 2.0f + label->getContentSize().height * 1.5f));
974+
_uiLayer->addChild(label);
975+
976+
Text* alert = Text::create("3 panels should be completely visible", "fonts/Marker Felt.ttf", 20);
977+
alert->setColor(Color3B(159, 168, 176));
978+
alert->setPosition(Vec2(widgetSize.width / 2.0f,
979+
widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
980+
_uiLayer->addChild(alert);
981+
982+
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
983+
984+
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
985+
const Size backgroundSize = background->getContentSize();
986+
987+
auto panel = ui::Layout::create();
988+
panel->setBackGroundColor(Color3B::RED);
989+
panel->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
990+
panel->setClippingType(ui::Layout::ClippingType::SCISSOR);
991+
panel->setPosition(backgroundSize / 2);
992+
panel->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
993+
panel->setClippingEnabled(true);
994+
panel->setContentSize(backgroundSize); // from the left to the screen end
995+
background->addChild(panel);
996+
997+
auto panel2 = ui::Layout::create();
998+
panel2->setBackGroundColor(Color3B::BLUE);
999+
panel2->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
1000+
panel2->setClippingType(ui::Layout::ClippingType::SCISSOR);
1001+
panel2->setPosition(panel->getContentSize() / 2);
1002+
panel2->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
1003+
panel2->setClippingEnabled(true);
1004+
panel2->setContentSize(panel->getContentSize() / 2); // from the left to the screen end
1005+
panel->addChild(panel2);
1006+
1007+
auto panel3 = ui::Layout::create();
1008+
panel3->setBackGroundColor(Color3B::GREEN);
1009+
panel3->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
1010+
panel3->setClippingType(ui::Layout::ClippingType::SCISSOR);
1011+
panel3->setPosition(panel2->getContentSize() / 2);
1012+
panel3->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
1013+
panel3->setClippingEnabled(true);
1014+
panel3->setContentSize(panel2->getContentSize() / 2); // from the left to the screen end
1015+
panel2->addChild(panel3);
1016+
1017+
return true;
1018+
}

tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILayoutTest/UILayoutTest.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,12 @@ class UILayoutComponent_Berth_Stretch_Test : public UILayoutComponentTest
162162
CREATE_FUNC(UILayoutComponent_Berth_Stretch_Test);
163163
};
164164

165+
class UILayoutTest_Issue19890 : public UIScene
166+
{
167+
public:
168+
virtual bool init() override;
169+
170+
CREATE_FUNC(UILayoutTest_Issue19890);
171+
};
172+
165173
#endif /* defined(__TestCpp__UILayoutTest__) */

0 commit comments

Comments
 (0)