fix accelerometer on runtime (#8139)

This commit is contained in:
PP
2021-01-14 17:39:30 +08:00
committed by GitHub
parent 10eba6f859
commit b94556b77e
6 changed files with 72 additions and 67 deletions
@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -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;