Skip to main content

GamepadManager

Struct GamepadManager 

Source
pub struct GamepadManager { /* private fields */ }
Expand description

Owns the state of every gamepad the page has seen and drives the per-frame poll.

A poll re-reads navigator.get_gamepads() and folds the fresh raw readings into the stored state, computing this frame’s edge sets from the previous frame’s held set. The DOM connect / disconnect events are honoured too: a device announced between two polls is already present as a state when the next poll runs, so polling alone never loses a connect or a disconnect.

Implementations§

Source§

impl GamepadManager

Implements the manager query surface on GamepadManager.

Source

pub fn state(&self, index: u32) -> Option<&GamepadState>

Looks up the state of one gamepad by index.

§Arguments
  • u32 - The browser-assigned gamepad index.
§Returns
  • Option<&GamepadState> - The stored state, or None when this index has never been seen.
Source

pub fn state_mut(&mut self, index: u32) -> Option<&mut GamepadState>

Looks up the state of one gamepad by index, mutably.

§Arguments
  • u32 - The browser-assigned gamepad index.
§Returns
  • Option<&mut GamepadState> - The stored state, or None when this index has never been seen.
Source

pub fn is_connected(&self, index: u32) -> bool

Tests whether a gamepad index is currently connected.

§Arguments
  • u32 - The browser-assigned gamepad index.
§Returns
  • bool - True if the pad is connected.
Source

pub fn axis(&self, index: u32, axis: u32) -> Option<f64>

Reads one analog axis of one gamepad with the deadzone applied.

§Arguments
  • u32 - The browser-assigned gamepad index.
  • u32 - The axis index to read.
§Returns
  • Option<f64> - The deadzone-corrected reading, or None when the index is unknown.
Source

pub fn button_value(&self, index: u32, button: u32) -> Option<f64>

Reads one button’s raw pressure on one gamepad.

§Arguments
  • u32 - The browser-assigned gamepad index.
  • u32 - The button index to read.
§Returns
  • Option<f64> - The pressure, or None when the index is unknown.
Source

pub fn is_button_pressed(&self, index: u32, button: u32) -> bool

Tests whether a button on one gamepad went down on this frame.

§Arguments
  • u32 - The browser-assigned gamepad index.
  • u32 - The button index to check.
§Returns
  • bool - True if the button was pressed this frame, false when the index is unknown.
Source

pub fn is_button_held(&self, index: u32, button: u32) -> bool

Tests whether a button on one gamepad is currently held down.

§Arguments
  • u32 - The browser-assigned gamepad index.
  • u32 - The button index to check.
§Returns
  • bool - True if the button is held, false when the index is unknown.
Source

pub fn is_button_released(&self, index: u32, button: u32) -> bool

Tests whether a button on one gamepad came up on this frame.

§Arguments
  • u32 - The browser-assigned gamepad index.
  • u32 - The button index to check.
§Returns
  • bool - True if the button was released this frame, false when the index is unknown.
Source

pub fn button_action(&self, index: u32, button: u32) -> InputAction

Resolves a button’s current state on one gamepad into enum form.

§Arguments
  • u32 - The browser-assigned gamepad index.
  • u32 - The button index to check.
§Returns
  • InputAction - The edge state, Idle when the index is unknown.
Source

pub fn connected_count(&self) -> usize

Counts the gamepads that are currently connected.

§Returns
  • usize - The number of connected gamepads.
Source

pub fn end_frame(&mut self)

Clears every pad’s per-frame data and the manager’s edge sets.

Mirrors InputState::end_frame: pressed and released are per-frame and are dropped, while held sets and the connected flags survive.

Source§

impl GamepadManager

Implements the DOM wiring layer on GamepadManager.

The Gamepad API is a polling API: the browser only mutates navigator.get_gamepads() snapshots and never emits per-button events, so button edges must be derived by diffing one frame’s snapshot against the last. The two connect / disconnect events are still bound so a device that appears or vanishes between two polls is reflected in connected / disconnected on the same frame rather than one frame late; a poll alone would also detect both, so the events are a latency optimisation and the poll is the authority.

Source

pub fn poll(&mut self, window: &Window)

Re-reads every gamepad from the DOM and recomputes this frame’s button edges.

A pad that the browser no longer lists is marked disconnected and its edge sets flushed, so a yanked controller cannot keep reporting held buttons forever.

§Arguments
  • &Window - The global window, used to reach navigator.
Source

pub fn attach( manager_cell: GamepadManagerCell, window: &Window, ) -> GamepadManagerCell

Attaches the gamepad listeners and returns the shared cell.

§Arguments
  • GamepadManagerCell - The shared manager to mutate.
  • &Window - The global window, receiving the connect and disconnect events.
§Returns
  • GamepadManagerCell - The same cell passed in, for convenient chaining.
Source

pub fn attach_gamepad(manager_cell: &GamepadManagerCell, window: &Window)

Binds the connect / disconnect listeners to window.

§Arguments
  • &GamepadManagerCell - The shared gamepad manager.
  • &Window - The global window.
Source§

impl GamepadManager

Source

pub fn get_states(&self) -> &GamepadStateMap

Source

pub fn set_states(&mut self, val: GamepadStateMap) -> &mut Self

Source

pub fn get_connected(&self) -> &GamepadIndexSet

Source

pub fn set_connected(&mut self, val: GamepadIndexSet) -> &mut Self

Source

pub fn get_disconnected(&self) -> &GamepadIndexSet

Source

pub fn set_disconnected(&mut self, val: GamepadIndexSet) -> &mut Self

Trait Implementations§

Source§

impl Clone for GamepadManager

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GamepadManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for GamepadManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for GamepadManager

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for GamepadManager

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more