Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repair mac platform to open the pointerlock click mouse will appear offset problem #15750

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions native/cocos/platform/mac/View.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ of this software and associated engine source code (the "Software"), a limited,
@implementation View {
cc::MouseEvent _mouseEvent;
cc::KeyboardEvent _keyboardEvent;
NSRect _contentRect;
AppDelegate *_delegate;
}

Expand Down Expand Up @@ -73,6 +74,11 @@ - (instancetype)initWithFrame:(NSRect)frameRect {
owner:self
userInfo:nil] autorelease];
[self addTrackingArea:trackingArea];

NSWindow* window = self.window;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification
object:window];
[self updateContentRect];
}
return self;
}
Expand All @@ -88,6 +94,7 @@ - (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size {
ev.width = static_cast<int>(size.width);
ev.height = static_cast<int>(size.height);
cc::events::WindowEvent::broadcast(ev);
[self updateContentRect];
}

- (void)displayLayer:(CALayer *)layer {
Expand All @@ -110,6 +117,8 @@ - (void)setFrameSize:(NSSize)newSize {
ev.height = static_cast<int>(nativeSize.height);
cc::events::WindowEvent::broadcast(ev);
}

[self updateContentRect];
}

- (void)viewDidChangeBackingProperties {
Expand All @@ -133,6 +142,7 @@ - (void)viewDidChangeBackingProperties {
ev.height = static_cast<int>(height);
cc::events::WindowEvent::broadcast(ev);
}
[self updateContentRect];
}

- (void)keyDown:(NSEvent *)event {
Expand Down Expand Up @@ -272,6 +282,15 @@ - (BOOL)acceptsFirstResponder {
return YES;
}

- (void)updateContentRect {
NSWindow* window = self.window;
_contentRect = [window contentRectForFrameRect:[window frame]];
}

- (void)windowDidMove:(NSNotification *)aNotification {
[self updateContentRect];
}

- (void)sendMouseEvent:(int)button type:(cc::MouseEvent::Type)type event:(NSEvent *)event {
_mouseEvent.windowId = [self getWindowId];
_mouseEvent.type = type;
Expand Down Expand Up @@ -311,9 +330,11 @@ - (void)sendMouseEvent:(int)button type:(cc::MouseEvent::Type)type event:(NSEven
}

auto mainDisplayId = CGMainDisplayID();
float windowX = contentRect.origin.x;

float windowX = _contentRect.origin.x;
float windowY =
CGDisplayPixelsHigh(mainDisplayId) - contentRect.origin.y - contentRect.size.height;
CGDisplayPixelsHigh(mainDisplayId) - _contentRect.origin.y - _contentRect.size.height;

window->setLastMousePos(windowX + _mouseEvent.x, windowY + _mouseEvent.y);
}
cc::events::Mouse::broadcast(_mouseEvent);
Expand Down
Loading