feat: selectbox options context menu

This commit is contained in:
MihailRis 2025-06-26 22:32:45 +03:00
parent 3571d0c902
commit e648703f7c
4 changed files with 59 additions and 0 deletions

View File

@ -333,6 +333,15 @@ void Batch2D::rect(
vertex(v1, glm::vec2(0, 0), r2,g2,b2,1.0f);
}
void Batch2D::triangle(float x1, float y1, float x2, float y2, float x3, float y3) {
if (index + 3 >= capacity) {
flush();
}
vertex({x1, y1}, {x1, y1}, color.r, color.g, color.b, color.a);
vertex({x2, y2}, {x2, y2}, color.r, color.g, color.b, color.a);
vertex({x3, y3}, {x3, y3}, color.r, color.g, color.b, color.a);
}
void Batch2D::sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint){
rect(x, y, w, h, region.u1, region.v1, region.u2-region.u1, region.v2-region.v1, tint.r, tint.g, tint.b, tint.a);
}

View File

@ -117,6 +117,8 @@ public:
float r4, float g4, float b4, int sh
);
void triangle(float x1, float y1, float x2, float y2, float x3, float y3);
void flush() override;
void lineWidth(float width);

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
);
}

View File

@ -22,5 +22,9 @@ namespace gui {
int contentWidth,
const glm::vec4& padding
);
void setSelected(const Element& selected);
void drawBackground(const DrawContext& pctx, const Assets&) override;
};
}