From e7e8036dcd78d2eec1c53ffba3eed5f8ded8fe69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E7=8E=AE?= Date: Tue, 27 Sep 2022 17:45:21 +0800 Subject: [PATCH] fix: editor and cocos-play issues (#12908) * fix: editor and cocos-play issues * * Change 'windowId' to optional. --- cocos/core/platform/view.ts | 10 +++++----- cocos/core/root.ts | 3 ++- native/cocos/application/BaseGame.cpp | 5 +++++ native/cocos/core/Root.cpp | 2 +- native/cocos/core/Root.h | 7 ++++--- pal/screen-adapter/minigame/screen-adapter.ts | 2 +- pal/screen-adapter/native/screen-adapter.ts | 2 +- pal/screen-adapter/web/screen-adapter.ts | 8 ++++---- 8 files changed, 23 insertions(+), 16 deletions(-) diff --git a/cocos/core/platform/view.ts b/cocos/core/platform/view.ts index 317a407798..6685b513a6 100644 --- a/cocos/core/platform/view.ts +++ b/cocos/core/platform/view.ts @@ -592,14 +592,14 @@ export class View extends Eventify(System) { point.y = (point.y - viewport.y) / this._scaleY; } - private _updateAdaptResult (event: jsb.WindowEvent) { - legacyCC.director.root.resize(event.windowId, event.width, event.height); + private _updateAdaptResult (width: number, height: number, windowId?: number) { + legacyCC.director.root.resize(width, height, windowId === undefined ? 1 : windowId); // Frame size changed, do resize works - const width = this._designResolutionSize.width; - const height = this._designResolutionSize.height; + const w = this._designResolutionSize.width; + const h = this._designResolutionSize.height; if (width > 0) { - this.setDesignResolutionSize(width, height, this._resolutionPolicy); + this.setDesignResolutionSize(w, h, this._resolutionPolicy); } this.emit('canvas-resize'); diff --git a/cocos/core/root.ts b/cocos/core/root.ts index 34fb7c90c1..96e3353a3b 100644 --- a/cocos/core/root.ts +++ b/cocos/core/root.ts @@ -341,8 +341,9 @@ export class Root { * @zh 重置在屏窗口的大小。 * @param width The new width of the window. * @param height The new height of the window. + * @param windowId The system window ID, optional for now. */ - public resize (width: number, height: number) { + public resize (width: number, height: number, windowId?: number) { for (const window of this._windows) { if (window.swapchain) { window.resize(width, height); diff --git a/native/cocos/application/BaseGame.cpp b/native/cocos/application/BaseGame.cpp index 637aec3652..ac2e1620c0 100644 --- a/native/cocos/application/BaseGame.cpp +++ b/native/cocos/application/BaseGame.cpp @@ -50,8 +50,13 @@ int BaseGame::init() { std::call_once(_windowCreateFlag, [&]() { ISystemWindowInfo info; info.title = _windowInfo.title; +#if CC_PLATFORM == CC_PLATFORM_WINDOWS info.x = _windowInfo.x == -1 ? 50 : _windowInfo.x; // 50 meams move window a little for now info.y = _windowInfo.y == -1 ? 50 : _windowInfo.y; // same above +#else + info.x = _windowInfo.x == -1 ? 0 : _windowInfo.x; + info.y = _windowInfo.y == -1 ? 0 : _windowInfo.y; +#endif info.width = _windowInfo.width; info.height = _windowInfo.height; info.flags = _windowInfo.flags; diff --git a/native/cocos/core/Root.cpp b/native/cocos/core/Root.cpp index 7af7290264..e9acbec3a7 100644 --- a/native/cocos/core/Root.cpp +++ b/native/cocos/core/Root.cpp @@ -167,7 +167,7 @@ void Root::destroy() { // this.dataPoolManager.clear(); } -void Root::resize(uint32_t windowId, uint32_t width, uint32_t height) { +void Root::resize(uint32_t width, uint32_t height, uint32_t windowId) { for (const auto &window : _renderWindows) { auto *swapchain = window->getSwapchain(); if (swapchain && (swapchain->getWindowId() == windowId)) { diff --git a/native/cocos/core/Root.h b/native/cocos/core/Root.h index 202c0b003c..5318313d20 100644 --- a/native/cocos/core/Root.h +++ b/native/cocos/core/Root.h @@ -76,10 +76,11 @@ public: /** * @zh * 重置大小 - * @param width 屏幕宽度 - * @param height 屏幕高度 + * @param width 窗口宽度 + * @param height 窗口高度 + * @param windowId 窗口 ID */ - void resize(uint32_t windowId, uint32_t width, uint32_t height); + void resize(uint32_t width, uint32_t height, uint32_t windowId); bool setRenderPipeline(pipeline::RenderPipeline *rppl = nullptr); void onGlobalPipelineStateChanged(); diff --git a/pal/screen-adapter/minigame/screen-adapter.ts b/pal/screen-adapter/minigame/screen-adapter.ts index e57800a6d1..961e6a4b1f 100644 --- a/pal/screen-adapter/minigame/screen-adapter.ts +++ b/pal/screen-adapter/minigame/screen-adapter.ts @@ -127,7 +127,7 @@ class ScreenAdapter extends EventTarget { // TODO: onResize or onOrientationChange is not supported well if (WECHAT || COCOSPLAY) { minigame.onWindowResize?.(() => { - this.emit('window-resize'); + this.emit('window-resize', this.windowSize.width, this.windowSize.height); }); } } diff --git a/pal/screen-adapter/native/screen-adapter.ts b/pal/screen-adapter/native/screen-adapter.ts index a99681c4c6..5c89b0900b 100644 --- a/pal/screen-adapter/native/screen-adapter.ts +++ b/pal/screen-adapter/native/screen-adapter.ts @@ -118,7 +118,7 @@ class ScreenAdapter extends EventTarget { if (event.width === 0 || event.height === 0) return; // TODO: remove this function calling window.resize(event.width / this.devicePixelRatio, event.height / this.devicePixelRatio); - this.emit('window-resize', event); + this.emit('window-resize', event.width, event.height, event.windowId); }; jsb.onOrientationChanged = (event) => { this.emit('orientation-change'); diff --git a/pal/screen-adapter/web/screen-adapter.ts b/pal/screen-adapter/web/screen-adapter.ts index 52e77d8126..cee9e4ca14 100644 --- a/pal/screen-adapter/web/screen-adapter.ts +++ b/pal/screen-adapter/web/screen-adapter.ts @@ -350,7 +350,7 @@ class ScreenAdapter extends EventTarget { const dpr = window.devicePixelRatio; // NOTE: some browsers especially on iPhone doesn't support MediaQueryList window.matchMedia(`(resolution: ${dpr}dppx)`)?.addEventListener?.('change', () => { - this.emit('window-resize'); + this.emit('window-resize', this.windowSize.width, this.windowSize.height); updateDPRChangeListener(); }, { once: true }); }; @@ -366,13 +366,13 @@ class ScreenAdapter extends EventTarget { } this._updateFrameState(); this._resizeFrame(); - this.emit('orientation-change'); + this.emit('orientation-change', this.windowSize.width, this.windowSize.height); this._orientationChangeTimeoutId = -1; }, EVENT_TIMEOUT); }); document.addEventListener(this._fn.fullscreenchange, () => { this._onFullscreenChange?.(); - this.emit('fullscreen-change'); + this.emit('fullscreen-change', this.windowSize.width, this.windowSize.height); }); } private _convertToSizeInCssPixels (size: Size) { @@ -515,7 +515,7 @@ class ScreenAdapter extends EventTarget { || this._cachedFrameStyle.height !== this._gameFrame.style.height || this._cachedContainerStyle.width !== this._gameContainer.style.width || this._cachedContainerStyle.height !== this._gameContainer.style.height)) { - this.emit('window-resize'); + this.emit('window-resize', this.windowSize.width, this.windowSize.height); // Update Cache this._cachedFrameStyle.width = this._gameFrame.style.width;