Files
cocos4/pal/input/web/touch-input.ts
T
fqamic d4055f7e32 new basic capabilities of supporting handset ar and webxr (#13420)
* feat: add the basic capability of handset ar native

* feat: add handset input device, for ar camera pose

* feat: native pack tool supports xr platform name

* feat: web gl context supports webxr

* feat: webxr has been converted to screen coordinates via camera

* fix: pr ts-expect-error undefined xr

* feat: support webxr, and remove ar redundant code

* fix:xr-spaces gradlew permission error

* feat: revert redundant ar engine feature

* fix: eslint check error

* feat: remove redundant code

* fix: eslint check warning

* adjust armodule process in forward stage, mtl texture and jsb

* fix: exlint warning, and add license

* adjust ar lib and interfaces

* change webxr check interface

* merge external arlib header and move it to engine

* improve: update native external version

* move ar background stage to xr/rendering and move its init to webxr device in xr plugin

* fix format error

* fix format error

* adjust jsb_ar_manual, ARModule, ARBackground

* adjust arbackground

Co-authored-by: Parryzhai <1306895355@qq.com>
Co-authored-by: MoHai <1192652394@qq.com>
2022-11-24 13:33:50 +08:00

106 lines
4.3 KiB
TypeScript

import { TouchCallback } from 'pal/input';
import { EDITOR, TEST } from 'internal:constants';
import { systemInfo } from 'pal/system-info';
import { screenAdapter } from 'pal/screen-adapter';
import { Rect, Vec2 } from '../../../cocos/core/math';
import { EventTarget } from '../../../cocos/core/event';
import { Touch, EventTouch } from '../../../cocos/input/types';
import { touchManager } from '../touch-manager';
import { macro } from '../../../cocos/core/platform/macro';
import { InputEventType } from '../../../cocos/input/types/event-enum';
import { Feature } from '../../system-info/enum-type';
export class TouchInputSource {
private _canvas?: HTMLCanvasElement;
private _eventTarget: EventTarget = new EventTarget();
constructor () {
if (systemInfo.hasFeature(Feature.INPUT_TOUCH)) {
this._canvas = document.getElementById('GameCanvas') as HTMLCanvasElement;
if (!this._canvas && !TEST && !EDITOR) {
console.warn('failed to access canvas');
}
// In Editor, we don't receive touch event but maybe receive simulated touch event.
if (!EDITOR) {
this._registerEvent();
}
}
}
private _registerEvent () {
// IDEA: need to register on window ?
this._canvas?.addEventListener('touchstart', this._createCallback(InputEventType.TOUCH_START));
this._canvas?.addEventListener('touchmove', this._createCallback(InputEventType.TOUCH_MOVE));
this._canvas?.addEventListener('touchend', this._createCallback(InputEventType.TOUCH_END));
this._canvas?.addEventListener('touchcancel', this._createCallback(InputEventType.TOUCH_CANCEL));
}
private _createCallback (eventType: InputEventType) {
return (event: TouchEvent) => {
const canvasRect = this._getCanvasRect();
const handleTouches: Touch[] = [];
const length = event.changedTouches.length;
for (let i = 0; i < length; ++i) {
const changedTouch = event.changedTouches[i];
const touchID = changedTouch.identifier;
if (touchID === null) {
continue;
}
const location = this._getLocation(changedTouch, canvasRect);
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);
}
event.stopPropagation();
if (event.target === this._canvas) {
event.preventDefault();
}
if (eventType === InputEventType.TOUCH_START) {
this._canvas?.focus();
}
if (handleTouches.length > 0) {
const eventTouch = new EventTouch(handleTouches, false, eventType,
macro.ENABLE_MULTI_TOUCH ? touchManager.getAllTouches() : handleTouches);
this._eventTarget.emit(eventType, eventTouch);
}
};
}
private _getCanvasRect (): Rect {
const canvas = this._canvas;
const box = canvas?.getBoundingClientRect();
if (box) {
return new Rect(box.x, box.y, box.width, box.height);
}
return new Rect(0, 0, 0, 0);
}
private _getLocation (touch: globalThis.Touch, canvasRect: Rect): Vec2 {
// webxr has been converted to screen coordinates via camera
if (globalThis.__globalXR.ar && globalThis.__globalXR.ar.isWebXR()) {
return new Vec2(touch.clientX, touch.clientY);
}
let x = touch.clientX - canvasRect.x;
let y = canvasRect.y + canvasRect.height - touch.clientY;
if (screenAdapter.isFrameRotated) {
const tmp = x;
x = canvasRect.height - y;
y = tmp;
}
const dpr = screenAdapter.devicePixelRatio;
x *= dpr;
y *= dpr;
return new Vec2(x, y);
}
public on (eventType: InputEventType, callback: TouchCallback, target?: any) {
this._eventTarget.on(eventType, callback, target);
}
}