Struct leafwing_input_manager::action_state::ActionState
source · [−]pub struct ActionState<A: Actionlike> { /* private fields */ }
Expand description
Stores the canonical input-method-agnostic representation of the inputs received
Intended to be used as a [Component
] on entities that you wish to control directly from player input.
Example
use leafwing_input_manager::prelude::*;
use bevy::utils::Instant;
use strum::EnumIter;
#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug, EnumIter)]
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));
Implementations
Updates the ActionState
based on the InputMap
and the provided [Input
]s
Presses and releases buttons according to the current state of the inputs.
Combine with ActionState::tick
to update just_pressed
and just_released
.
Advances the time for all virtual buttons
The underlying VirtualButtonState
state will be advanced according to the current_time
.
- if no [
Instant
] is set, thecurrent_time
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::action_state::VirtualButtonState;
use strum::EnumIter;
use bevy::utils::Instant;
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, EnumIter, Debug)]
enum Action {
Run,
Jump,
}
let mut action_state = ActionState::<Action>::default();
// Virtual buttons start released
assert!(action_state.state(Action::Run).just_released());
assert!(action_state.just_released(Action::Jump));
// 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));
Gets the VirtualButtonState
of the corresponding action
Generally, it’ll be clearer to call pressed
or so on directly on the ActionState
.
However, accessing the state directly allows you to examine the detailed Timing
information.
Example
use leafwing_input_manager::prelude::*;
use leafwing_input_manager::action_state::VirtualButtonState;
use strum::EnumIter;
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, EnumIter, Debug)]
enum Action {
Run,
Jump,
}
let mut action_state = ActionState::<Action>::default();
let run_state = action_state.state(Action::Run);
// States can either be pressed or released,
// and store an internal `Timing`
if let VirtualButtonState::Pressed(timing) = run_state {
let pressed_duration = timing.current_duration;
let last_released_duration = timing.previous_duration;
}
Manually sets the VirtualButtonState
of the corresponding action
You should almost always be using the ActionState::press
and ActionState::release
methods instead,
as they will ensure that the duration is correct.
However, this method can be useful for testing,
or when transferring VirtualButtonState
between action maps.
Example
use leafwing_input_manager::prelude::*;
use leafwing_input_manager::action_state::VirtualButtonState;
use strum::EnumIter;
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, EnumIter, Debug)]
enum AbilitySlot {
Slot1,
Slot2,
}
#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, EnumIter, 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.state(AbilitySlot::Slot1);
// And transfer it to the actual ability that we care about
// without losing timing information
action_state.set_state(Action::Run, slot_1_state);
Releases all action virtual buttons
Was this action
pressed since the last time tick was called?
Is this action
currently released?
This is always the logical negation of pressed
Was this action
pressed since the last time tick was called?
Creates a Hashmap with all of the possible A variants as keys, and false as the values
Trait Implementations
type Storage = TableStorage
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
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 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