From f883ebe3e208150dd7d92be87967b20d8ff07ca4 Mon Sep 17 00:00:00 2001 From: khanhduytran0 Date: Sun, 13 Apr 2025 17:25:26 +0700 Subject: [PATCH] Fix[input]: fix mouse input on Mac Catalyst --- Natives/SurfaceViewController.m | 59 ++++++++++++--------------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/Natives/SurfaceViewController.m b/Natives/SurfaceViewController.m index 29e0cd58..1bcfcd72 100644 --- a/Natives/SurfaceViewController.m +++ b/Natives/SurfaceViewController.m @@ -32,7 +32,7 @@ int memorystatus_control(uint32_t command, int32_t pid, uint32_t flags, void *bu static int currentHotbarSlot = -1; static GameSurfaceView* pojavWindow; -@interface SurfaceViewController () { +@interface SurfaceViewController () { } @property(nonatomic) NSDictionary* metadata; @@ -122,7 +122,7 @@ static GameSurfaceView* pojavWindow; pojavWindow = self.surfaceView; self.touchView = [[UIView alloc] initWithFrame:self.view.frame]; - self.touchView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1]; + self.touchView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; self.touchView.multipleTouchEnabled = YES; [self.touchView addSubview:self.surfaceView]; @@ -131,17 +131,13 @@ static GameSurfaceView* pojavWindow; [self performSelector:@selector(setupCategory_Navigation)]; - if (self.isMacCatalystApp) { - UIHoverGestureRecognizer *hoverGesture = [[NSClassFromString(@"UIHoverGestureRecognizer") alloc] - initWithTarget:self action:@selector(surfaceOnHover:)]; - [self.surfaceView addGestureRecognizer:hoverGesture]; - } + + UIHoverGestureRecognizer *hoverGesture = [[NSClassFromString(@"UIHoverGestureRecognizer") alloc] initWithTarget:self action:@selector(surfaceOnHover:)]; + [self.touchView addGestureRecognizer:hoverGesture]; self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(surfaceOnClick:)]; - if (!self.isMacCatalystApp) { - self.tapGesture.allowedTouchTypes = @[@(UITouchTypeDirect)]; - } + self.tapGesture.allowedTouchTypes = @[@(UITouchTypeDirect)]; self.tapGesture.delegate = self; self.tapGesture.numberOfTapsRequired = 1; self.tapGesture.numberOfTouchesRequired = 1; @@ -179,10 +175,6 @@ static GameSurfaceView* pojavWindow; self.scrollPanGesture.maximumNumberOfTouches = 2; [self.touchView addGestureRecognizer:self.scrollPanGesture]; - if (!isTVOS) { - [self.touchView addInteraction:[[NSClassFromString(@"UIPointerInteraction") alloc] initWithDelegate:self]]; - } - // Virtual mouse virtualMouseEnabled = getPrefBool(@"control.virtmouse_enable"); virtualMouseFrame = CGRectMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2, 18, 27); @@ -216,9 +208,7 @@ static GameSurfaceView* pojavWindow; NSLog(@"Input: Mouse connected!"); GCMouse* mouse = note.object; [self registerMouseCallbacks:mouse]; - self.mousePointerView.hidden = isGrabbing; - virtualMouseEnabled = YES; - [self setNeedsUpdateOfPrefersPointerLocked]; + self.mousePointerView.hidden = isGrabbing || !virtualMouseEnabled; [self setNeedsUpdateOfPrefersPointerLocked]; if (getPrefBool(@"control.hardware_hide")) { self.ctrlView.hidden = YES; } @@ -237,7 +227,7 @@ static GameSurfaceView* pojavWindow; } }]; if (GCMouse.current != nil) { - [self registerMouseCallbacks: GCMouse.current]; + [self registerMouseCallbacks:GCMouse.current]; } @@ -369,6 +359,8 @@ static GameSurfaceView* pojavWindow; ((CAMetalLayer *)self.surfaceView.layer).developerHUDProperties = perfHUDEnabled ? @{@"mode": @"default"} : nil; } } + // Update pointer lock state + [self setNeedsUpdateOfPrefersPointerLocked]; } - (void)updateSavedResolution { @@ -429,6 +421,7 @@ static GameSurfaceView* pojavWindow; } self.scrollPanGesture.enabled = !isGrabbing; self.mousePointerView.hidden = isGrabbing || !virtualMouseEnabled; + [self setNeedsUpdateOfPrefersPointerLocked]; // Update buttons visibility [self updateControlHiddenState:NO]; @@ -659,7 +652,7 @@ static GameSurfaceView* pojavWindow; } - (BOOL)prefersPointerLocked { - return GCMouse.mice.count > 0; + return GCMouse.mice.count > 0 && (isGrabbing || virtualMouseEnabled); } - (void)registerMouseCallbacks:(GCMouse *)mouse { @@ -668,19 +661,13 @@ static GameSurfaceView* pojavWindow; if (!self.view.window.windowScene.pointerLockState.locked) { return; } - [self sendTouchPoint:CGPointMake(deltaX * 2.0 / 3.0, -deltaY * 2.0 / 3.0) withEvent:ACTION_MOVE_MOTION]; + [self sendTouchPoint:CGPointMake(deltaX, -deltaY) withEvent:ACTION_MOVE_MOTION]; }; mouse.mouseInput.leftButton.pressedChangedHandler = ^(GCControllerButtonInput * _Nonnull button, float value, BOOL pressed) { - if (!self.view.window.windowScene.pointerLockState.locked) { - return; - } CallbackBridge_nativeSendMouseButton(GLFW_MOUSE_BUTTON_LEFT, pressed, 0); }; mouse.mouseInput.middleButton.pressedChangedHandler = ^(GCControllerButtonInput * _Nonnull button, float value, BOOL pressed) { - if (!self.view.window.windowScene.pointerLockState.locked) { - return; - } CallbackBridge_nativeSendMouseButton(GLFW_MOUSE_BUTTON_MIDDLE, pressed, 0); }; mouse.mouseInput.rightButton.pressedChangedHandler = ^(GCControllerButtonInput * _Nonnull button, float value, BOOL pressed) { @@ -826,14 +813,6 @@ static GameSurfaceView* pojavWindow; } } -- (UIPointerRegion *)pointerInteraction:(UIPointerInteraction *)interaction regionForRequest:(UIPointerRegionRequest *)request defaultRegion:(UIPointerRegion *)defaultRegion { - return nil; -} - -- (UIPointerStyle *)pointerInteraction:(UIPointerInteraction *)interaction styleForRegion:(UIPointerRegion *)region { - return [NSClassFromString(@"UIPointerStyle") hiddenPointerStyle]; -} - #pragma mark - Input view stuff -(BOOL)textFieldShouldReturn:(UITextField *)textField { @@ -897,6 +876,7 @@ static GameSurfaceView* pojavWindow; virtualMouseEnabled = !virtualMouseEnabled; self.mousePointerView.hidden = !virtualMouseEnabled; setPrefBool(@"control.virtmouse_enable", virtualMouseEnabled); + [self setNeedsUpdateOfPrefersPointerLocked]; } break; @@ -1001,7 +981,7 @@ int touchesMovedCount; [super touchesBegan:touches withEvent:event]; int i = 0; for (UITouch *touch in touches) { - if (!self.isMacCatalystApp && touch.type == UITouchTypeIndirectPointer) { + if (touch.type == UITouchTypeIndirectPointer) { continue; // handle this in a different place } CGPoint locationInView = [touch locationInView:self.rootView]; @@ -1022,11 +1002,14 @@ int touchesMovedCount; // Equals to Android ACTION_MOVE - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { - [super touchesMoved:touches withEvent:event]; for (UITouch *touch in touches) { - if (!self.isMacCatalystApp && touch.type == UITouchTypeIndirectPointer) { + if (touch.type == UITouchTypeIndirectPointer) { + if (!isGrabbing && !virtualMouseEnabled) { + CGPoint point = [touch locationInView:self.rootView]; + [self sendTouchPoint:point withEvent:ACTION_MOVE]; + } continue; // handle this in a different place } if (self.hotbarTouch != touch && [self isTouchInactive:self.primaryTouch]) { @@ -1042,7 +1025,7 @@ int touchesMovedCount; - (void)touchesEndedGlobal:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *touch in touches) { - if (!self.isMacCatalystApp && touch.type == UITouchTypeIndirectPointer) { + if (touch.type == UITouchTypeIndirectPointer) { continue; // handle this in a different place } [self sendTouchEvent:touch withUIEvent:event withEvent:ACTION_UP];