psemek/libs/ui/source/key_interceptor.cpp
2022-02-19 13:00:39 +03:00

55 lines
1.1 KiB
C++

#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;
}
}