🪲 Grid did not set its size when auto-sizing

This commit is contained in:
Bruno Van de Velde 2017-10-18 20:06:56 +02:00
parent 2835dcdbf4
commit d130c58bb8
2 changed files with 22 additions and 4 deletions

View File

@ -83,6 +83,7 @@ namespace tgui
// Make sure it is not the same widget // Make sure it is not the same widget
if (this != &right) if (this != &right)
{ {
m_autoSize = right.m_autoSize; // Has to be set before Container::operator= is called which will call removeAllWidgets which could set size to (0,0)
Container::operator=(right); Container::operator=(right);
for (std::size_t row = 0; row < right.m_gridWidgets.size(); ++row) for (std::size_t row = 0; row < right.m_gridWidgets.size(); ++row)
@ -151,6 +152,8 @@ namespace tgui
{ {
Container::setSize(size); Container::setSize(size);
m_autoSize = false;
updatePositionsOfAllWidgets(); updatePositionsOfAllWidgets();
} }
@ -742,7 +745,22 @@ namespace tgui
} }
} }
// Reposition all widgets if (m_autoSize)
{
sf::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];
}
Container::setSize(size);
}
updatePositionsOfAllWidgets(); updatePositionsOfAllWidgets();
} }

View File

@ -37,7 +37,7 @@ TEST_CASE("[Grid]")
SECTION("Adding widgets") SECTION("Adding widgets")
{ {
auto widget1 = tgui::ClickableWidget::create(); auto widget1 = tgui::ClickableWidget::create({100, 50});
grid->addWidget(widget1, 3, 2); grid->addWidget(widget1, 3, 2);
REQUIRE(grid->getWidgets().size() == 1); REQUIRE(grid->getWidgets().size() == 1);
@ -50,7 +50,7 @@ TEST_CASE("[Grid]")
REQUIRE(grid->getWidgetBorders(0, 0) == tgui::Borders{0}); REQUIRE(grid->getWidgetBorders(0, 0) == tgui::Borders{0});
REQUIRE(grid->getWidgetAlignment(0, 0) == tgui::Grid::Alignment::Center); REQUIRE(grid->getWidgetAlignment(0, 0) == tgui::Grid::Alignment::Center);
auto widget2 = tgui::ClickableWidget::create(); auto widget2 = tgui::ClickableWidget::create({20, 10});
grid->add(widget2); // Widget is added before calling addWidget here grid->add(widget2); // Widget is added before calling addWidget here
grid->addWidget(widget2, 0, 0, {1, 2, 3, 4}, tgui::Grid::Alignment::UpperLeft); grid->addWidget(widget2, 0, 0, {1, 2, 3, 4}, tgui::Grid::Alignment::UpperLeft);
@ -78,7 +78,7 @@ TEST_CASE("[Grid]")
SECTION("Borders") SECTION("Borders")
{ {
auto widget = tgui::ClickableWidget::create(); auto widget = tgui::ClickableWidget::create({40, 30});
grid->addWidget(widget, 3, 2, {1, 2, 3, 4}); grid->addWidget(widget, 3, 2, {1, 2, 3, 4});
REQUIRE(grid->getWidgetBorders(3, 2) == tgui::Borders(1, 2, 3, 4)); REQUIRE(grid->getWidgetBorders(3, 2) == tgui::Borders(1, 2, 3, 4));