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 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.
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::InputButton;
// 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 `InputButton` or `UserInput` if needed
// as unifiying types
(Action::Run, GamepadButtonType::South),
(Action::Hide, GamepadButtonType::LeftTrigger),
(Action::Hide, GamepadButtonType::RightTrigger),
]);
// Insertion
input_map.insert(Action::Run, MouseButton::Left)
.insert(Action::Run, KeyCode::LShift)
// Chords
.insert_chord(Action::Run, [KeyCode::LControl, KeyCode::R])
.insert_chord(Action::Hide, [InputButton::Keyboard(KeyCode::H),
InputButton::Gamepad(GamepadButtonType::South),
InputButton::Mouse(MouseButton::Middle)]);
// 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 = (A, impl Into<UserInput>)>
) -> Self
pub fn new(
bindings: impl IntoIterator<Item = (A, impl Into<UserInput>)>
) -> Self
Creates a new InputMap
from an iterator of (action, user_input)
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([
(Action::Run, KeyCode::LShift),
(Action::Jump, KeyCode::Space),
]);
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(Action::Jump, KeyCode::Space).build();
sourceimpl<A: Actionlike> InputMap<A>
impl<A: Actionlike> InputMap<A>
sourcepub fn insert(&mut self, action: A, input: impl Into<UserInput>) -> &mut Self
pub fn insert(&mut self, action: A, input: impl Into<UserInput>) -> &mut Self
Insert a mapping between action
and input
Panics
Panics if the map is full and input
is not a duplicate.
sourcepub fn insert_at(
&mut self,
action: A,
input: impl Into<UserInput>,
index: usize
) -> &mut Self
pub fn insert_at(
&mut self,
action: A,
input: impl Into<UserInput>,
index: usize
) -> &mut Self
Insert a mapping between action
and input
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,
inputs: impl IntoIterator<Item = (A, impl Into<UserInput>)>
) -> &mut Self
pub fn insert_multiple(
&mut self,
inputs: impl IntoIterator<Item = (A, impl Into<UserInput>)>
) -> &mut Self
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.
Panics
Panics if the map is full and any of inputs
is not a duplicate.
sourcepub fn insert_chord(
&mut self,
action: A,
buttons: impl IntoIterator<Item = impl Into<InputButton>>
) -> &mut Self
pub fn insert_chord(
&mut self,
action: A,
buttons: impl IntoIterator<Item = impl Into<InputButton>>
) -> &mut Self
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.
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
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
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
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
sourceimpl<T> FromWorld for T where
T: Default,
impl<T> FromWorld for T where
T: Default,
sourcefn 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>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
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>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
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