Skip to content

Commit

Permalink
Automatically calculated content size of ScrollablePanel was wrong wh…
Browse files Browse the repository at this point in the history
…en the child widgets didn't have their origin at the top-left corner
  • Loading branch information
texus committed Dec 2, 2023
1 parent 0cc7738 commit fde78fe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Widgets/ScrollablePanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ namespace tgui

if (m_contentSize == Vector2f{0, 0})
{
const Vector2f bottomRight = widget->getPosition() + widget->getFullSize();
const Vector2f bottomRight{
widget->getPosition().x - (widget->getOrigin().x * widget->getSize().x) + widget->getFullSize().x,
widget->getPosition().y - (widget->getOrigin().y * widget->getSize().y) + widget->getFullSize().y
};
if (bottomRight.x > m_mostBottomRightPosition.x)
m_mostBottomRightPosition.x = bottomRight.x;
if (bottomRight.y > m_mostBottomRightPosition.y)
Expand Down Expand Up @@ -246,7 +249,10 @@ namespace tgui

if (m_contentSize == Vector2f{0, 0})
{
const Vector2f bottomRight = widget->getPosition() + widget->getFullSize();
const Vector2f bottomRight{
widget->getPosition().x - (widget->getOrigin().x * widget->getSize().x) + widget->getFullSize().x,
widget->getPosition().y - (widget->getOrigin().y * widget->getSize().y) + widget->getFullSize().y
};
if ((bottomRight.x == m_mostBottomRightPosition.x) || (bottomRight.y == m_mostBottomRightPosition.y))
{
recalculateMostBottomRightPosition();
Expand Down Expand Up @@ -859,7 +865,10 @@ namespace tgui

for (const auto& widget : m_widgets)
{
const Vector2f bottomRight = widget->getPosition() + widget->getFullSize();
const Vector2f bottomRight{
widget->getPosition().x - (widget->getOrigin().x * widget->getSize().x) + widget->getFullSize().x,
widget->getPosition().y - (widget->getOrigin().y * widget->getSize().y) + widget->getFullSize().y
};
if (bottomRight.x > m_mostBottomRightPosition.x)
m_mostBottomRightPosition.x = bottomRight.x;
if (bottomRight.y > m_mostBottomRightPosition.y)
Expand Down
26 changes: 26 additions & 0 deletions tests/Widgets/ScrollablePanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@ TEST_CASE("[ScrollablePanel]")
REQUIRE(panel->getScrollbarWidth() == 15);
}

SECTION("Content size")
{
panel->setSize(200, 100);

auto widget1 = tgui::Button::create();
widget1->setPosition({20, 30});
widget1->setSize({200, 50});
panel->add(widget1);

REQUIRE(panel->getContentSize() == tgui::Vector2f{220, 80});

auto widget2 = tgui::Button::create();
widget2->setPosition({200, 70});
widget2->setSize({300, 60});
widget2->setOrigin({0.5f, 1.f});
panel->add(widget2);

REQUIRE(panel->getContentSize().x == Approx{350});
REQUIRE(panel->getContentSize().y == 80);

panel->setContentSize({200, 100});
REQUIRE(panel->getContentSize() == tgui::Vector2f{200, 100});
}

SECTION("Events / Signals")
{
unsigned int mousePressedCount = 0;
Expand Down Expand Up @@ -237,6 +261,8 @@ TEST_CASE("[ScrollablePanel]")
REQUIRE(mouseReleasedCount == 2);
REQUIRE(clickedCount == 1);
}

// TODO: Scroll event
}

testWidgetRenderer(panel->getRenderer());
Expand Down

0 comments on commit fde78fe

Please sign in to comment.