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, the current_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);

Press the action virtual button

Release the action virtual button

Releases all action virtual buttons

Is this action currently pressed?

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

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

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

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

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

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait. Read more

Performs the conversion.

Creates Self using data from the given [World]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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