add SDL_SetRelativeMouseTransform

This commit is contained in:
expikr
2025-02-21 19:33:14 +08:00
committed by Sam Lantinga
parent 67127afe3f
commit 428f0dcd61
4 changed files with 70 additions and 10 deletions

View File

@@ -705,14 +705,19 @@ static void SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL
if (relative) {
if (mouse->relative_mode) {
if (mouse->enable_relative_system_scale) {
if (mouse->ApplySystemScale) {
mouse->ApplySystemScale(mouse->system_scale_data, timestamp, window, mouseID, &x, &y);
if (mouse->InputTransform) {
void *data = mouse->input_transform_data;
mouse->InputTransform(data, timestamp, window, mouseID, &x, &y);
} else {
if (mouse->enable_relative_system_scale) {
if (mouse->ApplySystemScale) {
mouse->ApplySystemScale(mouse->system_scale_data, timestamp, window, mouseID, &x, &y);
}
}
if (mouse->enable_relative_speed_scale) {
x *= mouse->relative_speed_scale;
y *= mouse->relative_speed_scale;
}
}
if (mouse->enable_relative_speed_scale) {
x *= mouse->relative_speed_scale;
y *= mouse->relative_speed_scale;
}
} else {
if (mouse->enable_normal_speed_scale) {
@@ -1115,6 +1120,13 @@ void SDL_QuitMouse(void)
SDL_mice = NULL;
}
void SDL_SetRelativeMouseTransform(SDL_MouseMotionTransformCallback transform, void *userdata)
{
SDL_Mouse *mouse = SDL_GetMouse();
mouse->InputTransform = transform;
mouse->input_transform_data = userdata;
}
SDL_MouseButtonFlags SDL_GetMouseState(float *x, float *y)
{
SDL_Mouse *mouse = SDL_GetMouse();

View File

@@ -87,10 +87,14 @@ typedef struct
// Get absolute mouse coordinates. (x) and (y) are never NULL and set to zero before call.
SDL_MouseButtonFlags (*GetGlobalMouseState)(float *x, float *y);
// Platform-specific system mouse transform
void (*ApplySystemScale)(void *internal, Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, float *x, float *y);
// Platform-specific system mouse transform applied in relative mode
SDL_MouseMotionTransformCallback ApplySystemScale;
void *system_scale_data;
// User-defined mouse input transform applied in relative mode
SDL_MouseMotionTransformCallback InputTransform;
void *input_transform_data;
// Data common to all mice
SDL_Window *focus;
float x;

View File

@@ -1012,6 +1012,7 @@ static void relative_pointer_handle_relative_motion(void *data,
struct SDL_WaylandInput *input = data;
SDL_VideoData *d = input->display;
SDL_WindowData *window = input->pointer_focus;
SDL_Mouse *mouse = SDL_GetMouse();
// Relative pointer event times are in microsecond granularity.
const Uint64 timestamp = Wayland_GetEventTimestamp(SDL_US_TO_NS(((Uint64)time_hi << 32) | (Uint64)time_lo));
@@ -1019,7 +1020,7 @@ static void relative_pointer_handle_relative_motion(void *data,
if (input->pointer_focus && d->relative_mouse_mode) {
double dx;
double dy;
if (!SDL_GetMouse()->enable_relative_system_scale) {
if (mouse->InputTransform || !mouse->enable_relative_system_scale) {
dx = wl_fixed_to_double(dx_unaccel_w);
dy = wl_fixed_to_double(dy_unaccel_w);
} else {