Skip to content

Commit

Permalink
新增'获取当前触控数量' 的方法( touch-manager.ts)
Browse files Browse the repository at this point in the history
需要获取 当前有效的触控数量(按在屏幕上的手指数) 时使用.

使用ccLog 代替 console.log

Update touch-manager.ts

不依赖 ccLog ,直接禁用警告

Update touch-manager.ts

重新用回 ccLog

Update input.ts

Add some proxy methods of touchManager for getting touche infomation

Update cocos/input/input.ts

Co-authored-by: PP <[email protected]>

Update pal/input/touch-manager.ts

Co-authored-by: PP <[email protected]>

Update cocos/input/input.ts

Co-authored-by: PP <[email protected]>

Update cocos/input/input.ts

Co-authored-by: PP <[email protected]>

Update cocos/input/input.ts

Co-authored-by: PP <[email protected]>

Update input.ts

直接通过 touchManager._touchMap 获取数据

Update touch-manager.ts

将 _touchMap 暴露出去. 并且移除之前添加的 getTouchCount()方法

Update touch-manager.ts

直接使用 log()

Update input.ts

Update input.ts

获得 touch对象时, 不克隆.

Update touch-manager.ts

新增是否 克隆 touch对象的 参数.  不指定值时, 默认克隆

Update input.ts

微调注释, 避免歧义

Update input.ts

格式化

Update touch-manager.ts

移除多余的赋值操作

Update touch-manager.ts

移除无用的clone

Update touch-manager.ts

将 `_cloneTouch()` 设置为public, 暴露给 input 使用.

Update input.ts

在获取 touch 对象时, 可通过参数决定是否clone.

Update input.ts

更新注释

Update input.ts

优化格式和类型定义.

Update input.ts

优化格式

Update input.ts

格式化

Update touch-manager.ts

优化格式

Update touch-manager.ts

# Conflicts:
#	cocos/input/input.ts
  • Loading branch information
finscn authored and PPpro committed Sep 11, 2023
1 parent d7f90a3 commit 205cba2
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 39 deletions.
134 changes: 107 additions & 27 deletions cocos/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
*/

import { EDITOR_NOT_IN_PREVIEW, NATIVE } from 'internal:constants';
import { TouchInputSource, MouseInputSource, KeyboardInputSource, AccelerometerInputSource, GamepadInputDevice, HandleInputDevice, HMDInputDevice, HandheldInputDevice } from 'pal/input';
import { AccelerometerInputSource, GamepadInputDevice, HMDInputDevice, HandheldInputDevice, HandleInputDevice, KeyboardInputSource, MouseInputSource, TouchInputSource } from 'pal/input';
import { touchManager } from '../../pal/input/touch-manager';
import { sys, EventTarget } from '../core';
import { EventTarget, error, sys } from '../core';
import { Event, EventAcceleration, EventGamepad, EventHandle, EventHandheld, EventHMD, EventKeyboard, EventMouse, EventTouch, Touch } from './types';
import { InputEventType } from './types/event-enum';

Expand Down Expand Up @@ -62,7 +62,7 @@ class InputEventDispatcher implements IEventDispatcher {
}
}

