add 'onselect' callback & remove 'default' tag
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user