diff --git a/changelog.md b/changelog.md index b8f389319..dced03aec 100644 --- a/changelog.md +++ b/changelog.md @@ -5,7 +5,8 @@ TGUI 1.4 (TBD) - Added MaxValue getter to Scrollbar - Added ScrollbarMaxValue getters to widgets with a scrollbar - Added getPixelsPerPoint() to BackendRenderTarget -- Replace VerticalScroll with Orientation in Slider, Scrollbar and SpinButton +- Inner size of ScrollablePanel now depends on shown scrollbars +- Replaced VerticalScroll with Orientation in Slider, Scrollbar and SpinButton - Multiple fixes to EditBoxSlider widget diff --git a/include/TGUI/Widgets/ScrollablePanel.hpp b/include/TGUI/Widgets/ScrollablePanel.hpp index 909071f6b..859260fba 100644 --- a/include/TGUI/Widgets/ScrollablePanel.hpp +++ b/include/TGUI/Widgets/ScrollablePanel.hpp @@ -431,6 +431,9 @@ TGUI_MODULE_EXPORT namespace tgui unsigned int m_verticalScrollAmount = 0; unsigned int m_horizontalScrollAmount = 0; + bool m_verticalScrollbarWasVisibleOnSizeUpdate = false; + bool m_horizontalScrollbarWasVisibleOnSizeUpdate = false; + std::unordered_map m_connectedPositionCallbacks; std::unordered_map m_connectedSizeCallbacks; diff --git a/src/Widgets/ScrollablePanel.cpp b/src/Widgets/ScrollablePanel.cpp index cc12a7432..2e7278082 100644 --- a/src/Widgets/ScrollablePanel.cpp +++ b/src/Widgets/ScrollablePanel.cpp @@ -63,6 +63,10 @@ namespace tgui m_horizontalScrollbar {other.m_horizontalScrollbar}, m_verticalScrollbarPolicy {other.m_verticalScrollbarPolicy}, m_horizontalScrollbarPolicy {other.m_horizontalScrollbarPolicy}, + m_verticalScrollAmount {other.m_verticalScrollAmount}, + m_horizontalScrollAmount {other.m_horizontalScrollAmount}, + m_verticalScrollbarWasVisibleOnSizeUpdate{other.m_verticalScrollbarWasVisibleOnSizeUpdate}, + m_horizontalScrollbarWasVisibleOnSizeUpdate{other.m_horizontalScrollbarWasVisibleOnSizeUpdate}, m_connectedPositionCallbacks{}, m_connectedSizeCallbacks {} { @@ -83,6 +87,10 @@ namespace tgui m_horizontalScrollbar {std::move(other.m_horizontalScrollbar)}, m_verticalScrollbarPolicy {std::move(other.m_verticalScrollbarPolicy)}, m_horizontalScrollbarPolicy {std::move(other.m_horizontalScrollbarPolicy)}, + m_verticalScrollAmount {std::move(other.m_verticalScrollAmount)}, + m_horizontalScrollAmount {std::move(other.m_horizontalScrollAmount)}, + m_verticalScrollbarWasVisibleOnSizeUpdate{std::move(other.m_verticalScrollbarWasVisibleOnSizeUpdate)}, + m_horizontalScrollbarWasVisibleOnSizeUpdate{std::move(other.m_horizontalScrollbarWasVisibleOnSizeUpdate)}, m_connectedPositionCallbacks{std::move(other.m_connectedPositionCallbacks)}, m_connectedSizeCallbacks {std::move(other.m_connectedSizeCallbacks)} { @@ -108,6 +116,10 @@ namespace tgui m_horizontalScrollbar = other.m_horizontalScrollbar; m_verticalScrollbarPolicy = other.m_verticalScrollbarPolicy; m_horizontalScrollbarPolicy = other.m_horizontalScrollbarPolicy; + m_verticalScrollAmount = other.m_verticalScrollAmount; + m_horizontalScrollAmount = other.m_horizontalScrollAmount; + m_verticalScrollbarWasVisibleOnSizeUpdate = other.m_verticalScrollbarWasVisibleOnSizeUpdate; + m_horizontalScrollbarWasVisibleOnSizeUpdate = other.m_horizontalScrollbarWasVisibleOnSizeUpdate; disconnectAllChildWidgets(); @@ -133,6 +145,10 @@ namespace tgui m_horizontalScrollbar = std::move(other.m_horizontalScrollbar); m_verticalScrollbarPolicy = std::move(other.m_verticalScrollbarPolicy); m_horizontalScrollbarPolicy = std::move(other.m_horizontalScrollbarPolicy); + m_verticalScrollAmount = std::move(other.m_verticalScrollAmount); + m_horizontalScrollAmount = std::move(other.m_horizontalScrollAmount); + m_verticalScrollbarWasVisibleOnSizeUpdate = other.m_verticalScrollbarWasVisibleOnSizeUpdate; + m_horizontalScrollbarWasVisibleOnSizeUpdate = other.m_horizontalScrollbarWasVisibleOnSizeUpdate; Panel::operator=(std::move(other)); disconnectAllChildWidgets(); @@ -192,6 +208,9 @@ namespace tgui void ScrollablePanel::setSize(const Layout2d& size) { + m_horizontalScrollbarWasVisibleOnSizeUpdate = m_horizontalScrollbar->isShown(); + m_verticalScrollbarWasVisibleOnSizeUpdate = m_verticalScrollbar->isShown(); + Panel::setSize(size); updateScrollbars(); } @@ -302,9 +321,9 @@ namespace tgui Vector2f ScrollablePanel::getInnerSize() const { Vector2f size = Panel::getInnerSize(); - if (m_verticalScrollbarPolicy == Scrollbar::Policy::Always) + if (m_verticalScrollbar->isShown()) size.x -= std::min(size.x, getScrollbarWidth()); - if (m_horizontalScrollbarPolicy == Scrollbar::Policy::Always) + if (m_horizontalScrollbar->isShown()) size.y -= std::min(size.y, getScrollbarWidth()); return size; } @@ -872,6 +891,13 @@ namespace tgui if (m_horizontalScrollAmount == 0) setHorizontalScrollAmount(0); + + if ((m_horizontalScrollbarWasVisibleOnSizeUpdate != m_horizontalScrollbar->isShown()) || (m_verticalScrollbarWasVisibleOnSizeUpdate != m_verticalScrollbar->isShown())) + { + m_horizontalScrollbarWasVisibleOnSizeUpdate = m_horizontalScrollbar->isShown(); + m_verticalScrollbarWasVisibleOnSizeUpdate = m_verticalScrollbar->isShown(); + recalculateBoundSizeLayouts(); + } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/tests/Widgets/ScrollablePanel.cpp b/tests/Widgets/ScrollablePanel.cpp index 3050b5733..8b175428f 100644 --- a/tests/Widgets/ScrollablePanel.cpp +++ b/tests/Widgets/ScrollablePanel.cpp @@ -201,6 +201,36 @@ TEST_CASE("[ScrollablePanel]") REQUIRE(panel->getContentSize() == tgui::Vector2f{200, 100}); } + SECTION("Inner size") + { + panel->setSize(202, 102); + panel->getRenderer()->setBorders({1}); + + auto widget1 = tgui::Button::create(); + widget1->setPosition({0, 0}); + widget1->setSize({"100%", "100%"}); + panel->add(widget1); + + auto widget2 = tgui::Button::create(); + widget2->setPosition({20, 30}); + widget2->setSize({50, 50}); + panel->add(widget2); + + REQUIRE(widget1->getSize() == tgui::Vector2f{200, 100}); + + widget2->setPosition({180, 50 - panel->getScrollbarWidth()}); + REQUIRE(widget1->getSize() == tgui::Vector2f{200, 100 - panel->getScrollbarWidth()}); + + widget2->setPosition({40, 90}); + REQUIRE(widget1->getSize() == tgui::Vector2f{200 - panel->getScrollbarWidth(), 100}); + + widget2->setPosition({250, 120}); + REQUIRE(widget1->getSize() == tgui::Vector2f{200 - panel->getScrollbarWidth(), 100 - panel->getScrollbarWidth()}); + + panel->remove(widget2); + REQUIRE(widget1->getSize() == tgui::Vector2f{200, 100}); + } + SECTION("Events / Signals") { unsigned int mousePressedCount = 0;