From b94556b77e261fc0acf331cdbb9ffcb8cdedee4d Mon Sep 17 00:00:00 2001 From: PP Date: Thu, 14 Jan 2021 17:39:30 +0800 Subject: [PATCH] fix accelerometer on runtime (#8139) --- .../platforms/cocos-play/jsb/accelerometer.js | 27 +++++++++++-------- .../huawei-quick-game/jsb/accelerometer.js | 26 +++++++++--------- .../platforms/link-sure/jsb/accelerometer.js | 24 ++++++++--------- .../oppo-mini-game/jsb/accelerometer.js | 23 ++++++++-------- .../platforms/qtt/jsb/accelerometer.js | 16 +++++------ .../vivo-mini-game/jsb/accelerometer.js | 23 ++++++++-------- 6 files changed, 72 insertions(+), 67 deletions(-) diff --git a/platforms/runtime/platforms/cocos-play/jsb/accelerometer.js b/platforms/runtime/platforms/cocos-play/jsb/accelerometer.js index bc2284eee6..00117e46fc 100644 --- a/platforms/runtime/platforms/cocos-play/jsb/accelerometer.js +++ b/platforms/runtime/platforms/cocos-play/jsb/accelerometer.js @@ -5,10 +5,17 @@ window.jsb = window.jsb || {}; -const PORTRAIT = 0; -const LANDSCAPE_LEFT = -90; -const PORTRAIT_UPSIDE_DOWN = 180; -const LANDSCAPE_RIGHT = 90; +// KNOW_ISSUE: on COCOS_PLAY, systemInfo can't provide with accurate screenWidth or screenHeight +// so we need to read game config to access the screen orientation +let _rt = loadRuntime(); +let fs = _rt.getFileSystemManager(); +let gameConfig = JSON.parse(fs.readFileSync('./game.config.json', 'utf8')); +let isLandscape = gameConfig.deviceOrientation === 'landscape'; + +// const PORTRAIT = 0; +// const LANDSCAPE_LEFT = -90; +// const PORTRAIT_UPSIDE_DOWN = 180; +// const LANDSCAPE_RIGHT = 90; let _didAccelerateFun; // API for event listner is overwritten on DOM-adapter @@ -26,16 +33,14 @@ Object.assign(jsb, { let y = (eventAcceleration.y || 0) * 0.1; let z = (eventAcceleration.z || 0) * 0.1; - const tmpX = x; - if (window.orientation === LANDSCAPE_RIGHT) { + // KNOWN_ISSUE: don't know LANDSCAPE_RIGHT or LANDSCAPE_LEFT + // here isLandscape means isLandscapeRight + if (isLandscape) { + const tmpX = x; x = y; y = -tmpX; } - else if (window.orientation === LANDSCAPE_LEFT) { - x = -y; - y = tmpX; - } - else if (window.orientation === PORTRAIT) { + else { x = -x; y = -y; } diff --git a/platforms/runtime/platforms/huawei-quick-game/jsb/accelerometer.js b/platforms/runtime/platforms/huawei-quick-game/jsb/accelerometer.js index 5a801deb33..84f697b5fc 100644 --- a/platforms/runtime/platforms/huawei-quick-game/jsb/accelerometer.js +++ b/platforms/runtime/platforms/huawei-quick-game/jsb/accelerometer.js @@ -6,12 +6,15 @@ window.jsb = window.jsb || {}; -const PORTRAIT = 0; -const LANDSCAPE_LEFT = -90; -const PORTRAIT_UPSIDE_DOWN = 180; -const LANDSCAPE_RIGHT = 90; +// const PORTRAIT = 0; +// const LANDSCAPE_LEFT = -90; +// const PORTRAIT_UPSIDE_DOWN = 180; +// const LANDSCAPE_RIGHT = 90; let _didAccelerateFun; +let sysInfo = hbs.getSystemInfoSync(); +let isLandscape = sysInfo.screenWidth > sysInfo.screenHeight; + // API for event listner maybe overwritten on DOM-adapter let nativeAddEventListener = window.addEventListener.bind(window); let nativeRemoveEventListener = window.removeEventListener.bind(window); @@ -26,18 +29,15 @@ Object.assign(jsb, { let x = (eventAcceleration.x || 0) * 0.1; let y = (eventAcceleration.y || 0) * 0.1; let z = (eventAcceleration.z || 0) * 0.1; - - // FIXME: not supported to detect the orientation - const tmpX = x; - if (window.orientation === LANDSCAPE_RIGHT) { + + // KNOWN_ISSUE: don't know LANDSCAPE_RIGHT or LANDSCAPE_LEFT + // here isLandscape means isLandscapeRight + if (isLandscape) { + const tmpX = x; x = y; y = -tmpX; } - else if (window.orientation === LANDSCAPE_LEFT) { - x = -y; - y = tmpX; - } - else if (window.orientation === PORTRAIT) { + else { x = -x; y = -y; } diff --git a/platforms/runtime/platforms/link-sure/jsb/accelerometer.js b/platforms/runtime/platforms/link-sure/jsb/accelerometer.js index c1a1e5ddbe..92a35f1e2e 100644 --- a/platforms/runtime/platforms/link-sure/jsb/accelerometer.js +++ b/platforms/runtime/platforms/link-sure/jsb/accelerometer.js @@ -4,11 +4,13 @@ */ window.jsb = window.jsb || {}; +let sysInfo = wuji.getSystemInfoSync(); +let isLandscape = sysInfo.screenWidth > sysInfo.screenHeight; -const PORTRAIT = 0; -const LANDSCAPE_LEFT = -90; -const PORTRAIT_UPSIDE_DOWN = 180; -const LANDSCAPE_RIGHT = 90; +// const PORTRAIT = 0; +// const LANDSCAPE_LEFT = -90; +// const PORTRAIT_UPSIDE_DOWN = 180; +// const LANDSCAPE_RIGHT = 90; let _didAccelerateFun; // API for event listner is overwritten on DOM-adapter @@ -25,17 +27,15 @@ Object.assign(jsb, { let x = (eventAcceleration.x || 0) * 0.1; let y = (eventAcceleration.y || 0) * 0.1; let z = (eventAcceleration.z || 0) * 0.1; - - const tmpX = x; - if (window.orientation === LANDSCAPE_RIGHT) { + + // KNOWN_ISSUE: don't know LANDSCAPE_RIGHT or LANDSCAPE_LEFT + // here isLandscape means isLandscapeRight + if (isLandscape) { + const tmpX = x; x = y; y = -tmpX; } - else if (window.orientation === LANDSCAPE_LEFT) { - x = -y; - y = tmpX; - } - else if (window.orientation === PORTRAIT) { + else { x = -x; y = -y; } diff --git a/platforms/runtime/platforms/oppo-mini-game/jsb/accelerometer.js b/platforms/runtime/platforms/oppo-mini-game/jsb/accelerometer.js index 5a801deb33..231690ce43 100644 --- a/platforms/runtime/platforms/oppo-mini-game/jsb/accelerometer.js +++ b/platforms/runtime/platforms/oppo-mini-game/jsb/accelerometer.js @@ -5,11 +5,13 @@ */ window.jsb = window.jsb || {}; +let sysInfo = qg.getSystemInfoSync(); +let isLandscape = sysInfo.screenWidth > sysInfo.screenHeight; -const PORTRAIT = 0; -const LANDSCAPE_LEFT = -90; -const PORTRAIT_UPSIDE_DOWN = 180; -const LANDSCAPE_RIGHT = 90; +// const PORTRAIT = 0; +// const LANDSCAPE_LEFT = -90; +// const PORTRAIT_UPSIDE_DOWN = 180; +// const LANDSCAPE_RIGHT = 90; let _didAccelerateFun; // API for event listner maybe overwritten on DOM-adapter @@ -27,17 +29,14 @@ Object.assign(jsb, { let y = (eventAcceleration.y || 0) * 0.1; let z = (eventAcceleration.z || 0) * 0.1; - // FIXME: not supported to detect the orientation - const tmpX = x; - if (window.orientation === LANDSCAPE_RIGHT) { + // KNOWN_ISSUE: don't know LANDSCAPE_RIGHT or LANDSCAPE_LEFT + // here isLandscape means isLandscapeRight + if (isLandscape) { + const tmpX = x; x = y; y = -tmpX; } - else if (window.orientation === LANDSCAPE_LEFT) { - x = -y; - y = tmpX; - } - else if (window.orientation === PORTRAIT) { + else { x = -x; y = -y; } diff --git a/platforms/runtime/platforms/qtt/jsb/accelerometer.js b/platforms/runtime/platforms/qtt/jsb/accelerometer.js index e484352cb3..92b876f13a 100644 --- a/platforms/runtime/platforms/qtt/jsb/accelerometer.js +++ b/platforms/runtime/platforms/qtt/jsb/accelerometer.js @@ -4,6 +4,9 @@ */ window.jsb = window.jsb || {}; +let _rt = loadRuntime(); +let sysInfo = _rt.getSystemInfoSync(); +let isLandscape = sysInfo.screenWidth > sysInfo.screenHeight; const PORTRAIT = 0; const LANDSCAPE_LEFT = -90; @@ -26,17 +29,14 @@ Object.assign(jsb, { let y = (eventAcceleration.y || 0) * 0.1; let z = (eventAcceleration.z || 0) * 0.1; - // FIXME: not supported to detect the orientation - const tmpX = x; - if (window.orientation === LANDSCAPE_RIGHT) { + // KNOWN_ISSUE: don't know LANDSCAPE_RIGHT or LANDSCAPE_LEFT + // here isLandscape means isLandscapeRight + if (isLandscape) { + const tmpX = x; x = y; y = -tmpX; } - else if (window.orientation === LANDSCAPE_LEFT) { - x = -y; - y = tmpX; - } - else if (window.orientation === PORTRAIT) { + else { x = -x; y = -y; } diff --git a/platforms/runtime/platforms/vivo-mini-game/jsb/accelerometer.js b/platforms/runtime/platforms/vivo-mini-game/jsb/accelerometer.js index d5b3e5719c..5d72b338e1 100644 --- a/platforms/runtime/platforms/vivo-mini-game/jsb/accelerometer.js +++ b/platforms/runtime/platforms/vivo-mini-game/jsb/accelerometer.js @@ -2,12 +2,12 @@ window.jsb = window.jsb || {}; let isAccelerometerInit = false; let sysInfo = qg.getSystemInfoSync(); -let isLandscape = sysInfo.windowWidth > sysInfo.windowHeight; +let isLandscape = sysInfo.screenWidth > sysInfo.screenHeight; -const PORTRAIT = 0; -const LANDSCAPE_LEFT = -90; -const PORTRAIT_UPSIDE_DOWN = 180; -const LANDSCAPE_RIGHT = 90; +// const PORTRAIT = 0; +// const LANDSCAPE_LEFT = -90; +// const PORTRAIT_UPSIDE_DOWN = 180; +// const LANDSCAPE_RIGHT = 90; Object.assign(jsb, { startAccelerometer (cb) { @@ -18,12 +18,13 @@ Object.assign(jsb, { let y = res.y; let factor = 0.1; - // FIXME: not supported to detect the orientation - // if (isLandscape) { - // let tmp = x; - // x = -y; - // y = tmp; - // } + // KNOWN_ISSUE: don't know LANDSCAPE_RIGHT or LANDSCAPE_LEFT + // here isLandscape means isLandscapeRight + if (isLandscape) { + let tmp = x; + x = -y; + y = tmp; + } resClone.x = x * -factor; resClone.y = y * -factor; resClone.z = res.z * factor;