feat: selectbox options context menu

This commit is contained in:
MihailRis
2025-06-27 22:50:47 +03:00
parent 3571d0c902
commit e648703f7c
4 changed files with 59 additions and 0 deletions
+44
View File
@@ -1,6 +1,11 @@
#include "SelectBox.hpp"
#include "Label.hpp"
#include "assets/Assets.hpp"
#include "graphics/ui/GUI.hpp"
#include "graphics/ui/elements/Panel.hpp"
#include "graphics/core/Batch2D.hpp"
#include "graphics/core/DrawContext.hpp"
using namespace gui;
@@ -13,4 +18,43 @@ SelectBox::SelectBox(
)
: Button(gui, selected.text, padding, nullptr, glm::vec2(contentWidth, -1)),
elements(std::move(elements)) {
listenAction([this](GUI& gui) {
auto panel = std::make_shared<Panel>(gui, getSize());
panel->setPos(calcPos() + glm::vec2(0, size.y));
for (const auto& option : this->elements) {
auto button = std::make_shared<Button>(
gui, option.text, glm::vec4(10.0f), nullptr, glm::vec2(-1.0f)
);
button->listenFocus([this, option](GUI&) {
setSelected(option);
});
panel->add(button);
}
panel->setZIndex(999);
gui.setFocus(panel);
panel->listenDefocus([panel=panel.get()](GUI& gui) {
gui.remove(panel);
});
gui.add(panel);
});
}
void SelectBox::setSelected(const Element& selected) {
this->selected = selected;
this->label->setText(selected.text);
}
void SelectBox::drawBackground(const DrawContext& pctx, const Assets&) {
glm::vec2 pos = calcPos();
auto batch = pctx.getBatch2D();
batch->texture(nullptr);
batch->setColor(calcColor());
batch->rect(pos.x, pos.y, size.x, size.y);
batch->setColor({1.0f, 1.0f, 1.0f, 0.333f});
batch->triangle(
pos.x + size.x - 32, pos.y + size.y / 2.0f - 4,
pos.x + size.x - 32 + 16, pos.y + size.y / 2.0f - 4,
pos.x + size.x - 32 + 8, pos.y + size.y / 2.0f + 4
);
}