Struct leafwing_input_manager::input_map::InputMap
source · [−]pub struct InputMap<A: Actionlike> {
pub map: HashMap<A, PetitSet<UserInput, 16>>,
/* 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 one of [GamepadButtonType
], [KeyCode
] or [MouseButton
].
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.
In addition, you can configure the per-mode cap for each InputMode
using InputMap::new
or InputMap::set_per_mode_cap
.
This can be useful if your UI can only display one or two possible keybindings for each input mode.
Example
use bevy::prelude::*;
use leafwing_input_manager::prelude::*;
use leafwing_input_manager::user_input::InputButton;
use strum::EnumIter;
// You can Run!
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, EnumIter)]
enum Action {
Run,
Hide,
}
let mut input_map: InputMap<Action> = InputMap::default();
// Basic insertion
input_map.insert(Action::Run, GamepadButtonType::South);
input_map.insert(Action::Run, MouseButton::Left);
input_map.insert(Action::Run, KeyCode::LShift);
input_map.insert_multiple(Action::Hide, [GamepadButtonType::LeftTrigger, GamepadButtonType::RightTrigger]);
// Combinations!
input_map.insert_chord(Action::Run, [KeyCode::LControl, KeyCode::R]);
input_map.insert_chord(Action::Hide, [InputButton::Keyboard(KeyCode::H),
InputButton::Gamepad(GamepadButtonType::South),
InputButton::Mouse(MouseButton::Middle)]);
// But you can't Hide :(
input_map.clear_action(Action::Hide, None);
Fields
map: HashMap<A, PetitSet<UserInput, 16>>
The raw [HashMap] of PetitSets used to store the input mapping
Implementations
Creates a new empty InputMap
The per_mode_cap
controls the maximum number of inputs of each InputMode
that can be stored.
If a value of 0 is supplied, no cap will be provided (although the global CAP
must still be obeyed).
PANICS: 3 * per_mode_cap
cannot exceed the global CAP
, as we need space to store all mappings.
Is at least one of the corresponding inputs for action
found in the provided input
stream?
pub fn any_pressed(
&self,
inputs: &PetitSet<UserInput, 16>,
gamepad_input_stream: &Input<GamepadButton>,
keyboard_input_stream: &Input<KeyCode>,
mouse_input_stream: &Input<MouseButton>
) -> bool
pub fn any_pressed(
&self,
inputs: &PetitSet<UserInput, 16>,
gamepad_input_stream: &Input<GamepadButton>,
keyboard_input_stream: &Input<KeyCode>,
mouse_input_stream: &Input<MouseButton>
) -> bool
Is at least one of the inputs
pressed?
Is the button
pressed?
Are all of the buttons
pressed?
Returns the mapping between the action
that uses the supplied input_mode
If input_mode
is None
, all inputs will be returned regardless of input mode.
For chords, an input will be returned if any of the contained buttons use that input mode.
If no matching bindings are found, an empty PetitSet
will be returned.
A copy of the values are returned, rather than a reference to them.
The order of these values is stable, in a first-in, first-out fashion.
Use self.map.get
or self.map.get_mut
if you require a reference.
Returns how many bindings are currently registered for the provided action with the provided InputMode
If None
is provided, a total across all input modes will be provided.
A maximum of CAP
bindings across all input modes can be stored for each action,
and insert operations will silently fail if used when CAP
bindings already exist.
Insert a mapping between action
and input
Existing mappings for that action will not be overwritten. If the set for this action is already full, this insertion will silently fail.
pub fn insert_multiple(
&mut self,
action: A,
inputs: impl IntoIterator<Item = impl Into<UserInput>>
)
pub fn insert_multiple(
&mut self,
action: A,
inputs: impl IntoIterator<Item = impl Into<UserInput>>
)
Insert a mapping between action
and the provided inputs
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.
Existing mappings for that action will not be overwritten.
pub fn insert_chord(
&mut self,
action: A,
buttons: impl IntoIterator<Item = impl Into<InputButton>>
)
pub fn insert_chord(
&mut self,
action: A,
buttons: impl IntoIterator<Item = impl Into<InputButton>>
)
Insert a mapping between action
and the simultaneous combination of buttons
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.
Existing mappings for that action will not be overwritten.
Replaces any existing inputs for the action
of the same InputMode
with the provided input
Returns all previously registered inputs, if any
Replaces the input for the action
of the same InputMode
at the same index with the provided input
If the input is a UserInput::Chord
that combines multiple input modes or UserInput::Null
, this method will silently fail.
Returns the replaced input, if any.
Clears all inputs registered for the action
that use the supplied input_mode
If input_mode
is None
, all inputs will be cleared regardless of input mode.
For chords, an input will be removed if any of the contained buttons use that input mode.
Returns all previously registered inputs, if any
Clears the input for the action
with the specified InputMode
at the provided index
Returns the removed input, if any
Clears all inputs that use the supplied input_mode
If input_mode
is None
, all inputs will be cleared regardless of input mode.
For chords, an input will be removed if any of the contained buttons use that input mode.
Returns the subset of the action map that was removed
Returns the per-InputMode
cap on input bindings for every action
Each individual action can have at most this many bindings, making them easier to display and configure.
Sets the per-InputMode
cap on input bindings for every action
Each individual action can have at most this many bindings, making them easier to display and configure. Any excess actions will be removed, and returned from this method.
Supplying a value of 0 removes any per-mode cap.
PANICS: 3 * per_mode_cap
cannot exceed the global CAP
, as we need space to store all mappings.
Assigns a particular [Gamepad
] to the entity controlled by this input map
Clears any [Gamepad] associated with the entity controlled by this input map
Trait Implementations
Auto Trait Implementations
impl<A> RefUnwindSafe for InputMap<A> where
A: RefUnwindSafe,
impl<A> UnwindSafe for InputMap<A> where
A: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
impl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
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
pub fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
pub fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
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
pub fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
pub fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
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
impl<T> FromWorld for T where
T: Default,
impl<T> FromWorld for T where
T: Default,
pub fn from_world(_world: &mut World) -> T
pub fn from_world(_world: &mut World) -> T
Creates Self
using data from the given [World]
pub fn clone_type_data(&self) -> Box<dyn TypeData + 'static, Global>
pub fn vzip(self) -> V
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