mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 16:52:43 +08:00
add getSafeArea for android and iOS & improve safe area (#7863)
* add getSafeArea for android and iOS & improve safe area * refine code
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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() {};
|
||||
|
||||
@@ -34,3 +34,4 @@ require('./jsb-webview.js');
|
||||
require('./jsb-audio.js');
|
||||
require('./jsb-editbox.js');
|
||||
require('./jsb-pipeline.js');
|
||||
require('./jsb-safearea.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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user