Struct leafwing_input_manager::input_map::InputMap
source · 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, Reflect)]
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 unifying types
(GamepadButtonType::South, Action::Run),
(GamepadButtonType::LeftTrigger, Action::Hide),
(GamepadButtonType::RightTrigger, Action::Hide),
]);
// Insertion
input_map.insert(MouseButton::Left, Action::Run)
.insert(KeyCode::ShiftLeft, Action::Run)
// Chords
.insert_modified(Modifier::Control, KeyCode::R, Action::Run)
.insert_chord([InputKind::Keyboard(KeyCode::H),
InputKind::GamepadButton(GamepadButtonType::South),
InputKind::Mouse(MouseButton::Middle)],
Action::Run);
// Removal
input_map.clear_action(Action::Hide);
Implementations§
source§impl<A: Actionlike> InputMap<A>
impl<A: Actionlike> InputMap<A>
sourcepub fn handle_clashes(
&self,
action_data: &mut [ActionData],
input_streams: &InputStreams<'_>,
clash_strategy: ClashStrategy
)
pub fn handle_clashes( &self, action_data: &mut [ActionData], input_streams: &InputStreams<'_>, clash_strategy: ClashStrategy )
Resolve clashing inputs, removing action presses that have been overruled
The usize
stored in pressed_actions
corresponds to Actionlike::index
source§impl<A: Actionlike> InputMap<A>
impl<A: Actionlike> InputMap<A>
sourcepub fn new(
bindings: impl IntoIterator<Item = (impl Into<UserInput>, A)>
) -> Self
pub fn new( bindings: impl IntoIterator<Item = (impl Into<UserInput>, A)> ) -> Self
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;
use bevy::prelude::Reflect;
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
enum Action {
Run,
Jump,
}
let input_map = InputMap::new([
(KeyCode::ShiftLeft, Action::Run),
(KeyCode::Space, Action::Jump),
]);
assert_eq!(input_map.len(), 2);
sourcepub fn build(&mut self) -> Self
pub fn build(&mut self) -> Self
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”:
- Create a new
InputMap
struct usingInputMap::default
orInputMap::new
. - Add bindings and configure the struct using a chain of method calls directly on this struct.
- Finish building your struct by calling
.build()
, receiving a concrete struct you can insert as a component.
Note that this is not the original 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;
use bevy::prelude::Reflect;
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
enum Action {
Run,
Jump,
}
let input_map: InputMap<Action> = InputMap::default()
.insert(KeyCode::Space, Action::Jump).build();
source§impl<A: Actionlike> InputMap<A>
impl<A: Actionlike> InputMap<A>
sourcepub fn insert(&mut self, input: impl Into<UserInput>, action: A) -> &mut Self
pub fn insert(&mut self, input: impl Into<UserInput>, action: A) -> &mut Self
Insert a mapping between input
and action
Panics
Panics if the map is full and input
is not a duplicate.
sourcepub fn insert_many_to_one(
&mut self,
input: impl IntoIterator<Item = impl Into<UserInput>>,
action: A
) -> &mut Self
pub fn insert_many_to_one( &mut self, input: impl IntoIterator<Item = impl Into<UserInput>>, action: A ) -> &mut Self
Insert a mapping between many input
’s and one action
Panics
Panics if the map is full and input
is not a duplicate.
sourcepub fn insert_one_to_many(
&mut self,
input: impl Into<UserInput>,
action: impl IntoIterator<Item = A>
) -> &mut Self
pub fn insert_one_to_many( &mut self, input: impl Into<UserInput>, action: impl IntoIterator<Item = A> ) -> &mut Self
Insert a mapping between one input
and many action
’s
Panics
Panics if the map is full and input
is not a duplicate.
sourcepub fn insert_at(
&mut self,
input: impl Into<UserInput>,
action: A,
index: usize
) -> &mut Self
pub fn insert_at( &mut self, input: impl Into<UserInput>, action: A, index: usize ) -> &mut Self
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.
sourcepub fn insert_multiple(
&mut self,
input_action_pairs: impl IntoIterator<Item = (impl Into<UserInput>, A)>
) -> &mut Self
pub fn insert_multiple( &mut self, input_action_pairs: impl IntoIterator<Item = (impl Into<UserInput>, A)> ) -> &mut Self
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.
sourcepub fn insert_chord(
&mut self,
buttons: impl IntoIterator<Item = impl Into<InputKind>>,
action: A
) -> &mut Self
pub fn insert_chord( &mut self, buttons: impl IntoIterator<Item = impl Into<InputKind>>, action: A ) -> &mut Self
Insert a mapping between the simultaneous combination of buttons
and the action
provided
Any iterator that can be converted into a InputKind
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.
When working with keyboard modifier keys, consider using the insert_modified
method instead.
Panics
Panics if the map is full and buttons
is not a duplicate.
sourcepub fn insert_modified(
&mut self,
modifier: Modifier,
input: impl Into<InputKind>,
action: A
) -> &mut Self
pub fn insert_modified( &mut self, modifier: Modifier, input: impl Into<InputKind>, action: A ) -> &mut Self
Inserts a mapping between the simultaneous combination of the Modifier
plus the input
and the action
provided.
When working with keyboard modifiers, should be preferred over insert_chord
.
source§impl<A: Actionlike> InputMap<A>
impl<A: Actionlike> InputMap<A>
sourcepub fn gamepad(&self) -> Option<Gamepad>
pub fn gamepad(&self) -> Option<Gamepad>
Fetches the [Gamepad] associated with the entity controlled by this entity map
If this is None
, input from any connected gamepad will be used.
sourcepub fn set_gamepad(&mut self, gamepad: Gamepad) -> &mut Self
pub fn set_gamepad(&mut self, gamepad: Gamepad) -> &mut Self
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.
sourcepub fn clear_gamepad(&mut self) -> &mut Self
pub fn clear_gamepad(&mut self) -> &mut Self
Clears any [Gamepad] associated with the entity controlled by this input map
source§impl<A: Actionlike> InputMap<A>
impl<A: Actionlike> InputMap<A>
sourcepub fn pressed(
&self,
action: A,
input_streams: &InputStreams<'_>,
clash_strategy: ClashStrategy
) -> bool
pub fn pressed( &self, action: A, input_streams: &InputStreams<'_>, clash_strategy: ClashStrategy ) -> bool
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.
sourcepub fn which_pressed(
&self,
input_streams: &InputStreams<'_>,
clash_strategy: ClashStrategy
) -> Vec<ActionData>
pub fn which_pressed( &self, input_streams: &InputStreams<'_>, clash_strategy: ClashStrategy ) -> Vec<ActionData>
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()
.
source§impl<A: Actionlike> InputMap<A>
impl<A: Actionlike> InputMap<A>
source§impl<A: Actionlike> InputMap<A>
impl<A: Actionlike> InputMap<A>
Trait Implementations§
source§impl<A: Actionlike> Default for InputMap<A>
impl<A: Actionlike> Default for InputMap<A>
source§impl<'de, A> Deserialize<'de> for InputMap<A>where
A: Actionlike + Deserialize<'de> + Eq + Hash,
impl<'de, A> Deserialize<'de> for InputMap<A>where A: Actionlike + Deserialize<'de> + Eq + Hash,
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,
source§impl<A: Actionlike> From<HashMap<A, Vec<UserInput, Global>, RandomState>> for InputMap<A>
impl<A: Actionlike> From<HashMap<A, Vec<UserInput, Global>, RandomState>> for InputMap<A>
source§fn from(map: HashMap<A, Vec<UserInput>>) -> Self
fn from(map: HashMap<A, Vec<UserInput>>) -> Self
Create InputMap<A>
from HashMap<A, Vec<UserInput>>
Panics
Panics if the any value in map contains more than 16 distinct inputs.
Example
use leafwing_input_manager::input_map::InputMap;
use leafwing_input_manager::user_input::UserInput;
use leafwing_input_manager::Actionlike;
use bevy::input::keyboard::KeyCode;
use std::collections::HashMap;
use bevy::prelude::Reflect;
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
enum Action {
Run,
Jump,
}
let mut map: HashMap<Action, Vec<UserInput>> = HashMap::default();
map.insert(
Action::Run,
vec![KeyCode::ShiftLeft.into(), KeyCode::ShiftRight.into()],
);
let input_map = InputMap::from(map);
source§impl<A: Actionlike> FromIterator<(A, UserInput)> for InputMap<A>
impl<A: Actionlike> FromIterator<(A, UserInput)> for InputMap<A>
source§impl<A: PartialEq + Actionlike> PartialEq<InputMap<A>> for InputMap<A>
impl<A: PartialEq + Actionlike> PartialEq<InputMap<A>> for InputMap<A>
impl<A: Eq + Actionlike> Eq for InputMap<A>
impl<A: Actionlike> Resource for InputMap<A>where Self: Send + Sync + 'static,
impl<A: Actionlike> StructuralEq for InputMap<A>
impl<A: Actionlike> StructuralPartialEq for InputMap<A>
Auto Trait Implementations§
impl<A> RefUnwindSafe for InputMap<A>where A: RefUnwindSafe,
impl<A> Send for InputMap<A>
impl<A> Sync for InputMap<A>
impl<A> Unpin for InputMap<A>where A: Unpin,
impl<A> UnwindSafe for InputMap<A>where A: UnwindSafe,
Blanket Implementations§
§impl<T, U> AsBindGroupShaderType<U> for Twhere
U: ShaderType,
&'a T: for<'a> Into<U>,
impl<T, U> AsBindGroupShaderType<U> for Twhere U: ShaderType, &'a T: for<'a> Into<U>,
§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
[ShaderType
] for self
. When used in [AsBindGroup
]
derives, it is safe to assume that all images in self
exist.source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<C> Bundle for Cwhere
C: Component,
impl<C> Bundle for Cwhere C: Component,
fn component_ids( components: &mut Components, storages: &mut Storages, ids: &mut impl FnMut(ComponentId) )
unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> Cwhere F: for<'a> FnMut(&'a mut T) -> OwningPtr<'a, Aligned>,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere T: Any,
§fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>
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
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<C> DynamicBundle for Cwhere
C: Component,
impl<C> DynamicBundle for Cwhere C: Component,
fn get_components( self, func: &mut impl FnMut(StorageType, OwningPtr<'_, Aligned>) )
§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere T: Default,
§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given [World]