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 14, 2022
1 parent f9a5ee8 commit 1f203dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
30 changes: 19 additions & 11 deletions Engine/ac/sys_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "ac/sys_events.h"
#include <chrono>
#include <deque>
#include <math.h>
#include <SDL.h>
#include "core/platform.h"
#include "ac/common.h"
Expand Down Expand Up @@ -495,6 +496,7 @@ static int fingers_down = 0;
// For touch-to-mouse emulation:
static Point touch_mouse_pos;
static Point touch_mouse_start_pos;
static Point touch_mouse_prev_pos;
const float touch_drag_trigger_dist = 0.05f; // relative to screen size
static bool touch_mouse_ignore_motion = false;
static int touch_mouse_force_button = 0;
Expand Down Expand Up @@ -576,7 +578,7 @@ void on_sdl_touch_down(const SDL_TouchFingerEvent &event)
int mouse_but = tfinger_to_mouse_but(event.fingerId);
if (mouse_but == SDL_BUTTON_LEFT)
{
send_mouse_button_event(SDL_MOUSEBUTTONDOWN, mouse_but, event.x * w, event.y * h);
send_mouse_button_event(SDL_MOUSEBUTTONDOWN, mouse_but, std::roundf(event.x * w), std::roundf(event.y * h));
}
break;
}
Expand All @@ -602,10 +604,12 @@ 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_prev_pos = touch_mouse_pos;
touch_mouse_pos = Point(std::roundf(event.x * w), std::roundf(event.y * h));
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,
touch_mouse_prev_pos.X - touch_mouse_pos.X, touch_mouse_prev_pos.Y - touch_mouse_pos.Y);
}
// If more than one finger was down, lock the cursor motion,
// and force any following emulated clicks to RMB,
Expand Down Expand Up @@ -639,7 +643,7 @@ void on_sdl_touch_up(const SDL_TouchFingerEvent &event)
int mouse_but = tfinger_to_mouse_but(event.fingerId);
if (mouse_but == SDL_BUTTON_LEFT)
{
send_mouse_button_event(SDL_MOUSEBUTTONUP, mouse_but, event.x * w, event.y * h);
send_mouse_button_event(SDL_MOUSEBUTTONUP, mouse_but, std::roundf(event.x * w), std::roundf(event.y * h));
touch_mouse_is_dragging = false;
}
break;
Expand Down Expand Up @@ -698,9 +702,11 @@ 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? */);
Point trigger_dist(w * touch_drag_trigger_dist, h * touch_drag_trigger_dist);
touch_mouse_prev_pos = touch_mouse_pos;
touch_mouse_pos = Point(std::roundf(event.x * w), std::roundf(event.y * h));
send_mouse_motion_event(touch_mouse_pos.X, touch_mouse_pos.Y,
touch_mouse_prev_pos.X - touch_mouse_pos.X, touch_mouse_prev_pos.Y - touch_mouse_pos.Y);
Point trigger_dist(std::roundf(w * touch_drag_trigger_dist), std::roundf(h * touch_drag_trigger_dist));
if (DistanceBetween(touch_mouse_pos, touch_mouse_start_pos) >= touch_drag_trigger_dist)
{
touch_mouse_is_dragging = true;
Expand Down Expand Up @@ -820,7 +826,8 @@ void sys_evt_process_one(const SDL_Event &event) {
break;
// MOUSE INPUT
case SDL_MOUSEMOTION:
Debug::Printf("SDL_MOUSEMOTION: event.motion.which: %d (%d, %d)", event.motion.which, event.motion.x, event.motion.y);
Debug::Printf("SDL_MOUSEMOTION: event.motion.which: %d (%d, %d) - rel(%d, %d)",
event.motion.which, event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel);
on_sdl_mouse_motion(event.motion);
break;
case SDL_MOUSEBUTTONDOWN:
Expand All @@ -835,15 +842,16 @@ void sys_evt_process_one(const SDL_Event &event) {
break;
// TOUCH INPUT
case SDL_FINGERDOWN:
Debug::Printf("SDL_FINGERDOWN: tfinger: %d at %.3f, %.3f", event.tfinger.fingerId, event.tfinger.x, event.tfinger.y);
Debug::Printf("SDL_FINGERDOWN: tfinger: %lld at %.3f, %.3f", event.tfinger.fingerId, event.tfinger.x, event.tfinger.y);
on_sdl_touch_down(event.tfinger);
break;
case SDL_FINGERUP:
Debug::Printf("SDL_FINGERUP: tfinger: %d at %.3f, %.3f", event.tfinger.fingerId, event.tfinger.x, event.tfinger.y);
Debug::Printf("SDL_FINGERUP: tfinger: %lld at %.3f, %.3f", event.tfinger.fingerId, event.tfinger.x, event.tfinger.y);
on_sdl_touch_up(event.tfinger);
break;
case SDL_FINGERMOTION:
Debug::Printf("SDL_FINGERMOTION: tfinger: %d at %.3f, %.3f", event.tfinger.fingerId, event.tfinger.x, event.tfinger.y);
Debug::Printf("SDL_FINGERMOTION: tfinger: %lld at %.3f, %.3f; dxy: %.3f, %.3f",
event.tfinger.fingerId, event.tfinger.x, event.tfinger.y, event.tfinger.dx, event.tfinger.dy);
on_sdl_touch_motion(event.tfinger);
break;
default: break;
Expand Down
7 changes: 4 additions & 3 deletions Engine/device/mousew32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ void mgetgraphpos()
{
// TODO: review and possibly rewrite whole thing;
// research what disable_mgetgraphpos does, and is this still necessary?
// disable or update mouse speed control to sdl
// (does sdl support mouse cursor speed? is it even necessary anymore?);

// TODO: [sonneveld] find out where mgetgraphpos is needed, are events polled before that?
sys_evt_process_pending();
Expand All @@ -97,8 +95,10 @@ void mgetgraphpos()
// Use relative mouse movement; speed factor should already be applied by SDL in this mode
int rel_x, rel_y;
ags_mouse_get_relxy(rel_x, rel_y);
Point old_pos = Point(real_mouse_x, real_mouse_y);
real_mouse_x = Math::Clamp(real_mouse_x + rel_x, Mouse::ControlRect.Left, Mouse::ControlRect.Right);
real_mouse_y = Math::Clamp(real_mouse_y + rel_y, Mouse::ControlRect.Top, Mouse::ControlRect.Bottom);
Debug::Printf("mouse control: real was %d,%d; rel = %d,%d; result = %d,%d", old_pos.X, old_pos.Y, rel_x, rel_y, real_mouse_x, real_mouse_y);
}
else
{
Expand Down Expand Up @@ -208,7 +208,8 @@ void Mouse::SetMovementControl(bool on)
SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE, "1.0");
#else
ControlEnabled = false;
Debug::Printf(kDbgMsg_Warn, "WARNING: SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE not supported, mouse control can't be enabled");
if (on)
Debug::Printf(kDbgMsg_Warn, "WARNING: SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE not supported, mouse control can't be enabled");
#endif
ags_clear_mouse_movement();
}
Expand Down

0 comments on commit 1f203dc

Please sign in to comment.