UNPKG

@threlte/extras

Version:

Utilities, abstractions and plugins for your Threlte apps

172 lines (171 loc) 7.08 kB
import { type Task } from '@threlte/core'; import { type GamepadMappings } from './mappings.js'; type UseGamepadOptions = { /** The threshold value of any axis before change events are fired. Default is 0.05. */ axisDeadzone?: number; /** An optional gamepad index, if multiple gamepads are used. */ index?: number; /** * Custom mappings for non-standard controllers, keyed by a `vvvv:pppp` * signature (hex USB vendor:product, lowercase) parsed from `Gamepad.id`. * Custom entries override the built-in table for the same signature. */ mappings?: GamepadMappings; /** * When `false`, skip the built-in mapping table and only use entries * provided via `mappings`. Default `true`. */ useBuiltinMappings?: boolean; xr?: never; hand?: never; } | { /** The threshold value of any axis before change events are fired. Default is 0.05. */ axisDeadzone?: number; xr: true; hand: 'left' | 'right'; }; declare const standardButtons: readonly ["clusterBottom", "clusterRight", "clusterLeft", "clusterTop", "leftBumper", "rightBumper", "leftTrigger", "rightTrigger", "select", "start", "leftStickButton", "rightStickButton", "directionalTop", "directionalBottom", "directionalLeft", "directionalRight", "center"]; declare const xrButtons: readonly ["trigger", "squeeze", "touchpadButton", "thumbstickButton", "clusterBottom", "clusterTop"]; declare const standardAxes: readonly ["leftStick", "rightStick"]; declare const xrAxes: readonly ["touchpad", "thumbstick"]; type StandardGamepadEvents = 'change' | 'press' | 'down' | 'up' | 'touch' | 'touchstart' | 'touchend'; type StandardGamepadButtons = (typeof standardButtons)[number]; type StandardGamepadAxes = (typeof standardAxes)[number]; type XRGamepadButtons = (typeof xrButtons)[number]; type XRGamepadAxes = (typeof xrAxes)[number]; export type StandardGamepadEvent = { type: StandardGamepadEvents; target: StandardGamepadButtons; value: number; } | { type: StandardGamepadEvents; target: XRGamepadButtons; value: number; } | { type: 'change'; target: StandardGamepadAxes; value: { x: number; y: number; }; } | { type: 'change'; target: XRGamepadAxes; value: { x: number; y: number; }; }; type Fn = (event: StandardGamepadEvent) => void; type Events = { [K in StandardGamepadEvents]?: Set<Fn>; }; declare class GamepadButtonState { pressed: boolean; justPressed: boolean; justReleased: boolean; touched: boolean; value: number; on: (name: StandardGamepadEvents, fn: Fn) => () => void; off: (name: StandardGamepadEvents, fn: Fn) => void; constructor(events: Events[], index: number); } declare class GamepadAxisState { x: number; y: number; on: (name: 'change', fn: Fn) => () => void; off: (name: 'change', fn: Fn) => void; constructor(events: Events[], index: number); } declare const createXrStandard: (allEvents: Events, events: Events[]) => { on: (name: StandardGamepadEvents, fn: Fn) => () => void; off: (name: StandardGamepadEvents, fn: Fn) => void; /** The Gamepad connection status */ connected: import("@threlte/core").CurrentWritable<boolean>; /** The raw Gamepad object */ raw: Gamepad | null; /** Get a button by name. */ button: (name: XRGamepadButtons) => GamepadButtonState; /** Get a stick by name. */ stick: (name: XRGamepadAxes) => GamepadAxisState; /** @deprecated Use `button('trigger')` instead */ trigger: GamepadButtonState; /** @deprecated Use `button('squeeze')` instead */ squeeze: GamepadButtonState; /** @deprecated Use `button('touchpadButton')` instead */ touchpadButton: GamepadButtonState; /** @deprecated Use `button('thumbstickButton')` instead */ thumbstickButton: GamepadButtonState; /** @deprecated Use `button('clusterBottom')` instead */ clusterBottom: GamepadButtonState; /** @deprecated Use `button('clusterTop')` instead */ clusterTop: GamepadButtonState; /** @deprecated Use `stick('touchpad')` instead */ touchpad: GamepadAxisState; /** @deprecated Use `stick('thumbstick')` instead */ thumbstick: GamepadAxisState; }; declare const createStandard: (allEvents: Events, events: Events[]) => { on: (name: StandardGamepadEvents, fn: Fn) => () => void; off: (name: StandardGamepadEvents, fn: Fn) => void; /** The Gamepad connection status */ connected: import("@threlte/core").CurrentWritable<boolean>; /** The raw Gamepad object */ raw: Gamepad | null; /** Get a button by name. */ button: (name: StandardGamepadButtons) => GamepadButtonState; /** Get a stick by name. */ stick: (name: StandardGamepadAxes) => GamepadAxisState; /** @deprecated Use `button('clusterBottom')` instead */ clusterBottom: GamepadButtonState; /** @deprecated Use `button('clusterRight')` instead */ clusterRight: GamepadButtonState; /** @deprecated Use `button('clusterLeft')` instead */ clusterLeft: GamepadButtonState; /** @deprecated Use `button('clusterTop')` instead */ clusterTop: GamepadButtonState; /** @deprecated Use `button('leftBumper')` instead */ leftBumper: GamepadButtonState; /** @deprecated Use `button('rightBumper')` instead */ rightBumper: GamepadButtonState; /** @deprecated Use `button('leftTrigger')` instead */ leftTrigger: GamepadButtonState; /** @deprecated Use `button('rightTrigger')` instead */ rightTrigger: GamepadButtonState; /** @deprecated Use `button('select')` instead */ select: GamepadButtonState; /** @deprecated Use `button('start')` instead */ start: GamepadButtonState; /** @deprecated Use `button('leftStickButton')` instead */ leftStickButton: GamepadButtonState; /** @deprecated Use `button('rightStickButton')` instead */ rightStickButton: GamepadButtonState; /** @deprecated Use `button('directionalTop')` instead */ directionalTop: GamepadButtonState; /** @deprecated Use `button('directionalBottom')` instead */ directionalBottom: GamepadButtonState; /** @deprecated Use `button('directionalLeft')` instead */ directionalLeft: GamepadButtonState; /** @deprecated Use `button('directionalRight')` instead */ directionalRight: GamepadButtonState; /** @deprecated Use `button('center')` instead */ center: GamepadButtonState; /** @deprecated Use `stick('leftStick')` instead */ leftStick: GamepadAxisState; /** @deprecated Use `stick('rightStick')` instead */ rightStick: GamepadAxisState; }; export type StandardGamepad = ReturnType<typeof createStandard> & { task: Task; }; export type StandardXRGamepad = ReturnType<typeof createXrStandard> & { task: Task; }; export declare function useGamepad(): StandardGamepad; export declare function useGamepad(options: UseGamepadOptions & { xr?: never; }): StandardGamepad; export declare function useGamepad(options: UseGamepadOptions & { xr: true; }): StandardXRGamepad; export {};