bwidgets/src/w/feat/mouse_handler_impl.cpp

51 lines
2.3 KiB
C++

#include <basic_widgets/core/math.hpp>
#include <basic_widgets/w/feat/mouse_handler_impl.hpp>
using namespace bwidgets;
void MouseHandlerImpl::disable_mouse_handler()
{
EventHandlerImpl::_remove_event_handler(SDL_MOUSEBUTTONDOWN);
EventHandlerImpl::_remove_event_handler(SDL_MOUSEBUTTONUP);
_remove_event_handler(SDL_MOUSEMOTION);
}
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
void MouseHandlerImpl::enable_mouse_handler(const SDL_Rect* area, const SDL_Rect* vp)
{
if (!area || !vp) throw std::logic_error("ptr parameters cannot be null.");
_add_event_handler({SDL_MOUSEBUTTONDOWN, [this, area, vp](const SDL_Event& ev) {
auto absolute_area = rect_offset(*area, *vp);
SDL_Point p = {ev.button.x, ev.button.y};
if (SDL_PointInRect(&p, &absolute_area) == SDL_FALSE) {
FocusHandlerImpl::focus(false);
return;
}
_on_push(true);
_pushed = true;
}});
_add_event_handler({SDL_MOUSEBUTTONUP, [this, area, vp](const SDL_Event& ev) {
auto absolute_area = rect_offset(*area, *vp);
SDL_Point p = {ev.button.x, ev.button.y};
if (_pushed) {
_on_push(false);
_pushed = false;
}
if (SDL_PointInRect(&p, &absolute_area) == SDL_FALSE) return;
FocusHandlerImpl::focus(true);
_click_handler(ev.button);
}});
_add_event_handler({SDL_MOUSEMOTION, [this, area, vp](const SDL_Event& ev) {
auto absolute_area = rect_offset(*area, *vp);
SDL_Point p = {ev.motion.x, ev.motion.y};
if (SDL_PointInRect(&p, &absolute_area) == SDL_FALSE) {
_hovered = false;
return;
}
_hovered = true;
_motion_handler(ev.motion);
}});
}