handle mouse input with UIHoverGestureRecognizer

This commit is contained in:
Jesús A. Álvarez 2024-02-17 14:35:19 +01:00
parent ef6ef61037
commit 772ce2c326
1 changed files with 45 additions and 41 deletions

View File

@ -43,16 +43,7 @@ static int8_t usb_to_adb_scancode[] = {
UIControl *pointingDeviceView; UIControl *pointingDeviceView;
UIViewController *_keyboardViewController; UIViewController *_keyboardViewController;
BOOL physicalCapsLocked; BOOL physicalCapsLocked;
id interaction; UIPointerInteraction *pointerInteraction;
}
- (Point)mouseLocForCGPoint:(CGPoint)point {
Point mouseLoc;
CGRect screenBounds = self.screenView.screenBounds;
CGSize screenSize = self.screenView.screenSize;
mouseLoc.h = (point.x - screenBounds.origin.x) * (screenSize.width/screenBounds.size.width);
mouseLoc.v = (point.y - screenBounds.origin.y) * (screenSize.height/screenBounds.size.height);
return mouseLoc;
} }
- (void)viewDidLoad { - (void)viewDidLoad {
@ -170,31 +161,6 @@ static int8_t usb_to_adb_scancode[] = {
[[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:@"trackpad"]; [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:@"trackpad"];
} }
- (void)setUpPointingDevice {
if (pointingDeviceView) {
[pointingDeviceView removeFromSuperview];
pointingDeviceView = nil;
}
if (interaction == nil) {
interaction = [[UIPointerInteraction alloc] initWithDelegate: self];
[self.view addInteraction:interaction];
}
#if defined(TARGET_OS_VISION) && TARGET_OS_VISION == 1
Class pointingDeviceClass = [TouchScreen class];
#else
BOOL useTrackPad = [[NSUserDefaults standardUserDefaults] boolForKey:@"trackpad"];
Class pointingDeviceClass = useTrackPad ? [TrackPad class] : [TouchScreen class];
#endif
pointingDeviceView = [[pointingDeviceClass alloc] initWithFrame:self.view.bounds];
pointingDeviceView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view insertSubview:pointingDeviceView aboveSubview:self.screenView];
if ([UIApplication instancesRespondToSelector:@selector(btcMouseSetRawMode:)]) {
[[UIApplication sharedApplication] btcMouseSetRawMode:YES];
}
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) { if (motion == UIEventSubtypeMotionShake) {
[[AppDelegate sharedInstance] showInsertDisk:self]; [[AppDelegate sharedInstance] showInsertDisk:self];
@ -499,14 +465,47 @@ static int8_t usb_to_adb_scancode[] = {
} }
} }
} }
@end
@implementation ViewController (PointerInteraction) #pragma mark - Mouse
- (UIPointerRegion *)pointerInteraction:(UIPointerInteraction *)interaction regionForRequest:(UIPointerRegionRequest *)request defaultRegion:(UIPointerRegion *)defaultRegion {
if (request != nil) { - (void)setUpPointingDevice {
Point mouseLoc = [self mouseLocForCGPoint:request.location]; if (pointingDeviceView) {
[[AppDelegate sharedEmulator] setMouseX:mouseLoc.h Y:mouseLoc.v]; [pointingDeviceView removeFromSuperview];
pointingDeviceView = nil;
} }
if (pointerInteraction == nil) {
pointerInteraction = [[UIPointerInteraction alloc] initWithDelegate: self];
[self.view addInteraction:pointerInteraction];
UIHoverGestureRecognizer *hoverGestureRecognizer = [[UIHoverGestureRecognizer alloc] initWithTarget:self action:@selector(hover:)];
[self.view addGestureRecognizer:hoverGestureRecognizer];
}
#if defined(TARGET_OS_VISION) && TARGET_OS_VISION == 1
Class pointingDeviceClass = [TouchScreen class];
#else
BOOL useTrackPad = [[NSUserDefaults standardUserDefaults] boolForKey:@"trackpad"];
Class pointingDeviceClass = useTrackPad ? [TrackPad class] : [TouchScreen class];
#endif
pointingDeviceView = [[pointingDeviceClass alloc] initWithFrame:self.view.bounds];
pointingDeviceView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view insertSubview:pointingDeviceView aboveSubview:self.screenView];
if ([UIApplication instancesRespondToSelector:@selector(btcMouseSetRawMode:)]) {
[[UIApplication sharedApplication] btcMouseSetRawMode:YES];
}
}
- (Point)mouseLocForCGPoint:(CGPoint)point {
Point mouseLoc;
CGRect screenBounds = self.screenView.screenBounds;
CGSize screenSize = self.screenView.screenSize;
mouseLoc.h = (point.x - screenBounds.origin.x) * (screenSize.width/screenBounds.size.width);
mouseLoc.v = (point.y - screenBounds.origin.y) * (screenSize.height/screenBounds.size.height);
return mouseLoc;
}
- (UIPointerRegion *)pointerInteraction:(UIPointerInteraction *)interaction regionForRequest:(UIPointerRegionRequest *)request defaultRegion:(UIPointerRegion *)defaultRegion {
return defaultRegion; return defaultRegion;
} }
@ -514,4 +513,9 @@ static int8_t usb_to_adb_scancode[] = {
return [UIPointerStyle hiddenPointerStyle]; return [UIPointerStyle hiddenPointerStyle];
} }
- (void)hover:(UIHoverGestureRecognizer *)hoverGestureRecognizer {
Point mouseLoc = [self mouseLocForCGPoint:[hoverGestureRecognizer locationInView:self.screenView]];
[[AppDelegate sharedEmulator] setMouseX:mouseLoc.h Y:mouseLoc.v];
}
@end @end