UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

133 lines (132 loc) 4.13 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { MOUSEBUTTON_NONE } from "./constants.js"; function isMousePointerLocked() { return !!(document.pointerLockElement || document.mozPointerLockElement || document.webkitPointerLockElement); } 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. */ __publicField(this, "x", 0); /** * The y coordinate of the mouse pointer relative to the element {@link Mouse} is attached to. */ __publicField(this, "y", 0); /** * The change in x coordinate since the last mouse event. */ __publicField(this, "dx", 0); /** * The change in y coordinate since the last mouse event. */ __publicField(this, "dy", 0); /** * The mouse button associated with this event. Can be: * * - {@link MOUSEBUTTON_LEFT} * - {@link MOUSEBUTTON_MIDDLE} * - {@link MOUSEBUTTON_RIGHT} * * @type {number} */ __publicField(this, "button", MOUSEBUTTON_NONE); /** * The pressed state of all mouse buttons at the time this event was fired. A 3-element * array of booleans for left, middle and right buttons respectively. * * @type {boolean[]} */ __publicField(this, "buttons"); /** * A value representing the amount the mouse wheel has moved, only valid for * {@link Mouse.EVENT_MOUSEWHEEL} events. */ __publicField(this, "wheelDelta", 0); /** * The element that the mouse was fired from. * * @type {Element} */ __publicField(this, "element"); /** * True if the ctrl key was pressed when this event was fired. */ __publicField(this, "ctrlKey", false); /** * True if the alt key was pressed when this event was fired. */ __publicField(this, "altKey", false); /** * True if the shift key was pressed when this event was fired. */ __publicField(this, "shiftKey", false); /** * True if the meta key was pressed when this event was fired. */ __publicField(this, "metaKey", false); /** * The original browser event. * * @type {globalThis.MouseEvent|globalThis.WheelEvent} */ __publicField(this, "event"); let 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; } if (event.type === "wheel") { if (event.deltaY > 0) { this.wheelDelta = 1; } else if (event.deltaY < 0) { this.wheelDelta = -1; } } 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; this.ctrlKey = event.ctrlKey ?? false; this.altKey = event.altKey ?? false; this.shiftKey = event.shiftKey ?? false; this.metaKey = event.metaKey ?? false; this.event = event; } } export { MouseEvent, isMousePointerLocked };