diff --git a/src/graphics/ui/elements/Container.cpp b/src/graphics/ui/elements/Container.cpp index c869e9b3b..954c9879d 100644 --- a/src/graphics/ui/elements/Container.cpp +++ b/src/graphics/ui/elements/Container.cpp @@ -11,7 +11,7 @@ using namespace gui; Container::Container(GUI& gui, glm::vec2 size) : UINode(gui, size) { - actualLength = size.y; + actualLengthY = size.y; setColor(glm::vec4()); } @@ -26,7 +26,7 @@ std::shared_ptr Container::getAt(const glm::vec2& pos) { if (!isInside(pos)) { return nullptr; } - int diff = (actualLength-size.y); + int diff = (actualLengthY-size.y); if (scrollable && diff > 0 && pos.x > calcPos().x + getSize().x - scrollBarWidth) { return UINode::getAt(pos); } @@ -65,11 +65,11 @@ void Container::mouseMove(int x, int y) { auto pos = calcPos(); x -= pos.x; y -= pos.y; - int diff = (actualLength-size.y); + int diff = (actualLengthY-size.y); if (diff > 0) { - scroll -= (y - prevScrollY) / static_cast(size.y) * actualLength; - scroll = -glm::min( - glm::max(static_cast(-scroll), 0.0f), actualLength - size.y + scrollY -= (y - prevScrollY) / static_cast(size.y) * actualLengthY; + scrollY = -glm::min( + glm::max(static_cast(-scrollY), 0.0f), actualLengthY - size.y ); } prevScrollY = y; @@ -110,7 +110,10 @@ void Container::act(float delta) { } void Container::scrolled(int value) { - int diff = (actualLength-getSize().y); + auto size = getSize(); + int diff = (actualLengthY > size.y ? actualLengthY - size.y : actualLengthX - size.x); + int& scroll = (actualLengthY > size.y ? scrollY : scrollX); + if (scroll < 0 && diff <= 0) { scroll = 0; } @@ -146,14 +149,14 @@ void Container::draw(const DrawContext& pctx, const Assets& assets) { node->draw(pctx, assets); } - int diff = (actualLength-size.y); + int diff = (actualLengthY-size.y); if (scrollable && diff > 0) { - int h = glm::max(size.y / actualLength * size.y, scrollBarWidth / 2.0f); + int h = glm::max(size.y / actualLengthY * size.y, scrollBarWidth / 2.0f); batch->untexture(); batch->setColor(glm::vec4(1, 1, 1, 0.3f)); batch->rect( pos.x + size.x - scrollBarWidth, - pos.y - scroll / static_cast(diff) * (size.y - h), + pos.y - scrollY / static_cast(diff) * (size.y - h), scrollBarWidth, h ); } @@ -248,7 +251,7 @@ void Container::refresh() { } void Container::setScroll(int scroll) { - this->scroll = scroll; + this->scrollY = scroll; } const std::vector>& Container::getNodes() const { diff --git a/src/graphics/ui/elements/Container.hpp b/src/graphics/ui/elements/Container.hpp index 3ede2c2a1..36c366010 100644 --- a/src/graphics/ui/elements/Container.hpp +++ b/src/graphics/ui/elements/Container.hpp @@ -13,10 +13,12 @@ namespace gui { protected: std::vector> nodes; std::vector intervalEvents; - int scroll = 0; + int scrollY = 0; + int scrollX = 0; int scrollStep = 40; int scrollBarWidth = 10; - int actualLength = 0; + int actualLengthY = 0; + int actualLengthX = 0; bool scrollable = true; bool isScrolling() const { @@ -40,7 +42,7 @@ namespace gui { virtual void setScrollable(bool flag); void listenInterval(float interval, OnTimeOut callback, int repeat=-1); virtual glm::vec2 getContentOffset() const override { - return glm::vec2(0.0f, scroll); + return glm::vec2(scrollX, scrollY); }; virtual void setSize(const glm::vec2& size) override; virtual int getScrollStep() const; diff --git a/src/graphics/ui/elements/Panel.cpp b/src/graphics/ui/elements/Panel.cpp index 87796a50c..3b452e892 100644 --- a/src/graphics/ui/elements/Panel.cpp +++ b/src/graphics/ui/elements/Panel.cpp @@ -41,10 +41,10 @@ glm::vec2 Panel::getContentSize() const { void Panel::cropToContent() { if (maxLength > 0.0f) { setSize(glm::vec2( - getSize().x, glm::max(minLength, glm::min(maxLength, actualLength)) + glm::max(minLength, glm::min(maxLength, actualLengthX)), glm::max(minLength, glm::min(maxLength, actualLengthY)) )); } else { - setSize(glm::vec2(getSize().x, glm::max(minLength, actualLength))); + setSize(glm::vec2(glm::max(minLength, actualLengthX), glm::max(minLength, actualLengthY))); } } @@ -93,7 +93,8 @@ void Panel::refresh() { y += nodeSize.y + margin.w + interval; maxw = fmax(maxw, ex + nodeSize.x + margin.z + padding.z); } - actualLength = y + padding.w; + actualLengthX = size.x; + actualLengthY = y + padding.w; } else { float maxh = size.y; for (auto& node : nodes) { @@ -115,6 +116,7 @@ void Panel::refresh() { x += nodesize.x + margin.z + interval; maxh = fmax(maxh, ey + nodesize.y + margin.w + padding.w); } - actualLength = size.y; + actualLengthY = size.y; + actualLengthX = x + padding.z; } } diff --git a/src/graphics/ui/elements/TextBox.cpp b/src/graphics/ui/elements/TextBox.cpp index f37673fc1..7bd7cd646 100644 --- a/src/graphics/ui/elements/TextBox.cpp +++ b/src/graphics/ui/elements/TextBox.cpp @@ -452,7 +452,7 @@ void TextBox::refreshLabel() { uint height = label->getLinesNumber() * rawTextCache.metrics.lineHeight * label->getLineInterval(); label->setSize(glm::vec2(label->getSize().x, height)); - actualLength = height; + actualLengthY = height; } else { setScrollable(false); }