Make ui::selector callback retrievable so that ui::spawn can replace it

This commit is contained in:
Nikita Lisitsa 2022-04-30 20:42:45 +03:00
parent 1cdfcc5aa5
commit 26ca210ecf
2 changed files with 20 additions and 10 deletions

View file

@ -33,9 +33,13 @@ namespace psemek::ui
virtual geom::interval<float> y_range(std::size_t index) const; virtual geom::interval<float> y_range(std::size_t index) const;
using callback_type = std::function<void(std::size_t)>;
virtual std::optional<std::size_t> selected() const { return selected_; } virtual std::optional<std::size_t> selected() const { return selected_; }
virtual void on_selected(std::function<void(std::size_t)> callback); virtual void on_selected(callback_type callback);
virtual void on_submenu(std::function<void(std::size_t)> callback); virtual void on_submenu(callback_type callback);
virtual callback_type on_selected() const;
protected: protected:
@ -54,10 +58,10 @@ namespace psemek::ui
std::optional<std::size_t> selected_; std::optional<std::size_t> selected_;
std::function<void(std::size_t)> callback_; callback_type callback_;
std::function<void(std::size_t)> submenu_callback_; callback_type submenu_callback_;
}; };
bool spawn(element * root, std::shared_ptr<selector> selector, geom::point<float, 2> const & position, std::function<void(std::size_t)> on_selected); bool spawn(element * root, std::shared_ptr<selector> selector, geom::point<float, 2> const & position);
} }

View file

@ -161,17 +161,22 @@ namespace psemek::ui
return child_boxes_[index][1]; return child_boxes_[index][1];
} }
void selector::on_selected(std::function<void(std::size_t)> callback) void selector::on_selected(callback_type callback)
{ {
callback_ = std::move(callback); callback_ = std::move(callback);
} }
void selector::on_submenu(std::function<void(std::size_t)> callback) void selector::on_submenu(callback_type callback)
{ {
submenu_callback_ = std::move(callback); submenu_callback_ = std::move(callback);
} }
bool spawn(element * root, std::shared_ptr<selector> selector, geom::point<float, 2> const & position, std::function<void(std::size_t)> on_selected) selector::callback_type selector::on_selected() const
{
return callback_;
}
bool spawn(element * root, std::shared_ptr<selector> selector, geom::point<float, 2> const & position)
{ {
ui::screen * screen = find_last_parent_of_type<struct screen>(root); ui::screen * screen = find_last_parent_of_type<struct screen>(root);
if (!screen) if (!screen)
@ -191,9 +196,10 @@ namespace psemek::ui
p->remove_child(event_interceptor); p->remove_child(event_interceptor);
}; };
selector->on_selected([close, cb = std::move(on_selected)](std::size_t index){ selector->on_selected([close, cb = selector->on_selected()](std::size_t index){
close(); close();
cb(index); if (cb)
cb(index);
}); });
event_interceptor->on_mouse_click([close](ui::mouse_click const & e) -> bool { event_interceptor->on_mouse_click([close](ui::mouse_click const & e) -> bool {