leafwing_input_manager/
common_conditions.rs

1//! Run conditions for actions.
2
3use crate::{prelude::ActionState, Actionlike};
4use bevy::prelude::Res;
5
6/// Stateful run condition that can be toggled via an action press using [`ActionState::just_pressed`].
7pub fn action_toggle_active<A>(default: bool, action: A) -> impl FnMut(Res<ActionState<A>>) -> bool
8where
9    A: Actionlike + Clone,
10{
11    let mut active = default;
12    move |action_state: Res<ActionState<A>>| {
13        active ^= action_state.just_pressed(&action);
14        active
15    }
16}
17
18/// Run condition that is active if [`ActionState::pressed`] is true for the given action.
19pub fn action_pressed<A>(action: A) -> impl FnMut(Res<ActionState<A>>) -> bool
20where
21    A: Actionlike + Clone,
22{
23    move |action_state: Res<ActionState<A>>| action_state.pressed(&action)
24}
25
26/// Run condition that is active if [`ActionState::just_pressed`] is true for the given action.
27pub fn action_just_pressed<A>(action: A) -> impl FnMut(Res<ActionState<A>>) -> bool
28where
29    A: Actionlike + Clone,
30{
31    move |action_state: Res<ActionState<A>>| action_state.just_pressed(&action)
32}
33
34/// Run condition that is active if [`ActionState::just_released`] is true for the given action.
35pub fn action_just_released<A>(action: A) -> impl FnMut(Res<ActionState<A>>) -> bool
36where
37    A: Actionlike + Clone,
38{
39    move |action_state: Res<ActionState<A>>| action_state.just_released(&action)
40}