pub struct InputMap<A: Actionlike> { /* private fields */ }
Expand description

Maps from raw inputs to an input-method agnostic representation

Multiple inputs can be mapped to the same action, and each input can be mapped to multiple actions.

The provided input types must be able to be converted into a UserInput.

The maximum number of bindings (total) that can be stored for each action is 16. Insertions will silently fail if you have reached this cap.

By default, if two actions would be triggered by a combination of buttons, and one combination is a strict subset of the other, only the larger input is registered. For example, pressing both S and Ctrl + S in your text editor app would save your file, but not enter the letters s. Set the ClashStrategy resource to configure this behavior.

Example

use bevy::prelude::*;
use leafwing_input_manager::prelude::*;
use leafwing_input_manager::user_input::InputKind;

// You can Run!
// But you can't Hide :(
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash)]
enum Action {
    Run,
    Hide,
}

// Construction
let mut input_map = InputMap::new([
   // Note that the type of your iterators must be homogenous;
   // you can use `InputKind` or `UserInput` if needed
   // as unifiying types
  (GamepadButtonType::South, Action::Run),
  (GamepadButtonType::LeftTrigger, Action::Hide),
  (GamepadButtonType::RightTrigger, Action::Hide),
]);

// Insertion
input_map.insert(MouseButton::Left, Action::Run)
.insert(KeyCode::LShift, Action::Run)
// Chords
.insert_chord([KeyCode::LControl, KeyCode::R], Action::Run)
.insert_chord([InputKind::Keyboard(KeyCode::H),
               InputKind::GamepadButton(GamepadButtonType::South),
               InputKind::Mouse(MouseButton::Middle)],
           Action::Run)
.insert_chord([InputKind::Keyboard(KeyCode::H),
               InputKind::GamepadButton(GamepadButtonType::South),
               InputKind::Mouse(MouseButton::Middle)],
           Action::Hide);

// Removal
input_map.clear_action(Action::Hide);

Implementations

Resolve clashing inputs, removing action presses that have been overruled

The usize stored in pressed_actions corresponds to Actionlike::index

Creates a new InputMap from an iterator of (user_input, action) pairs

To create an empty input map, use the Default::default method instead.

Example
use leafwing_input_manager::input_map::InputMap;
use leafwing_input_manager::Actionlike;
use bevy::input::keyboard::KeyCode;

#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash)]
enum Action {
    Run,
    Jump,
}

let input_map = InputMap::new([
    (KeyCode::LShift, Action::Run),
    (KeyCode::Space, Action::Jump),
]);

assert_eq!(input_map.len(), 2);

Constructs a new InputMap from a &mut InputMap, allowing you to insert or otherwise use it

This is helpful when constructing input maps using the “builder pattern”:

  1. Create a new InputMap struct using InputMap::default or InputMap::new.
  2. Add bindings and configure the struct using a chain of method calls directly on this struct.
  3. Finish building your struct by calling .build(), receiving a concrete struct you can insert as a component.

Note that this is not the orginal input map, as we do not have ownership of the struct. Under the hood, this is just a more-readable call to .clone().

Example
use leafwing_input_manager::prelude::*;
use bevy::input::keyboard::KeyCode;

#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash)]
enum Action {
    Run,
    Jump,
}

let input_map: InputMap<Action> = InputMap::default()
  .insert(KeyCode::Space, Action::Jump).build();

Insert a mapping between input and action

Panics

Panics if the map is full and input is not a duplicate.

Insert a mapping between input and action at the provided index

If a matching input already existed in the set, it will be moved to the supplied index. Any input that was previously there will be moved to the matching input’s original index.

Panics

Panics if the map is full and input is not a duplicate.

Insert a mapping between the provided input_action_pairs

This method creates multiple distinct bindings. If you want to require multiple buttons to be pressed at once, use insert_chord. Any iterator that can be converted into a UserInput can be supplied.

Panics

Panics if the map is full and any of inputs is not a duplicate.

Insert a mapping between the simultaneous combination of buttons and the action provided

Any iterator that can be converted into a [Button] can be supplied, but will be converted into a PetitSet for storage and use. Chords can also be added with the insert method, if the UserInput::Chord variant is constructed explicitly.

Panics

Panics if the map is full and buttons is not a duplicate.

Merges the provided InputMap into the InputMap this method was called on

This adds both of their bindings to the resulting InputMap. Like usual, any duplicate bindings are ignored.

If the associated gamepads do not match, the resulting associated gamepad will be set to None.

Fetches the [Gamepad] associated with the entity controlled by this entity map

If this is None, input from any connected gamepad will be used.

Assigns a particular [Gamepad] to the entity controlled by this input map

If this is not called, input from any connected gamepad will be used. The first matching non-zero input will be accepted, as determined by gamepad registration order.

Because of this robust fallback behavior, this method can typically be ignored when writing single-player games.

Clears any [Gamepad] associated with the entity controlled by this input map

Is at least one of the corresponding inputs for action found in the provided input streams?

Accounts for clashing inputs according to the ClashStrategy. If you need to inspect many inputs at once, prefer InputMap::which_pressed instead.

Returns the actions that are currently pressed, and the responsible UserInput for each action

Accounts for clashing inputs according to the ClashStrategy. The position in each vector corresponds to Actionlike::index().

Returns an iterator over actions with their inputs

Returns an iterator over all mapped inputs

Returns the action mappings

How many input bindings are registered total?

Are any input bindings registered at all?

Clears all inputs registered for the action

Removes the input for the action at the provided index

Returns true if an element was found.

Removes the input for the action, if it exists

Returns Some with index if the input was found, or None if no matching input was found.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Deserialize this value from the given Serde deserializer. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Return the T [ShaderType] for self. When used in [AsBindGroup] derives, it is safe to assume that all images in self exist. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait. Read more

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read more

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s. Read more

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait. Read more

Returns the argument unchanged.

Creates Self using data from the given [World]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more