From 53621516089639eef66d80c2ab9db1b4529205d4 Mon Sep 17 00:00:00 2001 From: Knox Date: Wed, 25 Nov 2020 16:09:06 +0800 Subject: [PATCH] add getSafeArea for android and iOS & improve safe area (#7863) * add getSafeArea for android and iOS & improve safe area * refine code --- cocos/ui/components/safe-area.ts | 13 +++++----- cocos/ui/components/widget.ts | 20 ++++++++-------- port/native/builtin/jsb-adapter/window.js | 6 ++++- port/native/engine/index.js | 1 + port/native/engine/jsb-game.js | 7 ++---- port/native/engine/jsb-safearea.js | 29 +++++++++++++++++++++++ port/native/engine/jsb-sys.js | 2 +- 7 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 port/native/engine/jsb-safearea.js diff --git a/cocos/ui/components/safe-area.ts b/cocos/ui/components/safe-area.ts index ce7849d96d..98bd1121cc 100644 --- a/cocos/ui/components/safe-area.ts +++ b/cocos/ui/components/safe-area.ts @@ -83,8 +83,8 @@ export class SafeArea extends Component { */ public updateArea () { // TODO Remove Widget dependencies in the future - const widget = this.node.getComponent(Widget); - const uiTransComp = this.node.getComponent(UITransform); + const widget = this.node.getComponent(Widget) as Widget; + const uiTransComp = this.node.getComponent(UITransform) as UITransform; if (!widget || !uiTransComp) { return; } @@ -96,11 +96,12 @@ export class SafeArea extends Component { } // IMPORTANT: need to update alignment to get the latest position widget.updateAlignment(); - const lastPos = this.node.position; - const lastAnchorPoint = uiTransComp.anchorPoint; + const lastPos = this.node.position.clone(); + const lastAnchorPoint = uiTransComp.anchorPoint.clone(); // widget.isAlignTop = widget.isAlignBottom = widget.isAlignLeft = widget.isAlignRight = true; - const screenWidth = legacyCC.winSize.width, screenHeight = legacyCC.winSize.height; + const screenWidth = legacyCC.winSize.width; + const screenHeight = legacyCC.winSize.height; const safeArea = sys.getSafeAreaRect(); widget.top = screenHeight - safeArea.y - safeArea.height; widget.bottom = safeArea.y; @@ -108,7 +109,7 @@ export class SafeArea extends Component { widget.right = screenWidth - safeArea.x - safeArea.width; widget.updateAlignment(); // set anchor, keep the original position unchanged - const curPos = this.node.position; + const curPos = this.node.position.clone(); const anchorX = lastAnchorPoint.x - (curPos.x - lastPos.x) / uiTransComp.width; const anchorY = lastAnchorPoint.y - (curPos.y - lastPos.y) / uiTransComp.height; uiTransComp.setAnchorPoint(anchorX, anchorY); diff --git a/cocos/ui/components/widget.ts b/cocos/ui/components/widget.ts index 487a9aeb55..f9d4a9b4cc 100644 --- a/cocos/ui/components/widget.ts +++ b/cocos/ui/components/widget.ts @@ -814,7 +814,7 @@ export class Widget extends Component { } public _adjustWidgetToAllowMovingInEditor (eventType: TransformBit) { - if (/*!EDITOR ||*/ !(eventType & TransformBit.POSITION)) { + if (!EDITOR || !(eventType & TransformBit.POSITION)) { return; } @@ -866,11 +866,7 @@ export class Widget extends Component { } public _adjustWidgetToAllowResizingInEditor () { - // if (!EDITOR) { - // return; - // } - - if (legacyCC._widgetManager.isAligning) { + if (!EDITOR || legacyCC._widgetManager.isAligning) { return; } @@ -928,15 +924,19 @@ export class Widget extends Component { } protected _registerEvent () { - this.node.on(SystemEventType.TRANSFORM_CHANGED, this._adjustWidgetToAllowMovingInEditor, this); - this.node.on(SystemEventType.SIZE_CHANGED, this._adjustWidgetToAllowResizingInEditor, this); + if (EDITOR && !legacyCC.GAME_VIEW) { + this.node.on(SystemEventType.TRANSFORM_CHANGED, this._adjustWidgetToAllowMovingInEditor, this); + this.node.on(SystemEventType.SIZE_CHANGED, this._adjustWidgetToAllowResizingInEditor, this); + } this.node.on(SystemEventType.ANCHOR_CHANGED, this._adjustWidgetToAnchorChanged, this); this.node.on(SystemEventType.PARENT_CHANGED, this._adjustTargetToParentChanged, this); } protected _unregisterEvent () { - this.node.off(SystemEventType.TRANSFORM_CHANGED, this._adjustWidgetToAllowMovingInEditor, this); - this.node.off(SystemEventType.SIZE_CHANGED, this._adjustWidgetToAllowResizingInEditor, this); + if (EDITOR && !legacyCC.GAME_VIEW) { + this.node.off(SystemEventType.TRANSFORM_CHANGED, this._adjustWidgetToAllowMovingInEditor, this); + this.node.off(SystemEventType.SIZE_CHANGED, this._adjustWidgetToAllowResizingInEditor, this); + } this.node.off(SystemEventType.ANCHOR_CHANGED, this._adjustWidgetToAnchorChanged, this); } diff --git a/port/native/builtin/jsb-adapter/window.js b/port/native/builtin/jsb-adapter/window.js index cb65182e6e..57908a3c55 100755 --- a/port/native/builtin/jsb-adapter/window.js +++ b/port/native/builtin/jsb-adapter/window.js @@ -90,7 +90,7 @@ function inject () { height: window.innerHeight, orientation: { //FIXME:cjh type: 'portrait-primary' // portrait-primary, portrait-secondary, landscape-primary, landscape-secondary - }, + }, onorientationchange: function(event) {} }; @@ -128,6 +128,10 @@ function inject () { window.screen.height = window.innerHeight; window.clientWidth = window.innerWidth; window.clientHeight = window.innerHeight; + // emit resize consistent with web behavior + let resizeEvent = new Event('resize'); + resizeEvent._target = window; + window.dispatchEvent(resizeEvent); }; window.focus = function() {}; diff --git a/port/native/engine/index.js b/port/native/engine/index.js index 001ba90aa8..8110fa20f8 100644 --- a/port/native/engine/index.js +++ b/port/native/engine/index.js @@ -34,3 +34,4 @@ require('./jsb-webview.js'); require('./jsb-audio.js'); require('./jsb-editbox.js'); require('./jsb-pipeline.js'); +require('./jsb-safearea.js'); diff --git a/port/native/engine/jsb-game.js b/port/native/engine/jsb-game.js index 02b457aa93..acd0375874 100644 --- a/port/native/engine/jsb-game.js +++ b/port/native/engine/jsb-game.js @@ -44,14 +44,11 @@ jsb.onResume = function () { jsb.onResize = function (size) { if (size.width === 0 || size.height === 0) return; - window.resize(size.width, size.height); - cc.sys.windowPixelResolution = { width: size.width, height: size.height, }; - - cc.view.setCanvasSize(window.innerWidth, window.innerHeight); + window.resize(size.width, size.height); }; jsb.onOrientationChanged = function (event) { @@ -63,4 +60,4 @@ jsb.onOrientationChanged = function (event) { jsb.onMemoryWarning = function () { cc.game.emit(cc.Game.EVENT_LOW_MEMORY); -} +}; diff --git a/port/native/engine/jsb-safearea.js b/port/native/engine/jsb-safearea.js new file mode 100644 index 0000000000..fd5715337d --- /dev/null +++ b/port/native/engine/jsb-safearea.js @@ -0,0 +1,29 @@ +let SafeArea = cc.SafeArea; +if (SafeArea) { + let _onEnable = SafeArea.prototype.onEnable; + let _onDisable = SafeArea.prototype.onDisable; + Object.assign(SafeArea.prototype, { + onEnable(){ + _onEnable.call(this); + this._adaptSafeAreaChangeWithThis = this.adaptSafeAreaChange.bind(this); + this._updateAreaWithThis = this.adaptSafeAreaChange.bind(this); + window.addEventListener('orientationchange', this._adaptSafeAreaChangeWithThis); + window.addEventListener('safearea-change', this._updateAreaWithThis); + }, + + onDisable(){ + _onDisable.call(this); + window.removeEventListener('orientationchange', this._adaptSafeAreaChangeWithThis); + window.removeEventListener('safearea-change', this._updateAreaWithThis); + }, + + adaptSafeAreaChange(){ + if (JSB && (cc.sys.os === cc.sys.OS_IOS || cc.sys.os === cc.sys.OS_ANDROID)) { + setTimeout(() => { + this.updateArea(); + }, 200); + } + } + }); +} + diff --git a/port/native/engine/jsb-sys.js b/port/native/engine/jsb-sys.js index 8d006dfde9..d730473b23 100644 --- a/port/native/engine/jsb-sys.js +++ b/port/native/engine/jsb-sys.js @@ -51,4 +51,4 @@ sys.getSafeAreaRect = function() { cc.view._convertPointWithScale(rightTop); return cc.rect(leftBottom.x, leftBottom.y, rightTop.x - leftBottom.x, rightTop.y - leftBottom.y); -} +};