diff --git a/sdk/src/TouchHandler.js b/sdk/src/TouchHandler.js index b043b6f3d2dbdb0f1d81781a89da26b5f6716634..9dbc4ed20029d82a5f42c9f82eb36df99c9a6cf7 100644 --- a/sdk/src/TouchHandler.js +++ b/sdk/src/TouchHandler.js @@ -26,6 +26,13 @@ const ORIENTATION_DEGRESS = { 'REVERSE_PORTRAIT': 180 }; +const ORIENTATION_ORIGIN = { + 'PORTRAIT': 0, + 'LANDSCAPE': 1, + 'REVERSE_LANDSCAPE': 3, + 'REVERSE_PORTRAIT': 2 +}; + export default class TouchHandler { constructor({player, isMobile, sendHandler, isDebug, autoRotate}) { // 监听触控鼠键事件的Dom @@ -79,8 +86,6 @@ export default class TouchHandler { start() { this.updateScale(); - this.resize(); - this.util.bind(window, 'resize', this.resize.bind(this)); if (this.displayDom) { if (this.isMobile) { this.util.bind(this.displayDom, 'touchstart', this.onTouchStart.bind(this)); @@ -112,12 +117,10 @@ export default class TouchHandler { **/ updateOrientation(orientation) { this.orientation = orientation; - this.resize(); } updateResolution(width, height) { this.resolution = {width, height}; - this.resize(); } /** @@ -264,23 +267,6 @@ export default class TouchHandler { } } - // 计算键盘按键位置映射信息 - calcDisplayKeyboardMap() { - let displayKeyboardMap = {}; - if (this.isKeyboardOperate()) { - let keys = Object.keys(this.keyboardMap); - keys.forEach(key => { - let pos = this.keyboardMap[key]; - displayKeyboardMap[key] = { - x: pos.left * this.displayBox.width, - y: pos.top * this.displayBox.height - }; - }); - } - - this.displayKeyboardMap = displayKeyboardMap; - } - isSameOrientation() { // 若云手机画面横屏,当前认为云手机方向为逆时针旋转90度。 // 故若不自动旋转,云手机画面竖屏时,浏览器和云手机画面方向一致; @@ -364,24 +350,6 @@ export default class TouchHandler { }; } - // 云手机画面横屏表示逆时针旋转了90度 - // 发送给server的(x, y)相对于手机竖屏时左上角的点。 - // 不管手机横屏还是竖屏,或是css旋转,监听到的触控点(x, y)的值所见即所得,其参考点始终是浏览器左上角; - // 云手机画面在本地横屏还是竖屏是相对浏览器的,也就是说,最终显示(不管是否有旋转)存在角度的都需要做位置转换 - transform(pos) { - if (this.isSameOrientation()) { - return { - x: Math.ceil(pos.x * this.scale.x), - y: Math.ceil(pos.y * this.scale.y) - }; - } - - return { - x: Math.ceil((this.displayBox.height - pos.y) * this.scale.x), - y: Math.ceil(pos.x * this.scale.y) - }; - } - // 分配虚拟ID allocVirtualTouchId() { const index = this.virtualTouchIds.indexOf(0); @@ -406,7 +374,7 @@ export default class TouchHandler { } const PACKAGE_HEADER_LENGTH = 8; - const TOUCH_MSG_BODY_LENGTH = 8; + const TOUCH_MSG_BODY_LENGTH = 17; const TOUCH_ACTION = 6; let buf = new Uint8Array(PACKAGE_HEADER_LENGTH + TOUCH_MSG_BODY_LENGTH); @@ -421,6 +389,17 @@ export default class TouchHandler { buf[11] = data.x & 0xFF; buf[12] = (data.y >> 8) & 0xFF; buf[13] = data.y & 0xFF; + buf[14] = (data.pressure >> 8) & 0xFF; + buf[15] = data.pressure & 0xFF; + buf[16] = (data.time & 0xFF000000) >> 24; + buf[17] = (data.time & 0x00FF0000) >> 16; + buf[18] = (data.time & 0x0000FF00) >> 8; + buf[19] = data.time & 0x000000FF; + buf[20] = data.orientation; + buf[21] = (data.height >> 8) & 0xFF; + buf[22] = data.height & 0xFF; + buf[23] = (data.width >> 8) & 0xFF; + buf[24] = data.width & 0xFF; return buf; } @@ -433,22 +412,21 @@ export default class TouchHandler { * touchleave :移动的手指离开一个dom元素。 */ sendTouchMsg(touch, action) { - touch.x = Math.max(touch.x, 0); - touch.y = Math.max(touch.y, 0); - touch.x = Math.min(touch.x, this.displayBox.width); - touch.y = Math.min(touch.y, this.displayBox.height); - const pos = this.transform(touch); const msg = { - ...pos, + ...touch, action: PROTOCOL_CONFIG.ACTIONS_TYPE[action], - id: touch.vId + id: touch.vId, + pressure: 129, + time: -1, + orientation: ORIENTATION_ORIGIN[this.orientation], + height: this.displayBox.height, + width: this.displayBox.width }; const msgBuf = this.getMsgBuf(msg); this.sendHandler(msgBuf); } destroy() { - this.util.unbind(window, 'resize'); this.util.unbind(window, 'keydown'); this.util.unbind(window, 'keyup'); this.util.unbind(this.displayDom);