const pointerEventTypeMap: Record<string, string> = {
const pointerEventTypeMap: Record<string, InputEventType> = {
[InputEventType.MOUSE_DOWN]: InputEventType.TOUCH_START,
[InputEventType.MOUSE_MOVE]: InputEventType.TOUCH_MOVE,
[InputEventType.MOUSE_UP]: InputEventType.TOUCH_END,
Expand Down Expand Up @@ -160,28 +160,40 @@ export class Input {
/**
* This should be a private method, but it's exposed for Editor Only.
*/
private _dispatchMouseDownEvent (nativeMouseEvent: any): void { this._mouseInput.dispatchMouseDownEvent?.(nativeMouseEvent); }
private _dispatchMouseDownEvent (nativeMouseEvent: any): void {
this._mouseInput.dispatchMouseDownEvent?.(nativeMouseEvent);
}
/**
* This should be a private method, but it's exposed for Editor Only.
*/
private _dispatchMouseMoveEvent (nativeMouseEvent: any): void { this._mouseInput.dispatchMouseMoveEvent?.(nativeMouseEvent); }
private _dispatchMouseMoveEvent (nativeMouseEvent: any): void {
this._mouseInput.dispatchMouseMoveEvent?.(nativeMouseEvent);
}
/**
* This should be a private method, but it's exposed for Editor Only.
*/
private _dispatchMouseUpEvent (nativeMouseEvent: any): void { this._mouseInput.dispatchMouseUpEvent?.(nativeMouseEvent); }
private _dispatchMouseUpEvent (nativeMouseEvent: any): void {
this._mouseInput.dispatchMouseUpEvent?.(nativeMouseEvent);
}
/**
* This should be a private method, but it's exposed for Editor Only.
*/
private _dispatchMouseScrollEvent (nativeMouseEvent: any): void { this._mouseInput.dispatchScrollEvent?.(nativeMouseEvent); }
private _dispatchMouseScrollEvent (nativeMouseEvent: any): void {
this._mouseInput.dispatchScrollEvent?.(nativeMouseEvent);
}

/**
* This should be a private method, but it's exposed for Editor Only.
*/
private _dispatchKeyboardDownEvent (nativeKeyboardEvent: any): void { this._keyboardInput.dispatchKeyboardDownEvent?.(nativeKeyboardEvent); }
private _dispatchKeyboardDownEvent (nativeKeyboardEvent: any): void {
this._keyboardInput.dispatchKeyboardDownEvent?.(nativeKeyboardEvent);
}
/**
* This should be a private method, but it's exposed for Editor Only.
*/
private _dispatchKeyboardUpEvent (nativeKeyboardEvent: any): void { this._keyboardInput.dispatchKeyboardUpEvent?.(nativeKeyboardEvent); }
private _dispatchKeyboardUpEvent (nativeKeyboardEvent: any): void {
this._keyboardInput.dispatchKeyboardUpEvent?.(nativeKeyboardEvent);
}

/**
* @en
Expand Down Expand Up @@ -229,6 +241,42 @@ export class Input {
}
this._eventTarget.off(eventType, callback, target);
}

/**
* @en
* Get touch object by touch ID.
* @zh
* 通过 touch ID 获取 touch对象。
* @param touchID
* @param clone - Whether to clone touch object
* @returns
*/
public getTouch (touchID: number, clone?: boolean): Touch | undefined {
const touch = touchManager._touchMap.get(touchID);
return touch ? (clone === false ? touch : touchManager._cloneTouch(touch)) : undefined;
}

/**
* @en
* Get all the current touches objects as array.
* @zh
* 获取当前 所有touch对象 的数组。
* @param clone - Whether to clone touch object
*/
public getAllTouches (clone?: boolean): Touch[] {
return touchManager.getAllTouches(clone);
}

/**
* @en
* Get the number of touches.
* @zh
* 获取当前 touch 对象的数量。
*/
public getTouchCount (): number {
return touchManager._touchMap.size;
}

/**
* @en
* Sets whether to enable the accelerometer event listener or not.
Expand Down Expand Up @@ -295,19 +343,27 @@ export class Input {
break;
}
} catch (e) {
console.error(`Error occurs in an event listener: ${event.type}`);
console.error(e);
error(`Error occurs in an event listener: ${event.type}`);
error(e);
}
}
}

private _registerEvent (): void {
if (sys.hasFeature(sys.Feature.INPUT_TOUCH)) {
const eventTouchList = this._eventTouchList;
this._touchInput.on(InputEventType.TOUCH_START, (event): void => { this._dispatchOrPushEventTouch(event, eventTouchList); });
this._touchInput.on(InputEventType.TOUCH_MOVE, (event): void => { this._dispatchOrPushEventTouch(event, eventTouchList); });
this._touchInput.on(InputEventType.TOUCH_END, (event): void => { this._dispatchOrPushEventTouch(event, eventTouchList); });
this._touchInput.on(InputEventType.TOUCH_CANCEL, (event): void => { this._dispatchOrPushEventTouch(event, eventTouchList); });
this._touchInput.on(InputEventType.TOUCH_START, (event): void => {
this._dispatchOrPushEventTouch(event, eventTouchList);
});
this._touchInput.on(InputEventType.TOUCH_MOVE, (event): void => {
this._dispatchOrPushEventTouch(event, eventTouchList);
});
this._touchInput.on(InputEventType.TOUCH_END, (event): void => {
this._dispatchOrPushEventTouch(event, eventTouchList);
});
this._touchInput.on(InputEventType.TOUCH_CANCEL, (event): void => {
this._dispatchOrPushEventTouch(event, eventTouchList);
});
}

if (sys.hasFeature(sys.Feature.EVENT_MOUSE)) {
Expand All @@ -328,42 +384,66 @@ export class Input {
this._simulateEventTouch(event);
this._dispatchOrPushEvent(event, eventMouseList);
});
this._mouseInput.on(InputEventType.MOUSE_WHEEL, (event): void => { this._dispatchOrPushEvent(event, eventMouseList); });
this._mouseInput.on(InputEventType.MOUSE_WHEEL, (event): void => {
this._dispatchOrPushEvent(event, eventMouseList);
});
}

if (sys.hasFeature(sys.Feature.EVENT_KEYBOARD)) {
const eventKeyboardList = this._eventKeyboardList;
this._keyboardInput.on(InputEventType.KEY_DOWN, (event): void => { this._dispatchOrPushEvent(event, eventKeyboardList); });
this._keyboardInput.on(InputEventType.KEY_PRESSING, (event): void => { this._dispatchOrPushEvent(event, eventKeyboardList); });
this._keyboardInput.on(InputEventType.KEY_UP, (event): void => { this._dispatchOrPushEvent(event, eventKeyboardList); });
this._keyboardInput.on(InputEventType.KEY_DOWN, (event): void => {
this._dispatchOrPushEvent(event, eventKeyboardList);
});
this._keyboardInput.on(InputEventType.KEY_PRESSING, (event): void => {
this._dispatchOrPushEvent(event, eventKeyboardList);
});
this._keyboardInput.on(InputEventType.KEY_UP, (event): void => {
this._dispatchOrPushEvent(event, eventKeyboardList);
});
}

if (sys.hasFeature(sys.Feature.EVENT_ACCELEROMETER)) {
const eventAccelerationList = this._eventAccelerationList;
this._accelerometerInput.on(InputEventType.DEVICEMOTION, (event): void => { this._dispatchOrPushEvent(event, eventAccelerationList); });
this._accelerometerInput.on(InputEventType.DEVICEMOTION, (event): void => {
this._dispatchOrPushEvent(event, eventAccelerationList);
});
}

if (sys.hasFeature(sys.Feature.EVENT_GAMEPAD)) {
const eventGamepadList = this._eventGamepadList;
GamepadInputDevice._on(InputEventType.GAMEPAD_CHANGE, (event): void => { this._dispatchOrPushEvent(event, eventGamepadList); });
GamepadInputDevice._on(InputEventType.GAMEPAD_INPUT, (event): void => { this._dispatchOrPushEvent(event, eventGamepadList); });
GamepadInputDevice._on(InputEventType.HANDLE_POSE_INPUT, (event): void => { this._dispatchOrPushEvent(event, eventGamepadList); });
GamepadInputDevice._on(InputEventType.GAMEPAD_CHANGE, (event): void => {
this._dispatchOrPushEvent(event, eventGamepadList);
});
GamepadInputDevice._on(InputEventType.GAMEPAD_INPUT, (event): void => {
this._dispatchOrPushEvent(event, eventGamepadList);
});
GamepadInputDevice._on(InputEventType.HANDLE_POSE_INPUT, (event): void => {
this._dispatchOrPushEvent(event, eventGamepadList);
});
}

if (sys.hasFeature(sys.Feature.EVENT_HANDLE)) {
const eventHandleList = this._eventHandleList;
this._handleInput._on(InputEventType.HANDLE_INPUT, (event): void => { this._dispatchOrPushEvent(event, eventHandleList); });
this._handleInput._on(InputEventType.HANDLE_POSE_INPUT, (event): void => { this._dispatchOrPushEvent(event, eventHandleList); });
this._handleInput._on(InputEventType.HANDLE_INPUT, (event): void => {
this._dispatchOrPushEvent(event, eventHandleList);
});
this._handleInput._on(InputEventType.HANDLE_POSE_INPUT, (event): void => {
this._dispatchOrPushEvent(event, eventHandleList);
});
}

if (sys.hasFeature(sys.Feature.EVENT_HMD)) {
const eventHMDList = this._eventHMDList;
this._hmdInput._on(InputEventType.HMD_POSE_INPUT, (event): void => { this._dispatchOrPushEvent(event, eventHMDList); });
this._hmdInput._on(InputEventType.HMD_POSE_INPUT, (event): void => {
this._dispatchOrPushEvent(event, eventHMDList);
});
}

if (sys.hasFeature(sys.Feature.EVENT_HANDHELD)) {
const eventHandheldList = this._eventHandheldList;
this._handheldInput._on(InputEventType.HANDHELD_POSE_INPUT, (event): void => { this._dispatchOrPushEvent(event, eventHandheldList); });
this._handheldInput._on(InputEventType.HANDHELD_POSE_INPUT, (event): void => {
this._dispatchOrPushEvent(event, eventHandheldList);
});
}
}

Expand Down
24 changes: 12 additions & 12 deletions pal/input/touch-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
THE SOFTWARE.
*/

import { Vec2 } from '../../cocos/core/math/vec2';
import { log } from '../../cocos/core/platform/debug';
import { macro } from '../../cocos/core/platform/macro';
import { Touch } from '../../cocos/input/types';
import { Vec2 } from '../../cocos/core/math/vec2';

const tempVec2 = new Vec2();

class TouchManager {
/**
* A map from touch ID to touch object.
*/
private _touchMap: Map<number, Touch>;
public _touchMap: Map<number, Touch>;
private readonly _maxTouches = 8;

constructor () {
Expand All @@ -44,7 +45,7 @@ class TouchManager {
* @param touch
* @returns
*/
private _cloneTouch (touch: Touch): Touch {
public _cloneTouch (touch: Touch): Touch {
const touchID = touch.getID();
touch.getStartLocation(tempVec2);
const clonedTouch = new Touch(tempVec2.x, tempVec2.y, touchID);
Expand All @@ -68,18 +69,18 @@ class TouchManager {
*/
private _createTouch (touchID: number, x: number, y: number): Touch | undefined {
if (this._touchMap.has(touchID)) {
console.log('Cannot create the same touch object.');
log('Cannot create the same touch object.');
return undefined;
}
const checkResult = this._checkTouchMapSizeMoreThanMax(touchID);
if (checkResult) {
console.log('The touches is more than MAX_TOUCHES.'); // TODO: logID 2300
log('The touches is more than MAX_TOUCHES.'); // TODO: logID 2300
return undefined;
}
const touch = new Touch(x, y, touchID);
this._touchMap.set(touchID, touch);
this._updateTouch(touch, x, y);
return this._cloneTouch(touch);
return touch;
}

/**
Expand All @@ -99,26 +100,25 @@ class TouchManager {
* @param touchID
* @returns
*/
public getTouch (touchID: number, x: number, y: number): Touch | undefined {
public getTouch (touchID: number, x: number, y: number, clone?: boolean): Touch | undefined {
let touch = this._touchMap.get(touchID);
if (!touch) {
touch = this._createTouch(touchID, x, y);
} else {
this._updateTouch(touch, x, y);
}
return touch ? this._cloneTouch(touch) : undefined;
return touch ? (clone === false ? touch : this._cloneTouch(touch)) : undefined;
}

/**
* Get all the current touches objects.
* @returns
*/
public getAllTouches (): Touch[] {
public getAllTouches (clone?: boolean): Touch[] {
const touches: Touch[] = [];
this._touchMap.forEach((touch) => {
if (touch) {
const clonedTouch = this._cloneTouch(touch);
touches.push(clonedTouch);
touches.push(clone === false ? touch : this._cloneTouch(touch));
}
});
return touches;
Expand Down Expand Up @@ -148,7 +148,7 @@ class TouchManager {
const now = performance.now();
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()}.`);
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());
}
Expand Down

0 comments on commit 205cba2

Please sign in to comment.