use crate::ButtonInput;
use bevy_ecs::system::Res;
use core::hash::Hash;
pub fn input_toggle_active<T>(
default: bool,
input: T,
) -> impl FnMut(Res<ButtonInput<T>>) -> bool + Clone
where
T: Clone + Eq + Hash + Send + Sync + 'static,
{
let mut active = default;
move |inputs: Res<ButtonInput<T>>| {
active ^= inputs.just_pressed(input.clone());
active
}
}
pub fn input_pressed<T>(input: T) -> impl FnMut(Res<ButtonInput<T>>) -> bool + Clone
where
T: Clone + Eq + Hash + Send + Sync + 'static,
{
move |inputs: Res<ButtonInput<T>>| inputs.pressed(input.clone())
}
pub fn input_just_pressed<T>(input: T) -> impl FnMut(Res<ButtonInput<T>>) -> bool + Clone
where
T: Clone + Eq + Hash + Send + Sync + 'static,
{
move |inputs: Res<ButtonInput<T>>| inputs.just_pressed(input.clone())
}
pub fn input_just_released<T>(input: T) -> impl FnMut(Res<ButtonInput<T>>) -> bool + Clone
where
T: Clone + Eq + Hash + Send + Sync + 'static,
{
move |inputs: Res<ButtonInput<T>>| inputs.just_released(input.clone())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::KeyCode;
use bevy_ecs::schedule::{IntoScheduleConfigs, Schedule};
fn test_system() {}
#[test]
fn distributive_run_if_compiles() {
Schedule::default().add_systems(
(test_system, test_system)
.distributive_run_if(input_toggle_active(false, KeyCode::Escape))
.distributive_run_if(input_pressed(KeyCode::Escape))
.distributive_run_if(input_just_pressed(KeyCode::Escape))
.distributive_run_if(input_just_released(KeyCode::Escape)),
);
}
}