UNPKG

@threlte/extras

Version:

Utilities, abstractions and plugins for your Threlte apps

73 lines (72 loc) 3.12 kB
import type { UseKeyboardReturn } from './useKeyboard.svelte.js'; import type { StandardGamepad, StandardXRGamepad } from './useGamepad/useGamepad.svelte.js'; type GamepadButtonName = Parameters<StandardGamepad['button']>[0] | Parameters<StandardXRGamepad['button']>[0]; type GamepadStickName = Parameters<StandardGamepad['stick']>[0] | Parameters<StandardXRGamepad['stick']>[0]; interface KeyboardBinding { type: 'keyboard'; key: string; } interface GamepadButtonBinding { type: 'gamepadButton'; button: GamepadButtonName; } interface GamepadAxisBinding { type: 'gamepadAxis'; stick: GamepadStickName; axis: 'x' | 'y'; direction: 1 | -1; threshold: number; } type Binding = KeyboardBinding | GamepadButtonBinding | GamepadAxisBinding; declare class ActionState { /** Whether any binding for this action is currently active */ pressed: boolean; /** Whether this action became active this frame */ justPressed: boolean; /** Whether this action became inactive this frame */ justReleased: boolean; /** Analog strength 0–1. Digital bindings produce 0 or 1. */ strength: number; } type ActionDefinitions = Record<string, Binding[]>; interface UseInputMapOptions { /** A `useKeyboard` instance for resolving keyboard bindings. */ keyboard: UseKeyboardReturn; /** * A gamepad returned by `useGamepad()`. Required only if any action uses * `useInputMap.gamepadButton()` or `useInputMap.gamepadAxis()` bindings. */ gamepad?: StandardGamepad | StandardXRGamepad; } declare const bindingHelpers: { /** Bind a keyboard key by its `KeyboardEvent.key` value. Matching is case-insensitive. */ readonly key: (key: string) => KeyboardBinding; /** Bind a standard gamepad button (e.g. `'clusterBottom'`, `'leftTrigger'`). */ readonly gamepadButton: (button: GamepadButtonName) => GamepadButtonBinding; /** * Bind a gamepad stick axis. * * ```ts * gamepadAxis('leftStick', 'x', 1) // right on the left stick * gamepadAxis('leftStick', 'y', -1) // up on the left stick * ``` */ readonly gamepadAxis: (stick: GamepadStickName, axis: "x" | "y", direction: 1 | -1, threshold?: number) => GamepadAxisBinding; }; export type BindingHelpers = typeof bindingHelpers; export declare function useInputMap<T extends ActionDefinitions>(definitionsFn: (map: BindingHelpers) => T, options: UseInputMapOptions): { /** The internal task, for ordering other tasks via `after`/`before`. */ task: import("@threlte/core").Task; /** The most recently active input device. Switches when a new device provides input. */ activeDevice: { readonly current: "keyboard" | "gamepad"; }; action: (name: keyof T & string) => ActionState; axis: (negative: keyof T & string, positive: keyof T & string) => number; vector: (negativeX: keyof T & string, positiveX: keyof T & string, negativeY: keyof T & string, positiveY: keyof T & string) => { x: number; y: number; }; }; export type UseInputMapReturn = ReturnType<typeof useInputMap>; export {};