From d03a29fc9c7ee13b29a88543c64200778dfd2f40 Mon Sep 17 00:00:00 2001 From: PP Date: Tue, 24 Nov 2020 09:59:58 +0800 Subject: [PATCH] support window event on native desktop platform (#3058) * support window event on Mac * support window event on Win32 --- native/cocos/platform/win32/View-win32.cpp | 3 +-- .../template-link/platforms/mac/AppDelegate.mm | 17 +++++++++++++++++ .../platforms/win32/AppDelegate.cpp | 2 ++ .../runtime-src/proj.ios_mac/mac/AppDelegate.mm | 17 +++++++++++++++++ .../runtime-src/proj.win32/SimulatorWin.cpp | 2 ++ 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/native/cocos/platform/win32/View-win32.cpp b/native/cocos/platform/win32/View-win32.cpp index 97f181af1d..b065f63a9e 100644 --- a/native/cocos/platform/win32/View-win32.cpp +++ b/native/cocos/platform/win32/View-win32.cpp @@ -157,8 +157,6 @@ bool View::pollEvent(bool * quit, bool *resume, bool *pause) { int cnt = SDL_PollEvent(&sdlEvent); if (cnt == 0) return false; - *resume = false; - *pause = false; cc::TouchEvent touch; cc::MouseEvent mouse; cc::KeyboardEvent keyboard; @@ -182,6 +180,7 @@ bool View::pollEvent(bool * quit, bool *resume, bool *pause) cc::EventDispatcher::dispatchResizeEvent(wevent.data1, wevent.data2); break; case SDL_WINDOWEVENT_HIDDEN: + case SDL_WINDOWEVENT_MINIMIZED: *pause = true; break; case SDL_WINDOWEVENT_ENTER: diff --git a/native/templates/template-link/platforms/mac/AppDelegate.mm b/native/templates/template-link/platforms/mac/AppDelegate.mm index 7f0ec2bd52..77098bf807 100644 --- a/native/templates/template-link/platforms/mac/AppDelegate.mm +++ b/native/templates/template-link/platforms/mac/AppDelegate.mm @@ -33,6 +33,23 @@ _game = new Game(rect.size.width, rect.size.height); _game->init(); + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(windowWillMiniaturizeNotification)name:NSWindowWillMiniaturizeNotification + object:_window]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(windowDidDeminiaturizeNotification)name:NSWindowDidDeminiaturizeNotification + object:_window]; +} + +- (void)windowWillMiniaturizeNotification +{ + _game->onPause(); +} + +- (void)windowDidDeminiaturizeNotification +{ + _game->onResume(); } - (NSWindow*)getWindow diff --git a/native/templates/template-link/platforms/win32/AppDelegate.cpp b/native/templates/template-link/platforms/win32/AppDelegate.cpp index 15470e6cef..9c8dc3f4a2 100644 --- a/native/templates/template-link/platforms/win32/AppDelegate.cpp +++ b/native/templates/template-link/platforms/win32/AppDelegate.cpp @@ -133,6 +133,8 @@ void AppDelegage::start() { desiredInterval = (LONGLONG)(1.0 / _game->getPreferredFramesPerSecond() * nFreq.QuadPart); + resume = false; + pause = false; while (_view->pollEvent(&_quit, &resume, &pause)) {} if(pause) _game->onPause(); diff --git a/native/tools/simulator/frameworks/runtime-src/proj.ios_mac/mac/AppDelegate.mm b/native/tools/simulator/frameworks/runtime-src/proj.ios_mac/mac/AppDelegate.mm index 8e135919cf..4235044669 100644 --- a/native/tools/simulator/frameworks/runtime-src/proj.ios_mac/mac/AppDelegate.mm +++ b/native/tools/simulator/frameworks/runtime-src/proj.ios_mac/mac/AppDelegate.mm @@ -33,6 +33,23 @@ _game = new Game(rect.size.width, rect.size.height); _game->init(); + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(windowWillMiniaturizeNotification)name:NSWindowWillMiniaturizeNotification + object:_window]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(windowDidDeminiaturizeNotification)name:NSWindowDidDeminiaturizeNotification + object:_window]; +} + +- (void)windowWillMiniaturizeNotification +{ + _game->onPause(); +} + +- (void)windowDidDeminiaturizeNotification +{ + _game->onResume(); } - (NSWindow*)getWindow diff --git a/native/tools/simulator/frameworks/runtime-src/proj.win32/SimulatorWin.cpp b/native/tools/simulator/frameworks/runtime-src/proj.win32/SimulatorWin.cpp index 6d4d456b36..4d6472c09f 100755 --- a/native/tools/simulator/frameworks/runtime-src/proj.win32/SimulatorWin.cpp +++ b/native/tools/simulator/frameworks/runtime-src/proj.win32/SimulatorWin.cpp @@ -559,6 +559,8 @@ int SimulatorWin::run() { desiredInterval = (LONGLONG)(1.0 / _app->getPreferredFramesPerSecond() * nFreq.QuadPart); + resume = false; + pause = false; while (_view->pollEvent(&_quit, &resume, &pause)) {} if (pause) _app->onPause();