Cocos2d-x 3.16 OSX Events

Is it possible to use native AppDelegate in cocos2d-x 3.16 on Mac, or at least capture NSApplicationDelegate and NSWindowDelegate events? Replacing NSApplicationMain as app entry point with c++ AppDelegate looks very odd to me.

It is true, as i see, only for ios. In OSX AppController not involved at all:

int main(int argc, char *argv[])
{
    AppDelegate app;
    return Application::getInstance()->run();
}

And then AppDelegate is already c++ class. So how to catch NSWindowDelegate events in this AppDelegate?

It absolutely is for ios.

Mac uses main.cpp.

I think that something along the lines of this would work fine: macos - Getting mouse events from HIDManager in OSX with C++ - Stack Overflow

By this I mean mix c++ and objective-c.

I found an elegant way and captured all NSWindowDelegate events! Case closed.

Can you share?

As it turned out, my solution is far from perfect - after closing window OpenGL erros appears and hang app. Will dig deeper.

I was tried to set NSWindow delegate to my own objective-c object:
AppDelegate.mm

@interface MyWindowDelegate: NSObject NSWindowDelegate

@end

@implementation MyWindowDelegate

  • (void)windowDidMiniaturize:(NSNotification *)notification {
    NSLog(@“miniaturzed!”);
    }

  • (void)windowDidDeminiaturize:(NSNotification *)notification {
    NSLog(@“DE miniaturzed!”);
    }

  • (void)windowWillClose:(NSNotification *)notification {
    NSLog(@“they want me close!!!”);
    }

@end

and in
bool AppDelegate::applicationDidFinishLaunching() {

MyWindowDelegate * myWndDelegate = [[MyWindowDelegate alloc] init];
NSWindow * wind = glview->getCocoaWindow();
wind.delegate = myWndDelegate;

As i’m find out it is NOT good solution - it seems like somwhere inside libglfw3 a delegate is already hooked. Replacing inner glfw3 implementation leads to huge OpenGL crashes on app exit (but my delegate did work, however it is still bad realisation). Then i tryed few experiments with handmade delegate proxy, but with no change in app crash. Digging much deeper is useless, so i choose a more lightweight, but working solution:

AppDelegate.mm

@interface MyWindowDelegate: NSObject

@end

@implementation MyWindowDelegate

-(id)init {

self = [super init];
if (self) {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification object:nil];
}
return self;

}

  • (void)windowDidMiniaturize:(NSNotification *)notification {
    NSLog(@“miniaturzed!”);
    }

  • (void)windowDidDeminiaturize:(NSNotification *)notification {
    NSLog(@“DE miniaturzed!”);
    }

  • (void)windowWillClose:(NSNotification *)notification {
    NSLog(@“they want me close!!!”);
    }

@end

static MyWindowDelegate * s_windowDelegate;

and in
bool AppDelegate::applicationDidFinishLaunching() {

s_windowDelegate = [[MyWindowDelegate alloc] init];

Thats all, all messages, interesting for me are received.

interesting idea!