Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.

Commit f883ebe

Browse files
committed
Fix[input]: fix mouse input on Mac Catalyst
1 parent c5042e2 commit f883ebe

1 file changed

Lines changed: 21 additions & 38 deletions

File tree

Natives/SurfaceViewController.m

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
static int currentHotbarSlot = -1;
3333
static GameSurfaceView* pojavWindow;
3434

35-
@interface SurfaceViewController ()<UITextFieldDelegate, UIPointerInteractionDelegate, UIGestureRecognizerDelegate> {
35+
@interface SurfaceViewController ()<UITextFieldDelegate, UIGestureRecognizerDelegate> {
3636
}
3737

3838
@property(nonatomic) NSDictionary* metadata;
@@ -122,7 +122,7 @@ - (void)viewDidLoad
122122
pojavWindow = self.surfaceView;
123123

124124
self.touchView = [[UIView alloc] initWithFrame:self.view.frame];
125-
self.touchView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
125+
self.touchView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
126126
self.touchView.multipleTouchEnabled = YES;
127127
[self.touchView addSubview:self.surfaceView];
128128

@@ -131,17 +131,13 @@ - (void)viewDidLoad
131131

132132
[self performSelector:@selector(setupCategory_Navigation)];
133133

134-
if (self.isMacCatalystApp) {
135-
UIHoverGestureRecognizer *hoverGesture = [[NSClassFromString(@"UIHoverGestureRecognizer") alloc]
136-
initWithTarget:self action:@selector(surfaceOnHover:)];
137-
[self.surfaceView addGestureRecognizer:hoverGesture];
138-
}
134+
135+
UIHoverGestureRecognizer *hoverGesture = [[NSClassFromString(@"UIHoverGestureRecognizer") alloc] initWithTarget:self action:@selector(surfaceOnHover:)];
136+
[self.touchView addGestureRecognizer:hoverGesture];
139137

140138
self.tapGesture = [[UITapGestureRecognizer alloc]
141139
initWithTarget:self action:@selector(surfaceOnClick:)];
142-
if (!self.isMacCatalystApp) {
143-
self.tapGesture.allowedTouchTypes = @[@(UITouchTypeDirect)];
144-
}
140+
self.tapGesture.allowedTouchTypes = @[@(UITouchTypeDirect)];
145141
self.tapGesture.delegate = self;
146142
self.tapGesture.numberOfTapsRequired = 1;
147143
self.tapGesture.numberOfTouchesRequired = 1;
@@ -179,10 +175,6 @@ - (void)viewDidLoad
179175
self.scrollPanGesture.maximumNumberOfTouches = 2;
180176
[self.touchView addGestureRecognizer:self.scrollPanGesture];
181177

182-
if (!isTVOS) {
183-
[self.touchView addInteraction:[[NSClassFromString(@"UIPointerInteraction") alloc] initWithDelegate:self]];
184-
}
185-
186178
// Virtual mouse
187179
virtualMouseEnabled = getPrefBool(@"control.virtmouse_enable");
188180
virtualMouseFrame = CGRectMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2, 18, 27);
@@ -216,9 +208,7 @@ - (void)viewDidLoad
216208
NSLog(@"Input: Mouse connected!");
217209
GCMouse* mouse = note.object;
218210
[self registerMouseCallbacks:mouse];
219-
self.mousePointerView.hidden = isGrabbing;
220-
virtualMouseEnabled = YES;
221-
[self setNeedsUpdateOfPrefersPointerLocked];
211+
self.mousePointerView.hidden = isGrabbing || !virtualMouseEnabled; [self setNeedsUpdateOfPrefersPointerLocked];
222212
if (getPrefBool(@"control.hardware_hide")) {
223213
self.ctrlView.hidden = YES;
224214
}
@@ -237,7 +227,7 @@ - (void)viewDidLoad
237227
}
238228
}];
239229
if (GCMouse.current != nil) {
240-
[self registerMouseCallbacks: GCMouse.current];
230+
[self registerMouseCallbacks:GCMouse.current];
241231
}
242232

243233

@@ -369,6 +359,8 @@ - (void)updatePreferenceChanges {
369359
((CAMetalLayer *)self.surfaceView.layer).developerHUDProperties = perfHUDEnabled ? @{@"mode": @"default"} : nil;
370360
}
371361
}
362+
// Update pointer lock state
363+
[self setNeedsUpdateOfPrefersPointerLocked];
372364
}
373365

374366
- (void)updateSavedResolution {
@@ -429,6 +421,7 @@ - (void)updateGrabState {
429421
}
430422
self.scrollPanGesture.enabled = !isGrabbing;
431423
self.mousePointerView.hidden = isGrabbing || !virtualMouseEnabled;
424+
[self setNeedsUpdateOfPrefersPointerLocked];
432425

433426
// Update buttons visibility
434427
[self updateControlHiddenState:NO];
@@ -659,7 +652,7 @@ - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)eve
659652
}
660653

