Struct leafwing_input_manager::action_state::ActionState
source · [−]pub struct ActionState<A: Actionlike> {
pub action_data: Vec<ActionData>,
/* private fields */
}
Expand description
Stores the canonical input-method-agnostic representation of the inputs received
Can be used as either a resource or as a Component
on entities that you wish to control directly from player input.
Example
use leafwing_input_manager::prelude::*;
use bevy_utils::Instant;
#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Debug)]
enum Action {
Left,
Right,
Jump,
}
let mut action_state = ActionState::<Action>::default();
// Typically, this is done automatically by the `InputManagerPlugin` from user inputs
// using the `ActionState::update` method
action_state.press(Action::Jump);
assert!(action_state.pressed(Action::Jump));
assert!(action_state.just_pressed(Action::Jump));
assert!(action_state.released(Action::Left));
// Resets just_pressed and just_released
action_state.tick(Instant::now());
assert!(action_state.pressed(Action::Jump));
assert!(!action_state.just_pressed(Action::Jump));
action_state.release(Action::Jump);
assert!(!action_state.pressed(Action::Jump));
assert!(action_state.released(Action::Jump));
assert!(action_state.just_released(Action::Jump));
action_state.tick(Instant::now());
assert!(action_state.released(Action::Jump));
assert!(!action_state.just_released(Action::Jump));
Fields
action_data: Vec<ActionData>
The ActionData
of each action
The position in this vector corresponds to Actionlike::index
.
Implementations
sourceimpl<A: Actionlike> ActionState<A>
impl<A: Actionlike> ActionState<A>
sourcepub fn update(&mut self, action_data: Vec<ActionData>)
pub fn update(&mut self, action_data: Vec<ActionData>)
Updates the ActionState
based on a vector of ActionData
, ordered by Actionlike::id
.
The action_data
is typically constructed from InputMap::which_pressed
,
which reads from the assorted Input
resources.
sourcepub fn tick(&mut self, current_time: Instant)
pub fn tick(&mut self, current_time: Instant)
Advances the time for all actions
The underlying Timing
and ButtonState
will be advanced according to the current_instant
.
- if no [
Instant
] is set, thecurrent_instant
will be set as the initial time at which the button was pressed / released - the
Duration
will advance to reflect elapsed time
Example
use leafwing_input_manager::prelude::*;
use leafwing_input_manager::buttonlike::ButtonState;
use bevy_utils::Instant;
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Debug)]
enum Action {
Run,
Jump,
}
let mut action_state = ActionState::<Action>::default();
// Actions start released
assert!(action_state.released(Action::Jump));
assert!(!action_state.just_released(Action::Run));
// Ticking time moves causes buttons that were just released to no longer be just released
action_state.tick(Instant::now());
assert!(action_state.released(Action::Jump));
assert!(!action_state.just_released(Action::Jump));
action_state.press(Action::Jump);
assert!(action_state.just_pressed(Action::Jump));
// Ticking time moves causes buttons that were just pressed to no longer be just pressed
action_state.tick(Instant::now());
assert!(action_state.pressed(Action::Jump));
assert!(!action_state.just_pressed(Action::Jump));
sourcepub fn action_data(&self, action: A) -> ActionData
pub fn action_data(&self, action: A) -> ActionData
Gets a copy of the ActionData
of the corresponding action
Generally, it’ll be clearer to call pressed
or so on directly on the ActionState
.
However, accessing the raw data directly allows you to examine detailed metadata holistically.
Example
use leafwing_input_manager::prelude::*;
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Debug)]
enum Action {
Run,
Jump,
}
let mut action_state = ActionState::<Action>::default();
let run_data = action_state.action_data(Action::Run);
dbg!(run_data);
sourcepub fn set_action_data(&mut self, action: A, data: ActionData)
pub fn set_action_data(&mut self, action: A, data: ActionData)
Manually sets the ActionData
of the corresponding action
You should almost always use more direct methods, as they are simpler and less error-prone.
However, this method can be useful for testing,
or when transferring ActionData
between action states.
Example
use leafwing_input_manager::prelude::*;
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Debug)]
enum AbilitySlot {
Slot1,
Slot2,
}
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Debug)]
enum Action {
Run,
Jump,
}
let mut ability_slot_state = ActionState::<AbilitySlot>::default();
let mut action_state = ActionState::<Action>::default();
// Extract the state from the ability slot
let slot_1_state = ability_slot_state.action_data(AbilitySlot::Slot1);
// And transfer it to the actual ability that we care about
// without losing timing information
action_state.set_action_data(Action::Run, slot_1_state);
sourcepub fn press(&mut self, action: A)
pub fn press(&mut self, action: A)
Press the action
No initial instant or reasons why the button was pressed will be recorded
Instead, this is set through ActionState::tick()
sourcepub fn release(&mut self, action: A)
pub fn release(&mut self, action: A)
Release the action
No initial instant will be recorded
Instead, this is set through ActionState::tick()
sourcepub fn consume(&mut self, action: A)
pub fn consume(&mut self, action: A)
Consumes the action
The action will be released, and will not be able to be pressed again
until it would have otherwise been released by ActionState::release
,
ActionState::release_all
or ActionState::update
.
No initial instant will be recorded
Instead, this is set through ActionState::tick()
Example
use leafwing_input_manager::prelude::*;
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Debug)]
enum Action {
Eat,
Sleep,
}
let mut action_state = ActionState::<Action>::default();
action_state.press(Action::Eat);
assert!(action_state.pressed(Action::Eat));
// Consuming actions releases them
action_state.consume(Action::Eat);
assert!(action_state.released(Action::Eat));
// Doesn't work, as the action was consumed
action_state.press(Action::Eat);
assert!(action_state.released(Action::Eat));
// Releasing consumed actions allows them to be pressed again
action_state.release(Action::Eat);
action_state.press(Action::Eat);
assert!(action_state.pressed(Action::Eat));
sourcepub fn release_all(&mut self)
pub fn release_all(&mut self)
Releases all actions
sourcepub fn just_pressed(&self, action: A) -> bool
pub fn just_pressed(&self, action: A) -> bool
Was this action
pressed since the last time tick was called?
sourcepub fn released(&self, action: A) -> bool
pub fn released(&self, action: A) -> bool
Is this action
currently released?
This is always the logical negation of pressed
sourcepub fn just_released(&self, action: A) -> bool
pub fn just_released(&self, action: A) -> bool
Was this action
pressed since the last time tick was called?
sourcepub fn get_pressed(&self) -> Vec<A>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn get_pressed(&self) -> Vec<A>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Which actions are currently pressed?
sourcepub fn get_just_pressed(&self) -> Vec<A>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn get_just_pressed(&self) -> Vec<A>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Which actions were just pressed?
sourcepub fn get_released(&self) -> Vec<A>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn get_released(&self) -> Vec<A>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Which actions are currently released?
sourcepub fn get_just_released(&self) -> Vec<A>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn get_just_released(&self) -> Vec<A>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Which actions were just released?
sourcepub fn reasons_pressed(&self, action: A) -> Vec<UserInput>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn reasons_pressed(&self, action: A) -> Vec<UserInput>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
The reasons (in terms of UserInput
) that the button was pressed
If the button is currently released, the Vec<UserInput
> returned will be empty
Example
use leafwing_input_manager::prelude::*;
use leafwing_input_manager::buttonlike::ButtonState;
use leafwing_input_manager::action_state::ActionData;
use bevy_input::keyboard::KeyCode;
#[derive(Actionlike, Clone)]
enum PlatformerAction{
Move,
Jump,
}
let mut action_state = ActionState::<PlatformerAction>::default();
// Usually this will be done automatically for you, via [`ActionState::update`]
action_state.set_action_data(PlatformerAction::Jump,
ActionData {
state: ButtonState::JustPressed,
// Manually setting the reason why this action was pressed
reasons_pressed: vec![KeyCode::Space.into()],
// For the sake of this example, we don't care about any other fields
..Default::default()
}
);
let reasons_jumped = action_state.reasons_pressed(PlatformerAction::Jump);
assert_eq!(reasons_jumped[0], KeyCode::Space.into());
sourcepub fn instant_started(&self, action: A) -> Option<Instant>
pub fn instant_started(&self, action: A) -> Option<Instant>
The [Instant
] that the action was last pressed or released
If the action was pressed or released since the last time ActionState::tick
was called
the value will be None
.
This ensures that all of our actions are assigned a timing and duration
that corresponds exactly to the start of a frame, rather than relying on idiosyncratic timing.
sourcepub fn current_duration(&self, action: A) -> Duration
pub fn current_duration(&self, action: A) -> Duration
The Duration
for which the action has been held or released
sourcepub fn previous_duration(&self, action: A) -> Duration
pub fn previous_duration(&self, action: A) -> Duration
The Duration
for which the action was last held or released
This is a snapshot of the ActionState::current_duration
state at the time
the action was last pressed or released.
Trait Implementations
sourceimpl<A: Clone + Actionlike> Clone for ActionState<A>
impl<A: Clone + Actionlike> Clone for ActionState<A>
sourcefn clone(&self) -> ActionState<A>
fn clone(&self) -> ActionState<A>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<A: Actionlike> Component for ActionState<A> where
Self: Send + Sync + 'static,
impl<A: Actionlike> Component for ActionState<A> where
Self: Send + Sync + 'static,
type Storage = TableStorage
sourceimpl<A: Debug + Actionlike> Debug for ActionState<A>
impl<A: Debug + Actionlike> Debug for ActionState<A>
sourceimpl<A: Actionlike> Default for ActionState<A>
impl<A: Actionlike> Default for ActionState<A>
sourcefn default() -> ActionState<A>
fn default() -> ActionState<A>
Returns the “default value” for a type. Read more
sourceimpl<'de, A: Actionlike> Deserialize<'de> for ActionState<A>
impl<'de, A: Actionlike> Deserialize<'de> for ActionState<A>
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: PartialEq + Actionlike> PartialEq<ActionState<A>> for ActionState<A>
impl<A: PartialEq + Actionlike> PartialEq<ActionState<A>> for ActionState<A>
sourcefn eq(&self, other: &ActionState<A>) -> bool
fn eq(&self, other: &ActionState<A>) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &ActionState<A>) -> bool
fn ne(&self, other: &ActionState<A>) -> bool
This method tests for !=
.
sourceimpl<A: Actionlike> Serialize for ActionState<A>
impl<A: Actionlike> Serialize for ActionState<A>
impl<A: Actionlike> StructuralPartialEq for ActionState<A>
Auto Trait Implementations
impl<A> RefUnwindSafe for ActionState<A> where
A: RefUnwindSafe,
impl<A> Send for ActionState<A>
impl<A> Sync for ActionState<A>
impl<A> Unpin for ActionState<A> where
A: Unpin,
impl<A> UnwindSafe for ActionState<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