add 'onselect' callback & remove 'default' tag

This commit is contained in:
MihailRis 2025-06-28 02:46:54 +03:00
parent 47f7259c72
commit f17167f8c9
6 changed files with 60 additions and 19 deletions

View File

@ -26,8 +26,9 @@ SelectBox::SelectBox(
auto button = std::make_shared<Button>(
gui, option.text, glm::vec4(10.0f), nullptr, glm::vec2(-1.0f)
);
button->listenFocus([this, option](GUI&) {
button->listenFocus([this, option](GUI& gui) {
setSelected(option);
changeCallbacks.notify(gui, option.value);
});
panel->add(button);
}
@ -40,6 +41,10 @@ SelectBox::SelectBox(
});
}
void SelectBox::listenChange(onstringchange&& callback) {
changeCallbacks.listen(std::move(callback));
}
void SelectBox::setSelected(const Option& selected) {
this->selected = selected;
this->label->setText(selected.text);

View File

@ -14,6 +14,7 @@ namespace gui {
private:
std::vector<Option> options;
Option selected {};
StringCallbacksSet changeCallbacks;
public:
SelectBox(
GUI& gui,
@ -23,6 +24,8 @@ namespace gui {
const glm::vec4& padding
);
void listenChange(onstringchange&& callback);
void setSelected(const Option& selected);
const Option& getSelected() const;

View File

@ -21,25 +21,33 @@ namespace gui {
using onaction = std::function<void(GUI&)>;
using onnumberchange = std::function<void(GUI&, double)>;
using onstringchange = std::function<void(GUI&, const std::string&)>;
class ActionsSet {
std::unique_ptr<std::vector<onaction>> callbacks;
template<typename... Args>
class CallbacksSet {
public:
void listen(const onaction& callback) {
using Func = std::function<void(Args...)>;
private:
std::unique_ptr<std::vector<Func>> callbacks;
public:
void listen(const Func& callback) {
if (callbacks == nullptr) {
callbacks = std::make_unique<std::vector<onaction>>();
callbacks = std::make_unique<std::vector<Func>>();
}
callbacks->push_back(callback);
}
void notify(GUI& gui) {
void notify(Args&&... args) {
if (callbacks) {
for (auto& callback : *callbacks) {
callback(gui);
callback(std::forward<Args>(args)...);
}
}
}
};
using ActionsSet = CallbacksSet<GUI&>;
using StringCallbacksSet = CallbacksSet<GUI&, const std::string&>;
enum class Align {
left, center, right,

View File

@ -439,19 +439,16 @@ static std::shared_ptr<UINode> read_select(
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::Option option {std::move(value), std::move(text)};
if (elem->attr("selected", "false").asBool()) {
selected = option;
}
options.push_back(std::move(option));
} else if (tag == "default") {
auto value = elem->attr("value").getText();
auto text = parse_inner_text(*elem, reader.getContext());
selected = SelectBox::Option {std::move(value), std::move(text)};
if (tag != "option") {
continue;
}
auto value = elem->attr("value").getText();
auto text = parse_inner_text(*elem, reader.getContext());
SelectBox::Option option {std::move(value), std::move(text)};
if (elem->attr("selected", "false").asBool()) {
selected = option;
}
options.push_back(std::move(option));
}
auto selectBox = std::make_shared<SelectBox>(
@ -461,6 +458,17 @@ static std::shared_ptr<UINode> read_select(
contentWidth,
std::move(padding)
);
if (element.has("onselect")) {
auto callback = scripting::create_string_consumer(
reader.getEnvironment(),
element.attr("onselect").getText(),
reader.getFilename()
);
selectBox->listenChange(
[callback=std::move(callback)](GUI&, const std::string& value) {
callback(value);
});
}
read_panel_impl(reader, element, *selectBox, false);
return selectBox;
}

View File

@ -69,6 +69,17 @@ wstringconsumer scripting::create_wstring_consumer(
};
}
stringconsumer scripting::create_string_consumer(
const scriptenv& env, const std::string& src, const std::string& file
) {
return [=](const std::string& x) {
if (auto L = process_callback(env, src, file)) {
lua::pushstring(L, x);
lua::call_nothrow(L, 1);
}
};
}
wstringsupplier scripting::create_wstring_supplier(
const scriptenv& env, const std::string& src, const std::string& file
) {

View File

@ -23,6 +23,12 @@ namespace scripting {
const std::string& file = "[string]"
);
stringconsumer create_string_consumer(
const scriptenv& env,
const std::string& src,
const std::string& file = "[string]"
);
wstringconsumer create_wstring_consumer(
const scriptenv& env,
const std::string& src,