From 9a35994615289a356e8e4ba0504685e4f04d41b9 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 31 May 2018 17:21:13 +0800 Subject: [PATCH] Supports DeviceMotion event. (#2770) * MouseEvent readonly properties. * Adds DeviceMotionEvent.js * Supports DeviceMotion event. * set devicemotion to null. --- jsb/builtin/index.js | 55 +++++++++++- jsb/builtin/jsb-adapter/DeviceMotionEvent.js | 61 ++++++++++++++ jsb/builtin/jsb-adapter/EventTarget.js | 89 +++++++++++++++++--- jsb/builtin/jsb-adapter/MouseEvent.js | 50 ++++++++++- jsb/builtin/jsb-adapter/TouchEvent.js | 13 ++- jsb/builtin/jsb-adapter/window.js | 24 ++++++ 6 files changed, 266 insertions(+), 26 deletions(-) create mode 100644 jsb/builtin/jsb-adapter/DeviceMotionEvent.js diff --git a/jsb/builtin/index.js b/jsb/builtin/index.js index 97b21136db..526539959e 100644 --- a/jsb/builtin/index.js +++ b/jsb/builtin/index.js @@ -28,6 +28,8 @@ window.CC_JSB = true; window.CanvasRenderingContext2D = cc.CanvasRenderingContext2D; delete cc.CanvasRenderingContext2D; +jsb.device = cc.Device; // cc namespace will be reset to {} in creator, use jsb namespace instead. + const { btoa, atob } = require('./base64/base64.min'); window.btoa = btoa; window.atob = atob; @@ -147,12 +149,61 @@ window.clearTimeout = function(id) { window.setInterval = function(cb) { return createTimeoutInfo(arguments, true); -} +}; window.clearInterval = window.clearTimeout; - window.alert = console.error.bind(console); +var __motionCallbackID = 0; +var __motionEnabled = false; +var __motionInterval = 16.6; // milliseconds + +jsb.device.setMotionInterval = function(milliseconds) { + __motionInterval = milliseconds; + // convert to seconds + jsb.device.setAccelerometerInterval(__motionInterval / 1000); + if (__motionEnabled) { + jsb.device.setMotionEnabled(false); + jsb.device.setMotionEnabled(true); + } +}; + +jsb.device.setMotionEnabled = function(enabled) { + if (__motionEnabled === enabled) + return; + + if (enabled) { + jsb.device.setAccelerometerEnabled(enabled); + var motionValue; + var event = new DeviceMotionEvent(); + __motionCallbackID = window.setInterval(function(){ + motionValue = jsb.getDeviceMotionValue(); + + event._acceleration.x = motionValue[0]; + event._acceleration.y = motionValue[1]; + event._acceleration.z = motionValue[2]; + + event._accelerationIncludingGravity.x = motionValue[3]; + event._accelerationIncludingGravity.y = motionValue[4]; + event._accelerationIncludingGravity.z = motionValue[5]; + + event._rotationRate.alpha = motionValue[6]; + event._rotationRate.beta = motionValue[7]; + event._rotationRate.gamma = motionValue[8]; + + event._interval = __motionInterval; + + jsb.dispatchDeviceMotionEvent(event); + }, __motionInterval); + } + else { + window.clearInterval(__motionCallbackID); + __motionCallbackID = 0; + } + + __motionEnabled = enabled; +}; + // File utils (Temporary, won't be accessible) cc.fileUtils = cc.FileUtils.getInstance(); cc.fileUtils.setPopupNotify(false); diff --git a/jsb/builtin/jsb-adapter/DeviceMotionEvent.js b/jsb/builtin/jsb-adapter/DeviceMotionEvent.js new file mode 100644 index 0000000000..5a40e5b3bc --- /dev/null +++ b/jsb/builtin/jsb-adapter/DeviceMotionEvent.js @@ -0,0 +1,61 @@ +/**************************************************************************** + Copyright (c) 2018 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +const Event = require('./Event') + +class DeviceMotionEvent extends Event { + constructor(initArgs) { + super('devicemotion'); + if (initArgs) { + this._acceleration = initArgs.acceleration ? initArgs.acceleration : {x: 0, y: 0, z: 0}; + this._accelerationIncludingGravity = initArgs.accelerationIncludingGravity ? initArgs.accelerationIncludingGravity : {x: 0, y: 0, z: 0}; + this._rotationRate = initArgs.rotationRate ? initArgs.rotationRate : {alpha: 0, beta: 0, gamma: 0}; + this._interval = initArgs.interval; + } + else { + this._acceleration = {x: 0, y: 0, z: 0}; + this._accelerationIncludingGravity = {x: 0, y: 0, z: 0}; + this._rotationRate = {alpha: 0, beta: 0, gamma: 0}; + this._interval = 0; + } + } + + get acceleration() { + return this._acceleration; + } + + get accelerationIncludingGravity() { + return this._accelerationIncludingGravity; + } + + get rotationRate() { + return this._rotationRate; + } + + get interval() { + return this._interval; + } +} + +module.exports = DeviceMotionEvent; diff --git a/jsb/builtin/jsb-adapter/EventTarget.js b/jsb/builtin/jsb-adapter/EventTarget.js index 6552930abc..0a46252146 100644 --- a/jsb/builtin/jsb-adapter/EventTarget.js +++ b/jsb/builtin/jsb-adapter/EventTarget.js @@ -28,13 +28,48 @@ var __targetID = 0; var __listenerMap = { touch: {}, mouse: {}, - keyboard: {} + keyboard: {}, + devicemotion: {} }; -var __handleEventNames = { +var __listenerCountMap = { + touch: 0, + mouse: 0, + keyboard: 0, + devicemotion: 0 +}; + +var __enableCallbackMap = { + touch: null, + mouse: null, + keyboard: null, + //FIXME: Cocos Creator invokes addEventListener('devicemotion') when engine initializes, it will active sensor hardware. + // In that case, CPU and temperature cost will increase. Therefore, we require developer to invoke 'jsb.device.setMotionEnabled(true)' + // on native platforms since most games will not listen motion event. + devicemotion: null + // devicemotion: function() { + // jsb.device.setMotionEnabled(true); + // } +}; + +var __disableCallbackMap = { + touch: null, + mouse: null, + //FIXME: Cocos Creator invokes addEventListener('devicemotion') when engine initializes, it will active sensor hardware. + // In that case, CPU and temperature cost will increase. Therefore, we require developer to invoke 'jsb.device.setMotionEnabled(true)' + // on native platforms since most games will not listen motion event. + keyboard: null, + devicemotion: null + // devicemotion: function() { + // jsb.device.setMotionEnabled(false); + // } +}; + +const __handleEventNames = { touch: ['touchstart', 'touchmove', 'touchend', 'touchcancel'], mouse: ['mousedown', 'mousemove', 'mouseup', 'mousewheel'], - keyboard: ['keydown', 'keyup', 'keypress'] + keyboard: ['keydown', 'keyup', 'keypress'], + devicemotion: ['devicemotion'] } // Listener types @@ -67,7 +102,8 @@ class EventTarget { this._listenerCount = { touch: 0, mouse: 0, - keyboard: 0 + keyboard: 0, + devicemotion: 0 }; this._listeners = new Map(); } @@ -77,9 +113,14 @@ class EventTarget { for (var key in __handleEventNames) { handleEventNames = __handleEventNames[key]; if (handleEventNames.indexOf(eventName) > -1) { + if (__enableCallbackMap[key] && __listenerCountMap[key] === 0) { + __enableCallbackMap[key](); + } + if (this._listenerCount[key] === 0) __listenerMap[key][this._targetID] = this; ++this._listenerCount[key]; + ++__listenerCountMap[key]; break; } } @@ -92,6 +133,11 @@ class EventTarget { if (handleEventNames.indexOf(eventName) > -1) { if (this._listenerCount[key] <= 0) delete __listenerMap[key][this._targetID]; + --__listenerCountMap[key]; + + if (__disableCallbackMap[key] && __listenerCountMap[key] === 0) { + __disableCallbackMap[key](); + } break; } } @@ -294,14 +340,22 @@ jsb.onTouchCancel = touchEventHandlerFactory('touchcancel'); function mouseEventHandlerFactory(type) { return (event) => { - const mouseEvent = new MouseEvent(type); var button = event.button; - mouseEvent.button = button; - mouseEvent.which = button + 1; - mouseEvent.wheelDelta = event.wheelDeltaY; - mouseEvent.clientX = mouseEvent.screenX = mouseEvent.pageX = event.x; - mouseEvent.clientY = mouseEvent.screenY = mouseEvent.pageY = event.y; + var x = event.x; + var y = event.y; + const mouseEvent = new MouseEvent(type, { + button: button, + which: button + 1, + wheelDelta: event.wheelDeltaY, + clientX: x, + clientY: y, + screenX: x, + screenY: y, + pageX: x, + pageY: y + }); + var target; var mouseListenerMap = __listenerMap.mouse; for (let key in mouseListenerMap) { @@ -319,16 +373,14 @@ jsb.onMouseWheel = mouseEventHandlerFactory('mousewheel'); function keyboardEventHandlerFactory(type) { return (event) => { - var initArg = { + const keyboardEvent = new KeyboardEvent(type, { altKey: event.altKey, ctrlKey: event.ctrlKey, metaKey: event.metaKey, shiftKey: event.shiftKey, repeat: event.repeat, keyCode: event.keyCode - }; - - const keyboardEvent = new KeyboardEvent(type, initArg); + }); var target; var keyboardListenerMap = __listenerMap.keyboard; for (let key in keyboardListenerMap) { @@ -341,5 +393,14 @@ function keyboardEventHandlerFactory(type) { jsb.onKeyDown = keyboardEventHandlerFactory('keydown'); jsb.onKeyUp = keyboardEventHandlerFactory('keyup'); +jsb.dispatchDeviceMotionEvent = function(event) { + var target; + var devicemotionListenerMap = __listenerMap.devicemotion; + for (let key in devicemotionListenerMap) { + target = devicemotionListenerMap[key]; + target.dispatchEvent(event); + } +}; + module.exports = EventTarget diff --git a/jsb/builtin/jsb-adapter/MouseEvent.js b/jsb/builtin/jsb-adapter/MouseEvent.js index affb418416..17093c5c72 100644 --- a/jsb/builtin/jsb-adapter/MouseEvent.js +++ b/jsb/builtin/jsb-adapter/MouseEvent.js @@ -25,10 +25,54 @@ const Event = require('./Event') class MouseEvent extends Event { + constructor(type, initArgs) { + super(type) + this._button = initArgs.button; + this._which = initArgs.which; + this._wheelDelta = initArgs.wheelDelta; + this._clientX = initArgs.clientX; + this._clientY = initArgs.clientY; + this._screenX = initArgs.screenX; + this._screenY = initArgs.screenY; + this._pageX = initArgs.pageX; + this._pageY = initArgs.pageY; + } - constructor(type) { - super(type) - } + get button() { + return this._button; + } + + get which() { + return this._which; + } + + get wheelDelta() { + return this._wheelDelta; + } + + get clientX() { + return this._clientX; + } + + get clientY() { + return this._clientY; + } + + get screenX() { + return this._screenX; + } + + get screenY() { + return this._screenY; + } + + get pageX() { + return this._pageX; + } + + get pageY() { + return this._pageY; + } } module.exports = MouseEvent diff --git a/jsb/builtin/jsb-adapter/TouchEvent.js b/jsb/builtin/jsb-adapter/TouchEvent.js index 275f2fc965..fb08e708f1 100755 --- a/jsb/builtin/jsb-adapter/TouchEvent.js +++ b/jsb/builtin/jsb-adapter/TouchEvent.js @@ -25,13 +25,12 @@ const Event = require('./Event') class TouchEvent extends Event { - - constructor(type, touchEventInit) { - super(type) - this.touches = [] - this.targetTouches = [] - this.changedTouches = [] - } + constructor(type, touchEventInit) { + super(type) + this.touches = [] + this.targetTouches = [] + this.changedTouches = [] + } } module.exports = TouchEvent diff --git a/jsb/builtin/jsb-adapter/window.js b/jsb/builtin/jsb-adapter/window.js index e7386b88ce..5024794b4a 100755 --- a/jsb/builtin/jsb-adapter/window.js +++ b/jsb/builtin/jsb-adapter/window.js @@ -60,6 +60,30 @@ function inject () { window.TouchEvent = require('./TouchEvent'); window.MouseEvent = require('./MouseEvent'); window.KeyboardEvent = require('./KeyboardEvent'); + window.DeviceMotionEvent = require('./DeviceMotionEvent'); + + const ROTATION_0 = 0; + const ROTATION_90 = 1; + const ROTATION_180 = 2; + const ROTATION_270 = 3; + var orientation = 0; + var rotation = cc.Device.getDeviceRotation(); + switch (rotation) { + case ROTATION_90: + orientation = 90; + break; + case ROTATION_180: + orientation = 180; + break; + case ROTATION_270: + orientation = -90; + break; + default: + break; + } + + //FIXME: The value needs to be updated when device orientation changes. + window.orientation = orientation; window.devicePixelRatio = 1.0; window.screen = {