mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
86f20da0f0
* support pal/input update * update * fix esLint * support minigame * support pal/input on minigame * remove minigame.isSubContext * support native update * deprecate static property on EventMouse * deprecated static property in EventTouch * remove TODO * remove x y position in MouseWheel Event * remove TODO * remove temp code * fix esLint * update AccelerometerIntervalMode * remove legacy code in JSB * fix esLint * optimize AccelerometerInputSource.setInterval * fix esLint * optimize code * add more details for documentation * update * don't pass game.canvas to inputManager.registerSystemEvent * optimize * Revert "remove x y position in MouseWheel Event" This reverts commit b521ec4948d693b420887cdeb2f5f3afc1500066. # Conflicts: # @types/pal/input.d.ts * update * fix esLint * fix esLint
71 lines
2.7 KiB
TypeScript
71 lines
2.7 KiB
TypeScript
import { MouseCallback, MouseInputEvent, MouseWheelCallback, MouseWheelInputEvent } from 'pal/input';
|
|
import { system } from 'pal/system';
|
|
import { EventMouse } from '../../../cocos/core/platform/event-manager/events';
|
|
import { EventTarget } from '../../../cocos/core/event/event-target';
|
|
import { Vec2 } from '../../../cocos/core/math';
|
|
import { SystemEventType } from '../../../cocos/core/platform/event-manager/event-enum';
|
|
|
|
export class MouseInputSource {
|
|
public support: boolean;
|
|
private _eventTarget: EventTarget = new EventTarget();
|
|
|
|
constructor () {
|
|
this.support = !system.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 viewSize = system.getViewSize();
|
|
const matchStandardFactor = 120;
|
|
const inputEvent: MouseWheelInputEvent = {
|
|
type: SystemEventType.MOUSE_WHEEL,
|
|
x: location.x,
|
|
y: viewSize.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: string) {
|
|
return (event: jsb.MouseEvent) => {
|
|
const location = this._getLocation(event);
|
|
const viewSize = system.getViewSize();
|
|
const inputEvent: MouseInputEvent = {
|
|
type: eventType,
|
|
x: location.x,
|
|
y: viewSize.height - location.y,
|
|
button: event.button,
|
|
timestamp: performance.now(),
|
|
};
|
|
// 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);
|
|
}
|
|
}
|