Files
cocos4/pal/input/native/touch.ts
T
PP 86f20da0f0 support pal/input (#8472)
* support pal/input

update

* update

* fix esLint

* support minigame

* support  pal/input on  minigame

* remove minigame.isSubContext

* support native

update

* deprecate static property on EventMouse

* deprecated static property in  EventTouch

* remove TODO

* remove  x y position in MouseWheel Event

* remove TODO

* remove temp code

* fix esLint

* update AccelerometerIntervalMode

* remove legacy code in JSB

* fix esLint

* optimize AccelerometerInputSource.setInterval

* fix esLint

* optimize code

* add more details for documentation

* update

* don't pass game.canvas to inputManager.registerSystemEvent

* optimize

* Revert "remove  x y position in MouseWheel Event"

This reverts commit b521ec4948d693b420887cdeb2f5f3afc1500066.

# Conflicts:
#	@types/pal/input.d.ts

* update

* fix esLint

* fix esLint
2021-04-19 16:24:42 +08:00

69 lines
2.5 KiB
TypeScript

import { TouchCallback, TouchData, TouchInputEvent } from 'pal/input';
import { system } from 'pal/system';
import { Rect, Vec2 } from '../../../cocos/core/math';
import { EventTarget } from '../../../cocos/core/event/event-target';
import { EventTouch } from '../../../cocos/core/platform/event-manager/events';
import { legacyCC } from '../../../cocos/core/global-exports';
import { SystemEventType } from '../../../cocos/core/platform/event-manager/event-enum';
export class TouchInputSource {
public support: boolean;
private _eventTarget: EventTarget = new EventTarget();
constructor () {
this.support = true;
this._registerEvent();
}
private _registerEvent () {
jsb.onTouchStart = this._createCallback(SystemEventType.TOUCH_START);
jsb.onTouchMove = this._createCallback(SystemEventType.TOUCH_MOVE);
jsb.onTouchEnd = this._createCallback(SystemEventType.TOUCH_END);
jsb.onTouchCancel = this._createCallback(SystemEventType.TOUCH_CANCEL);
}
private _createCallback (eventType: string) {
return (touchList: TouchList) => {
const touchDataList: TouchData[] = [];
const length = touchList.length;
const viewSize = system.getViewSize();
for (let i = 0; i < length; ++i) {
const touch = touchList[i];
const location = this._getLocation(touch);
const x = location.x;
const y = viewSize.height - location.y;
const touchData: TouchData = {
identifier: touch.identifier,
x,
y,
force: touch.force,
};
touchDataList.push(touchData);
}
const inputEvent: TouchInputEvent = {
type: eventType,
changedTouches: touchDataList,
timestamp: performance.now(),
};
this._eventTarget.emit(eventType, inputEvent);
};
}
private _getLocation (event: Touch): Vec2 {
return new Vec2(event.clientX, event.clientY);
}
public onStart (cb: TouchCallback) {
this._eventTarget.on(SystemEventType.TOUCH_START, cb);
}
public onMove (cb: TouchCallback) {
this._eventTarget.on(SystemEventType.TOUCH_MOVE, cb);
}
public onEnd (cb: TouchCallback) {
this._eventTarget.on(SystemEventType.TOUCH_END, cb);
}
public onCancel (cb: TouchCallback) {
this._eventTarget.on(SystemEventType.TOUCH_CANCEL, cb);
}
}