Implemented mouse relative mode for iOS 14.1 and newer

This commit is contained in:
Sam Lantinga 2021-07-08 07:23:29 -07:00
parent 3433f3c4cc
commit 46f19c311d
9 changed files with 80 additions and 23 deletions

0
Xcode/SDL/SDL.xcodeproj/project.pbxproj Normal file → Executable file
View File

0
build-scripts/config.guess vendored Executable file → Normal file
View File

0
build-scripts/config.sub vendored Executable file → Normal file
View File

View File

@ -31,6 +31,7 @@ extern void SDL_QuitGCKeyboard(void);
extern void SDL_InitGCMouse(void); extern void SDL_InitGCMouse(void);
extern SDL_bool SDL_HasGCMouse(void); extern SDL_bool SDL_HasGCMouse(void);
extern SDL_bool SDL_GCMouseRelativeMode(void);
extern void SDL_QuitGCMouse(void); extern void SDL_QuitGCMouse(void);
#endif /* SDL_uikitevents_h_ */ #endif /* SDL_uikitevents_h_ */

View File

@ -182,10 +182,22 @@ void SDL_QuitGCKeyboard(void)
static int mice_connected = 0; static int mice_connected = 0;
static id mouse_connect_observer = nil; static id mouse_connect_observer = nil;
static id mouse_disconnect_observer = nil; static id mouse_disconnect_observer = nil;
static bool mouse_relative_mode = SDL_FALSE;
static void UpdateMouseGrab()
{
SDL_VideoDevice *_this = SDL_GetVideoDevice();
SDL_Window *window;
for (window = _this->windows; window != NULL; window = window->next) {
SDL_UpdateWindowGrab(window);
}
}
static int SetGCMouseRelativeMode(SDL_bool enabled) static int SetGCMouseRelativeMode(SDL_bool enabled)
{ {
/* We'll always send relative motion and we can't warp, so nothing to do here */ mouse_relative_mode = enabled;
UpdateMouseGrab();
return 0; return 0;
} }
@ -230,6 +242,8 @@ static void OnGCMouseConnected(GCMouse *mouse) API_AVAILABLE(macos(11.0), ios(14
mouse.handlerQueue = queue; mouse.handlerQueue = queue;
++mice_connected; ++mice_connected;
UpdateMouseGrab();
} }
static void OnGCMouseDisconnected(GCMouse *mouse) API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) static void OnGCMouseDisconnected(GCMouse *mouse) API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0))
@ -245,6 +259,8 @@ static void OnGCMouseDisconnected(GCMouse *mouse) API_AVAILABLE(macos(11.0), ios
for (GCControllerButtonInput *button in mouse.mouseInput.auxiliaryButtons) { for (GCControllerButtonInput *button in mouse.mouseInput.auxiliaryButtons) {
button.pressedChangedHandler = nil; button.pressedChangedHandler = nil;
} }
UpdateMouseGrab();
} }
void SDL_InitGCMouse(void) void SDL_InitGCMouse(void)
@ -284,6 +300,11 @@ SDL_bool SDL_HasGCMouse(void)
return (mice_connected > 0); return (mice_connected > 0);
} }
SDL_bool SDL_GCMouseRelativeMode(void)
{
return mouse_relative_mode;
}
void SDL_QuitGCMouse(void) void SDL_QuitGCMouse(void)
{ {
@autoreleasepool { @autoreleasepool {
@ -320,6 +341,11 @@ SDL_bool SDL_HasGCMouse(void)
return SDL_FALSE; return SDL_FALSE;
} }
SDL_bool SDL_GCMouseRelativeMode(void)
{
return SDL_FALSE;
}
void SDL_QuitGCMouse(void) void SDL_QuitGCMouse(void)
{ {
} }

View File

@ -93,6 +93,7 @@ UIKit_CreateDevice(int devindex)
device->RaiseWindow = UIKit_RaiseWindow; device->RaiseWindow = UIKit_RaiseWindow;
device->SetWindowBordered = UIKit_SetWindowBordered; device->SetWindowBordered = UIKit_SetWindowBordered;
device->SetWindowFullscreen = UIKit_SetWindowFullscreen; device->SetWindowFullscreen = UIKit_SetWindowFullscreen;
device->SetWindowMouseGrab = UIKit_SetWindowMouseGrab;
device->DestroyWindow = UIKit_DestroyWindow; device->DestroyWindow = UIKit_DestroyWindow;
device->GetWindowWMInfo = UIKit_GetWindowWMInfo; device->GetWindowWMInfo = UIKit_GetWindowWMInfo;
device->GetDisplayUsableBounds = UIKit_GetDisplayUsableBounds; device->GetDisplayUsableBounds = UIKit_GetDisplayUsableBounds;

View File

@ -27,8 +27,9 @@
#include "../SDL_sysvideo.h" #include "../SDL_sysvideo.h"
#include "../../events/SDL_events_c.h" #include "../../events/SDL_events_c.h"
#import "SDL_uikitviewcontroller.h" #include "SDL_uikitviewcontroller.h"
#import "SDL_uikitmessagebox.h" #include "SDL_uikitmessagebox.h"
#include "SDL_uikitevents.h"
#include "SDL_uikitvideo.h" #include "SDL_uikitvideo.h"
#include "SDL_uikitmodes.h" #include "SDL_uikitmodes.h"
#include "SDL_uikitwindow.h" #include "SDL_uikitwindow.h"
@ -246,7 +247,20 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
return UIRectEdgeNone; return UIRectEdgeNone;
} }
} }
#endif
- (BOOL)prefersPointerLocked
{
SDL_VideoDevice *_this = SDL_GetVideoDevice();
if (SDL_HasGCMouse() &&
(SDL_GCMouseRelativeMode() || _this->grabbed_window == window)) {
return YES;
} else {
return NO;
}
}
#endif /* !TARGET_OS_TV */
/* /*
---- Keyboard related functionality below this line ---- ---- Keyboard related functionality below this line ----

View File

@ -33,6 +33,7 @@ extern void UIKit_HideWindow(_THIS, SDL_Window * window);
extern void UIKit_RaiseWindow(_THIS, SDL_Window * window); extern void UIKit_RaiseWindow(_THIS, SDL_Window * window);
extern void UIKit_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered); extern void UIKit_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered);
extern void UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen); extern void UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
extern void UIKit_SetWindowMouseGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
extern void UIKit_DestroyWindow(_THIS, SDL_Window * window); extern void UIKit_DestroyWindow(_THIS, SDL_Window * window);
extern SDL_bool UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, extern SDL_bool UIKit_GetWindowWMInfo(_THIS, SDL_Window * window,
struct SDL_SysWMinfo * info); struct SDL_SysWMinfo * info);

View File

@ -320,6 +320,20 @@ UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display
} }
} }
void
UIKit_SetWindowMouseGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
{
#if !TARGET_OS_TV
@autoreleasepool {
SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
SDL_uikitviewcontroller *viewcontroller = data.viewcontroller;
if (@available(iOS 14.0, *)) {
[viewcontroller setNeedsUpdateOfPrefersPointerLocked];
}
}
#endif /* !TARGET_OS_TV */
}
void void
UIKit_DestroyWindow(_THIS, SDL_Window * window) UIKit_DestroyWindow(_THIS, SDL_Window * window)
{ {