mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
add selectbox 'button' mode
This commit is contained in:
parent
2a079789cc
commit
3f68ed5fe4
3 changed files with 44 additions and 8 deletions
|
|
@ -13,14 +13,17 @@ SelectBox::SelectBox(
|
|||
GUI& gui,
|
||||
std::vector<Option>&& options,
|
||||
Option selected,
|
||||
Mode mode,
|
||||
int contentWidth,
|
||||
const glm::vec4& padding
|
||||
)
|
||||
: Button(gui, selected.text, padding, nullptr, glm::vec2(contentWidth, -1)),
|
||||
options(std::move(options)) {
|
||||
|
||||
listenAction(UIAction::CLICK, [this](GUI& gui) {
|
||||
auto panel = std::make_shared<Panel>(gui, getSize());
|
||||
: Button(gui, selected.text, padding, nullptr, glm::vec2(-1, -1)),
|
||||
options(std::move(options)), mode(mode) {
|
||||
listenAction(UIAction::CLICK, [this, contentWidth](GUI& gui) {
|
||||
auto panel = std::make_shared<Panel>(
|
||||
gui, glm::vec2 {contentWidth, contentWidth}, glm::vec4 {2.0f}, 0.0f
|
||||
);
|
||||
panel->setColor({});
|
||||
panel->setPadding(glm::vec4(0, size.y, 0, 0));
|
||||
panel->setPos(calcPos() + glm::vec2(0, 0));
|
||||
for (const auto& option : this->options) {
|
||||
|
|
@ -51,7 +54,9 @@ void SelectBox::listenChange(OnStringChange&& callback) {
|
|||
|
||||
void SelectBox::setSelected(const Option& selected) {
|
||||
this->selected = selected;
|
||||
this->label->setText(selected.text);
|
||||
if (mode == Mode::SELECT) {
|
||||
this->label->setText(selected.text);
|
||||
}
|
||||
}
|
||||
|
||||
const SelectBox::Option& SelectBox::getSelected() const {
|
||||
|
|
@ -66,6 +71,14 @@ void SelectBox::setOptions(std::vector<Option>&& options) {
|
|||
this->options = std::move(options);
|
||||
}
|
||||
|
||||
SelectBox::Mode SelectBox::getMode() const {
|
||||
return mode;
|
||||
}
|
||||
|
||||
void SelectBox::setMode(SelectBox::Mode mode) {
|
||||
this->mode = mode;
|
||||
}
|
||||
|
||||
void SelectBox::drawBackground(const DrawContext& pctx, const Assets&) {
|
||||
glm::vec2 pos = calcPos();
|
||||
auto batch = pctx.getBatch2D();
|
||||
|
|
@ -73,6 +86,10 @@ void SelectBox::drawBackground(const DrawContext& pctx, const Assets&) {
|
|||
batch->setColor(calcColor());
|
||||
batch->rect(pos.x, pos.y, size.x, size.y);
|
||||
batch->setColor({1.0f, 1.0f, 1.0f, 0.333f});
|
||||
|
||||
if (mode != Mode::SELECT) {
|
||||
return;
|
||||
}
|
||||
|
||||
int paddingRight = padding.w;
|
||||
int widthHalf = 8;
|
||||
|
|
|
|||
|
|
@ -11,15 +11,21 @@ namespace gui {
|
|||
std::string value;
|
||||
std::wstring text;
|
||||
};
|
||||
enum class Mode {
|
||||
SELECT,
|
||||
BUTTON,
|
||||
};
|
||||
private:
|
||||
std::vector<Option> options;
|
||||
Option selected {};
|
||||
StringCallbacksSet changeCallbacks;
|
||||
Mode mode;
|
||||
public:
|
||||
SelectBox(
|
||||
GUI& gui,
|
||||
std::vector<Option>&& elements,
|
||||
Option selected,
|
||||
Mode mode,
|
||||
int contentWidth,
|
||||
const glm::vec4& padding
|
||||
);
|
||||
|
|
@ -34,8 +40,13 @@ namespace gui {
|
|||
|
||||
void setOptions(std::vector<Option>&& options);
|
||||
|
||||
Mode getMode() const;
|
||||
|
||||
void setMode(Mode mode);
|
||||
|
||||
void drawBackground(const DrawContext& pctx, const Assets&) override;
|
||||
|
||||
|
||||
std::shared_ptr<Label> getLabel() const;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -458,8 +458,7 @@ static std::shared_ptr<UINode> read_select(
|
|||
}
|
||||
auto value = elem->attr("value").getText();
|
||||
auto text = parse_inner_text(*elem, reader.getContext());
|
||||
options.push_back(SelectBox::Option {std::move(value), std::move(text)}
|
||||
);
|
||||
options.push_back(SelectBox::Option {std::move(value), std::move(text)});
|
||||
}
|
||||
|
||||
if (element.has("selected")) {
|
||||
|
|
@ -481,10 +480,19 @@ static std::shared_ptr<UINode> read_select(
|
|||
selected.text = innerText;
|
||||
}
|
||||
|
||||
SelectBox::Mode mode = SelectBox::Mode::SELECT;
|
||||
if (element.has("mode")) {
|
||||
auto modeName = element.attr("mode").getText();
|
||||
if (modeName == "button") {
|
||||
mode = SelectBox::Mode::BUTTON;
|
||||
}
|
||||
}
|
||||
|
||||
auto selectBox = std::make_shared<SelectBox>(
|
||||
gui,
|
||||
std::move(options),
|
||||
std::move(selected),
|
||||
mode,
|
||||
contentWidth,
|
||||
std::move(padding)
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue