add 'options' selectbox property
This commit is contained in:
parent
2aff1a39eb
commit
ba13a4b61c
@ -58,6 +58,10 @@ const std::vector<SelectBox::Option>& SelectBox::getOptions() const {
|
||||
return options;
|
||||
}
|
||||
|
||||
void SelectBox::setOptions(std::vector<Option>&& options) {
|
||||
this->options = std::move(options);
|
||||
}
|
||||
|
||||
void SelectBox::drawBackground(const DrawContext& pctx, const Assets&) {
|
||||
glm::vec2 pos = calcPos();
|
||||
auto batch = pctx.getBatch2D();
|
||||
|
||||
@ -32,6 +32,8 @@ namespace gui {
|
||||
|
||||
const std::vector<Option>& getOptions() const;
|
||||
|
||||
void setOptions(std::vector<Option>&& options);
|
||||
|
||||
void drawBackground(const DrawContext& pctx, const Assets&) override;
|
||||
};
|
||||
}
|
||||
|
||||
@ -512,6 +512,28 @@ static int p_get_scroll(UINode* node, lua::State* L) {
|
||||
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) {
|
||||
if (!lua::isstring(L, 1)) {
|
||||
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},
|
||||
{"parent", p_get_parent},
|
||||
{"region", p_get_region},
|
||||
{"options", p_get_options},
|
||||
};
|
||||
auto func = getters.find(attr);
|
||||
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));
|
||||
}
|
||||
}
|
||||
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) {
|
||||
if (auto bar = dynamic_cast<TrackBar*>(node)) {
|
||||
bar->setValue(lua::tonumber(L, idx));
|
||||
@ -856,6 +904,7 @@ static int l_gui_setattr(lua::State* L) {
|
||||
{"cursor", p_set_cursor},
|
||||
{"focused", p_set_focused},
|
||||
{"region", p_set_region},
|
||||
{"options", p_set_options},
|
||||
};
|
||||
auto func = setters.find(attr);
|
||||
if (func != setters.end()) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user