Skip to content

Commit

Permalink
Fixed the problem of incorrect display position of the emulator. (#16238
Browse files Browse the repository at this point in the history
)
  • Loading branch information
qiuguohua authored Sep 11, 2023
1 parent 794b8b3 commit 290746a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
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

0 comments on commit 290746a

Please sign in to comment.