From e5040dc3ee031b29aaa4f80ebfff885f09444e87 Mon Sep 17 00:00:00 2001 From: alpha Date: Thu, 6 Jan 2022 18:22:32 +0800 Subject: [PATCH] [3.4.1] Fix bug multi touch (#9917) * :bug: fix multi touch can not disable * Update pal/input/touch-manager.ts Co-authored-by: PP * Update pal/input/touch-manager.ts Co-authored-by: PP Co-authored-by: PP --- pal/input/minigame/touch-input.ts | 3 -- pal/input/native/touch-input.ts | 3 -- pal/input/touch-manager.ts | 57 ++++++++++++------------------- pal/input/web/touch-input.ts | 3 -- 4 files changed, 21 insertions(+), 45 deletions(-) diff --git a/pal/input/minigame/touch-input.ts b/pal/input/minigame/touch-input.ts index fc51a5ec33..4ab070eed0 100644 --- a/pal/input/minigame/touch-input.ts +++ b/pal/input/minigame/touch-input.ts @@ -43,9 +43,6 @@ export class TouchInputSource { touchManager.releaseTouch(touchID); } handleTouches.push(touch); - if (!macro.ENABLE_MULTI_TOUCH) { - break; - } } if (handleTouches.length > 0) { const eventTouch = new EventTouch(handleTouches, false, eventType, diff --git a/pal/input/native/touch-input.ts b/pal/input/native/touch-input.ts index b2235f725f..b8ae70a467 100644 --- a/pal/input/native/touch-input.ts +++ b/pal/input/native/touch-input.ts @@ -41,9 +41,6 @@ export class TouchInputSource { touchManager.releaseTouch(touchID); } handleTouches.push(touch); - if (!macro.ENABLE_MULTI_TOUCH) { - break; - } } if (handleTouches.length > 0) { const eventTouch = new EventTouch(handleTouches, false, eventType, diff --git a/pal/input/touch-manager.ts b/pal/input/touch-manager.ts index 8141908d8a..2cb71343a8 100644 --- a/pal/input/touch-manager.ts +++ b/pal/input/touch-manager.ts @@ -2,22 +2,17 @@ import { macro } from '../../cocos/core/platform/macro'; import { Touch } from '../../cocos/input/types'; import { Vec2 } from '../../cocos/core/math/vec2'; -interface TouchMap { - [touchID: number]: number; -} - const tempVec2 = new Vec2(); class TouchManager { /** - * A map from touch ID to available touch index in _touches array. + * A map from touch ID to touch object. */ - private _touchMap: TouchMap = {}; - private _touches: (Touch|undefined)[]; + private _touchMap: Map; private readonly _maxTouches = 8; constructor () { - this._touches = new Array(this._maxTouches); + this._touchMap = new Map(); } /** @@ -48,18 +43,17 @@ class TouchManager { * @returns */ private _createTouch (touchID: number, x: number, y: number): Touch | undefined { - if (touchID in this._touchMap) { + if (this._touchMap.has(touchID)) { console.log('Cannot create the same touch object.'); return undefined; } - const availableTouchIndex = this._getAvailableTouchIndex(touchID); - if (availableTouchIndex === -1) { + const checkResult = this._checkTouchMapSizeMoreThanMax(touchID); + if (checkResult) { console.log('The touches is more than MAX_TOUCHES.'); // TODO: logID 2300 return undefined; } const touch = new Touch(x, y, touchID); - this._touches[availableTouchIndex] = touch; - this._touchMap[touchID] = availableTouchIndex; + this._touchMap.set(touchID, touch); this._updateTouch(touch, x, y); return this._cloneTouch(touch); } @@ -70,12 +64,10 @@ class TouchManager { * @returns */ public releaseTouch (touchID: number) { - if (!(touchID in this._touchMap)) { + if (!this._touchMap.has(touchID)) { return; } - const availableTouchIndex = this._touchMap[touchID]; - this._touches[availableTouchIndex] = undefined; - delete this._touchMap[touchID]; + this._touchMap.delete(touchID); } /** @@ -84,8 +76,7 @@ class TouchManager { * @returns */ public getTouch (touchID: number, x: number, y: number): Touch | undefined { - const availableTouchIndex = this._touchMap[touchID]; - let touch = this._touches[availableTouchIndex]; + let touch = this._touchMap.get(touchID); if (!touch) { touch = this._createTouch(touchID, x, y); } else { @@ -100,7 +91,7 @@ class TouchManager { */ public getAllTouches (): Touch[] { const touches: Touch[] = []; - this._touches.forEach((touch) => { + this._touchMap.forEach((touch) => { if (touch) { const clonedTouch = this._cloneTouch(touch); touches.push(clonedTouch); @@ -121,30 +112,24 @@ class TouchManager { touch.setPoint(x, y); } - private _getAvailableTouchIndex (touchID: number) { - const availableTouchIndex = this._touchMap[touchID]; - if (typeof availableTouchIndex !== 'undefined') { - return availableTouchIndex; + private _checkTouchMapSizeMoreThanMax (touchID: number) { + if (this._touchMap.has(touchID)) { + return false; } - for (let i = 0; i < this._maxTouches; i++) { - if (!this._touches[i]) { - return i; - } + const maxSize = macro.ENABLE_MULTI_TOUCH ? this._maxTouches : 1; + if (this._touchMap.size < maxSize) { + return false; } - // Handle when exceed the max number of touches const now = performance.now(); - const TOUCH_TIMEOUT = macro.TOUCH_TIMEOUT; - for (let i = 0; i < this._maxTouches; i++) { - const touch = this._touches[i]!; - if (now - touch.lastModified > TOUCH_TIMEOUT) { + this._touchMap.forEach((touch) => { + if (now - touch.lastModified > macro.TOUCH_TIMEOUT) { console.log(`The touches is more than MAX_TOUCHES, release touch id ${touch.getID()}.`); // TODO: need to handle touch cancel event when exceed the max number of touches ? this.releaseTouch(touch.getID()); - return i; } - } - return -1; + }); + return maxSize >= this._touchMap.size; } } diff --git a/pal/input/web/touch-input.ts b/pal/input/web/touch-input.ts index 489686c810..8c3981614e 100644 --- a/pal/input/web/touch-input.ts +++ b/pal/input/web/touch-input.ts @@ -52,9 +52,6 @@ export class TouchInputSource { touchManager.releaseTouch(touchID); } handleTouches.push(touch); - if (!macro.ENABLE_MULTI_TOUCH) { - break; - } } event.stopPropagation(); if (event.target === this._canvas) {