Skip to main content

InputState

Struct InputState 

Source
pub struct InputState<A: ActionSet> { /* private fields */ }
Expand description

Turns key and controller-button events and elapsed time into actions.

Feed it every key press (key_down) and release (key_up), and every controller button press (pad_down) and release (pad_up), then call update once per frame with the frame time. Keys and buttons follow the same rules. A press emits its action once. While a repeatable action’s key or button is held, it re-emits after the repeat delay and then every interval. The most recently pressed repeatable one is the one that repeats; releasing it hands repeating back to the most recent repeatable one still held, which waits a full delay before repeating. Button chords: see the module docs. After a time it heard no keys (a resume from a suspend), it lets go of everything (release_all).

Implementations§

Source§

impl<A: ActionSet> InputState<A>

Source

pub fn new(keymap: Keymap<A>) -> Self

Input handling with the given bindings and repeat timings.

Source

pub fn starting_on(self, device: Device) -> Self

Starts as if device had been pressed last (a platform whose only input is its pad).

Source

pub fn device(&self) -> Device

What the player pressed last: the keyboard, or a pad of some kind. Only presses that do something count (a bound key or button going down), so an unbound key or a resting stick never switches it. The keyboard until anything is pressed, unless the game started it on the platform’s own pad.

Source

pub fn keymap(&self) -> &Keymap<A>

The bindings in use.

Source

pub fn set_keymap(&mut self, keymap: Keymap<A>)

Switches to other bindings (the player picked a layout). Lets go of everything first (release_all), so nothing pressed under the old bindings leaks into the new ones.

Source

pub fn release_all(&mut self)

Lets go of everything: forgets held keys and buttons, pending presses, waiting chord buttons (which give nothing) and any running repeat. A key or button still down is ignored until it is pressed again. The actions it held are let go (released after the next update). The device and the bindings stay. The game calls it on the first frame after a resume, whose keys may have come up while it was suspended.

Source

pub fn key_down(&mut self, chord: Chord)

A key went down (with its modifier state). Unbound chords and keys already held are ignored.

Source

pub fn keyboard_used(&mut self)

A key went down that the game kept for itself (it opened the layout picker): the keyboard is the device pressed last, and nothing else happens.

Source

pub fn key_up(&mut self, key: Key)

A key went up. Stops its repeat, if it was the repeating one.

Source

pub fn pad_down(&mut self, button: Button, kind: PadKind)

A controller button went down, on a pad of kind (Pads merges the pads and gives the button’s binding position). Unbound buttons and buttons already down are ignored. A button in a chord may fire the chord, or hold back its own actions (see the module docs).

Source

pub fn pad_taken(&mut self, button: Button)

The controller button button, just pressed, was taken by the game for itself (it ended a “press any button” wait), so it does nothing else: if it is holding back its own actions for a chord, it gives none of them. (A button that acts on press has given its actions already, in this frame’s update, for the game to ignore.)

Source

pub fn pad_up(&mut self, button: Button)

A controller button went up. A waiting chord button gives its own actions now, unless a chord used it. Stops its repeat, if it was the repeating one, and ends the chords it is in.

Source

pub fn is_held(&self, action: A) -> bool

Whether any held key or button is bound to action (e.g. hold to fast-forward). A chord button holding back its action doesn’t hold it yet.

Source

pub fn presses(&self) -> &[A]

The last update’s actions from presses, in order, without its key repeats: what a fixed step reads (TickDriver). An action pressed again while another of its keys holds it is here again.

Source

pub fn released(&self) -> &[A]

The actions let go before the last update, in order: each when no key or button holds it any more, and a chord button’s own action, given as the button is let go (module docs), at once.

Source

pub fn update(&mut self, dt: f32) -> Vec<A>

Advances time by dt seconds and returns the actions to handle this frame, in order: chord buttons whose window has just passed, presses and releases since the last update, then repeats (at most MAX_REPEATS_PER_UPDATE; repeats beyond the cap are dropped, not deferred). Negative or non-finite dt counts as zero. The same actions without the repeats are presses until the next update, and the actions let go since the last one released.

Trait Implementations§

Source§

impl<A: Clone + ActionSet> Clone for InputState<A>

Source§

fn clone(&self) -> InputState<A>

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<A: Debug + ActionSet> Debug for InputState<A>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<A> Freeze for InputState<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for InputState<A>
where A: RefUnwindSafe,

§

impl<A> Send for InputState<A>
where A: Send,

§

impl<A> Sync for InputState<A>
where A: Sync,

§

impl<A> Unpin for InputState<A>
where A: Unpin,

§

impl<A> UnsafeUnpin for InputState<A>
where A: UnsafeUnpin,

§

impl<A> UnwindSafe for InputState<A>

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.