add 'value' selectbox property

This commit is contained in:
MihailRis
2025-06-28 02:29:43 +03:00
parent 0042dbfb3d
commit 47f7259c72
4 changed files with 41 additions and 21 deletions
+13 -5
View File
@@ -11,18 +11,18 @@ using namespace gui;
SelectBox::SelectBox( SelectBox::SelectBox(
GUI& gui, GUI& gui,
std::vector<Element>&& elements, std::vector<Option>&& options,
Element selected, Option selected,
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(contentWidth, -1)),
elements(std::move(elements)) { options(std::move(options)) {
listenAction([this](GUI& gui) { listenAction([this](GUI& gui) {
auto panel = std::make_shared<Panel>(gui, getSize()); auto panel = std::make_shared<Panel>(gui, getSize());
panel->setPos(calcPos() + glm::vec2(0, size.y)); 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>( auto button = std::make_shared<Button>(
gui, option.text, glm::vec4(10.0f), nullptr, glm::vec2(-1.0f) 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->selected = selected;
this->label->setText(selected.text); 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&) { void SelectBox::drawBackground(const DrawContext& pctx, const Assets&) {
glm::vec2 pos = calcPos(); glm::vec2 pos = calcPos();
auto batch = pctx.getBatch2D(); auto batch = pctx.getBatch2D();
+10 -6
View File
@@ -7,23 +7,27 @@ namespace gui {
class SelectBox : public Button { class SelectBox : public Button {
public: public:
struct Element { struct Option {
std::string value; std::string value;
std::wstring text; std::wstring text;
}; };
private: private:
std::vector<Element> elements; std::vector<Option> options;
Element selected {}; Option selected {};
public: public:
SelectBox( SelectBox(
GUI& gui, GUI& gui,
std::vector<Element>&& elements, std::vector<Option>&& elements,
Element selected, Option selected,
int contentWidth, int contentWidth,
const glm::vec4& padding 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; void drawBackground(const DrawContext& pctx, const Assets&) override;
}; };
+4 -10
View File
@@ -435,17 +435,14 @@ static std::shared_ptr<UINode> read_select(
int contentWidth = element.attr("width", "100").asInt(); int contentWidth = element.attr("width", "100").asInt();
auto& elements = element.getElements(); auto& elements = element.getElements();
std::vector<SelectBox::Element> options; std::vector<SelectBox::Option> options;
SelectBox::Element selected; SelectBox::Option selected;
for (const auto& elem : elements) { for (const auto& elem : elements) {
const auto& tag = elem->getTag(); const auto& tag = elem->getTag();
if (tag == "option") { if (tag == "option") {
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());
SelectBox::Element option { SelectBox::Option option {std::move(value), std::move(text)};
std::move(value),
std::move(text)
};
if (elem->attr("selected", "false").asBool()) { if (elem->attr("selected", "false").asBool()) {
selected = option; selected = option;
} }
@@ -453,10 +450,7 @@ static std::shared_ptr<UINode> read_select(
} else if (tag == "default") { } else if (tag == "default") {
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());
selected = SelectBox::Element { selected = SelectBox::Option {std::move(value), std::move(text)};
std::move(value),
std::move(text)
};
} }
} }
+14
View File
@@ -8,6 +8,7 @@
#include "graphics/ui/elements/Canvas.hpp" #include "graphics/ui/elements/Canvas.hpp"
#include "graphics/ui/elements/CheckBox.hpp" #include "graphics/ui/elements/CheckBox.hpp"
#include "graphics/ui/elements/Image.hpp" #include "graphics/ui/elements/Image.hpp"
#include "graphics/ui/elements/SelectBox.hpp"
#include "graphics/ui/elements/InventoryView.hpp" #include "graphics/ui/elements/InventoryView.hpp"
#include "graphics/ui/elements/Menu.hpp" #include "graphics/ui/elements/Menu.hpp"
#include "graphics/ui/elements/Panel.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) { static int p_get_value(UINode* node, lua::State* L) {
if (auto bar = dynamic_cast<TrackBar*>(node)) { if (auto bar = dynamic_cast<TrackBar*>(node)) {
return lua::pushnumber(L, bar->getValue()); return lua::pushnumber(L, bar->getValue());
} else if (auto box = dynamic_cast<SelectBox*>(node)) {
return lua::pushstring(L, box->getSelected().value);
} }
return 0; 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) { static void p_set_value(UINode* node, lua::State* L, int idx) {
if (auto bar = dynamic_cast<TrackBar*>(node)) { if (auto bar = dynamic_cast<TrackBar*>(node)) {
bar->setValue(lua::tonumber(L, idx)); 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) { static void p_set_min(UINode* node, lua::State* L, int idx) {