Fixed touch normalized coordinates

When converting normalized coordinates to pixel coordinates, the valid range is 0 to (width or height) - 1, and the pixel coordinate of width or height is past the edge of the window.

Fixes https://github.com/libsdl-org/SDL/issues/2913
This commit is contained in:
Sam Lantinga
2023-11-05 00:03:23 -07:00
parent ff3c20a799
commit 9302d7732d
6 changed files with 72 additions and 16 deletions

View File

@@ -196,6 +196,24 @@ public class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
return SDLActivity.handleKeyEvent(v, keyCode, event, null); return SDLActivity.handleKeyEvent(v, keyCode, event, null);
} }
private float getNormalizedX(float x)
{
if (mWidth <= 1) {
return 0.5f;
} else {
return (x / (mWidth - 1));
}
}
private float getNormalizedY(float y)
{
if (mHeight <= 1) {
return 0.5f;
} else {
return (y / (mHeight - 1));
}
}
// Touch events // Touch events
@Override @Override
public boolean onTouch(View v, MotionEvent event) { public boolean onTouch(View v, MotionEvent event) {
@@ -242,8 +260,8 @@ public class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
for (i = 0; i < pointerCount; i++) { for (i = 0; i < pointerCount; i++) {
pointerFingerId = event.getPointerId(i); pointerFingerId = event.getPointerId(i);
x = event.getX(i) / mWidth; x = getNormalizedX(event.getX(i));
y = event.getY(i) / mHeight; y = getNormalizedY(event.getY(i));
p = event.getPressure(i); p = event.getPressure(i);
if (p > 1.0f) { if (p > 1.0f) {
// may be larger than 1.0f on some devices // may be larger than 1.0f on some devices
@@ -267,8 +285,8 @@ public class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
} }
pointerFingerId = event.getPointerId(i); pointerFingerId = event.getPointerId(i);
x = event.getX(i) / mWidth; x = getNormalizedX(event.getX(i));
y = event.getY(i) / mHeight; y = getNormalizedY(event.getY(i));
p = event.getPressure(i); p = event.getPressure(i);
if (p > 1.0f) { if (p > 1.0f) {
// may be larger than 1.0f on some devices // may be larger than 1.0f on some devices
@@ -281,8 +299,8 @@ public class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
for (i = 0; i < pointerCount; i++) { for (i = 0; i < pointerCount; i++) {
pointerFingerId = event.getPointerId(i); pointerFingerId = event.getPointerId(i);
x = event.getX(i) / mWidth; x = getNormalizedX(event.getX(i));
y = event.getY(i) / mHeight; y = getNormalizedY(event.getY(i));
p = event.getPressure(i); p = event.getPressure(i);
if (p > 1.0f) { if (p > 1.0f) {
// may be larger than 1.0f on some devices // may be larger than 1.0f on some devices

View File

@@ -759,8 +759,16 @@ static EM_BOOL Emscripten_HandleTouch(int eventType, const EmscriptenTouchEvent
} }
id = touchEvent->touches[i].identifier; id = touchEvent->touches[i].identifier;
x = touchEvent->touches[i].targetX / client_w; if (client_w <= 1) {
y = touchEvent->touches[i].targetY / client_h; x = 0.5f;
} else {
x = touchEvent->touches[i].targetX / (client_w - 1);
}
if (client_h <= 1) {
y = 0.5f;
} else {
y = touchEvent->touches[i].targetY / (client_h - 1);
}
if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART) { if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART) {
SDL_SendTouch(0, deviceId, id, window_data->window, SDL_TRUE, x, y, 1.0f); SDL_SendTouch(0, deviceId, id, window_data->window, SDL_TRUE, x, y, 1.0f);

View File

@@ -36,8 +36,8 @@
internally in a portrait disposition so the internally in a portrait disposition so the
GSP_SCREEN constants are flipped. GSP_SCREEN constants are flipped.
*/ */
#define TOUCHSCREEN_SCALE_X 1.0f / GSP_SCREEN_HEIGHT_BOTTOM #define TOUCHSCREEN_SCALE_X 1.0f / (GSP_SCREEN_HEIGHT_BOTTOM - 1)
#define TOUCHSCREEN_SCALE_Y 1.0f / GSP_SCREEN_WIDTH #define TOUCHSCREEN_SCALE_Y 1.0f / (GSP_SCREEN_WIDTH - 1)
void N3DS_InitTouch(void) void N3DS_InitTouch(void)
{ {

View File

@@ -173,8 +173,18 @@ void VITA_PollTouch(void)
void VITA_ConvertTouchXYToSDLXY(float *sdl_x, float *sdl_y, int vita_x, int vita_y, int port) void VITA_ConvertTouchXYToSDLXY(float *sdl_x, float *sdl_y, int vita_x, int vita_y, int port)
{ {
float x = (vita_x - area_info[port].x) / area_info[port].w; float x, y;
float y = (vita_y - area_info[port].y) / area_info[port].h;
if (area_info[port].w <= 1) {
x = 0.5f;
} else {
x = (vita_x - area_info[port].x) / (area_info[port].w - 1);
}
if (area_info[port].h <= 1) {
y = 0.5f;
} else {
y = (vita_y - area_info[port].y) / (area_info[port].h - 1);
}
x = SDL_max(x, 0.0); x = SDL_max(x, 0.0);
x = SDL_min(x, 1.0); x = SDL_min(x, 1.0);

View File

@@ -953,8 +953,18 @@ static void touch_handler_down(void *data, struct wl_touch *touch, uint32_t seri
window_data = (SDL_WindowData *)wl_surface_get_user_data(surface); window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
if (window_data) { if (window_data) {
const float x = wl_fixed_to_double(fx) / window_data->wl_window_width; float x, y;
const float y = wl_fixed_to_double(fy) / window_data->wl_window_height;
if (window_data->wl_window_width <= 1) {
x = 0.5f;
} else {
x = wl_fixed_to_double(fx) / (window_data->wl_window_width - 1);
}
if (window_data->wl_window_height <= 1) {
y = 0.5f;
} else {
y = wl_fixed_to_double(fy) / (window_data->wl_window_height - 1);
}
SDL_SendTouch(Wayland_GetTouchTimestamp(input, timestamp), (SDL_TouchID)(intptr_t)touch, SDL_SendTouch(Wayland_GetTouchTimestamp(input, timestamp), (SDL_TouchID)(intptr_t)touch,
(SDL_FingerID)id, window_data->sdlwindow, SDL_TRUE, x, y, 1.0f); (SDL_FingerID)id, window_data->sdlwindow, SDL_TRUE, x, y, 1.0f);

View File

@@ -1419,6 +1419,8 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
for (i = 0; i < num_inputs; ++i) { for (i = 0; i < num_inputs; ++i) {
PTOUCHINPUT input = &inputs[i]; PTOUCHINPUT input = &inputs[i];
const int w = (rect.right - rect.left);
const int h = (rect.right - rect.left);
const SDL_TouchID touchId = (SDL_TouchID)((size_t)input->hSource); const SDL_TouchID touchId = (SDL_TouchID)((size_t)input->hSource);
@@ -1430,8 +1432,16 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
} }
/* Get the normalized coordinates for the window */ /* Get the normalized coordinates for the window */
x = (float)(input->x - rect.left) / (rect.right - rect.left); if (w <= 1) {
y = (float)(input->y - rect.top) / (rect.bottom - rect.top); x = 0.5f;
} else {
x = (float)(input->x - rect.left) / (w - 1);
}
if (h <= 1) {
y = 0.5f;
} else {
y = (float)(input->y - rect.top) / (h - 1);
}
/* FIXME: Should we use the input->dwTime field for the tick source of the timestamp? */ /* FIXME: Should we use the input->dwTime field for the tick source of the timestamp? */
if (input->dwFlags & TOUCHEVENTF_DOWN) { if (input->dwFlags & TOUCHEVENTF_DOWN) {