Change ui::key_interceptor to generic ui::event_interceptor
This commit is contained in:
parent
5be37d2b93
commit
5d116ad79a
4 changed files with 132 additions and 90 deletions
44
libs/ui/include/psemek/ui/event_interceptor.hpp
Normal file
44
libs/ui/include/psemek/ui/event_interceptor.hpp
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/ui/element.hpp>
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace psemek::ui
|
||||
{
|
||||
|
||||
struct event_interceptor
|
||||
: element
|
||||
{
|
||||
children_range children() const override { return children_; }
|
||||
|
||||
virtual std::shared_ptr<element> set_child(std::shared_ptr<element> c);
|
||||
|
||||
geom::box<float, 2> size_constraints() const override;
|
||||
|
||||
struct shape const & shape() const override;
|
||||
void reshape(geom::box<float, 2> const & bbox) override;
|
||||
|
||||
void on_mouse_move(std::function<bool(mouse_move const &)> callback);
|
||||
void on_mouse_click(std::function<bool(mouse_click const &)> callback);
|
||||
void on_mouse_wheel(std::function<bool(mouse_wheel const &)> callback);
|
||||
void on_key_press(std::function<bool(key_press const &)> callback);
|
||||
|
||||
bool on_event(mouse_move const & event) override;
|
||||
bool on_event(mouse_click const & event) override;
|
||||
bool on_event(mouse_wheel const & event) override;
|
||||
bool on_event(key_press const & event) override;
|
||||
|
||||
void draw(painter &) const override {}
|
||||
|
||||
private:
|
||||
std::shared_ptr<element> child_;
|
||||
element * children_[1]{nullptr};
|
||||
|
||||
std::function<bool(mouse_move const &)> mouse_move_callback_;
|
||||
std::function<bool(mouse_click const &)> mouse_click_callback_;
|
||||
std::function<bool(mouse_wheel const &)> mouse_wheel_callback_;
|
||||
std::function<bool(key_press const &)> key_press_callback_;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/ui/element.hpp>
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace psemek::ui
|
||||
{
|
||||
|
||||
struct key_interceptor
|
||||
: element
|
||||
{
|
||||
children_range children() const override { return children_; }
|
||||
|
||||
virtual std::shared_ptr<element> set_child(std::shared_ptr<element> c);
|
||||
|
||||
geom::box<float, 2> size_constraints() const override;
|
||||
|
||||
struct shape const & shape() const override;
|
||||
void reshape(geom::box<float, 2> const & bbox) override;
|
||||
|
||||
void on_key_down(std::function<bool(SDL_Keycode)> callback);
|
||||
|
||||
bool on_event(key_press const & event) override;
|
||||
|
||||
void draw(painter &) const override {}
|
||||
|
||||
private:
|
||||
std::shared_ptr<element> child_;
|
||||
element * children_[1]{nullptr};
|
||||
|
||||
std::function<bool(SDL_Keycode)> callback_;
|
||||
};
|
||||
|
||||
}
|
||||
88
libs/ui/source/event_interceptor.cpp
Normal file
88
libs/ui/source/event_interceptor.cpp
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
#include <psemek/ui/event_interceptor.hpp>
|
||||
#include <psemek/ui/null_shape.hpp>
|
||||
|
||||
namespace psemek::ui
|
||||
{
|
||||
|
||||
std::shared_ptr<element> event_interceptor::set_child(std::shared_ptr<element> c)
|
||||
{
|
||||
auto old = std::move(child_);
|
||||
if (old) old->set_parent(nullptr);
|
||||
|
||||
child_ = std::move(c);
|
||||
if (child_) child_->set_parent(this);
|
||||
children_[0] = child_.get();
|
||||
|
||||
return old;
|
||||
}
|
||||
|
||||
geom::box<float, 2> event_interceptor::size_constraints() const
|
||||
{
|
||||
if (child_)
|
||||
return child_->size_constraints();
|
||||
return element::size_constraints();
|
||||
}
|
||||
|
||||
shape const & event_interceptor::shape() const
|
||||
{
|
||||
static const null_shape fallback_shape;
|
||||
if (child_)
|
||||
return child_->shape();
|
||||
return fallback_shape;
|
||||
}
|
||||
|
||||
void event_interceptor::reshape(geom::box<float, 2> const & bbox)
|
||||
{
|
||||
if (child_)
|
||||
child_->reshape(bbox);
|
||||
}
|
||||
|
||||
void event_interceptor::on_mouse_move(std::function<bool(mouse_move const &)> callback)
|
||||
{
|
||||
mouse_move_callback_ = std::move(callback);
|
||||
}
|
||||
|
||||
void event_interceptor::on_mouse_click(std::function<bool(mouse_click const &)> callback)
|
||||
{
|
||||
mouse_click_callback_ = std::move(callback);
|
||||
}
|
||||
|
||||
void event_interceptor::on_mouse_wheel(std::function<bool(mouse_wheel const &)> callback)
|
||||
{
|
||||
mouse_wheel_callback_ = std::move(callback);
|
||||
}
|
||||
|
||||
void event_interceptor::on_key_press(std::function<bool(key_press const &)> callback)
|
||||
{
|
||||
key_press_callback_ = std::move(callback);
|
||||
}
|
||||
|
||||
bool event_interceptor::on_event(mouse_move const & event)
|
||||
{
|
||||
if (mouse_move_callback_)
|
||||
return mouse_move_callback_(event);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool event_interceptor::on_event(mouse_click const & event)
|
||||
{
|
||||
if (mouse_click_callback_)
|
||||
return mouse_click_callback_(event);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool event_interceptor::on_event(mouse_wheel const & event)
|
||||
{
|
||||
if (mouse_wheel_callback_)
|
||||
return mouse_wheel_callback_(event);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool event_interceptor::on_event(key_press const & event)
|
||||
{
|
||||
if (key_press_callback_)
|
||||
return key_press_callback_(event);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
#include <psemek/ui/key_interceptor.hpp>
|
||||
#include <psemek/ui/null_shape.hpp>
|
||||
|
||||
namespace psemek::ui
|
||||
{
|
||||
|
||||
std::shared_ptr<element> key_interceptor::set_child(std::shared_ptr<element> c)
|
||||
{
|
||||
auto old = std::move(child_);
|
||||
if (old) old->set_parent(nullptr);
|
||||
|
||||
child_ = std::move(c);
|
||||
if (child_) child_->set_parent(this);
|
||||
children_[0] = child_.get();
|
||||
|
||||
return old;
|
||||
}
|
||||
|
||||
geom::box<float, 2> key_interceptor::size_constraints() const
|
||||
{
|
||||
if (child_)
|
||||
return child_->size_constraints();
|
||||
return element::size_constraints();
|
||||
}
|
||||
|
||||
shape const & key_interceptor::shape() const
|
||||
{
|
||||
static const null_shape fallback_shape;
|
||||
if (child_)
|
||||
return child_->shape();
|
||||
return fallback_shape;
|
||||
}
|
||||
|
||||
void key_interceptor::reshape(geom::box<float, 2> const & bbox)
|
||||
{
|
||||
if (child_)
|
||||
child_->reshape(bbox);
|
||||
}
|
||||
|
||||
void key_interceptor::on_key_down(std::function<bool(SDL_Keycode)> callback)
|
||||
{
|
||||
callback_ = std::move(callback);
|
||||
}
|
||||
|
||||
bool key_interceptor::on_event(key_press const & event)
|
||||
{
|
||||
if (callback_ && event.down)
|
||||
{
|
||||
return callback_(event.key);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue