Added SDL_GetWindowMouseRect()

Also guarantee that we won't get mouse movement outside the confining area, even if the OS implementation allows it (e.g. macOS)
This commit is contained in:
Sam Lantinga
2021-11-08 21:34:48 -08:00
parent 4db546b092
commit fd79607eb0
13 changed files with 111 additions and 66 deletions

View File

@@ -124,7 +124,6 @@ struct SDL_WindowData
SDL_bool created;
SDL_bool inWindowFullscreenTransition;
NSInteger flash_request;
SDL_Rect mouse_rect;
Cocoa_WindowListener *listener;
struct SDL_VideoData *videodata;
#if SDL_VIDEO_OPENGL_EGL
@@ -155,7 +154,7 @@ extern void Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDispl
extern int Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp);
extern void* Cocoa_GetWindowICCProfile(_THIS, SDL_Window * window, size_t * size);
extern int Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp);
extern int Cocoa_SetWindowMouseRect(_THIS, SDL_Window * window, const SDL_Rect * rect);
extern void Cocoa_SetWindowMouseRect(_THIS, SDL_Window * window);
extern void Cocoa_SetWindowMouseGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
extern void Cocoa_DestroyWindow(_THIS, SDL_Window * window);
extern SDL_bool Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info);

View File

@@ -335,7 +335,7 @@ ShouldAdjustCoordinatesForGrab(SDL_Window * window)
return SDL_FALSE;
}
if ((window->flags & SDL_WINDOW_MOUSE_GRABBED) || (data->mouse_rect.w > 0 && data->mouse_rect.h > 0)) {
if ((window->flags & SDL_WINDOW_MOUSE_GRABBED) || (window->mouse_rect.w > 0 && window->mouse_rect.h > 0)) {
return SDL_TRUE;
}
return SDL_FALSE;
@@ -344,9 +344,7 @@ ShouldAdjustCoordinatesForGrab(SDL_Window * window)
static SDL_bool
AdjustCoordinatesForGrab(SDL_Window * window, int x, int y, CGPoint *adjusted)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
if (data->mouse_rect.w > 0 && data->mouse_rect.h > 0) {
if (window->mouse_rect.w > 0 && window->mouse_rect.h > 0) {
SDL_Rect window_rect;
SDL_Rect mouse_rect;
@@ -355,7 +353,7 @@ AdjustCoordinatesForGrab(SDL_Window * window, int x, int y, CGPoint *adjusted)
window_rect.w = window->w;
window_rect.h = window->h;
if (SDL_IntersectRect(&data->mouse_rect, &window_rect, &mouse_rect)) {
if (SDL_IntersectRect(&window->mouse_rect, &window_rect, &mouse_rect)) {
int left = window->x + mouse_rect.x;
int right = left + mouse_rect.w - 1;
int top = window->y + mouse_rect.y;
@@ -2093,17 +2091,9 @@ Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
return 0;
}
int
Cocoa_SetWindowMouseRect(_THIS, SDL_Window * window, const SDL_Rect * rect)
void
Cocoa_SetWindowMouseRect(_THIS, SDL_Window * window)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
if (rect) {
SDL_memcpy(&data->mouse_rect, rect, sizeof(*rect));
} else {
SDL_zero(data->mouse_rect);
}
/* Move the cursor to the nearest point in the mouse rect */
if (ShouldAdjustCoordinatesForGrab(window)) {
int x, y;
@@ -2115,7 +2105,6 @@ Cocoa_SetWindowMouseRect(_THIS, SDL_Window * window, const SDL_Rect * rect)
CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
}
}
return 0;
}
void