add 'value' selectbox property
This commit is contained in:
parent
0042dbfb3d
commit
47f7259c72
@ -11,18 +11,18 @@ using namespace gui;
|
||||
|
||||
SelectBox::SelectBox(
|
||||
GUI& gui,
|
||||
std::vector<Element>&& elements,
|
||||
Element selected,
|
||||
std::vector<Option>&& options,
|
||||
Option selected,
|
||||
int contentWidth,
|
||||
const glm::vec4& padding
|
||||
)
|
||||
: Button(gui, selected.text, padding, nullptr, glm::vec2(contentWidth, -1)),
|
||||
elements(std::move(elements)) {
|
||||
options(std::move(options)) {
|
||||
|
||||
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) {
|
||||
for (const auto& option : this->options) {
|
||||
auto button = std::make_shared<Button>(
|
||||
gui, option.text, glm::vec4(10.0f), nullptr, glm::vec2(-1.0f)
|
||||
);
|
||||
@ -40,11 +40,19 @@ SelectBox::SelectBox(
|
||||
});
|
||||
}
|
||||
|
||||
void SelectBox::setSelected(const Element& selected) {
|
||||
void SelectBox::setSelected(const Option& selected) {
|
||||
this->selected = selected;
|
||||
this->label->setText(selected.text);
|
||||
}
|
||||
|
||||
const SelectBox::Option& SelectBox::getSelected() const {
|
||||
return selected;
|
||||
}
|
||||
|
||||
const std::vector<SelectBox::Option>& SelectBox::getOptions() const {
|
||||
return options;
|
||||
}
|
||||
|
||||
void SelectBox::drawBackground(const DrawContext& pctx, const Assets&) {
|
||||
glm::vec2 pos = calcPos();
|
||||
auto batch = pctx.getBatch2D();
|
||||
|
||||
@ -7,23 +7,27 @@ namespace gui {
|
||||
|
||||
class SelectBox : public Button {
|
||||
public:
|
||||
struct Element {
|
||||
struct Option {
|
||||
std::string value;
|
||||
std::wstring text;
|
||||
};
|
||||
private:
|
||||
std::vector<Element> elements;
|
||||
Element selected {};
|
||||
std::vector<Option> options;
|
||||
Option selected {};
|
||||
public:
|
||||
SelectBox(
|
||||
GUI& gui,
|
||||
std::vector<Element>&& elements,
|
||||
Element selected,
|
||||
std::vector<Option>&& elements,
|
||||
Option selected,
|
||||
int contentWidth,
|
||||
const glm::vec4& padding
|
||||
);
|
||||
|
||||
void setSelected(const Element& selected);
|
||||
void setSelected(const Option& selected);
|
||||
|
||||
const Option& getSelected() const;
|
||||
|
||||
const std::vector<Option>& getOptions() const;
|
||||
|
||||
void drawBackground(const DrawContext& pctx, const Assets&) override;
|
||||
};
|
||||
|
||||
@ -435,17 +435,14 @@ static std::shared_ptr<UINode> read_select(
|
||||
int contentWidth = element.attr("width", "100").asInt();
|
||||
|
||||
auto& elements = element.getElements();
|
||||
std::vector<SelectBox::Element> options;
|
||||
SelectBox::Element selected;
|
||||
std::vector<SelectBox::Option> options;
|
||||
SelectBox::Option selected;
|
||||
for (const auto& elem : elements) {
|
||||
const auto& tag = elem->getTag();
|
||||
if (tag == "option") {
|
||||
auto value = elem->attr("value").getText();
|
||||
auto text = parse_inner_text(*elem, reader.getContext());
|
||||
SelectBox::Element option {
|
||||
std::move(value),
|
||||
std::move(text)
|
||||
};
|
||||
SelectBox::Option option {std::move(value), std::move(text)};
|
||||
if (elem->attr("selected", "false").asBool()) {
|
||||
selected = option;
|
||||
}
|
||||
@ -453,10 +450,7 @@ static std::shared_ptr<UINode> read_select(
|
||||
} else if (tag == "default") {
|
||||
auto value = elem->attr("value").getText();
|
||||
auto text = parse_inner_text(*elem, reader.getContext());
|
||||
selected = SelectBox::Element {
|
||||
std::move(value),
|
||||
std::move(text)
|
||||
};
|
||||
selected = SelectBox::Option {std::move(value), std::move(text)};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
#include "graphics/ui/elements/Canvas.hpp"
|
||||
#include "graphics/ui/elements/CheckBox.hpp"
|
||||
#include "graphics/ui/elements/Image.hpp"
|
||||
#include "graphics/ui/elements/SelectBox.hpp"
|
||||
#include "graphics/ui/elements/InventoryView.hpp"
|
||||
#include "graphics/ui/elements/Menu.hpp"
|
||||
#include "graphics/ui/elements/Panel.hpp"
|
||||
@ -225,6 +226,8 @@ static int p_is_checked(UINode* node, lua::State* L) {
|
||||
static int p_get_value(UINode* node, lua::State* L) {
|
||||
if (auto bar = dynamic_cast<TrackBar*>(node)) {
|
||||
return lua::pushnumber(L, bar->getValue());
|
||||
} else if (auto box = dynamic_cast<SelectBox*>(node)) {
|
||||
return lua::pushstring(L, box->getSelected().value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -709,6 +712,17 @@ static void p_set_region(UINode* node, lua::State* L, int idx) {
|
||||
static void p_set_value(UINode* node, lua::State* L, int idx) {
|
||||
if (auto bar = dynamic_cast<TrackBar*>(node)) {
|
||||
bar->setValue(lua::tonumber(L, idx));
|
||||
} else if (auto selectbox = dynamic_cast<SelectBox*>(node)) {
|
||||
auto value = lua::require_lstring(L, idx);
|
||||
const auto& options = selectbox->getOptions();
|
||||
for (const auto& option : options) {
|
||||
if (option.value == value) {
|
||||
selectbox->setSelected(option);
|
||||
return;
|
||||
}
|
||||
}
|
||||
selectbox->setSelected(SelectBox::Option {
|
||||
std::string(value), util::str2wstr_utf8(value)});
|
||||
}
|
||||
}
|
||||
static void p_set_min(UINode* node, lua::State* L, int idx) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user