add 'onselect' callback & remove 'default' tag
This commit is contained in:
parent
47f7259c72
commit
f17167f8c9
@ -26,8 +26,9 @@ SelectBox::SelectBox(
|
|||||||
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)
|
||||||
);
|
);
|
||||||
button->listenFocus([this, option](GUI&) {
|
button->listenFocus([this, option](GUI& gui) {
|
||||||
setSelected(option);
|
setSelected(option);
|
||||||
|
changeCallbacks.notify(gui, option.value);
|
||||||
});
|
});
|
||||||
panel->add(button);
|
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) {
|
void SelectBox::setSelected(const Option& selected) {
|
||||||
this->selected = selected;
|
this->selected = selected;
|
||||||
this->label->setText(selected.text);
|
this->label->setText(selected.text);
|
||||||
|
|||||||
@ -14,6 +14,7 @@ namespace gui {
|
|||||||
private:
|
private:
|
||||||
std::vector<Option> options;
|
std::vector<Option> options;
|
||||||
Option selected {};
|
Option selected {};
|
||||||
|
StringCallbacksSet changeCallbacks;
|
||||||
public:
|
public:
|
||||||
SelectBox(
|
SelectBox(
|
||||||
GUI& gui,
|
GUI& gui,
|
||||||
@ -23,6 +24,8 @@ namespace gui {
|
|||||||
const glm::vec4& padding
|
const glm::vec4& padding
|
||||||
);
|
);
|
||||||
|
|
||||||
|
void listenChange(onstringchange&& callback);
|
||||||
|
|
||||||
void setSelected(const Option& selected);
|
void setSelected(const Option& selected);
|
||||||
|
|
||||||
const Option& getSelected() const;
|
const Option& getSelected() const;
|
||||||
|
|||||||
@ -21,25 +21,33 @@ namespace gui {
|
|||||||
|
|
||||||
using onaction = std::function<void(GUI&)>;
|
using onaction = std::function<void(GUI&)>;
|
||||||
using onnumberchange = std::function<void(GUI&, double)>;
|
using onnumberchange = std::function<void(GUI&, double)>;
|
||||||
|
using onstringchange = std::function<void(GUI&, const std::string&)>;
|
||||||
|
|
||||||
class ActionsSet {
|
template<typename... Args>
|
||||||
std::unique_ptr<std::vector<onaction>> callbacks;
|
class CallbacksSet {
|
||||||
public:
|
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) {
|
if (callbacks == nullptr) {
|
||||||
callbacks = std::make_unique<std::vector<onaction>>();
|
callbacks = std::make_unique<std::vector<Func>>();
|
||||||
}
|
}
|
||||||
callbacks->push_back(callback);
|
callbacks->push_back(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void notify(GUI& gui) {
|
void notify(Args&&... args) {
|
||||||
if (callbacks) {
|
if (callbacks) {
|
||||||
for (auto& callback : *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 {
|
enum class Align {
|
||||||
left, center, right,
|
left, center, right,
|
||||||
|
|||||||
@ -439,19 +439,16 @@ static std::shared_ptr<UINode> read_select(
|
|||||||
SelectBox::Option 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();
|
continue;
|
||||||
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)};
|
|
||||||
}
|
}
|
||||||
|
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>(
|
auto selectBox = std::make_shared<SelectBox>(
|
||||||
@ -461,6 +458,17 @@ static std::shared_ptr<UINode> read_select(
|
|||||||
contentWidth,
|
contentWidth,
|
||||||
std::move(padding)
|
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);
|
read_panel_impl(reader, element, *selectBox, false);
|
||||||
return selectBox;
|
return selectBox;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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(
|
wstringsupplier scripting::create_wstring_supplier(
|
||||||
const scriptenv& env, const std::string& src, const std::string& file
|
const scriptenv& env, const std::string& src, const std::string& file
|
||||||
) {
|
) {
|
||||||
|
|||||||
@ -23,6 +23,12 @@ namespace scripting {
|
|||||||
const std::string& file = "[string]"
|
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(
|
wstringconsumer create_wstring_consumer(
|
||||||
const scriptenv& env,
|
const scriptenv& env,
|
||||||
const std::string& src,
|
const std::string& src,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user