661654
- (BOOL)prefersPointerLocked {
662-
return GCMouse.mice.count > 0;
655+
return GCMouse.mice.count > 0 && (isGrabbing || virtualMouseEnabled);
663656
}
664657

665658
- (void)registerMouseCallbacks:(GCMouse *)mouse {
@@ -668,19 +661,13 @@ - (void)registerMouseCallbacks:(GCMouse *)mouse {
668661
if (!self.view.window.windowScene.pointerLockState.locked) {
669662
return;
670663
}
671-
[self sendTouchPoint:CGPointMake(deltaX * 2.0 / 3.0, -deltaY * 2.0 / 3.0) withEvent:ACTION_MOVE_MOTION];
664+
[self sendTouchPoint:CGPointMake(deltaX, -deltaY) withEvent:ACTION_MOVE_MOTION];
672665
};
673666

674667
mouse.mouseInput.leftButton.pressedChangedHandler = ^(GCControllerButtonInput * _Nonnull button, float value, BOOL pressed) {
675-
if (!self.view.window.windowScene.pointerLockState.locked) {
676-
return;
677-
}
678668
CallbackBridge_nativeSendMouseButton(GLFW_MOUSE_BUTTON_LEFT, pressed, 0);
679669
};
680670
mouse.mouseInput.middleButton.pressedChangedHandler = ^(GCControllerButtonInput * _Nonnull button, float value, BOOL pressed) {
681-
if (!self.view.window.windowScene.pointerLockState.locked) {
682-
return;
683-
}
684671
CallbackBridge_nativeSendMouseButton(GLFW_MOUSE_BUTTON_MIDDLE, pressed, 0);
685672
};
686673
mouse.mouseInput.rightButton.pressedChangedHandler = ^(GCControllerButtonInput * _Nonnull button, float value, BOOL pressed) {
@@ -826,14 +813,6 @@ - (void)surfaceOnTouchesScroll:(UIPanGestureRecognizer *)sender {
826813
}
827814
}
828815

829-
- (UIPointerRegion *)pointerInteraction:(UIPointerInteraction *)interaction regionForRequest:(UIPointerRegionRequest *)request defaultRegion:(UIPointerRegion *)defaultRegion {
830-
return nil;
831-
}
832-
833-
- (UIPointerStyle *)pointerInteraction:(UIPointerInteraction *)interaction styleForRegion:(UIPointerRegion *)region {
834-
return [NSClassFromString(@"UIPointerStyle") hiddenPointerStyle];
835-
}
836-
837816
#pragma mark - Input view stuff
838817

839818
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
@@ -897,6 +876,7 @@ - (void)executebtn:(ControlButton *)sender withAction:(int)action {
897876
virtualMouseEnabled = !virtualMouseEnabled;
898877
self.mousePointerView.hidden = !virtualMouseEnabled;
899878
setPrefBool(@"control.virtmouse_enable", virtualMouseEnabled);
879+
[self setNeedsUpdateOfPrefersPointerLocked];
900880
}
901881
break;
902882

@@ -1001,7 +981,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
1001981
[super touchesBegan:touches withEvent:event];
1002982
int i = 0;
1003983
for (UITouch *touch in touches) {
1004-
if (!self.isMacCatalystApp && touch.type == UITouchTypeIndirectPointer) {
984+
if (touch.type == UITouchTypeIndirectPointer) {
1005985
continue; // handle this in a different place
1006986
}
1007987
CGPoint locationInView = [touch locationInView:self.rootView];
@@ -1022,11 +1002,14 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
10221002
// Equals to Android ACTION_MOVE
10231003
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
10241004
{
1025-
10261005
[super touchesMoved:touches withEvent:event];
10271006

10281007
for (UITouch *touch in touches) {
1029-
if (!self.isMacCatalystApp && touch.type == UITouchTypeIndirectPointer) {
1008+
if (touch.type == UITouchTypeIndirectPointer) {
1009+
if (!isGrabbing && !virtualMouseEnabled) {
1010+
CGPoint point = [touch locationInView:self.rootView];
1011+
[self sendTouchPoint:point withEvent:ACTION_MOVE];
1012+
}
10301013
continue; // handle this in a different place
10311014
}
10321015
if (self.hotbarTouch != touch && [self isTouchInactive:self.primaryTouch]) {
@@ -1042,7 +1025,7 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
10421025
- (void)touchesEndedGlobal:(NSSet *)touches withEvent:(UIEvent *)event
10431026
{
10441027
for (UITouch *touch in touches) {
1045-
if (!self.isMacCatalystApp && touch.type == UITouchTypeIndirectPointer) {
1028+
if (touch.type == UITouchTypeIndirectPointer) {
10461029
continue; // handle this in a different place
10471030
}
10481031
[self sendTouchEvent:touch withUIEvent:event withEvent:ACTION_UP];

0 commit comments

Comments
 (0)