Files
bworld/client/input_manager.ts

266 lines
6.1 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { platform } from "./platform.ts";
export type KeyCode =
// letters
| "KeyA"
| "KeyB"
| "KeyC"
| "KeyD"
| "KeyE"
| "KeyF"
| "KeyG"
| "KeyH"
| "KeyI"
| "KeyJ"
| "KeyK"
| "KeyL"
| "KeyM"
| "KeyN"
| "KeyO"
| "KeyP"
| "KeyQ"
| "KeyR"
| "KeyS"
| "KeyT"
| "KeyU"
| "KeyV"
| "KeyW"
| "KeyX"
| "KeyY"
| "KeyZ"
// top numbers
| "Digit0"
| "Digit1"
| "Digit2"
| "Digit3"
| "Digit4"
| "Digit5"
| "Digit6"
| "Digit7"
| "Digit8"
| "Digit9"
// arrows
| "ArrowUp"
| "ArrowDown"
| "ArrowLeft"
| "ArrowRight"
// controls
| "Space"
| "Escape"
| "Enter"
| "Tab"
| "ShiftLeft"
| "ShiftRight"
| "ControlLeft"
| "ControlRight"
| "MetaLeft"
| "MetaRight"
| "AltLeft"
| "AltRight"
| "Home"
| "End"
| "Backspace"
| "Slash"
| "Delete"
// f
| "F1"
| "F2"
| "F3"
| "F4"
| "F5"
| "F6"
| "F7"
| "F8"
| "F9"
| "F10"
| "F11"
| "F12";
// what the player presses and how the mouse moves, kept for the frame. where it runs feeds it events: the page's
// (browser_input.ts) or the desktop app's window (native/), and grabs the mouse for it (see platform.ts)
export class InputManager {
static keys_down = new Set<string>();
static keys_pressed = new Set<string>();
static keys_released = new Set<string>();
static mouse_buttons_down = new Set<number>();
static mouse_buttons_pressed = new Set<number>();
static mouse_buttons_released = new Set<number>();
static mouse_buttons_consumed = new Set<number>();
static mouse_x = 0;
static mouse_y = 0;
// the cursor's movement over the page, while it isn't locked, like for dragging windows
static mouse_delta_x = 0;
static mouse_delta_y = 0;
// the mouse's movement while it's locked to the game, which turns the camera. kept apart so the cursor moving
// around a screen can never turn it
static look_delta_x = 0;
static look_delta_y = 0;
static wheel_delta = 0;
// in the order they were typed. a list, since the same letter can be typed twice in one frame
static typed_characters: string[] = [];
// letters pressed with ctrl (cmd on macs) held, like "a" for select all
static shortcuts: string[] = [];
// code is a KeyCode, key what it types: a character, or a name like "Enter". shortcut is whether ctrl (cmd on
// macs) is held for one, then a letter is a shortcut instead of being typed
static key_down(code: string, key: string, shortcut: boolean) {
if (!this.keys_down.has(code)) {
this.keys_pressed.add(code);
}
this.keys_down.add(code);
if (key.length === 1) {
if (shortcut) {
this.shortcuts.push(key.toLowerCase());
} else {
this.typed_characters.push(key);
}
}
}
// for where typing comes apart from keys, the desktop app's window
static type_text(text: string) {
this.typed_characters.push(...text);
}
static key_up(code: string) {
this.keys_down.delete(code);
this.keys_released.add(code);
}
// buttons are numbered like the dom's: 0 left, 1 middle, 2 right
static mouse_down(button: number) {
if (!this.mouse_buttons_down.has(button)) {
this.mouse_buttons_pressed.add(button);
}
this.mouse_buttons_down.add(button);
}
static mouse_up(button: number) {
this.mouse_buttons_down.delete(button);
this.mouse_buttons_released.add(button);
}
// the cursor at x y, in the canvas's pixels
static move_cursor(x: number, y: number) {
// not from where it was before the mouse was grabbed, that's -1
if (this.mouse_x >= 0 && this.mouse_y >= 0) {
this.mouse_delta_x += x - this.mouse_x;
this.mouse_delta_y += y - this.mouse_y;
}
this.mouse_x = x;
this.mouse_y = y;
}
// the grabbed mouse moved
static look(x: number, y: number) {
this.look_delta_x += x;
this.look_delta_y += y;
}
// down is positive, about 100 for a notch of a wheel
static scroll(amount: number) {
this.wheel_delta += amount;
}
// keys and buttons let go while the game didn't have focus never come up, so they'd stay held
static release_all() {
this.keys_down.clear();
this.mouse_buttons_down.clear();
}
static is_key_down(code: KeyCode): boolean {
return this.keys_down.has(code);
}
static is_key_pressed(code: KeyCode): boolean {
return this.keys_pressed.has(code);
}
static is_key_released(code: KeyCode): boolean {
return this.keys_released.has(code);
}
static is_mouse_down(button: number): boolean {
return this.mouse_buttons_down.has(button);
}
static is_mouse_pressed(button: number): boolean {
return this.mouse_buttons_pressed.has(button) &&
!this.mouse_buttons_consumed.has(button);
}
static is_mouse_released(button: number): boolean {
return this.mouse_buttons_released.has(button);
}
static get_mouse_position() {
return { x: this.mouse_x, y: this.mouse_y };
}
static get_mouse_delta() {
return { x: this.mouse_delta_x, y: this.mouse_delta_y };
}
// how far the locked mouse moved this frame, for turning the camera
static get_look_delta() {
return { x: this.look_delta_x, y: this.look_delta_y };
}
static get_wheel_delta(): number {
return this.wheel_delta;
}
static consume_mouse(button: number) {
this.mouse_buttons_consumed.add(button);
}
static is_shortcut_pressed(letter: string): boolean {
return this.shortcuts.includes(letter);
}
static get_typed_characters(): string[] {
const chars = this.typed_characters;
this.typed_characters = [];
return chars;
}
static update() {
this.keys_pressed.clear();
this.keys_released.clear();
this.mouse_buttons_pressed.clear();
this.mouse_buttons_released.clear();
this.mouse_delta_x = 0;
this.mouse_delta_y = 0;
this.look_delta_x = 0;
this.look_delta_y = 0;
this.wheel_delta = 0;
this.typed_characters = [];
this.shortcuts = [];
this.mouse_buttons_consumed.clear();
}
// the game calls this every frame
static set_mouse_grabbed(grab: boolean) {
platform.set_mouse_grabbed(grab);
}
// whether the mouse was taken away since the last call, like browsers do with escape
static take_lost_pointer_lock() {
return platform.take_lost_mouse();
}
static is_mouse_grabbed() {
return platform.is_mouse_grabbed();
}
static toggle_fullscreen() {
platform.toggle_fullscreen();
}
}