add 'options' selectbox property

This commit is contained in:
MihailRis 2025-06-28 14:01:22 +03:00
parent 2aff1a39eb
commit ba13a4b61c
3 changed files with 55 additions and 0 deletions

View File

@ -58,6 +58,10 @@ const std::vector<SelectBox::Option>& SelectBox::getOptions() const {
return options; return options;
} }
void SelectBox::setOptions(std::vector<Option>&& options) {
this->options = std::move(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();

View File

@ -32,6 +32,8 @@ namespace gui {
const std::vector<Option>& getOptions() const; const std::vector<Option>& getOptions() const;
void setOptions(std::vector<Option>&& options);
void drawBackground(const DrawContext& pctx, const Assets&) override; void drawBackground(const DrawContext& pctx, const Assets&) override;
}; };
} }

View File

@ -512,6 +512,28 @@ static int p_get_scroll(UINode* node, lua::State* L) {
return 0; return 0;
} }
static int p_get_options(UINode* node, lua::State* L) {
if (auto selectbox = dynamic_cast<SelectBox*>(node)) {
const auto& options = selectbox->getOptions();
size_t size = options.size();
lua::createtable(L, size, 0);
for (size_t i = 0; i < size; i++) {
const auto& option = options[i];
lua::createtable(L, 0, 2);
lua::pushstring(L, option.value);
lua::setfield(L, "value");
lua::pushwstring(L, option.text);
lua::setfield(L, "text");
lua::rawseti(L, i + 1);
}
return 1;
}
return 0;
}
static int l_gui_getattr(lua::State* L) { static int l_gui_getattr(lua::State* L) {
if (!lua::isstring(L, 1)) { if (!lua::isstring(L, 1)) {
throw std::runtime_error("document name is not a string"); throw std::runtime_error("document name is not a string");
@ -598,6 +620,7 @@ static int l_gui_getattr(lua::State* L) {
{"data", p_get_data}, {"data", p_get_data},
{"parent", p_get_parent}, {"parent", p_get_parent},
{"region", p_get_region}, {"region", p_get_region},
{"options", p_get_options},
}; };
auto func = getters.find(attr); auto func = getters.find(attr);
if (func != getters.end()) { if (func != getters.end()) {
@ -709,6 +732,31 @@ static void p_set_region(UINode* node, lua::State* L, int idx) {
image->setRegion(UVRegion(vec.x, vec.y, vec.z, vec.w)); image->setRegion(UVRegion(vec.x, vec.y, vec.z, vec.w));
} }
} }
static void p_set_options(UINode* node, lua::State* L, int idx) {
if (auto selectbox = dynamic_cast<SelectBox*>(node)) {
if (!lua::istable(L, idx)) {
throw std::runtime_error("options table expected");
}
std::vector<SelectBox::Option> options;
size_t size = lua::objlen(L, idx);
for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1, idx);
SelectBox::Option option;
lua::getfield(L, "value");
option.value = lua::require_string(L, -1);
lua::pop(L);
lua::getfield(L, "text");
option.text = lua::require_wstring(L, -1);
lua::pop(L, 2);
options.push_back(std::move(option));
}
selectbox->setOptions(std::move(options));
}
}
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));
@ -856,6 +904,7 @@ static int l_gui_setattr(lua::State* L) {
{"cursor", p_set_cursor}, {"cursor", p_set_cursor},
{"focused", p_set_focused}, {"focused", p_set_focused},
{"region", p_set_region}, {"region", p_set_region},
{"options", p_set_options},
}; };
auto func = setters.find(attr); auto func = setters.find(attr);
if (func != setters.end()) { if (func != setters.end()) {