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
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
let t0 = Instant::now();
let t1 = Instant::now();
action_state.tick(t1, t0);
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));
let t2 = Instant::now();
action_state.tick(t2, t1);
assert!(action_state.released(Action::Jump));
assert!(!action_state.just_released(Action::Jump));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_instant: Instant, previous_instant: Instant)
pub fn tick(&mut self, current_instant: Instant, previous_instant: 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_instantwill be set as the initial time at which the button was pressed / released - the
Durationwill 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
let t0 = Instant::now();
let t1 = Instant::now();
action_state.tick(t1, t0);
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
let t2 = Instant::now();
action_state.tick(t2, t1);
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
A reference to 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 action_data_mut(&mut self, action: A) -> &mut ActionData
pub fn action_data_mut(&mut self, action: A) -> &mut ActionData
A mutable reference 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 mut run_data = action_state.action_data_mut(Action::Run);
run_data.axis_pair = None;
dbg!(run_data);sourcepub fn value(&self, action: A) -> f32
pub fn value(&self, action: A) -> f32
Get the value associated with the corresponding action
Different kinds of bindings have different ways of calculating the value:
- Binary buttons will have a value of
0.0when the button is not pressed, and a value of1.0when the button is pressed. - Some axes, such as an analog stick, will have a value in the range
-1.0..=1.0. - Some axes, such as a variable trigger, will have a value in the range
0.0..=1.0. - Some buttons will also return a value in the range
0.0..=1.0, such as analog gamepad triggers which may be tracked as buttons or axes. Examples of these include the Xbox LT/RT triggers and the Playstation L2/R2 triggers. See also theaxis_inputsexample in the repository. - Dual axis inputs will return the magnitude of its [
AxisPair] and will be in the range0.0..=1.0. - Chord inputs will return the value of its first input.
If multiple inputs trigger the same game action at the same time, the value of each triggering input will be added together.
Warning
This value may not be bounded as you might expect.
Consider clamping this to account for multiple triggering inputs,
typically using the clamped_value method instead.
sourcepub fn clamped_value(&self, action: A) -> f32
pub fn clamped_value(&self, action: A) -> f32
Get the value associated with the corresponding action, clamped to [-1.0, 1.0].
sourcepub fn axis_pair(&self, action: A) -> Option<DualAxisData>
pub fn axis_pair(&self, action: A) -> Option<DualAxisData>
Get the DualAxisData from the binding that triggered the corresponding action.
Only certain events such as [VirtualDPad][crate::user_input::VirtualDPad] and
[DualAxis][crate::user_input::DualAxis] provide an DualAxisData, and this
will return None for other events.
Chord inputs will return the DualAxisData of it’s first input.
If multiple inputs with an axis pair trigger the same game action at the same time, the value of each axis pair will be added together.
Warning
These values may not be bounded as you might expect.
Consider clamping this to account for multiple triggering inputs,
typically using the clamped_axis_pair method instead.
sourcepub fn clamped_axis_pair(&self, action: A) -> Option<DualAxisData>
pub fn clamped_axis_pair(&self, action: A) -> Option<DualAxisData>
Get the DualAxisData associated with the corresponding action, clamped to [-1.0, 1.0].
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.clone());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 released 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 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>
1.0.0 · sourceconst fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source. Read moresourceimpl<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>
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>,
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
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
impl<T, U> AsBindGroupShaderType<U> for Twhere
U: ShaderType,
&'a T: for<'a> Into<U>,
impl<T, U> AsBindGroupShaderType<U> for Twhere
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
T [ShaderType] for self. When used in [AsBindGroup]
derives, it is safe to assume that all images in self exist. Read moresourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
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>
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 morefn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read morefn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s. Read morefn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s. Read moreimpl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self using data from the given [World]