bwidgets/src/w/feat/mouse_handler.cpp
2021-08-08 14:30:56 +02:00

60 lines
1.4 KiB
C++

#include <SDL2/SDL_events.h>
#include <SDL2/SDL_rect.h>
#include <basic_widgets/core/math.hpp>
#include <basic_widgets/w/feat/mouse_handler.hpp>
using namespace bwidgets;
auto MouseHandler::handle_mouse(const SDL_MouseButtonEvent& ev, const SDL_Rect& orig)
-> MouseHandler*
{
if (_click_area == nullptr) return this;
const SDL_Point p {ev.x, ev.y};
const SDL_Rect vp {rect_offset(*_click_area, orig)};
if (ev.type == SDL_MOUSEBUTTONDOWN) {
if (SDL_PointInRect(&p, &vp) == SDL_TRUE) {
push(true);
_handle_mouse_button(ev, vp);
}
else focus(false);
}
else {
if (_is_pushed) {
if (SDL_PointInRect(&p, &vp) == SDL_TRUE) {
focus(true);
if (click_handler != nullptr) click_handler(ev);
}
push(false);
_handle_mouse_button(ev, vp);
}
}
return this;
}
auto MouseHandler::handle_mouse(const SDL_MouseMotionEvent& ev, const SDL_Rect& orig)
-> MouseHandler*
{
if (_click_area == nullptr) return this;
const SDL_Point p {ev.x, ev.y};
const SDL_Rect vp {rect_offset(*_click_area, orig)};
_is_hovered = SDL_PointInRect(&p, &vp) != 0;
if (_is_hovered) _handle_mouse_motion(ev, orig);
return this;
}
auto MouseHandler::push(bool state) -> MouseHandler*
{
_on_push(state);
_is_pushed = state;
return this;
}