Skip to content

Commit

Permalink
Engine: for touch-to-mouse implement a relative cursor motion
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-mogilko committed Sep 12, 2022
1 parent f9a5ee8 commit 6f9c569
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions Engine/ac/sys_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,19 @@ static void send_mouse_motion_event(int x, int y, int xrel, int yrel)
SDL_PushEvent(&evt);
}

static Point mouse_pos_from_finger(const SDL_TouchFingerEvent &event,
const Size &logical_sz, const Point &old_pos)
{
// Handle relative movement
if (event.dx != 0.f || event.dy != 0.f)
{
return Point(old_pos.X + event.dx * logical_sz.Width,
old_pos.Y + event.dy * logical_sz.Height);
}
// Handle absolute movement
return Point(event.x * logical_sz.Width, event.y * logical_sz.Height);
}

static void detect_double_tap(const SDL_TouchFingerEvent &event, bool down)
{
auto tap_ts = AGS_Clock::now();
Expand Down Expand Up @@ -602,10 +615,10 @@ void on_sdl_touch_down(const SDL_TouchFingerEvent &event)
// otherwise, ignore the movement for now
if ((!touch_mouse_ignore_motion) && (mouse_but == SDL_BUTTON_LEFT))
{
touch_mouse_pos = Point(event.x * w, event.y * h);
touch_mouse_pos = mouse_pos_from_finger(event, Size(w, h), touch_mouse_pos);
touch_mouse_start_pos = touch_mouse_pos;
touch_mouse_is_dragging = false;
send_mouse_motion_event(touch_mouse_pos.X, touch_mouse_pos.Y, 0, 0 /* TODO? */);
send_mouse_motion_event(touch_mouse_pos.X, touch_mouse_pos.Y, event.dx * w, event.dy * h);
}
// If more than one finger was down, lock the cursor motion,
// and force any following emulated clicks to RMB,
Expand Down Expand Up @@ -698,8 +711,8 @@ void on_sdl_touch_motion(const SDL_TouchFingerEvent &event)
int mouse_but = tfinger_to_mouse_but(event.fingerId);
if ((!touch_mouse_ignore_motion) && (mouse_but == SDL_BUTTON_LEFT))
{
touch_mouse_pos = Point(event.x * w, event.y * h);
send_mouse_motion_event(touch_mouse_pos.X, touch_mouse_pos.Y, 0, 0 /* TODO? */);
touch_mouse_pos = mouse_pos_from_finger(event, Size(w, h), touch_mouse_pos);
send_mouse_motion_event(touch_mouse_pos.X, touch_mouse_pos.Y, event.dx * w, event.dy * h);
Point trigger_dist(w * touch_drag_trigger_dist, h * touch_drag_trigger_dist);
if (DistanceBetween(touch_mouse_pos, touch_mouse_start_pos) >= touch_drag_trigger_dist)
{
Expand Down

0 comments on commit 6f9c569

Please sign in to comment.