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)]
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
sourceimpl<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
sourceimpl<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;
#[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);
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 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();
sourceimpl<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_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 [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.
sourceimpl<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
sourceimpl<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>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn which_pressed(
&self,
input_streams: &InputStreams<'_>,
clash_strategy: ClashStrategy
) -> Vec<ActionData>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
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()
.
sourceimpl<A: Actionlike> InputMap<A>
impl<A: Actionlike> InputMap<A>
sourceimpl<A: Actionlike> InputMap<A>
impl<A: Actionlike> InputMap<A>
Trait Implementations
sourceimpl<A: Clone + Actionlike> Clone for InputMap<A>
impl<A: Clone + Actionlike> Clone for InputMap<A>
sourceimpl<A: Actionlike> Component for InputMap<A> where
Self: Send + Sync + 'static,
impl<A: Actionlike> Component for InputMap<A> where
Self: Send + Sync + 'static,
type Storage = TableStorage
sourceimpl<A: Debug + Actionlike> Debug for InputMap<A>
impl<A: Debug + Actionlike> Debug for InputMap<A>
sourceimpl<A: Actionlike> Default for InputMap<A>
impl<A: Actionlike> Default for InputMap<A>
sourceimpl<'de, A: Actionlike> Deserialize<'de> for InputMap<A> where
InputMap<A>: Default,
impl<'de, A: Actionlike> Deserialize<'de> for InputMap<A> where
InputMap<A>: Default,
sourcefn 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>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<A: Actionlike> Serialize for InputMap<A>
impl<A: Actionlike> Serialize 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 T where
U: ShaderType,
&'a T: for<'a> Into<U>,
impl<T, U> AsBindGroupShaderType<U> for T where
U: ShaderType,
&'a T: for<'a> Into<U>,
fn as_bind_group_shader_type(
&self,
_images: &HashMap<Handle<Image>, <Image as RenderAsset>::PreparedAsset, RandomState, Global>
) -> U
fn as_bind_group_shader_type(
&self,
_images: &HashMap<Handle<Image>, <Image as RenderAsset>::PreparedAsset, RandomState, Global>
) -> U
Return the T
[ShaderType
] for self
. When used in [AsBindGroup
]
derives, it is safe to assume that all images in self
exist. Read more
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
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
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
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
fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
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,
fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self
using data from the given [World]
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> Serialize for T where
T: Serialize + ?Sized,
impl<T> Serialize for T where
T: Serialize + ?Sized,
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>
impl<T> TypeData for T where
T: 'static + Send + Sync + Clone,
impl<T> TypeData for T where
T: 'static + Send + Sync + Clone,
fn clone_type_data(&self) -> Box<dyn TypeData + 'static, Global>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more