Skip to content

Commit

Permalink
Fix for issue cocos2d#19890
Browse files Browse the repository at this point in the history
  • Loading branch information
rh101 committed Dec 10, 2019
1 parent 310d717 commit ae96b8d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 14 deletions.
27 changes: 13 additions & 14 deletions cocos/ui/UILayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,10 @@ const Rect& Layout::getClippingRect()
{
if (_clippingRectDirty)
{
Vec2 worldPos = convertToWorldSpace(Vec2::ZERO);
AffineTransform t = getNodeToWorldAffineTransform();
float scissorWidth = _contentSize.width*t.a;
float scissorHeight = _contentSize.height*t.d;
Rect parentClippingRect;
const Vec2 worldPos = convertToWorldSpace(Vec2::ZERO);
const AffineTransform t = getNodeToWorldAffineTransform();
const float scissorWidth = _contentSize.width * t.a;
const float scissorHeight = _contentSize.height * t.d;
Layout* parent = this;

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

if (_clippingParent)
{
parentClippingRect = _clippingParent->getClippingRect();
float finalX = worldPos.x - (scissorWidth * _anchorPoint.x);
float finalY = worldPos.y - (scissorHeight * _anchorPoint.y);
const Rect& parentClippingRect = _clippingParent->getClippingRect();
float finalX = worldPos.x;
float finalY = worldPos.y;
float finalWidth = scissorWidth;
float finalHeight = scissorHeight;

float leftOffset = worldPos.x - parentClippingRect.origin.x;
const float leftOffset = worldPos.x - parentClippingRect.origin.x;
if (leftOffset < 0.0f)
{
finalX = parentClippingRect.origin.x;
finalWidth += leftOffset;
}
float rightOffset = (worldPos.x + scissorWidth) - (parentClippingRect.origin.x + parentClippingRect.size.width);
const float rightOffset = (worldPos.x + scissorWidth) - (parentClippingRect.origin.x + parentClippingRect.size.width);
if (rightOffset > 0.0f)
{
finalWidth -= rightOffset;
}
float topOffset = (worldPos.y + scissorHeight) - (parentClippingRect.origin.y + parentClippingRect.size.height);
const float topOffset = (worldPos.y + scissorHeight) - (parentClippingRect.origin.y + parentClippingRect.size.height);
if (topOffset > 0.0f)
{
finalHeight -= topOffset;
}
float bottomOffset = worldPos.y - parentClippingRect.origin.y;
const float bottomOffset = worldPos.y - parentClippingRect.origin.y;
if (bottomOffset < 0.0f)
{
finalY = parentClippingRect.origin.y;
Expand All @@ -540,8 +539,8 @@ const Rect& Layout::getClippingRect()
}
else
{
_clippingRect.origin.x = worldPos.x - (scissorWidth * _anchorPoint.x);
_clippingRect.origin.y = worldPos.y - (scissorHeight * _anchorPoint.y);
_clippingRect.origin.x = worldPos.x;
_clippingRect.origin.y = worldPos.y;
_clippingRect.size.width = scissorWidth;
_clippingRect.size.height = scissorHeight;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ UILayoutTests::UILayoutTests()
ADD_TEST_CASE(UILayoutComponentTest);
ADD_TEST_CASE(UILayoutComponent_Berth_Test);
ADD_TEST_CASE(UILayoutComponent_Berth_Stretch_Test);
ADD_TEST_CASE(UILayoutTest_Issue19890);
}

// UILayoutTest
Expand Down Expand Up @@ -956,3 +957,62 @@ bool UILayoutComponent_Berth_Stretch_Test::init()
}
return false;
}

bool UILayoutTest_Issue19890::init()
{
if (!UIScene::init())
{
return false;
}

const Size widgetSize = _widget->getContentSize();

auto label = Text::create("Issue 19890", "fonts/Marker Felt.ttf", 32);
label->setAnchorPoint(Vec2(0.5f, -1.0f));
label->setPosition(Vec2(widgetSize.width / 2.0f,
widgetSize.height / 2.0f + label->getContentSize().height * 1.5f));
_uiLayer->addChild(label);

Text* alert = Text::create("3 panels should be completely visible", "fonts/Marker Felt.ttf", 20);
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Vec2(widgetSize.width / 2.0f,
widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
_uiLayer->addChild(alert);

Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));

Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
const Size backgroundSize = background->getContentSize();

auto panel = ui::Layout::create();
panel->setBackGroundColor(Color3B::RED);
panel->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
panel->setClippingType(ui::Layout::ClippingType::SCISSOR);
panel->setPosition(backgroundSize / 2);
panel->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
panel->setClippingEnabled(true);
panel->setContentSize(backgroundSize); // from the left to the screen end
background->addChild(panel);

auto panel2 = ui::Layout::create();
panel2->setBackGroundColor(Color3B::BLUE);
panel2->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
panel2->setClippingType(ui::Layout::ClippingType::SCISSOR);
panel2->setPosition(panel->getContentSize() / 2);
panel2->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
panel2->setClippingEnabled(true);
panel2->setContentSize(panel->getContentSize() / 2); // from the left to the screen end
panel->addChild(panel2);

auto panel3 = ui::Layout::create();
panel3->setBackGroundColor(Color3B::GREEN);
panel3->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
panel3->setClippingType(ui::Layout::ClippingType::SCISSOR);
panel3->setPosition(panel2->getContentSize() / 2);
panel3->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
panel3->setClippingEnabled(true);
panel3->setContentSize(panel2->getContentSize() / 2); // from the left to the screen end
panel2->addChild(panel3);

return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,12 @@ class UILayoutComponent_Berth_Stretch_Test : public UILayoutComponentTest
CREATE_FUNC(UILayoutComponent_Berth_Stretch_Test);
};

class UILayoutTest_Issue19890 : public UIScene
{
public:
virtual bool init() override;

CREATE_FUNC(UILayoutTest_Issue19890);
};

#endif /* defined(__TestCpp__UILayoutTest__) */

0 comments on commit ae96b8d

Please sign in to comment.