psemek/libs/ui/source/button.cpp

74 lines
1.2 KiB
C++

#include <psemek/ui/button.hpp>
namespace psemek::ui
{
element::children_range button::children() const
{
if (!label_)
return {};
return children_range{children_};
}
bool button::on_event(mouse_move const & e)
{
bool const over = shape().contains(geom::cast<float>(e.position));
switch (state_) {
case state_t::normal:
if (over)
{
state_ = state_t::mouseover;
on_state_changed();
}
break;
case state_t::mouseover:
case state_t::mousedown:
if (!over)
{
state_ = state_t::normal;
on_state_changed();
}
break;
}
return false;
}
bool button::on_event(mouse_click const & e)
{
if (e.button != mouse_button::left) return false;
switch (state_) {
case state_t::normal:
break;
case state_t::mouseover:
if (e.down)
{
state_ = state_t::mousedown;
if (callback_)
root()->loop()->post(callback_);
on_state_changed();
return true;
}
break;
case state_t::mousedown:
if (!e.down)
{
state_ = state_t::mouseover;
on_state_changed();
return true;
}
break;
}
return false;
}
void button::set_label(std::unique_ptr<struct label> label)
{
label_ = std::move(label);
children_[0] = label_.get();
}
}