psemek/libs/ui/source/event_interceptor.cpp

93 lines
2.1 KiB
C++

#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 (child_)
mouseover_ = child_->shape().contains(geom::cast<float>(event.position));
else
mouseover_ = false;
if (mouseover_ && mouse_move_callback_)
return mouse_move_callback_(event);
return false;
}
bool event_interceptor::on_event(mouse_click const & event)
{
if (mouseover_ && mouse_click_callback_)
return mouse_click_callback_(event);
return false;
}
bool event_interceptor::on_event(mouse_wheel const & event)
{
if (mouseover_ && 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;
}
}