mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
6487e67b36
* export KeyCode * rename to SystemEvent.EventType remove legacy import update pal * systemEventType =value type, SystemEvent.EventType =enum type * support KEY_PRESS KEY_RELEASE, deprecate KEY_UP event * add back Event.NO_TYPE * update pal/input interface * fix ci * Revert "support KEY_PRESS KEY_RELEASE, deprecate KEY_UP event" This reverts commit 623385e5f8e6c133f287753654b21a5bb7adafee. # Conflicts: # pal/input/minigame/keyboard.ts * remove KEY_PRESS event * remove legacy usage of macro.KEY * fix KeyCode spell
150 lines
5.9 KiB
TypeScript
150 lines
5.9 KiB
TypeScript
import { EDITOR, TEST } from 'internal:constants';
|
|
import { MouseCallback, MouseInputEvent, MouseWheelCallback, MouseWheelInputEvent } from 'pal/input';
|
|
import { SystemEventType } from '../../../cocos/core/platform/event-manager/event-enum';
|
|
import { EventTarget } from '../../../cocos/core/event/event-target';
|
|
import { Rect, Vec2 } from '../../../cocos/core/math';
|
|
import { SystemEvent } from '../../../cocos/core/platform/event-manager/system-event';
|
|
|
|
export class MouseInputSource {
|
|
public support: boolean;
|
|
private _canvas?: HTMLCanvasElement;
|
|
private _eventTarget: EventTarget = new EventTarget();
|
|
private _pointLocked = false;
|
|
private _isPressed = false;
|
|
private _preMousePos: Vec2 = new Vec2();
|
|
|
|
constructor () {
|
|
this.support = !EDITOR && document.documentElement.onmouseup !== undefined;
|
|
if (this.support) {
|
|
this._canvas = document.getElementById('GameCanvas') as HTMLCanvasElement;
|
|
if (!this._canvas && !TEST) {
|
|
console.warn('failed to access canvas');
|
|
}
|
|
this._registerEvent();
|
|
}
|
|
}
|
|
|
|
private _getCanvasRect (): Rect {
|
|
const canvas = this._canvas;
|
|
const box = canvas?.getBoundingClientRect();
|
|
if (box) {
|
|
return new Rect(box.x, box.y, box.width, box.height);
|
|
}
|
|
return new Rect(0, 0, 0, 0);
|
|
}
|
|
|
|
private _getLocation (event: any): Vec2 {
|
|
return new Vec2(event.clientX, event.clientY);
|
|
}
|
|
|
|
private _registerEvent () {
|
|
// register mouse down event
|
|
window.addEventListener('mousedown', () => {
|
|
this._isPressed = true;
|
|
});
|
|
this._canvas?.addEventListener('mousedown', this._createCallback(SystemEventType.MOUSE_DOWN));
|
|
|
|
// register mouse move event
|
|
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));
|
|
|
|
// 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._registerPointerLockEvent();
|
|
}
|
|
|
|
// To be removed in the future.
|
|
private _registerPointerLockEvent () {
|
|
const lockChangeAlert = () => {
|
|
const canvas = this._canvas;
|
|
// @ts-expect-error undefined mozPointerLockElement
|
|
if (document.pointerLockElement === canvas || document.mozPointerLockElement === canvas) {
|
|
this._pointLocked = true;
|
|
} else {
|
|
this._pointLocked = false;
|
|
}
|
|
};
|
|
if ('onpointerlockchange' in document) {
|
|
document.addEventListener('pointerlockchange', lockChangeAlert, false);
|
|
} else if ('onmozpointerlockchange' in document) {
|
|
document.addEventListener('mozpointerlockchange', lockChangeAlert, false);
|
|
}
|
|
}
|
|
|
|
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':
|
|
this._canvas?.focus();
|
|
this._isPressed = true;
|
|
break;
|
|
case 'mouseup':
|
|
this._isPressed = false;
|
|
break;
|
|
case 'mousemove':
|
|
if (!this._isPressed) {
|
|
button = -1; // TODO: should not access EventMouse.BUTTON_MISSING, need a button enum type
|
|
}
|
|
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,
|
|
};
|
|
// update previous mouse position.
|
|
this._preMousePos.set(inputEvent.x, inputEvent.y);
|
|
event.stopPropagation();
|
|
if (event.target === this._canvas) {
|
|
event.preventDefault();
|
|
}
|
|
// 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);
|
|
}
|
|
}
|