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

Fixed the problem of incorrect display position of the emulator. #16238

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion native/cocos/platform/mac/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#import <MetalKit/MetalKit.h>

@interface View : NSView <CALayerDelegate>

- (int)getWindowId;
@property (nonatomic, assign) id<MTLDevice> device;

@end
16 changes: 16 additions & 0 deletions native/cocos/platform/mac/View.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ of this software and associated engine source code (the "Software"), a limited,
#import "platform/mac/modules/SystemWindow.h"
#import "platform/mac/modules/SystemWindowManager.h"

#include "SDL2/SDL.h"

static int MetalViewEventWatch(void* userData, SDL_Event*event) {
if (event->type == SDL_WINDOWEVENT && event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
@autoreleasepool {
auto *view = (__bridge View *)userData;
if ([view getWindowId] == event->window.windowID) {
[view viewDidChangeBackingProperties];
}
}
}
return 0;
}

@implementation View {
cc::MouseEvent _mouseEvent;
cc::KeyboardEvent _keyboardEvent;
Expand All @@ -50,6 +64,8 @@ - (CALayer *)makeBackingLayer {

- (instancetype)initWithFrame:(NSRect)frameRect {
if (self = [super initWithFrame:frameRect]) {
// View is used as a subview, so the resize message needs to be handled manually.
SDL_AddEventWatch(MetalViewEventWatch, (__bridge void*)(self));
[self.window makeFirstResponder:self];
int pixelRatio = [[NSScreen mainScreen] backingScaleFactor];
CGSize size = CGSizeMake(frameRect.size.width * pixelRatio, frameRect.size.height * pixelRatio);
Expand Down
6 changes: 3 additions & 3 deletions native/cocos/platform/mac/modules/SystemWindow.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ of this software and associated documentation files (the "Software"), to deal

#include "platform/mac/modules/SystemWindow.h"
#include "platform/mac/View.h"

#include "base/Log.h"
#include "base/Macros.h"

Expand Down Expand Up @@ -57,15 +58,14 @@ of this software and associated documentation files (the "Software"), to deal
void SystemWindow::initWindowProperty(SDL_Window* window, const char *title, int x, int y, int w, int h) {
CC_ASSERT(window != nullptr);
auto* nsWindow = reinterpret_cast<NSWindow*>(SDLHelper::getWindowHandle(window));
NSRect rect = NSMakeRect(x, y, w, h);
NSString *astring = [NSString stringWithUTF8String:title];
nsWindow.title = astring;
// contentView is created internally by sdl.
NSView *view = nsWindow.contentView;
auto* newView = [[View alloc] initWithFrame:rect];
auto* newView = [[View alloc] initWithFrame:view.frame];
[view addSubview:newView];
[nsWindow.contentView setWantsBestResolutionOpenGLSurface:YES];
[nsWindow makeKeyAndOrderFront:nil];

_windowHandle = reinterpret_cast<uintptr_t>(nsWindow.contentView) ;

auto dpr = [nsWindow backingScaleFactor];
Expand Down
Loading