UNPKG

playcanvas

Version:

PlayCanvas WebGL game engine

134 lines (131 loc) 4.42 kB
import { MOUSEBUTTON_NONE } from './constants.js'; /** * @import { Mouse } from './mouse.js' */ /** * Returns true if pointer lock is currently enabled. * * @returns {boolean} True if pointer lock is currently enabled. */ function isMousePointerLocked() { return !!(document.pointerLockElement || document.mozPointerLockElement || document.webkitPointerLockElement); } /** * MouseEvent object that is passed to events 'mousemove', 'mouseup', 'mousedown' and 'mousewheel'. * * @category Input */ class MouseEvent { /** * Create a new MouseEvent instance. * * @param {Mouse} mouse - The Mouse device that is firing this event. * @param {globalThis.MouseEvent|globalThis.WheelEvent} event - The original browser event that fired. */ constructor(mouse, event){ /** * The x coordinate of the mouse pointer relative to the element {@link Mouse} is attached to. * * @type {number} */ this.x = 0; /** * The y coordinate of the mouse pointer relative to the element {@link Mouse} is attached to. * * @type {number} */ this.y = 0; /** * The change in x coordinate since the last mouse event. * * @type {number} */ this.dx = 0; /** * The change in y coordinate since the last mouse event. * * @type {number} */ this.dy = 0; /** * The mouse button associated with this event. Can be: * * - {@link MOUSEBUTTON_LEFT} * - {@link MOUSEBUTTON_MIDDLE} * - {@link MOUSEBUTTON_RIGHT} * * @type {number} */ this.button = MOUSEBUTTON_NONE; /** * A value representing the amount the mouse wheel has moved, only valid for * {@link EVENT_MOUSEWHEEL} events. * * @type {number} */ this.wheelDelta = 0; /** * True if the ctrl key was pressed when this event was fired. * * @type {boolean} */ this.ctrlKey = false; /** * True if the alt key was pressed when this event was fired. * * @type {boolean} */ this.altKey = false; /** * True if the shift key was pressed when this event was fired. * * @type {boolean} */ this.shiftKey = false; /** * True if the meta key was pressed when this event was fired. * * @type {boolean} */ this.metaKey = false; var coords = { x: 0, y: 0 }; if (event) { if (event instanceof MouseEvent) { throw Error('Expected MouseEvent'); } coords = mouse._getTargetCoords(event); } else { event = {}; } if (coords) { this.x = coords.x; this.y = coords.y; } else if (isMousePointerLocked()) { this.x = 0; this.y = 0; } else { return; } // deltaY is in a different range across different browsers. The only thing // that is consistent is the sign of the value so snap to -1/+1. if (event.type === 'wheel') { if (event.deltaY > 0) { this.wheelDelta = 1; } else if (event.deltaY < 0) { this.wheelDelta = -1; } } // Get the movement delta in this event if (isMousePointerLocked()) { this.dx = event.movementX || event.webkitMovementX || event.mozMovementX || 0; this.dy = event.movementY || event.webkitMovementY || event.mozMovementY || 0; } else { this.dx = this.x - mouse._lastX; this.dy = this.y - mouse._lastY; } if (event.type === 'mousedown' || event.type === 'mouseup') { this.button = event.button; } this.buttons = mouse._buttons.slice(0); this.element = event.target; var _event_ctrlKey; this.ctrlKey = (_event_ctrlKey = event.ctrlKey) != null ? _event_ctrlKey : false; var _event_altKey; this.altKey = (_event_altKey = event.altKey) != null ? _event_altKey : false; var _event_shiftKey; this.shiftKey = (_event_shiftKey = event.shiftKey) != null ? _event_shiftKey : false; var _event_metaKey; this.metaKey = (_event_metaKey = event.metaKey) != null ? _event_metaKey : false; this.event = event; } } export { MouseEvent, isMousePointerLocked };