Bg2MouseEvent
The Bg2MouseEvent class represents mouse input events in the bg2 engine TypeScript API. It extends EventBase, providing support for event propagation control.
This class encapsulates mouse interaction data such as button state, cursor position, and wheel delta, while also exposing the original native browser event.
MouseButton
Section titled “MouseButton”Defines the mouse buttons:
export enum MouseButton { LEFT = 0, MIDDLE = 1, RIGHT = 2, NONE = -1}MouseButtonEventType
Section titled “MouseButtonEventType”Defines the type of button event:
export enum MouseButtonEventType { NONE = 0, UP = 1, DOWN = 2}Constructor
Section titled “Constructor”constructor(button?: MouseButton, x?: number, y?: number, delta?: number, event?: any)
Section titled “constructor(button?: MouseButton, x?: number, y?: number, delta?: number, event?: any)”Creates a new mouse event instance.
Parameters
Section titled “Parameters”button: Mouse button involved in the eventx: Cursor X position (canvas-relative)y: Cursor Y position (canvas-relative)delta: Wheel delta value (used in wheel events)event: Original native browser event
Properties
Section titled “Properties”button: MouseButton
Section titled “button: MouseButton”Mouse button associated with the event.
x: number
Section titled “x: number”Horizontal position of the cursor relative to the canvas.
y: number
Section titled “y: number”Vertical position of the cursor relative to the canvas.
delta: number
Section titled “delta: number”Scroll delta value for wheel events.
event: any
Section titled “event: any”Reference to the original native mouse event.
Utility Functions
Section titled “Utility Functions”createMouseEvent(evt: any, mainLoop: any, buttonType: MouseButtonEventType): MouseEvent
Section titled “createMouseEvent(evt: any, mainLoop: any, buttonType: MouseButtonEventType): MouseEvent”Creates a Bg2MouseEvent from a native browser event.
This function:
- Updates the internal mouse position
- Updates button state
- Returns a normalized event instance
Mouse Button State Helpers
Section titled “Mouse Button State Helpers”These functions provide global access to mouse button states:
leftMouseButton(): booleanmiddleMouseButton(): booleanrightMouseButton(): boolean
setMouseButton(event: any, status: boolean): void
Section titled “setMouseButton(event: any, status: boolean): void”Updates the internal state of a mouse button.
clearMouseButtons(): void
Section titled “clearMouseButtons(): void”Resets all mouse button states.
Usage Notes
Section titled “Usage Notes”- Used in
AppControllercallbacks such asmouseDown,mouseUp,mouseMove,mouseDrag, andmouseWheel - Coordinates are always relative to the canvas
- Use
deltaonly for wheel events - Use
eventfor low-level browser-specific data if needed
Example
Section titled “Example”mouseDown(evt: Bg2MouseEvent) { console.log(`Mouse down at ${evt.x}, ${evt.y}`);
if (evt.button === MouseButton.LEFT) { console.log("Left button pressed"); }}
mouseWheel(evt: Bg2MouseEvent) { console.log(`Wheel delta: ${evt.delta}`);}