Skip to content

Commit

Permalink
Grid didn't update its size in remove and setAutoSize functions while…
Browse files Browse the repository at this point in the history
… auto-sizing
  • Loading branch information
texus committed Aug 20, 2024
1 parent 98d03d6 commit 429c744
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ TGUI 1.5 (TBD)
- Added changeItemHierarchy to TreeView
- Scrollbar::setOrientation will no longer flip width and height
- Scrollbar::setSize no longer affects orientation once setOrientation is called
- Grid didn't update its size in remove and setAutoSize functions while auto-sizing
- Opacity of ScrollablePanel wasn't applied to its scrollbars
- Setting opacity of SeparatorLine had no effect
- SFML backend no longer uses sf::Keyboard::isKeyPressed to check modifier keys
Expand Down
5 changes: 5 additions & 0 deletions include/TGUI/Widgets/Grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void updateWidgets();

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Updates the grid to the correct size when auto-sizing. This function should only be called if m_autoSize is true.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void recalculateAutoSize();

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Makes a copy of the widget
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion include/TGUI/Widgets/ProgressBar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ TGUI_MODULE_EXPORT namespace tgui
public:

SignalUInt onValueChange = {"ValueChanged"}; //!< Value of the progress bar changed. Optional parameter: new value
Signal onFull = {"Full"}; //!< Value of the progress bar changed and he progress bar became full
Signal onFull = {"Full"}; //!< Value of the progress bar changed and the progress bar became full

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:
Expand Down
43 changes: 27 additions & 16 deletions src/Widgets/Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,14 @@ namespace tgui

void Grid::setAutoSize(bool autoSize)
{
if (m_autoSize != autoSize)
{
m_autoSize = autoSize;
if (m_autoSize == autoSize)
return;

m_autoSize = autoSize;
if (autoSize)
updateWidgets();
else // We don't need to change the size
updatePositionsOfAllWidgets();
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -275,6 +278,9 @@ namespace tgui
}
}

if (m_autoSize)
recalculateAutoSize();

// Update the positions of all remaining widgets
updatePositionsOfAllWidgets();
}
Expand Down Expand Up @@ -819,22 +825,27 @@ namespace tgui
}

if (m_autoSize)
{
Vector2f size;
for (std::size_t row = 0; row < m_gridWidgets.size(); ++row)
{
float rowWidth = 0;
for (std::size_t col = 0; col < m_gridWidgets[row].size(); ++col)
rowWidth += m_columnWidth[col];
recalculateAutoSize();

size.x = std::max(size.x, rowWidth);
size.y += m_rowHeight[row];
}
updatePositionsOfAllWidgets();
}

Container::setSize(size);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Grid::recalculateAutoSize()
{
Vector2f size;
for (std::size_t row = 0; row < m_gridWidgets.size(); ++row)
{
float rowWidth = 0;
for (std::size_t col = 0; col < m_gridWidgets[row].size(); ++col)
rowWidth += m_columnWidth[col];

size.x = std::max(size.x, rowWidth);
size.y += m_rowHeight[row];
}

updatePositionsOfAllWidgets();
Container::setSize(size);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
70 changes: 70 additions & 0 deletions tests/Widgets/Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,76 @@ TEST_CASE("[Grid]")
REQUIRE(grid->getWidget(1, 5) == nullptr);
}

SECTION("Size")
{
auto widget = tgui::ClickableWidget::create();
widget->setSize(40, 30);

auto widget2 = tgui::ClickableWidget::create();
widget2->setSize(35, 45);

SECTION("Auto-size")
{
REQUIRE(grid->getSize() == tgui::Vector2f(0, 0));

grid->addWidget(widget, 0, 0);
REQUIRE(grid->getSize() == tgui::Vector2f(40, 30));

grid->addWidget(widget2, 0, 1);
REQUIRE(grid->getSize() == tgui::Vector2f(75, 45));

grid->remove(widget);
REQUIRE(grid->getSize() == tgui::Vector2f(35, 45));

grid->addWidget(widget, 1, 1, tgui::Grid::Alignment::Center, {20, 15});
REQUIRE(grid->getSize() == tgui::Vector2f(80, 105));

grid->removeAllWidgets();
REQUIRE(grid->getSize() == tgui::Vector2f(0, 0));

grid->addWidget(widget, 0, 0, tgui::Grid::Alignment::Center, {25, 25});
grid->addWidget(widget2, 1, 1, tgui::Grid::Alignment::Center, {20, 15});
REQUIRE(grid->getSize() == tgui::Vector2f(165, 155));
}

SECTION("Custom size")
{
grid->setSize({120, 100});
REQUIRE(grid->getSize() == tgui::Vector2f(120, 100));

grid->addWidget(widget, 0, 0);
REQUIRE(grid->getSize() == tgui::Vector2f(120, 100));

grid->remove(widget);
REQUIRE(grid->getSize() == tgui::Vector2f(120, 100));

grid->addWidget(widget, 1, 1, tgui::Grid::Alignment::Center, {20, 15});
REQUIRE(grid->getSize() == tgui::Vector2f(120, 100));

grid->removeAllWidgets();
REQUIRE(grid->getSize() == tgui::Vector2f(120, 100));

grid->addWidget(widget, 0, 0, tgui::Grid::Alignment::Center, {25, 25});
grid->addWidget(widget2, 1, 1, tgui::Grid::Alignment::Center, {20, 15});
REQUIRE(grid->getSize() == tgui::Vector2f(120, 100));
}

SECTION("Explicitly switching mode")
{
grid->addWidget(widget, 0, 0);
REQUIRE(grid->getSize() == tgui::Vector2f(40, 30));

grid->setAutoSize(false);
REQUIRE(grid->getSize() == tgui::Vector2f(40, 30));

grid->setSize({120, 100});
REQUIRE(grid->getSize() == tgui::Vector2f(120, 100));

grid->setAutoSize(true);
REQUIRE(grid->getSize() == tgui::Vector2f(40, 30));
}
}

SECTION("Borders")
{
auto widget = tgui::ClickableWidget::create({40, 30});
Expand Down

0 comments on commit 429c744

Please sign in to comment.