format Container.cpp and Panel.cpp

This commit is contained in:
MihailRis 2026-08-07 22:28:48 +03:00
parent 863cc923a0
commit 660bd0a5d3
2 changed files with 58 additions and 39 deletions

View file

@ -1,9 +1,9 @@
#include "Container.hpp" #include "Container.hpp"
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/Batch2D.hpp"
#include "window/Window.hpp"
#include "../GUI.hpp" #include "../GUI.hpp"
#include "graphics/core/Batch2D.hpp"
#include "graphics/core/DrawContext.hpp"
#include "window/Window.hpp"
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
@ -26,15 +26,15 @@ std::shared_ptr<UINode> Container::getAt(const glm::vec2& pos) {
if (!isInside(pos)) { if (!isInside(pos)) {
return nullptr; return nullptr;
} }
int diff = (actualLengthY-size.y); int diff = (actualLengthY - size.y);
if (scrollable && diff > 0 && pos.x > calcPos().x + getSize().x - scrollBarWidth) { if (scrollable && diff > 0 &&
pos.x > calcPos().x + getSize().x - scrollBarWidth) {
return UINode::getAt(pos); return UINode::getAt(pos);
} }
for (int i = nodes.size()-1; i >= 0; i--) { for (int i = nodes.size() - 1; i >= 0; i--) {
auto& node = nodes[i]; auto& node = nodes[i];
if (!node->isVisible()) if (!node->isVisible()) continue;
continue;
auto hover = node->getAt(pos); auto hover = node->getAt(pos);
if (hover != nullptr) { if (hover != nullptr) {
return hover; return hover;
@ -45,7 +45,7 @@ std::shared_ptr<UINode> Container::getAt(const glm::vec2& pos) {
void Container::click(int x, int y) { void Container::click(int x, int y) {
UINode::click(x, y); UINode::click(x, y);
if (scrollable) { if (scrollable) {
auto pos = calcPos(); auto pos = calcPos();
x -= pos.x; x -= pos.x;
@ -65,9 +65,10 @@ void Container::mouseMove(int x, int y) {
auto pos = calcPos(); auto pos = calcPos();
x -= pos.x; x -= pos.x;
y -= pos.y; y -= pos.y;
int diff = (actualLengthY-size.y); int diff = (actualLengthY - size.y);
if (diff > 0) { if (diff > 0) {
scrollY -= (y - prevScrollY) / static_cast<float>(size.y) * actualLengthY; scrollY -=
(y - prevScrollY) / static_cast<float>(size.y) * actualLengthY;
scrollY = -glm::min( scrollY = -glm::min(
glm::max(static_cast<float>(-scrollY), 0.0f), actualLengthY - size.y glm::max(static_cast<float>(-scrollY), 0.0f), actualLengthY - size.y
); );
@ -101,26 +102,28 @@ void Container::act(float delta) {
} }
} }
} }
intervalEvents.erase(std::remove_if( intervalEvents.erase(
intervalEvents.begin(), intervalEvents.end(), std::remove_if(
[](const IntervalEvent& event) { intervalEvents.begin(),
return event.repeat == 0; intervalEvents.end(),
} [](const IntervalEvent& event) { return event.repeat == 0; }
), intervalEvents.end()); ),
intervalEvents.end()
);
} }
void Container::scrolled(int value) { void Container::scrolled(int value) {
auto size = getSize(); auto size = getSize();
int diff = (actualLengthY > size.y ? actualLengthY - size.y : actualLengthX - size.x); bool isVertical = actualLengthY > size.y;
int& scroll = (actualLengthY > size.y ? scrollY : scrollX); int diff = (isVertical ? actualLengthY - size.y : actualLengthX - size.x);
int& scroll = (isVertical ? scrollY : scrollX);
if (scroll < 0 && diff <= 0) { if (scroll < 0 && diff <= 0) {
scroll = 0; scroll = 0;
} }
if (diff > 0 && scrollable) { if (diff > 0 && scrollable) {
scroll += value * scrollStep; scroll += value * scrollStep;
if (scroll > 0) if (scroll > 0) scroll = 0;
scroll = 0;
if (-scroll > diff) { if (-scroll > diff) {
scroll = -diff; scroll = -diff;
} }
@ -143,21 +146,25 @@ void Container::draw(const DrawContext& pctx, const Assets& assets) {
if (!nodes.empty()) { if (!nodes.empty()) {
batch->flush(); batch->flush();
DrawContext ctx = pctx.sub(); DrawContext ctx = pctx.sub();
ctx.setScissors(glm::vec4(pos.x, pos.y, glm::ceil(size.x), glm::ceil(size.y))); ctx.setScissors(
glm::vec4(pos.x, pos.y, glm::ceil(size.x), glm::ceil(size.y))
);
for (const auto& node : nodes) { for (const auto& node : nodes) {
if (node->isVisible()) if (node->isVisible()) node->draw(pctx, assets);
node->draw(pctx, assets);
} }
int diff = (actualLengthY-size.y); int diff = (actualLengthY - size.y);
if (scrollable && diff > 0) { if (scrollable && diff > 0) {
int h = glm::max(size.y / actualLengthY * size.y, scrollBarWidth / 2.0f); int h = glm::max(
size.y / actualLengthY * size.y, scrollBarWidth / 2.0f
);
batch->untexture(); batch->untexture();
batch->setColor(glm::vec4(1, 1, 1, 0.3f)); batch->setColor(glm::vec4(1, 1, 1, 0.3f));
batch->rect( batch->rect(
pos.x + size.x - scrollBarWidth, pos.x + size.x - scrollBarWidth,
pos.y - scrollY / static_cast<float>(diff) * (size.y - h), pos.y - scrollY / static_cast<float>(diff) * (size.y - h),
scrollBarWidth, h scrollBarWidth,
h
); );
} }
batch->flush(); batch->flush();
@ -166,8 +173,7 @@ void Container::draw(const DrawContext& pctx, const Assets& assets) {
void Container::drawBackground(const DrawContext& pctx, const Assets&) { void Container::drawBackground(const DrawContext& pctx, const Assets&) {
glm::vec4 color = calcColor(); glm::vec4 color = calcColor();
if (color.a <= 0.001f) if (color.a <= 0.001f) return;
return;
glm::vec2 pos = calcPos(); glm::vec2 pos = calcPos();
auto batch = pctx.getBatch2D(); auto batch = pctx.getBatch2D();
@ -197,11 +203,16 @@ void Container::add(const std::shared_ptr<UINode>& node, glm::vec2 pos) {
void Container::remove(UINode* selected) { void Container::remove(UINode* selected) {
selected->setParent(nullptr); selected->setParent(nullptr);
nodes.erase(std::remove_if(nodes.begin(), nodes.end(), nodes.erase(
[selected](const std::shared_ptr<UINode>& node) { std::remove_if(
return node.get() == selected; nodes.begin(),
} nodes.end(),
), nodes.end()); [selected](const std::shared_ptr<UINode>& node) {
return node.get() == selected;
}
),
nodes.end()
);
refresh(); refresh();
} }
@ -245,9 +256,13 @@ void Container::setScrollStep(int step) {
} }
void Container::refresh() { void Container::refresh() {
std::stable_sort(nodes.begin(), nodes.end(), [](const auto& a, const auto& b) { std::stable_sort(
return a->getZIndex() < b->getZIndex(); nodes.begin(),
}); nodes.end(),
[](const auto& a, const auto& b) {
return a->getZIndex() < b->getZIndex();
}
);
} }
void Container::setScroll(int scroll) { void Container::setScroll(int scroll) {

View file

@ -41,10 +41,14 @@ glm::vec2 Panel::getContentSize() const {
void Panel::cropToContent() { void Panel::cropToContent() {
if (maxLength > 0.0f) { if (maxLength > 0.0f) {
setSize(glm::vec2( setSize(glm::vec2(
glm::max(minLength, glm::min(maxLength, actualLengthX)), glm::max(minLength, glm::min(maxLength, actualLengthY)) glm::max(minLength, glm::min(maxLength, actualLengthX)),
glm::max(minLength, glm::min(maxLength, actualLengthY))
)); ));
} else { } else {
setSize(glm::vec2(glm::max(minLength, actualLengthX), glm::max(minLength, actualLengthY))); setSize(glm::vec2(
glm::max(minLength, actualLengthX),
glm::max(minLength, actualLengthY)
));
} }
} }