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