2021-07-10 19:55:53 +02:00
|
|
|
#include <SDL2/SDL_events.h>
|
2021-07-29 16:06:03 +02:00
|
|
|
#include <SDL2/SDL_rect.h>
|
2021-07-10 19:55:53 +02:00
|
|
|
|
2021-07-18 18:26:12 +02:00
|
|
|
#include <basic_widgets/core/math.hpp>
|
2021-07-29 16:06:03 +02:00
|
|
|
#include <basic_widgets/w/feat/mouse_handler.hpp>
|
2021-07-10 19:55:53 +02:00
|
|
|
|
|
|
|
using namespace bwidgets;
|
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
auto MouseHandler::handle_mouse(const SDL_MouseButtonEvent& ev, const SDL_Rect& orig)
|
|
|
|
-> MouseHandler*
|
2021-07-10 19:55:53 +02:00
|
|
|
{
|
2021-08-01 11:12:54 +02:00
|
|
|
if (_click_area == nullptr) return this;
|
2021-07-10 19:55:53 +02:00
|
|
|
|
2021-08-08 14:30:56 +02:00
|
|
|
const SDL_Point p {ev.x, ev.y};
|
|
|
|
const SDL_Rect vp {rect_offset(*_click_area, orig)};
|
2021-07-12 00:58:59 +02:00
|
|
|
|
2021-07-29 16:06:03 +02:00
|
|
|
if (ev.type == SDL_MOUSEBUTTONDOWN) {
|
2021-07-30 20:41:47 +02:00
|
|
|
if (SDL_PointInRect(&p, &vp) == SDL_TRUE) {
|
2021-08-01 11:12:54 +02:00
|
|
|
push(true);
|
2021-07-10 19:55:53 +02:00
|
|
|
_handle_mouse_button(ev, vp);
|
|
|
|
}
|
2021-07-29 16:06:03 +02:00
|
|
|
else focus(false);
|
2021-07-10 19:55:53 +02:00
|
|
|
}
|
2021-07-29 16:06:03 +02:00
|
|
|
else {
|
|
|
|
if (_is_pushed) {
|
2021-07-30 20:41:47 +02:00
|
|
|
if (SDL_PointInRect(&p, &vp) == SDL_TRUE) {
|
2021-07-10 19:55:53 +02:00
|
|
|
focus(true);
|
2021-07-11 17:06:18 +02:00
|
|
|
|
2021-07-29 16:06:03 +02:00
|
|
|
if (click_handler != nullptr) click_handler(ev);
|
2021-07-10 19:55:53 +02:00
|
|
|
}
|
2021-08-01 11:12:54 +02:00
|
|
|
push(false);
|
2021-07-10 19:55:53 +02:00
|
|
|
_handle_mouse_button(ev, vp);
|
|
|
|
}
|
|
|
|
}
|
2021-08-01 11:12:54 +02:00
|
|
|
|
|
|
|
return this;
|
2021-07-10 19:55:53 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
auto MouseHandler::handle_mouse(const SDL_MouseMotionEvent& ev, const SDL_Rect& orig)
|
|
|
|
-> MouseHandler*
|
2021-07-10 19:55:53 +02:00
|
|
|
{
|
2021-08-01 11:12:54 +02:00
|
|
|
if (_click_area == nullptr) return this;
|
2021-07-10 19:55:53 +02:00
|
|
|
|
2021-08-08 14:30:56 +02:00
|
|
|
const SDL_Point p {ev.x, ev.y};
|
|
|
|
const SDL_Rect vp {rect_offset(*_click_area, orig)};
|
2021-07-12 00:58:59 +02:00
|
|
|
|
2021-07-30 20:41:47 +02:00
|
|
|
_is_hovered = SDL_PointInRect(&p, &vp) != 0;
|
2021-07-10 19:55:53 +02:00
|
|
|
|
2021-07-29 16:06:03 +02:00
|
|
|
if (_is_hovered) _handle_mouse_motion(ev, orig);
|
2021-08-01 11:12:54 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
auto MouseHandler::push(bool state) -> MouseHandler*
|
2021-08-01 11:12:54 +02:00
|
|
|
{
|
|
|
|
_on_push(state);
|
|
|
|
_is_pushed = state;
|
|
|
|
|
|
|
|
return this;
|
2021-07-10 19:55:53 +02:00
|
|
|
}
|