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>
impl<A: ActionSet> InputState<A>
Sourcepub fn new(keymap: Keymap<A>) -> Self
pub fn new(keymap: Keymap<A>) -> Self
Input handling with the given bindings and repeat timings.
Sourcepub fn starting_on(self, device: Device) -> Self
pub fn starting_on(self, device: Device) -> Self
Starts as if device had been pressed last (a platform whose only
input is its pad).
Sourcepub fn device(&self) -> Device
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.
Sourcepub fn set_keymap(&mut self, keymap: Keymap<A>)
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.
Sourcepub fn release_all(&mut self)
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.
Sourcepub fn key_down(&mut self, chord: Chord)
pub fn key_down(&mut self, chord: Chord)
A key went down (with its modifier state). Unbound chords and keys already held are ignored.
Sourcepub fn keyboard_used(&mut self)
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.
Sourcepub fn key_up(&mut self, key: Key)
pub fn key_up(&mut self, key: Key)
A key went up. Stops its repeat, if it was the repeating one.
Sourcepub fn pad_down(&mut self, button: Button, kind: PadKind)
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).
Sourcepub fn pad_taken(&mut self, button: Button)
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.)
Sourcepub fn pad_up(&mut self, button: Button)
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.
Sourcepub fn is_held(&self, action: A) -> bool
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.
Sourcepub fn presses(&self) -> &[A]
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.
Sourcepub fn released(&self) -> &[A]
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.
Sourcepub fn update(&mut self, dt: f32) -> Vec<A>
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.