blaze-2d
Version:
A fast and simple WebGL 2 2D game engine written in TypeScript
58 lines (57 loc) • 1.73 kB
TypeScript
import { vec2 } from "gl-matrix";
/**
* Enum for {@link MouseEvent.state} numbers
*/
export declare enum Mouse {
LEFT = 0,
MIDDLE = 1,
RIGHT = 2,
MOVE = 123
}
/**
* A callback to be executed on mouse button events.
*/
export declare type MouseCallback = (pressed: boolean, pos: vec2) => void;
/**
* Handles mouse events for an {@link HTMLElement}.
*/
export default class MouseHandler {
readonly element: HTMLElement;
/**
* The mouse's current position within the element.
*/
private position;
private buttons;
private listeners;
constructor(element: HTMLElement);
private addListeners;
/**
* Gets the current mouse position within the element.
*
* If the mouse has not yet entered the element then the position will be [0, 0].
*
* @returns The mouse position in pixels as a {@link vec2};
*/
getMousePos(): vec2;
/**
* Determines wether the given mouse button is pressed.
*
* @param button The {@link Mouse} button to check
* @returns Wether the given button is pressed or not
*/
isPressed(button?: Mouse): boolean;
/**
* Attaches a listener to a given mouse button that is called whenever the state of the button changes.
*
* @param button The {@link Mouse} button to attach the listener to
* @param cb The callback to execute on a mousedown/mouseup event
*/
addListener(button: Mouse, cb: MouseCallback): void;
/**
* Removes a listener from a given mouse button.
*
* @param button The {@link Mouse} button the listener is attached to
* @param cb The attached listener
*/
removeListener(button: Mouse, cb: MouseCallback): void;
}