mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
69 lines
2.8 KiB
TypeScript
69 lines
2.8 KiB
TypeScript
import { TouchCallback } from 'pal/input';
|
|
import { screenAdapter } from 'pal/screen-adapter';
|
|
import { Size, Vec2 } from '../../../cocos/core/math';
|
|
import { EventTarget } from '../../../cocos/core/event';
|
|
import { EventTouch, Touch } from '../../../cocos/input/types';
|
|
import { touchManager } from '../touch-manager';
|
|
import { macro } from '../../../cocos/core/platform/macro';
|
|
import { InputEventType } from '../../../cocos/input/types/event-enum';
|
|
|
|
declare const jsb: any;
|
|
|
|
export class TouchInputSource {
|
|
private _eventTarget: EventTarget = new EventTarget();
|
|
private _windowManager: any;
|
|
|
|
constructor () {
|
|
this._registerEvent();
|
|
this._windowManager = jsb.ISystemWindowManager.getInstance();
|
|
}
|
|
|
|
private _registerEvent () {
|
|
jsb.onTouchStart = this._createCallback(InputEventType.TOUCH_START);
|
|
jsb.onTouchMove = this._createCallback(InputEventType.TOUCH_MOVE);
|
|
jsb.onTouchEnd = this._createCallback(InputEventType.TOUCH_END);
|
|
jsb.onTouchCancel = this._createCallback(InputEventType.TOUCH_CANCEL);
|
|
}
|
|
|
|
private _createCallback (eventType: InputEventType) {
|
|
return (changedTouches: TouchList, windowId: number) => {
|
|
const handleTouches: Touch[] = [];
|
|
const length = changedTouches.length;
|
|
const windowSize = this._windowManager.getWindow(windowId).getViewSize();
|
|
for (let i = 0; i < length; ++i) {
|
|
const changedTouch = changedTouches[i];
|
|
const touchID = changedTouch.identifier;
|
|
if (touchID === null) {
|
|
continue;
|
|
}
|
|
const location = this._getLocation(changedTouch, windowSize);
|
|
const touch = touchManager.getTouch(touchID, location.x, location.y);
|
|
if (!touch) {
|
|
continue;
|
|
}
|
|
if (eventType === InputEventType.TOUCH_END || eventType === InputEventType.TOUCH_CANCEL) {
|
|
touchManager.releaseTouch(touchID);
|
|
}
|
|
handleTouches.push(touch);
|
|
}
|
|
if (handleTouches.length > 0) {
|
|
const eventTouch = new EventTouch(handleTouches, false, eventType,
|
|
macro.ENABLE_MULTI_TOUCH ? touchManager.getAllTouches() : handleTouches);
|
|
eventTouch.windowId = windowId;
|
|
this._eventTarget.emit(eventType, eventTouch);
|
|
}
|
|
};
|
|
}
|
|
|
|
private _getLocation (touch: globalThis.Touch, windowSize: Size): Vec2 {
|
|
const dpr = screenAdapter.devicePixelRatio;
|
|
const x = touch.clientX * dpr;
|
|
const y = windowSize.height - touch.clientY * dpr;
|
|
return new Vec2(x, y);
|
|
}
|
|
|
|
public on (eventType: InputEventType, callback: TouchCallback, target?: any) {
|
|
this._eventTarget.on(eventType, callback, target);
|
|
}
|
|
}
|