mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
migrate Engine Input Event into PAL (#9320)
* optimize input module * rename * fix CI * update input-box.d.ts
This commit is contained in:
Vendored
+34
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Class designed for UI input box.
|
||||
* TODO: add more description for this class
|
||||
*/
|
||||
export class InputBox {
|
||||
/**
|
||||
* Asynchronously show the UI input box, also show the soft keyboard on mobile.
|
||||
*/
|
||||
public show (): Promise<void>;
|
||||
/**
|
||||
* Asynchronously hide the UI input box, also hide the soft keyboard on mobile.
|
||||
*/
|
||||
public hide (): Promise<void>;
|
||||
/**
|
||||
* Register the UI input box change event callback.
|
||||
* @param cb
|
||||
*/
|
||||
public onChange (cb: () => void);
|
||||
/**
|
||||
* Register the UI input box complete event callback.
|
||||
* @param cb
|
||||
*/
|
||||
public onComplete (cb: () => void);
|
||||
/**
|
||||
* Unregister the UI input box change event callback.
|
||||
* @param cb If not specified, all callback would be unregistered.
|
||||
*/
|
||||
public offChange (cb?: () => void);
|
||||
/**
|
||||
* Unregister the UI input box complete event callback.
|
||||
* @param cb If not specified, all callback would be unregistered.
|
||||
*/
|
||||
public offComplete (cb?: () => void);
|
||||
}
|
||||
Vendored
+9
-164
@@ -1,14 +1,4 @@
|
||||
declare module 'pal/input' {
|
||||
interface BaseInputEvent {
|
||||
/**
|
||||
* Type of the input event used to quickly distinguish between event types.
|
||||
*/
|
||||
readonly type: import('cocos/input/types').SystemEventTypeUnion;
|
||||
/**
|
||||
* Timestamp when the input event is triggered.
|
||||
*/
|
||||
readonly timestamp: number;
|
||||
}
|
||||
/**
|
||||
* Basic class for all input sources.
|
||||
*/
|
||||
@@ -19,33 +9,7 @@ declare module 'pal/input' {
|
||||
public readonly support: boolean;
|
||||
}
|
||||
|
||||
interface TouchData {
|
||||
/**
|
||||
* A unique identifier for this touch.
|
||||
* A given touch point will have the same identifier for the duration of its movement around the surface.
|
||||
* This lets you ensure that you're tracking the same touch all the time.
|
||||
*/
|
||||
readonly identifier: number;
|
||||
/**
|
||||
* The x-coordinate of the touch whose origin is at the bottom-left of the canvas.
|
||||
*/
|
||||
readonly x: number;
|
||||
/**
|
||||
* The y-coordinate of the touch whose origin is at the bottom-left of the canvas.
|
||||
*/
|
||||
readonly y: number;
|
||||
/**
|
||||
* The amount of pressure being applied to the surface by the user, ranged from 0 to 1.
|
||||
*/
|
||||
readonly force: number;
|
||||
}
|
||||
export interface TouchInputEvent extends BaseInputEvent {
|
||||
/**
|
||||
* Individual points of contact whose states changed between the previous touch event and this one.
|
||||
*/
|
||||
readonly changedTouches: TouchData[];
|
||||
}
|
||||
type TouchCallback = (res: TouchInputEvent) => void;
|
||||
type TouchCallback = (res: import('cocos/input/types').EventTouch) => void;
|
||||
/**
|
||||
* Class designed for touch input.
|
||||
*/
|
||||
@@ -72,36 +36,7 @@ declare module 'pal/input' {
|
||||
public onCancel (cb: TouchCallback);
|
||||
}
|
||||
|
||||
export interface MouseInputEvent extends BaseInputEvent {
|
||||
/**
|
||||
* The x-coordinate of the mouse whose origin is at the bottom-left of the canvas.
|
||||
*/
|
||||
readonly x: number;
|
||||
/**
|
||||
* The y-coordinate of the mouse whose origin is at the bottom-left of the canvas.
|
||||
*/
|
||||
readonly y: number;
|
||||
/**
|
||||
* The pressed mouse button during the related mouse event.
|
||||
* The valid value maybe `EventMouse.BUTTON_LEFT`, `EventMouse.BUTTON_RIGHT` and `EventMouse.BUTTON_MIDDLE`.
|
||||
*/
|
||||
readonly button: number;
|
||||
// this is web only property, should be removed in the futrure.
|
||||
movementX?: number;
|
||||
movementY?: number;
|
||||
}
|
||||
type MouseCallback = (res: MouseInputEvent) => void;
|
||||
export interface MouseWheelInputEvent extends MouseInputEvent {
|
||||
/**
|
||||
* The horizontal scroll amount.
|
||||
*/
|
||||
readonly deltaX: number;
|
||||
/**
|
||||
* The vertical scroll amount.
|
||||
*/
|
||||
readonly deltaY: number;
|
||||
}
|
||||
type MouseWheelCallback = (res: MouseWheelInputEvent) => void;
|
||||
type MouseCallback = (res: import('cocos/input/types').EventMouse) => void;
|
||||
/**
|
||||
* Class designed for mouse input.
|
||||
*/
|
||||
@@ -125,16 +60,10 @@ declare module 'pal/input' {
|
||||
* Register the mouse wheel event callback.
|
||||
* @param cb
|
||||
*/
|
||||
public onWheel (cb: MouseWheelCallback);
|
||||
public onWheel (cb: MouseCallback);
|
||||
}
|
||||
|
||||
export interface KeyboardInputEvent extends BaseInputEvent {
|
||||
/**
|
||||
* Numerical code identifying the unique value of the pressed key.
|
||||
*/
|
||||
readonly code: import('cocos/input/types').KeyCode;
|
||||
}
|
||||
type KeyboardCallback = (res: KeyboardInputEvent) => void;
|
||||
type KeyboardCallback = (res: import('cocos/input/types').EventKeyboard) => void;
|
||||
/**
|
||||
* Class Designed for keyboard input.
|
||||
*/
|
||||
@@ -146,7 +75,7 @@ declare module 'pal/input' {
|
||||
public onDown (cb: KeyboardCallback);
|
||||
/**
|
||||
* Register the key pressing event callback.
|
||||
* NOTE: Compability for the deprecated KEY_DOWN event type. It should be removed in the future.
|
||||
* NOTE: Compatibility for the deprecated KEY_DOWN event type. It should be removed in the future.
|
||||
* @param cb
|
||||
*/
|
||||
public onPressing (cb: KeyboardCallback);
|
||||
@@ -164,21 +93,7 @@ declare module 'pal/input' {
|
||||
// TODO: add more details for GamepadInputSource class
|
||||
}
|
||||
|
||||
export interface AccelerometerInputEvent extends BaseInputEvent {
|
||||
/**
|
||||
* The current x value of the accelerometer ranged from -1 to 1.
|
||||
*/
|
||||
readonly x: number;
|
||||
/**
|
||||
* The current y value of the accelerometer ranged from -1 to 1.
|
||||
*/
|
||||
readonly y: number;
|
||||
/**
|
||||
* The current z value of the accelerometer ranged from -1 to 1.
|
||||
*/
|
||||
readonly z: number;
|
||||
}
|
||||
type AccelerometerCallback = (res: AccelerometerInputEvent) => void;
|
||||
type AccelerometerCallback = (res: import('cocos/input/types').EventAcceleration) => void;
|
||||
/**
|
||||
* Class designed for accelerometer input
|
||||
*/
|
||||
@@ -195,84 +110,14 @@ declare module 'pal/input' {
|
||||
public stop ();
|
||||
/**
|
||||
* Set interval of the accelerometer callback.
|
||||
* The interval is in mileseconds.
|
||||
* @param intervalInMileseconds interval in mileseconds
|
||||
* The interval is in mile seconds.
|
||||
* @param intervalInMileSeconds interval in mile seconds.
|
||||
*/
|
||||
public setInterval (intervalInMileseconds: number);
|
||||
public setInterval (intervalInMileSeconds: number);
|
||||
/**
|
||||
* Register the accelerometer change event callback.
|
||||
* @param cb
|
||||
*/
|
||||
public onChange (cb: AccelerometerCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Class designed for UI input box.
|
||||
* TODO: add more description for this class
|
||||
*/
|
||||
export class InputBox extends BaseInputSource {
|
||||
/**
|
||||
* Asynchronously show the UI input box, also show the soft keyboard on mobile.
|
||||
*/
|
||||
public show (): Promise<void>;
|
||||
/**
|
||||
* Asynchronously hide the UI input box, also hide the soft keyboard on mobile.
|
||||
*/
|
||||
public hide (): Promise<void>;
|
||||
/**
|
||||
* Register the UI input box change event callback.
|
||||
* @param cb
|
||||
*/
|
||||
public onChange (cb: ()=>void);
|
||||
/**
|
||||
* Register the UI input box complete event callback.
|
||||
* @param cb
|
||||
*/
|
||||
public onComplete (cb: ()=>void);
|
||||
/**
|
||||
* Unregister the UI input box change event callback.
|
||||
* @param cb If not specified, all callback would be unregistered.
|
||||
*/
|
||||
public offChange (cb?: ()=>void);
|
||||
/**
|
||||
* Unregister the UI input box complete event callback.
|
||||
* @param cb If not specified, all callback would be unregistered.
|
||||
*/
|
||||
public offComplete (cb?: ()=>void);
|
||||
}
|
||||
|
||||
/**
|
||||
* Class designed to manage all input sources.
|
||||
*/
|
||||
class Input {
|
||||
private _touch: TouchInputSource;
|
||||
private _mouse: MouseInputSource;
|
||||
private _keyboard: KeyboardInputSource;
|
||||
private _accelerometer: AccelerometerInputSource;
|
||||
private _inputBox: InputBox;
|
||||
|
||||
private _touchEvents: TouchInputEvent[];
|
||||
private _mouseEvents: MouseInputEvent[];
|
||||
private _keyboardEvents: KeyboardInputEvent[];
|
||||
private _accelerometerEvents: AccelerometerInputEvent[];
|
||||
|
||||
public pollTouchEvents (): TouchInputEvent[];
|
||||
public pollMouseEvents (): MouseInputEvent[];
|
||||
public pollKeyboardEvents (): KeyboardInputEvent[];
|
||||
public pollAccelerometerEvents (): AccelerometerInputEvent[];
|
||||
|
||||
public startAccelerometer (): void;
|
||||
public stopAccelerometer (): void;
|
||||
public setAccelerometerInterval (interval: number): void;
|
||||
|
||||
// // input box
|
||||
// public showInputBox (): Promise<void>;
|
||||
// public hideInputBox (): Promise<void>;
|
||||
// public onInputBoxChange (cb: Function); // TODO: don't use Function
|
||||
// public onInputBoxComplete (cb: Function);
|
||||
|
||||
// public pollEvent(): BaseInputEvent | undefined;
|
||||
}
|
||||
|
||||
export const input: Input;
|
||||
}
|
||||
|
||||
+103
-271
@@ -29,17 +29,10 @@
|
||||
* @hidden
|
||||
*/
|
||||
|
||||
import { AccelerometerInputEvent, input, MouseInputEvent, MouseWheelInputEvent, TouchInputEvent, KeyboardInputEvent, MouseInputSource, TouchData } from 'pal/input';
|
||||
import { Vec2 } from '../core/math/index';
|
||||
import { macro } from '../core/platform/macro';
|
||||
import { eventManager } from './event-manager';
|
||||
import { EventAcceleration, EventKeyboard, EventMouse, EventTouch, Touch, Acceleration, SystemEventType } from './types';
|
||||
import { legacyCC } from '../core/global-exports';
|
||||
import { TouchInputSource, MouseInputSource, KeyboardInputSource, AccelerometerInputSource } from 'pal/input';
|
||||
import { touchManager } from '../../pal/input/touch-manager';
|
||||
|
||||
interface IView {
|
||||
_devicePixelRatio: number;
|
||||
}
|
||||
import { eventManager } from './event-manager';
|
||||
import { EventAcceleration, EventKeyboard, EventMouse, EventTouch, SystemEventType, Touch } from './types';
|
||||
|
||||
const pointerEventTypeMap = {
|
||||
[SystemEventType.MOUSE_DOWN]: SystemEventType.TOUCH_START,
|
||||
@@ -51,295 +44,134 @@ const pointerEventTypeMap = {
|
||||
* This class manages all events of input. include: touch, mouse, accelerometer, keyboard
|
||||
*/
|
||||
class InputManager {
|
||||
private _prevMousePoint = new Vec2();
|
||||
public _touch = new TouchInputSource();
|
||||
public _mouse = new MouseInputSource();
|
||||
public _keyboard = new KeyboardInputSource();
|
||||
public _accelerometer = new AccelerometerInputSource();
|
||||
|
||||
// TODO: remove this property
|
||||
private _glView: IView | null = null;
|
||||
private _eventTouchList: EventTouch[] = [];
|
||||
private _eventMouseList: EventMouse[] = [];
|
||||
private _eventKeyboardList: EventKeyboard[] = [];
|
||||
private _eventAccelerationList: EventAcceleration[] = [];
|
||||
|
||||
private _needSimulateTouchMoveEvent = false;
|
||||
|
||||
constructor () {
|
||||
this._registerEvent();
|
||||
}
|
||||
|
||||
private _simulateEventTouch (eventMouse: EventMouse) {
|
||||
const eventType = pointerEventTypeMap[eventMouse.type];
|
||||
const touchID = 0;
|
||||
const touch = touchManager.getTouch(touchID, eventMouse.getLocationX(), eventMouse.getLocationY());
|
||||
if (!touch) {
|
||||
return;
|
||||
}
|
||||
const changedTouches = [touch];
|
||||
const eventTouch = new EventTouch(changedTouches, false, eventType, changedTouches);
|
||||
if (eventType === SystemEventType.TOUCH_END) {
|
||||
touchManager.releaseTouch(touchID);
|
||||
}
|
||||
this._eventTouchList.push(eventTouch);
|
||||
}
|
||||
|
||||
private _registerEvent () {
|
||||
if (this._touch.support) {
|
||||
const eventTouchList = this._eventTouchList;
|
||||
this._touch.onStart((event) => { eventTouchList.push(event); });
|
||||
this._touch.onMove((event) => { eventTouchList.push(event); });
|
||||
this._touch.onEnd((event) => { eventTouchList.push(event); });
|
||||
this._touch.onCancel((event) => { eventTouchList.push(event); });
|
||||
}
|
||||
|
||||
if (this._mouse.support) {
|
||||
const eventMouseList = this._eventMouseList;
|
||||
this._mouse.onDown((event) => {
|
||||
this._needSimulateTouchMoveEvent = true;
|
||||
this._simulateEventTouch(event);
|
||||
eventMouseList.push(event);
|
||||
});
|
||||
this._mouse.onMove((event) => {
|
||||
if (this._needSimulateTouchMoveEvent) {
|
||||
this._simulateEventTouch(event);
|
||||
}
|
||||
eventMouseList.push(event);
|
||||
});
|
||||
this._mouse.onUp((event) => {
|
||||
this._needSimulateTouchMoveEvent = false;
|
||||
this._simulateEventTouch(event);
|
||||
eventMouseList.push(event);
|
||||
});
|
||||
this._mouse.onWheel((event) => { eventMouseList.push(event); });
|
||||
}
|
||||
|
||||
if (this._keyboard.support) {
|
||||
const eventKeyboardList = this._eventKeyboardList;
|
||||
// this._keyboard.onDown((event) => { keyboardEvents.push(event); });
|
||||
this._keyboard.onPressing((event) => { eventKeyboardList.push(event); });
|
||||
this._keyboard.onUp((event) => { eventKeyboardList.push(event); });
|
||||
}
|
||||
|
||||
if (this._accelerometer.support) {
|
||||
const eventAccelerationList = this._eventAccelerationList;
|
||||
this._accelerometer.onChange((event) => { eventAccelerationList.push(event); });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear events when game is resumed.
|
||||
*/
|
||||
public clearEvents () {
|
||||
input.pollMouseEvents();
|
||||
input.pollTouchEvents();
|
||||
input.pollKeyboardEvents();
|
||||
input.pollAccelerometerEvents();
|
||||
this._eventMouseList.length = 0;
|
||||
this._eventTouchList.length = 0;
|
||||
this._eventKeyboardList.length = 0;
|
||||
this._eventAccelerationList.length = 0;
|
||||
}
|
||||
|
||||
public frameDispatchEvents () {
|
||||
const mouseEvents = input.pollMouseEvents();
|
||||
const eventMouseList = this._eventMouseList;
|
||||
// TODO: culling event queue
|
||||
for (let i = 0, length = mouseEvents.length; i < length; ++i) {
|
||||
const mouseEvent = mouseEvents[i];
|
||||
this._dispatchMouseEvent(mouseEvent);
|
||||
for (let i = 0, length = eventMouseList.length; i < length; ++i) {
|
||||
const eventMouse = eventMouseList[i];
|
||||
eventManager.dispatchEvent(eventMouse);
|
||||
}
|
||||
|
||||
const touchEvents = input.pollTouchEvents();
|
||||
const eventTouchList = this._eventTouchList;
|
||||
// TODO: culling event queue
|
||||
for (let i = 0, length = touchEvents.length; i < length; ++i) {
|
||||
const touchEvent = touchEvents[i];
|
||||
this._dispatchTouchEvent(touchEvent);
|
||||
for (let i = 0, length = eventTouchList.length; i < length; ++i) {
|
||||
const eventTouch = eventTouchList[i];
|
||||
eventManager.dispatchEvent(eventTouch);
|
||||
}
|
||||
|
||||
const keyboardEvents = input.pollKeyboardEvents();
|
||||
const eventKeyboardList = this._eventKeyboardList;
|
||||
// TODO: culling event queue
|
||||
for (let i = 0, length = keyboardEvents.length; i < length; ++i) {
|
||||
const keyboardEvent = keyboardEvents[i];
|
||||
this._dispatchKeyboardEvent(keyboardEvent);
|
||||
for (let i = 0, length = eventKeyboardList.length; i < length; ++i) {
|
||||
const eventKeyboard = eventKeyboardList[i];
|
||||
eventManager.dispatchEvent(eventKeyboard);
|
||||
}
|
||||
|
||||
const accelerometerEvents = input.pollAccelerometerEvents();
|
||||
const eventAccelerationList = this._eventAccelerationList;
|
||||
// TODO: culling event queue
|
||||
for (let i = 0, length = accelerometerEvents.length; i < length; ++i) {
|
||||
const accelerometerEvent = accelerometerEvents[i];
|
||||
this._dispatchAccelerometerEvent(accelerometerEvent);
|
||||
for (let i = 0, length = eventAccelerationList.length; i < length; ++i) {
|
||||
const eventAcceleration = eventAccelerationList[i];
|
||||
eventManager.dispatchEvent(eventAcceleration);
|
||||
}
|
||||
|
||||
this.clearEvents();
|
||||
}
|
||||
|
||||
// #region Mouse Handle
|
||||
private _dispatchMouseEvent (inputEvent: MouseInputEvent) {
|
||||
let touchInputEvent: TouchInputEvent;
|
||||
let mouseEvent: EventMouse;
|
||||
switch (inputEvent.type) {
|
||||
case SystemEventType.MOUSE_DOWN:
|
||||
mouseEvent = this._getMouseEvent(inputEvent);
|
||||
touchInputEvent = this._simulateTouchInputEvent(inputEvent);
|
||||
this._handleTouchesStart(touchInputEvent);
|
||||
eventManager.dispatchEvent(mouseEvent);
|
||||
break;
|
||||
case SystemEventType.MOUSE_MOVE:
|
||||
mouseEvent = this._getMouseEvent(inputEvent);
|
||||
touchInputEvent = this._simulateTouchInputEvent(inputEvent);
|
||||
this._handleTouchesMove(touchInputEvent);
|
||||
eventManager.dispatchEvent(mouseEvent);
|
||||
break;
|
||||
case SystemEventType.MOUSE_UP:
|
||||
mouseEvent = this._getMouseEvent(inputEvent);
|
||||
touchInputEvent = this._simulateTouchInputEvent(inputEvent);
|
||||
this._handleTouchesEnd(touchInputEvent);
|
||||
eventManager.dispatchEvent(mouseEvent);
|
||||
break;
|
||||
case SystemEventType.MOUSE_WHEEL:
|
||||
mouseEvent = this._getMouseEvent(inputEvent);
|
||||
mouseEvent.setScrollData((<MouseWheelInputEvent>inputEvent).deltaX, (<MouseWheelInputEvent>inputEvent).deltaY);
|
||||
eventManager.dispatchEvent(mouseEvent);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
// #endregion Mouse Handle
|
||||
|
||||
// #region Touch Handle
|
||||
private _dispatchTouchEvent (inputEvent: TouchInputEvent) {
|
||||
switch (inputEvent.type) {
|
||||
case SystemEventType.TOUCH_START:
|
||||
this._handleTouchesStart(inputEvent);
|
||||
break;
|
||||
case SystemEventType.TOUCH_MOVE:
|
||||
this._handleTouchesMove(inputEvent);
|
||||
break;
|
||||
case SystemEventType.TOUCH_END:
|
||||
this._handleTouchesEnd(inputEvent);
|
||||
break;
|
||||
case SystemEventType.TOUCH_CANCEL:
|
||||
this._handleTouchesCancel(inputEvent);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private _handleTouchesStart (inputEvent: TouchInputEvent) {
|
||||
const handleTouches: Touch[] = [];
|
||||
const changedTouches = inputEvent.changedTouches;
|
||||
const dpr = this._getViewPixelRatio();
|
||||
for (let i = 0; i < changedTouches.length; ++i) {
|
||||
const changedTouch = changedTouches[i];
|
||||
const touchID = changedTouch.identifier;
|
||||
if (touchID === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const touch = touchManager.createTouch(touchID, changedTouch.x * dpr, changedTouch.y * dpr);
|
||||
if (!touch) {
|
||||
continue;
|
||||
}
|
||||
handleTouches.push(touch);
|
||||
if (!macro.ENABLE_MULTI_TOUCH) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (handleTouches.length > 0) {
|
||||
const touchEvent = new EventTouch(handleTouches, false, SystemEventType.TOUCH_START, macro.ENABLE_MULTI_TOUCH ? touchManager.getAllTouches() : handleTouches);
|
||||
eventManager.dispatchEvent(touchEvent);
|
||||
}
|
||||
}
|
||||
|
||||
private _handleTouchesMove (inputEvent: TouchInputEvent) {
|
||||
const handleTouches: Touch[] = [];
|
||||
const changedTouches = inputEvent.changedTouches;
|
||||
const dpr = this._getViewPixelRatio();
|
||||
for (let i = 0; i < changedTouches.length; ++i) {
|
||||
const changedTouch = changedTouches[i];
|
||||
const touchID = changedTouch.identifier;
|
||||
if (touchID === null) {
|
||||
continue;
|
||||
}
|
||||
touchManager.updateTouch(touchID, changedTouch.x * dpr, changedTouch.y * dpr);
|
||||
const touch = touchManager.getTouch(touchID);
|
||||
if (!touch) {
|
||||
continue;
|
||||
}
|
||||
handleTouches.push(touch);
|
||||
if (!macro.ENABLE_MULTI_TOUCH) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (handleTouches.length > 0) {
|
||||
const touchEvent = new EventTouch(handleTouches, false, SystemEventType.TOUCH_MOVE, macro.ENABLE_MULTI_TOUCH ? touchManager.getAllTouches() : handleTouches);
|
||||
eventManager.dispatchEvent(touchEvent);
|
||||
}
|
||||
}
|
||||
|
||||
private _handleTouchesEnd (inputEvent: TouchInputEvent) {
|
||||
const handleTouches = this._getSetOfTouchesEndOrCancel(inputEvent);
|
||||
if (handleTouches.length > 0) {
|
||||
const touchEvent = new EventTouch(handleTouches, false, SystemEventType.TOUCH_END, macro.ENABLE_MULTI_TOUCH ? touchManager.getAllTouches() : handleTouches);
|
||||
eventManager.dispatchEvent(touchEvent);
|
||||
}
|
||||
}
|
||||
|
||||
private _handleTouchesCancel (inputEvent: TouchInputEvent) {
|
||||
const handleTouches = this._getSetOfTouchesEndOrCancel(inputEvent);
|
||||
if (handleTouches.length > 0) {
|
||||
const touchEvent = new EventTouch(handleTouches, false, SystemEventType.TOUCH_CANCEL, macro.ENABLE_MULTI_TOUCH ? touchManager.getAllTouches() : handleTouches);
|
||||
eventManager.dispatchEvent(touchEvent);
|
||||
}
|
||||
}
|
||||
|
||||
private _getSetOfTouchesEndOrCancel (inputEvent: TouchInputEvent) {
|
||||
const handleTouches: Touch[] = [];
|
||||
const changedTouches = inputEvent.changedTouches;
|
||||
const dpr = this._getViewPixelRatio();
|
||||
for (let i = 0; i < changedTouches.length; ++i) {
|
||||
const changedTouch = changedTouches[i];
|
||||
const touchID = changedTouch.identifier;
|
||||
if (touchID === null) {
|
||||
continue;
|
||||
}
|
||||
touchManager.updateTouch(touchID, changedTouch.x * dpr, changedTouch.y * dpr);
|
||||
const touch = touchManager.getTouch(touchID);
|
||||
if (!touch) {
|
||||
continue;
|
||||
}
|
||||
handleTouches.push(touch);
|
||||
touchManager.releaseTouch(touchID);
|
||||
if (!macro.ENABLE_MULTI_TOUCH) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return handleTouches;
|
||||
}
|
||||
|
||||
// TODO: remove this private method
|
||||
private _getViewPixelRatio () {
|
||||
if (!this._glView) {
|
||||
this._glView = legacyCC.view;
|
||||
}
|
||||
return this._glView ? this._glView._devicePixelRatio : 1;
|
||||
}
|
||||
|
||||
private _simulateTouchInputEvent (mouseInputEvent: MouseInputEvent): TouchInputEvent {
|
||||
const touchData: TouchData = {
|
||||
identifier: 0,
|
||||
x: mouseInputEvent.x,
|
||||
y: mouseInputEvent.y,
|
||||
force: 0,
|
||||
};
|
||||
|
||||
const touchInputEvent: TouchInputEvent = {
|
||||
type: pointerEventTypeMap[mouseInputEvent.type],
|
||||
changedTouches: [touchData],
|
||||
timestamp: mouseInputEvent.timestamp,
|
||||
};
|
||||
return touchInputEvent;
|
||||
}
|
||||
|
||||
private _getMouseEvent (inputEvent: MouseInputEvent): EventMouse {
|
||||
const locPreMouse = this._prevMousePoint;
|
||||
const mouseEvent = new EventMouse(inputEvent.type, false, locPreMouse);
|
||||
const pixelRatio = this._getViewPixelRatio();
|
||||
// update previous location
|
||||
locPreMouse.x = inputEvent.x * pixelRatio;
|
||||
locPreMouse.y = inputEvent.y * pixelRatio;
|
||||
// HACK: maybe it's an HACK operation
|
||||
if (legacyCC.GAME_VIEW) {
|
||||
locPreMouse.x /= legacyCC.gameView.canvas.width / legacyCC.game.canvas.width;
|
||||
locPreMouse.y /= legacyCC.gameView.canvas.height / legacyCC.game.canvas.height;
|
||||
}
|
||||
mouseEvent.setLocation(locPreMouse.x, locPreMouse.y);
|
||||
mouseEvent.setButton(inputEvent.button);
|
||||
|
||||
// Web only
|
||||
if (inputEvent.movementX) {
|
||||
mouseEvent.movementX = inputEvent.movementX;
|
||||
}
|
||||
if (inputEvent.movementY) {
|
||||
mouseEvent.movementY = inputEvent.movementY;
|
||||
}
|
||||
return mouseEvent;
|
||||
}
|
||||
// #endregion Touch Handle
|
||||
|
||||
// #region Keyboard Handle
|
||||
private _dispatchKeyboardEvent (inputEvent: KeyboardInputEvent) {
|
||||
switch (inputEvent.type) {
|
||||
// TODO: to support in Input Module
|
||||
// case 'keypress':
|
||||
// eventManager.dispatchEvent(new EventKeyboard(inputEvent.code, 'keypress'));
|
||||
// break;
|
||||
case SystemEventType.KEY_DOWN:
|
||||
eventManager.dispatchEvent(new EventKeyboard(inputEvent.code, SystemEventType.KEY_DOWN));
|
||||
break;
|
||||
case SystemEventType.KEY_UP:
|
||||
eventManager.dispatchEvent(new EventKeyboard(inputEvent.code, SystemEventType.KEY_UP));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
// #endregion Keyboard Handle
|
||||
|
||||
// #region Accelerometer Handle
|
||||
_dispatchAccelerometerEvent (inputEvent: AccelerometerInputEvent) {
|
||||
if (inputEvent.type === SystemEventType.DEVICEMOTION) {
|
||||
const { x, y, z, timestamp } = inputEvent;
|
||||
eventManager.dispatchEvent(new EventAcceleration(new Acceleration(x, y, z, timestamp)));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Whether enable accelerometer event.
|
||||
*/
|
||||
// #region Accelerometer
|
||||
public setAccelerometerEnabled (isEnable: boolean) {
|
||||
if (isEnable) {
|
||||
input.startAccelerometer();
|
||||
this._accelerometer.start();
|
||||
} else {
|
||||
input.stopAccelerometer();
|
||||
this._accelerometer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set accelerometer interval value in mile seconds
|
||||
* @method setAccelerometerInterval
|
||||
* @param {Number} intervalInMileSeconds
|
||||
*/
|
||||
public setAccelerometerInterval (intervalInMileSeconds) {
|
||||
input.setAccelerometerInterval(intervalInMileSeconds);
|
||||
public setAccelerometerInterval (intervalInMileSeconds: number): void {
|
||||
this._accelerometer.setInterval(intervalInMileSeconds);
|
||||
}
|
||||
// #endregion Accelerometer Handle
|
||||
// #endregion Accelerometer
|
||||
}
|
||||
|
||||
export const inputManager = new InputManager();
|
||||
|
||||
legacyCC.internal.inputManager = inputManager;
|
||||
|
||||
@@ -97,18 +97,7 @@ export class SystemEvent extends EventTarget {
|
||||
return;
|
||||
}
|
||||
|
||||
// for iOS 13+
|
||||
if (isEnabled && window.DeviceMotionEvent && typeof DeviceMotionEvent.requestPermission === 'function') {
|
||||
DeviceMotionEvent.requestPermission().then((response) => {
|
||||
logID(3520, response);
|
||||
inputManager.setAccelerometerEnabled(response === 'granted');
|
||||
}).catch((error) => {
|
||||
warnID(3521, error.message);
|
||||
inputManager.setAccelerometerEnabled(false);
|
||||
});
|
||||
} else {
|
||||
inputManager.setAccelerometerEnabled(isEnabled);
|
||||
}
|
||||
inputManager.setAccelerometerEnabled(isEnabled);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { AccelerometerCallback, AccelerometerInputEvent } from 'pal/input';
|
||||
import { AccelerometerCallback } from 'pal/input';
|
||||
import { minigame, AccelerometerIntervalMode } from 'pal/minigame';
|
||||
import { SystemEventType } from '../../../cocos/input/types';
|
||||
import { Acceleration, EventAcceleration, SystemEventType } from '../../../cocos/input/types';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
|
||||
export class AccelerometerInputSource {
|
||||
@@ -25,14 +25,10 @@ export class AccelerometerInputSource {
|
||||
}
|
||||
|
||||
private _didAccelerate (event: AccelerometerData) {
|
||||
const accelerometer: AccelerometerInputEvent = {
|
||||
type: SystemEventType.DEVICEMOTION,
|
||||
x: event.x,
|
||||
y: event.y,
|
||||
z: event.z,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
this._eventTarget.emit(SystemEventType.DEVICEMOTION, accelerometer);
|
||||
const timestamp = performance.now();
|
||||
const acceleration = new Acceleration(event.x, event.y, event.z, timestamp);
|
||||
const eventAcceleration = new EventAcceleration(acceleration);
|
||||
this._eventTarget.emit(SystemEventType.DEVICEMOTION, eventAcceleration);
|
||||
}
|
||||
|
||||
public start () {
|
||||
@@ -1,94 +1,5 @@
|
||||
import { TouchInputEvent, MouseInputEvent, KeyboardInputEvent, AccelerometerInputEvent } from 'pal/input';
|
||||
import { js } from '../../../cocos/core/utils/js';
|
||||
import { AccelerometerInputSource } from './accelerometer';
|
||||
import { InputBox } from './input-box';
|
||||
import { KeyboardInputSource } from './keyboard';
|
||||
import { MouseInputSource } from './mouse';
|
||||
import { TouchInputSource } from './touch';
|
||||
|
||||
export class Input {
|
||||
public _touch = new TouchInputSource();
|
||||
public _mouse = new MouseInputSource();
|
||||
public _keyboard = new KeyboardInputSource();
|
||||
public _accelerometer = new AccelerometerInputSource();
|
||||
public _inputBox = new InputBox();
|
||||
|
||||
private _touchEvents: TouchInputEvent[] = [];
|
||||
private _mouseEvents: MouseInputEvent[] = [];
|
||||
private _keyboardEvents: KeyboardInputEvent[] = [];
|
||||
private _accelerometerEvents: AccelerometerInputEvent[] = [];
|
||||
|
||||
constructor () {
|
||||
this._registerEvent();
|
||||
}
|
||||
|
||||
private _registerEvent () {
|
||||
if (this._touch.support) {
|
||||
const touchEvents = this._touchEvents;
|
||||
this._touch.onStart((event) => { touchEvents.push(event); });
|
||||
this._touch.onMove((event) => { touchEvents.push(event); });
|
||||
this._touch.onEnd((event) => { touchEvents.push(event); });
|
||||
this._touch.onCancel((event) => { touchEvents.push(event); });
|
||||
}
|
||||
|
||||
if (this._mouse.support) {
|
||||
const mouseEvents = this._mouseEvents;
|
||||
this._mouse.onDown((event) => { mouseEvents.push(event); });
|
||||
this._mouse.onMove((event) => { mouseEvents.push(event); });
|
||||
this._mouse.onUp((event) => { mouseEvents.push(event); });
|
||||
this._mouse.onWheel((event) => { mouseEvents.push(event); });
|
||||
}
|
||||
|
||||
if (this._keyboard.support) {
|
||||
const keyboardEvents = this._keyboardEvents;
|
||||
// this._keyboard.onDown((event) => { keyboardEvents.push(event); });
|
||||
this._keyboard.onPressing((event) => { keyboardEvents.push(event); });
|
||||
this._keyboard.onUp((event) => { keyboardEvents.push(event); });
|
||||
}
|
||||
|
||||
if (this._accelerometer.support) {
|
||||
const accelerometerEvents = this._accelerometerEvents;
|
||||
this._accelerometer.onChange((event) => { accelerometerEvents.push(event); });
|
||||
}
|
||||
}
|
||||
|
||||
// accelerometer
|
||||
public startAccelerometer (): void {
|
||||
this._accelerometer.start();
|
||||
}
|
||||
public stopAccelerometer (): void {
|
||||
this._accelerometer.stop();
|
||||
}
|
||||
public setAccelerometerInterval (interval: number): void {
|
||||
this._accelerometer.setInterval(interval);
|
||||
}
|
||||
|
||||
// // input box
|
||||
// public showInputBox (): Promise<void>;
|
||||
// public hideInputBox (): Promise<void>;
|
||||
// public onInputBoxChange (cb: Function);
|
||||
// public onInputBoxComplete (cb: Function);
|
||||
|
||||
public pollTouchEvents (): TouchInputEvent[] {
|
||||
const events = js.array.copy(this._touchEvents);
|
||||
this._touchEvents.length = 0;
|
||||
return events;
|
||||
}
|
||||
public pollMouseEvents (): MouseInputEvent[] {
|
||||
const events = js.array.copy(this._mouseEvents);
|
||||
this._mouseEvents.length = 0;
|
||||
return events;
|
||||
}
|
||||
public pollKeyboardEvents (): KeyboardInputEvent[] {
|
||||
const events = js.array.copy(this._keyboardEvents);
|
||||
this._keyboardEvents.length = 0;
|
||||
return events;
|
||||
}
|
||||
public pollAccelerometerEvents (): AccelerometerInputEvent[] {
|
||||
const events = js.array.copy(this._accelerometerEvents);
|
||||
this._accelerometerEvents.length = 0;
|
||||
return events;
|
||||
}
|
||||
}
|
||||
|
||||
export const input = new Input();
|
||||
export * from './accelerometer-input';
|
||||
export * from './gamepad-input';
|
||||
export * from './keyboard-input';
|
||||
export * from './mouse-input';
|
||||
export * from './touch-input';
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
export class InputBox {
|
||||
public support: boolean;
|
||||
|
||||
constructor () {
|
||||
this.support = true;
|
||||
}
|
||||
|
||||
show (): Promise<void> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
hide (): Promise<void> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
onChange () {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
onComplete () {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
offChange () {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
offComplete () {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { KeyboardCallback, KeyboardInputEvent } from 'pal/input';
|
||||
import { KeyboardCallback } from 'pal/input';
|
||||
import { KeyboardEventData, minigame } from 'pal/minigame';
|
||||
import { SystemEventType, KeyCode } from '../../../cocos/input/types';
|
||||
import { SystemEventType, KeyCode, EventKeyboard } from '../../../cocos/input/types';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { SystemEvent } from '../../../cocos/input';
|
||||
|
||||
@@ -128,34 +128,26 @@ export class KeyboardInputSource {
|
||||
private _registerEvent () {
|
||||
minigame.wx?.onKeyDown?.((res) => {
|
||||
const keyCode = getKeyCode(res.code);
|
||||
if (!this._keyStateMap[keyCode]) {
|
||||
const keyDownInputEvent = this._getInputEvent(res, SystemEventType.KEY_DOWN);
|
||||
this._eventTarget.emit(SystemEventType.KEY_DOWN, keyDownInputEvent);
|
||||
}
|
||||
const keyPressingInputEvent = this._getInputEvent(res, SystemEventType.KEY_DOWN);
|
||||
this._eventTarget.emit(SystemEventType.KEY_DOWN, keyPressingInputEvent);
|
||||
// if (!this._keyStateMap[keyCode]) {
|
||||
// const eventKeyDown = this._getInputEvent(res, 'keypress');
|
||||
// this._eventTarget.emit('keypress', eventKeyDown);
|
||||
// }
|
||||
const eventKeyPressing = this._getInputEvent(res, SystemEventType.KEY_DOWN);
|
||||
this._eventTarget.emit(SystemEventType.KEY_DOWN, eventKeyPressing);
|
||||
this._keyStateMap[keyCode] = true;
|
||||
});
|
||||
minigame.wx?.onKeyUp?.((res) => {
|
||||
const keyCode = getKeyCode(res.code);
|
||||
const inputEvent: KeyboardInputEvent = {
|
||||
code: keyCode,
|
||||
type: SystemEventType.KEY_UP,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
const eventKeyUp = this._getInputEvent(res, SystemEventType.KEY_UP);
|
||||
this._keyStateMap[keyCode] = false;
|
||||
this._eventTarget.emit(SystemEventType.KEY_UP, inputEvent);
|
||||
this._eventTarget.emit(SystemEventType.KEY_UP, eventKeyUp);
|
||||
});
|
||||
}
|
||||
|
||||
private _getInputEvent (event: KeyboardEventData, eventType: SystemEvent.EventType) {
|
||||
const keyCode = getKeyCode(event.code);
|
||||
const inputEvent: KeyboardInputEvent = {
|
||||
type: eventType,
|
||||
code: keyCode,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
return inputEvent;
|
||||
const eventKeyboard = new EventKeyboard(keyCode, eventType);
|
||||
return eventKeyboard;
|
||||
}
|
||||
|
||||
public onDown (cb: KeyboardCallback) {
|
||||
@@ -0,0 +1,103 @@
|
||||
import { MouseCallback } from 'pal/input';
|
||||
import { MouseEventData, MouseWheelEventData, minigame } from 'pal/minigame';
|
||||
import { screenAdapter } from 'pal/screen-adapter';
|
||||
import { SystemEvent } from '../../../cocos/input';
|
||||
import { Vec2 } from '../../../cocos/core/math';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { EventMouse, SystemEventType } from '../../../cocos/input/types';
|
||||
import { legacyCC } from '../../../cocos/core/global-exports';
|
||||
|
||||
export class MouseInputSource {
|
||||
public support: boolean;
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
private _isPressed = false;
|
||||
private _preMousePos: Vec2 = new Vec2();
|
||||
|
||||
constructor () {
|
||||
this.support = typeof minigame.wx === 'object' && typeof minigame.wx.onMouseDown !== 'undefined';
|
||||
if (this.support) {
|
||||
this._registerEvent();
|
||||
}
|
||||
}
|
||||
|
||||
private _getLocation (event: MouseEventData): Vec2 {
|
||||
const windowSize = screenAdapter.windowSize;
|
||||
let x = event.x;
|
||||
let y = windowSize.height - event.y;
|
||||
// TODO: should not call engine API
|
||||
const view = legacyCC.view;
|
||||
const dpr = view._devicePixelRatio;
|
||||
x *= dpr;
|
||||
y *= dpr;
|
||||
return new Vec2(x, y);
|
||||
}
|
||||
|
||||
private _registerEvent () {
|
||||
minigame.wx?.onMouseDown?.(this._createCallback(SystemEventType.MOUSE_DOWN));
|
||||
minigame.wx?.onMouseMove?.(this._createCallback(SystemEventType.MOUSE_MOVE));
|
||||
minigame.wx?.onMouseUp?.(this._createCallback(SystemEventType.MOUSE_UP));
|
||||
minigame.wx?.onWheel?.(this._handleMouseWheel.bind(this));
|
||||
}
|
||||
|
||||
private _createCallback (eventType: SystemEvent.EventType) {
|
||||
return (event: MouseEventData) => {
|
||||
const location = this._getLocation(event);
|
||||
let button = event.button;
|
||||
switch (eventType) {
|
||||
case SystemEventType.MOUSE_DOWN:
|
||||
this._isPressed = true;
|
||||
break;
|
||||
case SystemEventType.MOUSE_UP:
|
||||
this._isPressed = false;
|
||||
break;
|
||||
case SystemEventType.MOUSE_MOVE:
|
||||
if (!this._isPressed) {
|
||||
button = EventMouse.BUTTON_MISSING;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
const eventMouse = new EventMouse(eventType, false, this._preMousePos);
|
||||
eventMouse.setLocation(location.x, location.y);
|
||||
eventMouse.setButton(button);
|
||||
eventMouse.movementX = location.x - this._preMousePos.x;
|
||||
eventMouse.movementY = this._preMousePos.y - location.y;
|
||||
|
||||
// update previous mouse position.
|
||||
this._preMousePos.set(location.x, location.y);
|
||||
this._eventTarget.emit(eventType, eventMouse);
|
||||
};
|
||||
}
|
||||
|
||||
private _handleMouseWheel (event: MouseWheelEventData) {
|
||||
const eventType = SystemEventType.MOUSE_WHEEL;
|
||||
const location = this._getLocation(event);
|
||||
const button = event.button;
|
||||
|
||||
const eventMouse = new EventMouse(eventType, false, this._preMousePos);
|
||||
eventMouse.setLocation(location.x, location.y);
|
||||
eventMouse.setButton(button);
|
||||
eventMouse.movementX = location.x - this._preMousePos.x;
|
||||
eventMouse.movementY = this._preMousePos.y - location.y;
|
||||
|
||||
eventMouse.setScrollData(event.deltaX, event.deltaY);
|
||||
// update previous mouse position.
|
||||
this._preMousePos.set(location.x, location.y);
|
||||
this._eventTarget.emit(SystemEventType.MOUSE_WHEEL, eventMouse);
|
||||
}
|
||||
|
||||
onDown (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_DOWN, cb);
|
||||
}
|
||||
onMove (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_MOVE, cb);
|
||||
}
|
||||
onUp (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_UP, cb);
|
||||
}
|
||||
onWheel (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_WHEEL, cb);
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
import { MouseCallback, MouseInputEvent, MouseWheelCallback, MouseWheelInputEvent } from 'pal/input';
|
||||
import { MouseEventData, MouseWheelEventData, minigame } from 'pal/minigame';
|
||||
import { SystemEvent } from '../../../cocos/input';
|
||||
import { Vec2 } from '../../../cocos/core/math';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { SystemEventType } from '../../../cocos/input/types';
|
||||
|
||||
export class MouseInputSource {
|
||||
public support: boolean;
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
private _isPressed = false;
|
||||
private _preMousePos: Vec2 = new Vec2();
|
||||
|
||||
constructor () {
|
||||
this.support = typeof minigame.wx === 'object' && typeof minigame.wx.onMouseDown !== 'undefined';
|
||||
if (this.support) {
|
||||
this._registerEvent();
|
||||
}
|
||||
}
|
||||
|
||||
private _registerEvent () {
|
||||
minigame.wx?.onMouseDown?.(this._createCallback(SystemEventType.MOUSE_DOWN));
|
||||
minigame.wx?.onMouseMove?.(this._createCallback(SystemEventType.MOUSE_MOVE));
|
||||
minigame.wx?.onMouseUp?.(this._createCallback(SystemEventType.MOUSE_UP));
|
||||
minigame.wx?.onWheel?.((event: MouseWheelEventData) => {
|
||||
const sysInfo = minigame.getSystemInfoSync();
|
||||
const inputEvent: MouseWheelInputEvent = {
|
||||
type: SystemEventType.MOUSE_WHEEL,
|
||||
x: event.x,
|
||||
y: sysInfo.screenHeight - event.y,
|
||||
button: event.button,
|
||||
deltaX: event.deltaX,
|
||||
deltaY: event.deltaY,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
// emit web mouse event
|
||||
this._eventTarget.emit(SystemEventType.MOUSE_WHEEL, inputEvent);
|
||||
});
|
||||
}
|
||||
|
||||
private _createCallback (eventType: SystemEvent.EventType) {
|
||||
return (event: MouseEventData) => {
|
||||
const sysInfo = minigame.getSystemInfoSync();
|
||||
let button = event.button;
|
||||
switch (eventType) {
|
||||
case SystemEventType.MOUSE_DOWN:
|
||||
this._isPressed = true;
|
||||
break;
|
||||
case SystemEventType.MOUSE_UP:
|
||||
this._isPressed = false;
|
||||
break;
|
||||
case SystemEventType.MOUSE_MOVE:
|
||||
if (!this._isPressed) {
|
||||
button = -1; // TODO: should not access EventMouse.BUTTON_MISSING, need a button enum type
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
const locationX = event.x;
|
||||
const locationY = sysInfo.screenHeight - event.y;
|
||||
const inputEvent: MouseInputEvent = {
|
||||
type: eventType,
|
||||
x: locationX,
|
||||
y: locationY,
|
||||
movementX: locationX - this._preMousePos.x,
|
||||
movementY: this._preMousePos.y - locationY,
|
||||
button,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
// update previous mouse position.
|
||||
this._preMousePos.set(inputEvent.x, inputEvent.y);
|
||||
// emit web mouse event
|
||||
this._eventTarget.emit(eventType, inputEvent);
|
||||
};
|
||||
}
|
||||
|
||||
onDown (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_DOWN, cb);
|
||||
}
|
||||
onMove (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_MOVE, cb);
|
||||
}
|
||||
onUp (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_UP, cb);
|
||||
}
|
||||
onWheel (cb: MouseWheelCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_WHEEL, cb);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { TouchCallback } from 'pal/input';
|
||||
import { minigame } from 'pal/minigame';
|
||||
import { screenAdapter } from 'pal/screen-adapter';
|
||||
import { Size, Vec2 } from '../../../cocos/core/math';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { SystemEvent } from '../../../cocos/input';
|
||||
import { EventTouch, SystemEventType, Touch } from '../../../cocos/input/types';
|
||||
import { legacyCC } from '../../../cocos/core/global-exports';
|
||||
import { touchManager } from '../touch-manager';
|
||||
import { macro } from '../../../cocos/core/platform/macro';
|
||||
|
||||
export class TouchInputSource {
|
||||
public support: boolean;
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
|
||||
constructor () {
|
||||
this.support = true;
|
||||
this._registerEvent();
|
||||
}
|
||||
|
||||
private _registerEvent () {
|
||||
minigame.onTouchStart(this._createCallback(SystemEventType.TOUCH_START));
|
||||
minigame.onTouchMove(this._createCallback(SystemEventType.TOUCH_MOVE));
|
||||
minigame.onTouchEnd(this._createCallback(SystemEventType.TOUCH_END));
|
||||
minigame.onTouchCancel(this._createCallback(SystemEventType.TOUCH_CANCEL));
|
||||
}
|
||||
|
||||
private _createCallback (eventType: SystemEvent.EventType) {
|
||||
return (event: any) => {
|
||||
const handleTouches: Touch[] = [];
|
||||
const windowSize = screenAdapter.windowSize;
|
||||
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, windowSize);
|
||||
const touch = touchManager.getTouch(touchID, location.x, location.y);
|
||||
if (!touch) {
|
||||
continue;
|
||||
}
|
||||
if (eventType === SystemEventType.TOUCH_END || eventType === SystemEventType.TOUCH_CANCEL) {
|
||||
touchManager.releaseTouch(touchID);
|
||||
}
|
||||
handleTouches.push(touch);
|
||||
if (!macro.ENABLE_MULTI_TOUCH) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (handleTouches.length > 0) {
|
||||
const eventTouch = new EventTouch(handleTouches, false, eventType,
|
||||
macro.ENABLE_MULTI_TOUCH ? touchManager.getAllTouches() : handleTouches);
|
||||
this._eventTarget.emit(eventType, eventTouch);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private _getLocation (touch: globalThis.Touch, windowSize: Size): Vec2 {
|
||||
let x = touch.clientX;
|
||||
let y = windowSize.height - touch.clientY;
|
||||
// TODO: should not call engine API
|
||||
const view = legacyCC.view;
|
||||
const dpr = view._devicePixelRatio;
|
||||
x *= dpr;
|
||||
y *= dpr;
|
||||
return new Vec2(x, y);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
import { TouchCallback, TouchData, TouchInputEvent } from 'pal/input';
|
||||
import { minigame } from 'pal/minigame';
|
||||
import { Vec2 } from '../../../cocos/core/math';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { SystemEvent } from '../../../cocos/input';
|
||||
import { SystemEventType } from '../../../cocos/input/types';
|
||||
|
||||
export class TouchInputSource {
|
||||
public support: boolean;
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
|
||||
constructor () {
|
||||
this.support = true;
|
||||
this._registerEvent();
|
||||
}
|
||||
|
||||
private _registerEvent () {
|
||||
minigame.onTouchStart(this._createCallback(SystemEventType.TOUCH_START));
|
||||
minigame.onTouchMove(this._createCallback(SystemEventType.TOUCH_MOVE));
|
||||
minigame.onTouchEnd(this._createCallback(SystemEventType.TOUCH_END));
|
||||
minigame.onTouchCancel(this._createCallback(SystemEventType.TOUCH_CANCEL));
|
||||
}
|
||||
|
||||
private _createCallback (eventType: SystemEvent.EventType) {
|
||||
return (event: any) => {
|
||||
const sysInfo = minigame.getSystemInfoSync();
|
||||
const touchDataList: TouchData[] = [];
|
||||
const length = event.changedTouches.length;
|
||||
for (let i = 0; i < length; ++i) {
|
||||
const touch = event.changedTouches[i];
|
||||
const location = this._getLocation(touch);
|
||||
const touchData: TouchData = {
|
||||
identifier: touch.identifier,
|
||||
x: location.x,
|
||||
y: sysInfo.windowHeight - location.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);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import { AccelerometerCallback, AccelerometerInputEvent } from 'pal/input';
|
||||
import { AccelerometerCallback } from 'pal/input';
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { screenAdapter } from 'pal/screen-adapter';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { OS } from '../../system-info/enum-type';
|
||||
import { Orientation } from '../../screen-adapter/enum-type';
|
||||
import { SystemEventType } from '../../../cocos/input/types';
|
||||
import { Acceleration, EventAcceleration, SystemEventType } from '../../../cocos/input/types';
|
||||
|
||||
export class AccelerometerInputSource {
|
||||
public support: boolean;
|
||||
@@ -45,15 +45,11 @@ export class AccelerometerInputSource {
|
||||
x = -x;
|
||||
y = -y;
|
||||
}
|
||||
const accelerometer: AccelerometerInputEvent = {
|
||||
type: SystemEventType.DEVICEMOTION,
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
|
||||
this._eventTarget.emit(SystemEventType.DEVICEMOTION, accelerometer);
|
||||
const timestamp = performance.now();
|
||||
const acceleration = new Acceleration(x, y, z, timestamp);
|
||||
const eventAcceleration = new EventAcceleration(acceleration);
|
||||
this._eventTarget.emit(SystemEventType.DEVICEMOTION, eventAcceleration);
|
||||
}
|
||||
|
||||
public start () {
|
||||
@@ -1,94 +1,5 @@
|
||||
import { TouchInputEvent, MouseInputEvent, KeyboardInputEvent, AccelerometerInputEvent } from 'pal/input';
|
||||
import { js } from '../../../cocos/core/utils/js';
|
||||
import { AccelerometerInputSource } from './accelerometer';
|
||||
import { InputBox } from './input-box';
|
||||
import { KeyboardInputSource } from './keyboard';
|
||||
import { MouseInputSource } from './mouse';
|
||||
import { TouchInputSource } from './touch';
|
||||
|
||||
export class Input {
|
||||
public _touch = new TouchInputSource();
|
||||
public _mouse = new MouseInputSource();
|
||||
public _keyboard = new KeyboardInputSource();
|
||||
public _accelerometer = new AccelerometerInputSource();
|
||||
public _inputBox = new InputBox();
|
||||
|
||||
private _touchEvents: TouchInputEvent[] = [];
|
||||
private _mouseEvents: MouseInputEvent[] = [];
|
||||
private _keyboardEvents: KeyboardInputEvent[] = [];
|
||||
private _accelerometerEvents: AccelerometerInputEvent[] = [];
|
||||
|
||||
constructor () {
|
||||
this._registerEvent();
|
||||
}
|
||||
|
||||
private _registerEvent () {
|
||||
if (this._touch.support) {
|
||||
const touchEvents = this._touchEvents;
|
||||
this._touch.onStart((event) => { touchEvents.push(event); });
|
||||
this._touch.onMove((event) => { touchEvents.push(event); });
|
||||
this._touch.onEnd((event) => { touchEvents.push(event); });
|
||||
this._touch.onCancel((event) => { touchEvents.push(event); });
|
||||
}
|
||||
|
||||
if (this._mouse.support) {
|
||||
const mouseEvents = this._mouseEvents;
|
||||
this._mouse.onDown((event) => { mouseEvents.push(event); });
|
||||
this._mouse.onMove((event) => { mouseEvents.push(event); });
|
||||
this._mouse.onUp((event) => { mouseEvents.push(event); });
|
||||
this._mouse.onWheel((event) => { mouseEvents.push(event); });
|
||||
}
|
||||
|
||||
if (this._keyboard.support) {
|
||||
const keyboardEvents = this._keyboardEvents;
|
||||
// this._keyboard.onDown((event) => { keyboardEvents.push(event); });
|
||||
this._keyboard.onPressing((event) => { keyboardEvents.push(event); });
|
||||
this._keyboard.onUp((event) => { keyboardEvents.push(event); });
|
||||
}
|
||||
|
||||
if (this._accelerometer.support) {
|
||||
const accelerometerEvents = this._accelerometerEvents;
|
||||
this._accelerometer.onChange((event) => { accelerometerEvents.push(event); });
|
||||
}
|
||||
}
|
||||
|
||||
// accelerometer
|
||||
public startAccelerometer (): void {
|
||||
this._accelerometer.start();
|
||||
}
|
||||
public stopAccelerometer (): void {
|
||||
this._accelerometer.stop();
|
||||
}
|
||||
public setAccelerometerInterval (interval: number): void {
|
||||
this._accelerometer.setInterval(interval);
|
||||
}
|
||||
|
||||
// // input box
|
||||
// public showInputBox (): Promise<void>;
|
||||
// public hideInputBox (): Promise<void>;
|
||||
// public onInputBoxChange (cb: Function);
|
||||
// public onInputBoxComplete (cb: Function);
|
||||
|
||||
public pollTouchEvents (): TouchInputEvent[] {
|
||||
const events = js.array.copy(this._touchEvents);
|
||||
this._touchEvents.length = 0;
|
||||
return events;
|
||||
}
|
||||
public pollMouseEvents (): MouseInputEvent[] {
|
||||
const events = js.array.copy(this._mouseEvents);
|
||||
this._mouseEvents.length = 0;
|
||||
return events;
|
||||
}
|
||||
public pollKeyboardEvents (): KeyboardInputEvent[] {
|
||||
const events = js.array.copy(this._keyboardEvents);
|
||||
this._keyboardEvents.length = 0;
|
||||
return events;
|
||||
}
|
||||
public pollAccelerometerEvents (): AccelerometerInputEvent[] {
|
||||
const events = js.array.copy(this._accelerometerEvents);
|
||||
this._accelerometerEvents.length = 0;
|
||||
return events;
|
||||
}
|
||||
}
|
||||
|
||||
export const input = new Input();
|
||||
export * from './accelerometer-input';
|
||||
export * from './gamepad-input';
|
||||
export * from './keyboard-input';
|
||||
export * from './mouse-input';
|
||||
export * from './touch-input';
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
export class InputBox {
|
||||
public support: boolean;
|
||||
|
||||
constructor () {
|
||||
this.support = true;
|
||||
}
|
||||
|
||||
show (): Promise<void> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
hide (): Promise<void> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
onChange () {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
onComplete () {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
offChange () {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
offComplete () {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { KeyboardCallback, KeyboardInputEvent } from 'pal/input';
|
||||
import { KeyboardCallback } from 'pal/input';
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { SystemEventType, KeyCode } from '../../../cocos/input/types';
|
||||
import { SystemEventType, KeyCode, EventKeyboard } from '../../../cocos/input/types';
|
||||
import { SystemEvent } from '../../../cocos/input';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
|
||||
@@ -42,33 +42,25 @@ export class KeyboardInputSource {
|
||||
jsb.onKeyDown = (event: jsb.KeyboardEvent) => {
|
||||
const keyCode = getKeyCode(event.keyCode);
|
||||
// if (!this._keyStateMap[keyCode]) {
|
||||
// const keyDownInputEvent = this._getInputEvent(event, 'keypress');
|
||||
// this._eventTarget.emit('keypress', keyDownInputEvent);
|
||||
// const eventKeyDown = this._getInputEvent(event, 'keypress');
|
||||
// this._eventTarget.emit('keypress', eventKeyDown);
|
||||
// }
|
||||
const keyPressingInputEvent = this._getInputEvent(event, SystemEventType.KEY_DOWN);
|
||||
this._eventTarget.emit(SystemEventType.KEY_DOWN, keyPressingInputEvent);
|
||||
const eventKeyPressing = this._getInputEvent(event, SystemEventType.KEY_DOWN);
|
||||
this._eventTarget.emit(SystemEventType.KEY_DOWN, eventKeyPressing);
|
||||
this._keyStateMap[keyCode] = true;
|
||||
};
|
||||
jsb.onKeyUp = (event: jsb.KeyboardEvent) => {
|
||||
jsb.onKeyUp = (event: jsb.KeyboardEvent) => {
|
||||
const keyCode = getKeyCode(event.keyCode);
|
||||
const inputEvent: KeyboardInputEvent = {
|
||||
type: SystemEventType.KEY_UP,
|
||||
code: keyCode,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
const eventKeyUp = this._getInputEvent(event, SystemEventType.KEY_UP);
|
||||
this._keyStateMap[keyCode] = false;
|
||||
this._eventTarget.emit(SystemEventType.KEY_UP, inputEvent);
|
||||
this._eventTarget.emit(SystemEventType.KEY_UP, eventKeyUp);
|
||||
};
|
||||
}
|
||||
|
||||
private _getInputEvent (event: jsb.KeyboardEvent, eventType: SystemEvent.EventType) {
|
||||
const keyCode = getKeyCode(event.keyCode);
|
||||
const inputEvent: KeyboardInputEvent = {
|
||||
type: eventType,
|
||||
code: keyCode,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
return inputEvent;
|
||||
const eventKeyboard = new EventKeyboard(keyCode, eventType);
|
||||
return eventKeyboard;
|
||||
}
|
||||
|
||||
public onDown (cb: KeyboardCallback) {
|
||||
@@ -0,0 +1,101 @@
|
||||
import { MouseCallback } from 'pal/input';
|
||||
import { screenAdapter } from 'pal/screen-adapter';
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { EventMouse, SystemEventType } from '../../../cocos/input/types';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { Vec2 } from '../../../cocos/core/math';
|
||||
import { legacyCC } from '../../../cocos/core/global-exports';
|
||||
|
||||
export class MouseInputSource {
|
||||
public support: boolean;
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
private _preMousePos: Vec2 = new Vec2();
|
||||
private _isPressed = false;
|
||||
|
||||
constructor () {
|
||||
this.support = !systemInfo.isMobile;
|
||||
this._registerEvent();
|
||||
}
|
||||
|
||||
private _getLocation (event: jsb.MouseEvent): Vec2 {
|
||||
const windowSize = screenAdapter.windowSize;
|
||||
let x = event.x;
|
||||
let y = windowSize.height - event.y;
|
||||
// TODO: should not call engine API
|
||||
const view = legacyCC.view;
|
||||
const dpr = view._devicePixelRatio;
|
||||
x *= dpr;
|
||||
y *= dpr;
|
||||
return new Vec2(x, y);
|
||||
}
|
||||
|
||||
private _registerEvent () {
|
||||
jsb.onMouseDown = this._createCallback(SystemEventType.MOUSE_DOWN);
|
||||
jsb.onMouseMove = this._createCallback(SystemEventType.MOUSE_MOVE);
|
||||
jsb.onMouseUp = this._createCallback(SystemEventType.MOUSE_UP);
|
||||
jsb.onMouseWheel = this._handleMouseWheel.bind(this);
|
||||
}
|
||||
|
||||
private _createCallback (eventType: SystemEventType) {
|
||||
return (mouseEvent: jsb.MouseEvent) => {
|
||||
const location = this._getLocation(mouseEvent);
|
||||
let button = mouseEvent.button;
|
||||
switch (eventType) {
|
||||
case SystemEventType.MOUSE_DOWN:
|
||||
this._isPressed = true;
|
||||
break;
|
||||
case SystemEventType.MOUSE_UP:
|
||||
this._isPressed = false;
|
||||
break;
|
||||
case SystemEventType.MOUSE_MOVE:
|
||||
if (!this._isPressed) {
|
||||
button = EventMouse.BUTTON_MISSING;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
const eventMouse = new EventMouse(eventType, false, this._preMousePos);
|
||||
eventMouse.setLocation(location.x, location.y);
|
||||
eventMouse.setButton(button);
|
||||
eventMouse.movementX = location.x - this._preMousePos.x;
|
||||
eventMouse.movementY = this._preMousePos.y - location.y;
|
||||
|
||||
// update previous mouse position.
|
||||
this._preMousePos.set(location.x, location.y);
|
||||
this._eventTarget.emit(eventType, eventMouse);
|
||||
};
|
||||
}
|
||||
|
||||
private _handleMouseWheel (mouseEvent: jsb.MouseWheelEvent) {
|
||||
const eventType = SystemEventType.MOUSE_WHEEL;
|
||||
const location = this._getLocation(mouseEvent);
|
||||
const button = mouseEvent.button;
|
||||
|
||||
const eventMouse = new EventMouse(eventType, false, this._preMousePos);
|
||||
eventMouse.setLocation(location.x, location.y);
|
||||
eventMouse.setButton(button);
|
||||
eventMouse.movementX = location.x - this._preMousePos.x;
|
||||
eventMouse.movementY = this._preMousePos.y - location.y;
|
||||
|
||||
const matchStandardFactor = 120;
|
||||
eventMouse.setScrollData(mouseEvent.wheelDeltaX * matchStandardFactor, mouseEvent.wheelDeltaY * matchStandardFactor);
|
||||
// update previous mouse position.
|
||||
this._preMousePos.set(location.x, location.y);
|
||||
this._eventTarget.emit(eventType, eventMouse);
|
||||
}
|
||||
|
||||
onDown (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_DOWN, cb);
|
||||
}
|
||||
onMove (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_MOVE, cb);
|
||||
}
|
||||
onUp (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_UP, cb);
|
||||
}
|
||||
onWheel (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_WHEEL, cb);
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
import { MouseCallback, MouseInputEvent, MouseWheelCallback, MouseWheelInputEvent } from 'pal/input';
|
||||
import { screenAdapter } from 'pal/screen-adapter';
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { SystemEventType } from '../../../cocos/input/types';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { Vec2 } from '../../../cocos/core/math';
|
||||
import { SystemEvent } from '../../../cocos/input';
|
||||
|
||||
export class MouseInputSource {
|
||||
public support: boolean;
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
private _preMousePos: Vec2 = new Vec2();
|
||||
|
||||
constructor () {
|
||||
this.support = !systemInfo.isMobile;
|
||||
this._registerEvent();
|
||||
}
|
||||
|
||||
private _getLocation (event: jsb.MouseEvent): Vec2 {
|
||||
return new Vec2(event.x, event.y);
|
||||
}
|
||||
|
||||
private _registerEvent () {
|
||||
jsb.onMouseDown = this._createCallback(SystemEventType.MOUSE_DOWN);
|
||||
jsb.onMouseMove = this._createCallback(SystemEventType.MOUSE_MOVE);
|
||||
jsb.onMouseUp = this._createCallback(SystemEventType.MOUSE_UP);
|
||||
jsb.onMouseWheel = (event: jsb.MouseWheelEvent) => {
|
||||
const location = this._getLocation(event);
|
||||
const windowSize = screenAdapter.windowSize;
|
||||
const matchStandardFactor = 120;
|
||||
const inputEvent: MouseWheelInputEvent = {
|
||||
type: SystemEventType.MOUSE_WHEEL,
|
||||
x: location.x,
|
||||
y: windowSize.height - location.y,
|
||||
button: event.button,
|
||||
deltaX: event.wheelDeltaX * matchStandardFactor, // scale up to match the web interface
|
||||
deltaY: event.wheelDeltaY * matchStandardFactor,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
this._eventTarget.emit(SystemEventType.MOUSE_WHEEL, inputEvent);
|
||||
};
|
||||
}
|
||||
|
||||
private _createCallback (eventType: SystemEvent.EventType) {
|
||||
return (event: jsb.MouseEvent) => {
|
||||
const location = this._getLocation(event);
|
||||
const windowSize = screenAdapter.windowSize;
|
||||
const locationX = location.x;
|
||||
const locationY = windowSize.height - location.y;
|
||||
const inputEvent: MouseInputEvent = {
|
||||
type: eventType,
|
||||
x: locationX,
|
||||
y: locationY,
|
||||
movementX: locationX - this._preMousePos.x,
|
||||
movementY: this._preMousePos.y - locationY,
|
||||
button: event.button,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
// update previous mouse position.
|
||||
this._preMousePos.set(inputEvent.x, inputEvent.y);
|
||||
// emit web mouse event
|
||||
this._eventTarget.emit(eventType, inputEvent);
|
||||
};
|
||||
}
|
||||
|
||||
onDown (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_DOWN, cb);
|
||||
}
|
||||
onMove (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_MOVE, cb);
|
||||
}
|
||||
onUp (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_UP, cb);
|
||||
}
|
||||
onWheel (cb: MouseWheelCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_WHEEL, cb);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
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, SystemEventType, Touch } from '../../../cocos/input/types';
|
||||
import { touchManager } from '../touch-manager';
|
||||
import { legacyCC } from '../../../cocos/core/global-exports';
|
||||
import { macro } from '../../../cocos/core/platform/macro';
|
||||
|
||||
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: SystemEventType) {
|
||||
return (changedTouches: TouchList) => {
|
||||
const handleTouches: Touch[] = [];
|
||||
const length = changedTouches.length;
|
||||
const windowSize = screenAdapter.windowSize;
|
||||
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 === SystemEventType.TOUCH_END || eventType === SystemEventType.TOUCH_CANCEL) {
|
||||
touchManager.releaseTouch(touchID);
|
||||
}
|
||||
handleTouches.push(touch);
|
||||
if (!macro.ENABLE_MULTI_TOUCH) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (handleTouches.length > 0) {
|
||||
const eventTouch = new EventTouch(handleTouches, false, eventType,
|
||||
macro.ENABLE_MULTI_TOUCH ? touchManager.getAllTouches() : handleTouches);
|
||||
this._eventTarget.emit(eventType, eventTouch);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private _getLocation (touch: globalThis.Touch, windowSize: Size): Vec2 {
|
||||
let x = touch.clientX;
|
||||
let y = windowSize.height - touch.clientY;
|
||||
// TODO: should not call engine API
|
||||
const view = legacyCC.view;
|
||||
const dpr = view._devicePixelRatio;
|
||||
x *= dpr;
|
||||
y *= dpr;
|
||||
return new Vec2(x, y);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
import { TouchCallback, TouchData, TouchInputEvent } from 'pal/input';
|
||||
import { screenAdapter } from 'pal/screen-adapter';
|
||||
import { Vec2 } from '../../../cocos/core/math';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { SystemEvent } from '../../../cocos/input';
|
||||
import { SystemEventType } from '../../../cocos/input/types';
|
||||
|
||||
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: SystemEvent.EventType) {
|
||||
return (touchList: TouchList) => {
|
||||
const touchDataList: TouchData[] = [];
|
||||
const length = touchList.length;
|
||||
const windowSize = screenAdapter.windowSize;
|
||||
for (let i = 0; i < length; ++i) {
|
||||
const touch = touchList[i];
|
||||
const location = this._getLocation(touch);
|
||||
const x = location.x;
|
||||
const y = windowSize.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);
|
||||
}
|
||||
}
|
||||
+28
-25
@@ -20,11 +20,17 @@ class TouchManager {
|
||||
this._touches = new Array(this._maxTouches);
|
||||
}
|
||||
|
||||
// The original touch object can't be modified, so we need to return the cloned touch object.
|
||||
/**
|
||||
* The original touch object can't be modified, so we need to return the cloned touch object.
|
||||
* @param touch
|
||||
* @returns
|
||||
*/
|
||||
private _cloneTouch (touch: Touch): Touch {
|
||||
const touchID = touch.getID();
|
||||
touch.getLocation(tempVec2);
|
||||
touch.getStartLocation(tempVec2);
|
||||
const clonedTouch = new Touch(tempVec2.x, tempVec2.y, touchID);
|
||||
touch.getLocation(tempVec2);
|
||||
clonedTouch.setPoint(tempVec2.x, tempVec2.y);
|
||||
touch.getPreviousLocation(tempVec2);
|
||||
clonedTouch.setPrevPoint(tempVec2);
|
||||
return clonedTouch;
|
||||
@@ -41,7 +47,7 @@ class TouchManager {
|
||||
* @param y
|
||||
* @returns
|
||||
*/
|
||||
public createTouch (touchID: number, x: number, y: number): Touch | undefined {
|
||||
private _createTouch (touchID: number, x: number, y: number): Touch | undefined {
|
||||
if (touchID in this._touchMap) {
|
||||
console.log('Cannot create the same touch object.');
|
||||
return undefined;
|
||||
@@ -54,7 +60,7 @@ class TouchManager {
|
||||
const touch = new Touch(x, y, touchID);
|
||||
this._touches[availableTouchIndex] = touch;
|
||||
this._touchMap[touchID] = availableTouchIndex;
|
||||
this.updateTouch(touchID, x, y);
|
||||
this._updateTouch(touch, x, y);
|
||||
return this._cloneTouch(touch);
|
||||
}
|
||||
|
||||
@@ -72,35 +78,20 @@ class TouchManager {
|
||||
delete this._touchMap[touchID];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the location and previous location of current touch ID.
|
||||
* @param touchID
|
||||
* @param x The current location X
|
||||
* @param y The current location Y
|
||||
*/
|
||||
public updateTouch (touchID: number, x: number, y: number) {
|
||||
const availableTouchIndex = this._touchMap[touchID];
|
||||
const touch = this._touches[availableTouchIndex];
|
||||
if (!touch) {
|
||||
return;
|
||||
}
|
||||
touch.getLocation(tempVec2);
|
||||
touch.setPrevPoint(tempVec2);
|
||||
touch.setPoint(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get touch object by touch ID.
|
||||
* @param touchID
|
||||
* @returns
|
||||
*/
|
||||
public getTouch (touchID: number): Touch | undefined {
|
||||
public getTouch (touchID: number, x: number, y: number): Touch | undefined {
|
||||
const availableTouchIndex = this._touchMap[touchID];
|
||||
const touch = this._touches[availableTouchIndex];
|
||||
let touch = this._touches[availableTouchIndex];
|
||||
if (!touch) {
|
||||
return undefined;
|
||||
touch = this._createTouch(touchID, x, y);
|
||||
} else {
|
||||
this._updateTouch(touch, x, y);
|
||||
}
|
||||
return this._cloneTouch(touch);
|
||||
return touch ? this._cloneTouch(touch) : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,6 +109,18 @@ class TouchManager {
|
||||
return touches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the location and previous location of current touch ID.
|
||||
* @param touchID
|
||||
* @param x The current location X
|
||||
* @param y The current location Y
|
||||
*/
|
||||
private _updateTouch (touch: Touch, x: number, y: number) {
|
||||
touch.getLocation(tempVec2);
|
||||
touch.setPrevPoint(tempVec2);
|
||||
touch.setPoint(x, y);
|
||||
}
|
||||
|
||||
private _getAvailableTouchIndex (touchID: number) {
|
||||
const availableTouchIndex = this._touchMap[touchID];
|
||||
if (typeof availableTouchIndex !== 'undefined') {
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { AccelerometerCallback, AccelerometerInputEvent } from 'pal/input';
|
||||
import { AccelerometerCallback } from 'pal/input';
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { BrowserType, OS } from '../../system-info/enum-type';
|
||||
import { legacyCC } from '../../../cocos/core/global-exports';
|
||||
import { SystemEventType } from '../../../cocos/input/types';
|
||||
import { SystemEventType, EventAcceleration, Acceleration } from '../../../cocos/input/types';
|
||||
|
||||
export class AccelerometerInputSource {
|
||||
public support: boolean;
|
||||
|
||||
private _intervalInMileseconds = 200;
|
||||
private _intervalInMileSeconds = 200;
|
||||
private _accelTimer = 0;
|
||||
private _eventTarget: EventTarget = new EventTarget();
|
||||
private _deviceEventName: 'devicemotion' |'deviceorientation';
|
||||
@@ -40,7 +40,7 @@ export class AccelerometerInputSource {
|
||||
|
||||
private _didAccelerate (event: DeviceMotionEvent | DeviceOrientationEvent) {
|
||||
const now = performance.now();
|
||||
if (now - this._accelTimer < this._intervalInMileseconds) {
|
||||
if (now - this._accelTimer < this._intervalInMileSeconds) {
|
||||
return;
|
||||
}
|
||||
this._accelTimer = now;
|
||||
@@ -92,25 +92,29 @@ export class AccelerometerInputSource {
|
||||
x = -x;
|
||||
y = -y;
|
||||
}
|
||||
const accelerometer: AccelerometerInputEvent = {
|
||||
type: SystemEventType.DEVICEMOTION,
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
|
||||
this._eventTarget.emit(SystemEventType.DEVICEMOTION, accelerometer);
|
||||
const timestamp = performance.now();
|
||||
const acceleration = new Acceleration(x, y, z, timestamp);
|
||||
const eventAcceleration = new EventAcceleration(acceleration);
|
||||
this._eventTarget.emit(SystemEventType.DEVICEMOTION, eventAcceleration);
|
||||
}
|
||||
|
||||
public start () {
|
||||
this._registerEvent();
|
||||
// for iOS 13+, safari
|
||||
if (window.DeviceMotionEvent && typeof DeviceMotionEvent.requestPermission === 'function') {
|
||||
DeviceMotionEvent.requestPermission().then((response) => {
|
||||
if (response === 'granted') {
|
||||
this._registerEvent();
|
||||
}
|
||||
}).catch((e) => {});
|
||||
} else {
|
||||
this._registerEvent();
|
||||
}
|
||||
}
|
||||
public stop () {
|
||||
this._unregisterEvent();
|
||||
}
|
||||
public setInterval (intervalInMileseconds: number) {
|
||||
this._intervalInMileseconds = intervalInMileseconds;
|
||||
public setInterval (intervalInMileSeconds: number) {
|
||||
this._intervalInMileSeconds = intervalInMileSeconds;
|
||||
}
|
||||
public onChange (cb: AccelerometerCallback) {
|
||||
this._eventTarget.on(SystemEventType.DEVICEMOTION, cb);
|
||||
+5
-94
@@ -1,94 +1,5 @@
|
||||
import { TouchInputEvent, MouseInputEvent, KeyboardInputEvent, AccelerometerInputEvent } from 'pal/input';
|
||||
import { js } from '../../../cocos/core/utils/js';
|
||||
import { AccelerometerInputSource } from './accelerometer';
|
||||
import { InputBox } from './input-box';
|
||||
import { KeyboardInputSource } from './keyboard';
|
||||
import { MouseInputSource } from './mouse';
|
||||
import { TouchInputSource } from './touch';
|
||||
|
||||
export class Input {
|
||||
public _touch = new TouchInputSource();
|
||||
public _mouse = new MouseInputSource();
|
||||
public _keyboard = new KeyboardInputSource();
|
||||
public _accelerometer = new AccelerometerInputSource();
|
||||
public _inputBox = new InputBox();
|
||||
|
||||
private _touchEvents: TouchInputEvent[] = [];
|
||||
private _mouseEvents: MouseInputEvent[] = [];
|
||||
private _keyboardEvents: KeyboardInputEvent[] = [];
|
||||
private _accelerometerEvents: AccelerometerInputEvent[] = [];
|
||||
|
||||
constructor () {
|
||||
this._registerEvent();
|
||||
}
|
||||
|
||||
private _registerEvent () {
|
||||
if (this._touch.support) {
|
||||
const touchEvents = this._touchEvents;
|
||||
this._touch.onStart((event) => { touchEvents.push(event); });
|
||||
this._touch.onMove((event) => { touchEvents.push(event); });
|
||||
this._touch.onEnd((event) => { touchEvents.push(event); });
|
||||
this._touch.onCancel((event) => { touchEvents.push(event); });
|
||||
}
|
||||
|
||||
if (this._mouse.support) {
|
||||
const mouseEvents = this._mouseEvents;
|
||||
this._mouse.onDown((event) => { mouseEvents.push(event); });
|
||||
this._mouse.onMove((event) => { mouseEvents.push(event); });
|
||||
this._mouse.onUp((event) => { mouseEvents.push(event); });
|
||||
this._mouse.onWheel((event) => { mouseEvents.push(event); });
|
||||
}
|
||||
|
||||
if (this._keyboard.support) {
|
||||
const keyboardEvents = this._keyboardEvents;
|
||||
// this._keyboard.onDown((event) => { keyboardEvents.push(event); });
|
||||
this._keyboard.onPressing((event) => { keyboardEvents.push(event); });
|
||||
this._keyboard.onUp((event) => { keyboardEvents.push(event); });
|
||||
}
|
||||
|
||||
if (this._accelerometer.support) {
|
||||
const accelerometerEvents = this._accelerometerEvents;
|
||||
this._accelerometer.onChange((event) => { accelerometerEvents.push(event); });
|
||||
}
|
||||
}
|
||||
|
||||
// accelerometer
|
||||
public startAccelerometer (): void {
|
||||
this._accelerometer.start();
|
||||
}
|
||||
public stopAccelerometer (): void {
|
||||
this._accelerometer.stop();
|
||||
}
|
||||
public setAccelerometerInterval (interval: number): void {
|
||||
this._accelerometer.setInterval(interval);
|
||||
}
|
||||
|
||||
// // input box
|
||||
// public showInputBox (): Promise<void>;
|
||||
// public hideInputBox (): Promise<void>;
|
||||
// public onInputBoxChange (cb: Function);
|
||||
// public onInputBoxComplete (cb: Function);
|
||||
|
||||
public pollTouchEvents (): TouchInputEvent[] {
|
||||
const events = js.array.copy(this._touchEvents);
|
||||
this._touchEvents.length = 0;
|
||||
return events;
|
||||
}
|
||||
public pollMouseEvents (): MouseInputEvent[] {
|
||||
const events = js.array.copy(this._mouseEvents);
|
||||
this._mouseEvents.length = 0;
|
||||
return events;
|
||||
}
|
||||
public pollKeyboardEvents (): KeyboardInputEvent[] {
|
||||
const events = js.array.copy(this._keyboardEvents);
|
||||
this._keyboardEvents.length = 0;
|
||||
return events;
|
||||
}
|
||||
public pollAccelerometerEvents (): AccelerometerInputEvent[] {
|
||||
const events = js.array.copy(this._accelerometerEvents);
|
||||
this._accelerometerEvents.length = 0;
|
||||
return events;
|
||||
}
|
||||
}
|
||||
|
||||
export const input = new Input();
|
||||
export * from './accelerometer-input';
|
||||
export * from './gamepad-input';
|
||||
export * from './keyboard-input';
|
||||
export * from './mouse-input';
|
||||
export * from './touch-input';
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
export class InputBox {
|
||||
public support: boolean;
|
||||
|
||||
constructor () {
|
||||
this.support = true;
|
||||
}
|
||||
|
||||
show (): Promise<void> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
hide (): Promise<void> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
onChange () {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
onComplete () {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
offChange () {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
offComplete () {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { KeyboardCallback, KeyboardInputEvent } from 'pal/input';
|
||||
import { SystemEventType, KeyCode } from '../../../cocos/input/types';
|
||||
import { KeyboardCallback } from 'pal/input';
|
||||
import { SystemEventType, KeyCode, EventKeyboard } from '../../../cocos/input/types';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { SystemEvent } from '../../../cocos/input';
|
||||
|
||||
@@ -141,12 +141,8 @@ export class KeyboardInputSource {
|
||||
|
||||
private _getInputEvent (event: any, eventType: SystemEvent.EventType) {
|
||||
const keyCode = getKeyCode(event.code);
|
||||
const inputEvent: KeyboardInputEvent = {
|
||||
type: eventType,
|
||||
code: keyCode,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
return inputEvent;
|
||||
const eventKeyboard = new EventKeyboard(keyCode, eventType);
|
||||
return eventKeyboard;
|
||||
}
|
||||
|
||||
public onDown (cb: KeyboardCallback) {
|
||||
@@ -1,9 +1,9 @@
|
||||
import { EDITOR, TEST } from 'internal:constants';
|
||||
import { MouseCallback, MouseInputEvent, MouseWheelCallback, MouseWheelInputEvent } from 'pal/input';
|
||||
import { SystemEventType } from '../../../cocos/input/types';
|
||||
import { MouseCallback } from 'pal/input';
|
||||
import { SystemEventType, EventMouse } from '../../../cocos/input/types';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { Rect, Vec2 } from '../../../cocos/core/math';
|
||||
import { SystemEvent } from '../../../cocos/input';
|
||||
import legacyCC from '../../../predefine';
|
||||
|
||||
export class MouseInputSource {
|
||||
public support: boolean;
|
||||
@@ -33,8 +33,16 @@ export class MouseInputSource {
|
||||
return new Rect(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
private _getLocation (event: any): Vec2 {
|
||||
return new Vec2(event.clientX, event.clientY);
|
||||
private _getLocation (mouseEvent: MouseEvent): Vec2 {
|
||||
const canvasRect = this._getCanvasRect();
|
||||
let x = this._pointLocked ? (this._preMousePos.x + mouseEvent.movementX) : (mouseEvent.clientX - canvasRect.x);
|
||||
let y = this._pointLocked ? (this._preMousePos.y - mouseEvent.movementY) : (canvasRect.y + canvasRect.height - mouseEvent.clientY);
|
||||
// TODO: should not call engine API
|
||||
const view = legacyCC.view;
|
||||
const dpr = view._devicePixelRatio;
|
||||
x *= dpr;
|
||||
y *= dpr;
|
||||
return new Vec2(x, y);
|
||||
}
|
||||
|
||||
private _registerEvent () {
|
||||
@@ -48,29 +56,12 @@ export class MouseInputSource {
|
||||
this._canvas?.addEventListener('mousemove', this._createCallback(SystemEventType.MOUSE_MOVE));
|
||||
|
||||
// register mouse up event
|
||||
window.addEventListener('mouseup', this._createCallback(SystemEventType.MOUSE_UP));
|
||||
this._canvas?.addEventListener('mouseup', this._createCallback(SystemEventType.MOUSE_UP));
|
||||
const handleMouseUp = this._createCallback(SystemEventType.MOUSE_UP);
|
||||
window.addEventListener('mouseup', handleMouseUp);
|
||||
this._canvas?.addEventListener('mouseup', handleMouseUp);
|
||||
|
||||
// register wheel event
|
||||
this._canvas?.addEventListener('wheel', (event: WheelEvent) => {
|
||||
const canvasRect = this._getCanvasRect();
|
||||
const location = this._getLocation(event);
|
||||
const wheelSensitivityFactor = 5;
|
||||
const inputEvent: MouseWheelInputEvent = {
|
||||
type: SystemEventType.MOUSE_WHEEL,
|
||||
x: location.x - canvasRect.x,
|
||||
y: canvasRect.y + canvasRect.height - location.y,
|
||||
button: event.button, // TODO: what is the button when tracking mouse move ?
|
||||
deltaX: event.deltaX * wheelSensitivityFactor,
|
||||
deltaY: -event.deltaY * wheelSensitivityFactor,
|
||||
timestamp: performance.now(),
|
||||
movementX: event.movementX,
|
||||
movementY: event.movementY,
|
||||
};
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
this._eventTarget.emit(SystemEventType.MOUSE_WHEEL, inputEvent);
|
||||
});
|
||||
this._canvas?.addEventListener('wheel', this._handleMouseWheel.bind(this));
|
||||
this._registerPointerLockEvent();
|
||||
}
|
||||
|
||||
@@ -92,48 +83,65 @@ export class MouseInputSource {
|
||||
}
|
||||
}
|
||||
|
||||
private _createCallback (eventType: SystemEvent.EventType) {
|
||||
return (event: any) => {
|
||||
const canvasRect = this._getCanvasRect();
|
||||
const location = this._getLocation(event);
|
||||
let button = event.button;
|
||||
switch (event.type) {
|
||||
case 'mousedown':
|
||||
private _createCallback (eventType: SystemEventType) {
|
||||
return (mouseEvent: MouseEvent) => {
|
||||
const location = this._getLocation(mouseEvent);
|
||||
let button = mouseEvent.button;
|
||||
switch (eventType) {
|
||||
case SystemEventType.MOUSE_DOWN:
|
||||
this._canvas?.focus();
|
||||
this._isPressed = true;
|
||||
break;
|
||||
case 'mouseup':
|
||||
case SystemEventType.MOUSE_UP:
|
||||
this._isPressed = false;
|
||||
break;
|
||||
case 'mousemove':
|
||||
case SystemEventType.MOUSE_MOVE:
|
||||
if (!this._isPressed) {
|
||||
button = -1; // TODO: should not access EventMouse.BUTTON_MISSING, need a button enum type
|
||||
button = EventMouse.BUTTON_MISSING;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
const inputEvent: MouseInputEvent = {
|
||||
type: eventType,
|
||||
x: this._pointLocked ? (this._preMousePos.x + <number>event.movementX) : (location.x - canvasRect.x),
|
||||
y: this._pointLocked ? (this._preMousePos.y - <number>event.movementY) : (canvasRect.y + canvasRect.height - location.y),
|
||||
button,
|
||||
timestamp: performance.now(),
|
||||
// this is web only property
|
||||
movementX: event.movementX,
|
||||
movementY: event.movementY,
|
||||
};
|
||||
|
||||
const eventMouse = new EventMouse(eventType, false, this._preMousePos);
|
||||
eventMouse.setLocation(location.x, location.y);
|
||||
eventMouse.setButton(button);
|
||||
eventMouse.movementX = mouseEvent.movementX;
|
||||
eventMouse.movementY = mouseEvent.movementY;
|
||||
|
||||
// update previous mouse position.
|
||||
this._preMousePos.set(inputEvent.x, inputEvent.y);
|
||||
event.stopPropagation();
|
||||
if (event.target === this._canvas) {
|
||||
event.preventDefault();
|
||||
this._preMousePos.set(location.x, location.y);
|
||||
mouseEvent.stopPropagation();
|
||||
if (mouseEvent.target === this._canvas) {
|
||||
mouseEvent.preventDefault();
|
||||
}
|
||||
// emit web mouse event
|
||||
this._eventTarget.emit(eventType, inputEvent);
|
||||
this._eventTarget.emit(eventType, eventMouse);
|
||||
};
|
||||
}
|
||||
|
||||
private _handleMouseWheel (mouseEvent: WheelEvent) {
|
||||
const eventType = SystemEventType.MOUSE_WHEEL;
|
||||
const location = this._getLocation(mouseEvent);
|
||||
const button = mouseEvent.button;
|
||||
|
||||
const eventMouse = new EventMouse(eventType, false, this._preMousePos);
|
||||
eventMouse.setLocation(location.x, location.y);
|
||||
eventMouse.setButton(button);
|
||||
eventMouse.movementX = mouseEvent.movementX;
|
||||
eventMouse.movementY = mouseEvent.movementY;
|
||||
|
||||
const wheelSensitivityFactor = 5;
|
||||
eventMouse.setScrollData(mouseEvent.deltaX * wheelSensitivityFactor, -mouseEvent.deltaY * wheelSensitivityFactor);
|
||||
// update previous mouse position.
|
||||
this._preMousePos.set(location.x, location.y);
|
||||
mouseEvent.stopPropagation();
|
||||
if (mouseEvent.target === this._canvas) {
|
||||
mouseEvent.preventDefault();
|
||||
}
|
||||
this._eventTarget.emit(eventType, eventMouse);
|
||||
}
|
||||
|
||||
onDown (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_DOWN, cb);
|
||||
}
|
||||
@@ -143,7 +151,7 @@ export class MouseInputSource {
|
||||
onUp (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_UP, cb);
|
||||
}
|
||||
onWheel (cb: MouseWheelCallback) {
|
||||
onWheel (cb: MouseCallback) {
|
||||
this._eventTarget.on(SystemEventType.MOUSE_WHEEL, cb);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import { TouchCallback, TouchData, TouchInputEvent } from 'pal/input';
|
||||
import { TouchCallback } from 'pal/input';
|
||||
import { TEST } from 'internal:constants';
|
||||
import { Rect, Vec2 } from '../../../cocos/core/math';
|
||||
import { EventTarget } from '../../../cocos/core/event';
|
||||
import { legacyCC } from '../../../cocos/core/global-exports';
|
||||
import { SystemEvent } from '../../../cocos/input';
|
||||
import { SystemEventType } from '../../../cocos/input/types';
|
||||
import { SystemEventType, Touch, EventTouch } from '../../../cocos/input/types';
|
||||
import { touchManager } from '../touch-manager';
|
||||
import { macro } from '../../../cocos/core/platform/macro';
|
||||
|
||||
export class TouchInputSource {
|
||||
public support: boolean;
|
||||
@@ -30,44 +31,42 @@ export class TouchInputSource {
|
||||
this._canvas?.addEventListener('touchcancel', this._createCallback(SystemEventType.TOUCH_CANCEL));
|
||||
}
|
||||
|
||||
private _createCallback (eventType: SystemEvent.EventType) {
|
||||
return (event: any) => {
|
||||
private _createCallback (eventType: SystemEventType) {
|
||||
return (event: TouchEvent) => {
|
||||
const canvasRect = this._getCanvasRect();
|
||||
const touchDataList: TouchData[] = [];
|
||||
const handleTouches: Touch[] = [];
|
||||
const length = event.changedTouches.length;
|
||||
for (let i = 0; i < length; ++i) {
|
||||
const touch = event.changedTouches[i];
|
||||
const location = this._getLocation(touch);
|
||||
let x = location.x - canvasRect.x;
|
||||
let y = canvasRect.y + canvasRect.height - location.y;
|
||||
// TODO: should not call engine API
|
||||
if (legacyCC.view._isRotated) {
|
||||
const tmp = x;
|
||||
x = canvasRect.height - y;
|
||||
y = tmp;
|
||||
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 === SystemEventType.TOUCH_END || eventType === SystemEventType.TOUCH_CANCEL) {
|
||||
touchManager.releaseTouch(touchID);
|
||||
}
|
||||
handleTouches.push(touch);
|
||||
if (!macro.ENABLE_MULTI_TOUCH) {
|
||||
break;
|
||||
}
|
||||
|
||||
const touchData: TouchData = {
|
||||
identifier: touch.identifier,
|
||||
x,
|
||||
y,
|
||||
force: touch.force,
|
||||
};
|
||||
touchDataList.push(touchData);
|
||||
}
|
||||
const inputEvent: TouchInputEvent = {
|
||||
type: eventType,
|
||||
changedTouches: touchDataList,
|
||||
timestamp: performance.now(),
|
||||
};
|
||||
event.stopPropagation();
|
||||
if (event.target === this._canvas) {
|
||||
event.preventDefault();
|
||||
}
|
||||
if (event.type === 'touchstart') {
|
||||
if (eventType === SystemEventType.TOUCH_START) {
|
||||
this._canvas?.focus();
|
||||
}
|
||||
this._eventTarget.emit(eventType, inputEvent);
|
||||
if (handleTouches.length > 0) {
|
||||
const eventTouch = new EventTouch(handleTouches, false, eventType,
|
||||
macro.ENABLE_MULTI_TOUCH ? touchManager.getAllTouches() : handleTouches);
|
||||
this._eventTarget.emit(eventType, eventTouch);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -80,8 +79,20 @@ export class TouchInputSource {
|
||||
return new Rect(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
private _getLocation (event: Touch): Vec2 {
|
||||
return new Vec2(event.clientX, event.clientY);
|
||||
private _getLocation (touch: globalThis.Touch, canvasRect: Rect): Vec2 {
|
||||
let x = touch.clientX - canvasRect.x;
|
||||
let y = canvasRect.y + canvasRect.height - touch.clientY;
|
||||
// TODO: should not call engine API
|
||||
const view = legacyCC.view;
|
||||
if (view._isRotated) {
|
||||
const tmp = x;
|
||||
x = canvasRect.height - y;
|
||||
y = tmp;
|
||||
}
|
||||
const dpr = view._devicePixelRatio;
|
||||
x *= dpr;
|
||||
y *= dpr;
|
||||
return new Vec2(x, y);
|
||||
}
|
||||
|
||||
public onStart (cb: TouchCallback) {
|
||||
@@ -1,4 +1,3 @@
|
||||
const inputManager = cc.internal.inputManager;
|
||||
const renderer = cc.renderer;
|
||||
const game = cc.game;
|
||||
let _frameRate = 60;
|
||||
|
||||
Reference in New Issue
Block a user