use bevy_ecs::system::Resource;
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_utils::HashSet;
use std::hash::Hash;
#[allow(unused_imports)]
use bevy_ecs::schedule::State;
#[derive(Debug, Clone, Resource, Reflect)]
#[reflect(Default)]
pub struct ButtonInput<T: Copy + Eq + Hash + Send + Sync + 'static> {
pressed: HashSet<T>,
just_pressed: HashSet<T>,
just_released: HashSet<T>,
}
impl<T: Copy + Eq + Hash + Send + Sync + 'static> Default for ButtonInput<T> {
fn default() -> Self {
Self {
pressed: Default::default(),
just_pressed: Default::default(),
just_released: Default::default(),
}
}
}
impl<T> ButtonInput<T>
where
T: Copy + Eq + Hash + Send + Sync + 'static,
{
pub fn press(&mut self, input: T) {
if self.pressed.insert(input) {
self.just_pressed.insert(input);
}
}
pub fn pressed(&self, input: T) -> bool {
self.pressed.contains(&input)
}
pub fn any_pressed(&self, inputs: impl IntoIterator<Item = T>) -> bool {
inputs.into_iter().any(|it| self.pressed(it))
}
pub fn all_pressed(&self, inputs: impl IntoIterator<Item = T>) -> bool {
inputs.into_iter().all(|it| self.pressed(it))
}
pub fn release(&mut self, input: T) {
if self.pressed.remove(&input) {
self.just_released.insert(input);
}
}
pub fn release_all(&mut self) {
self.just_released.extend(self.pressed.drain());
}
pub fn just_pressed(&self, input: T) -> bool {
self.just_pressed.contains(&input)
}
pub fn any_just_pressed(&self, inputs: impl IntoIterator<Item = T>) -> bool {
inputs.into_iter().any(|it| self.just_pressed(it))
}
pub fn clear_just_pressed(&mut self, input: T) -> bool {
self.just_pressed.remove(&input)
}
pub fn just_released(&self, input: T) -> bool {
self.just_released.contains(&input)
}
pub fn any_just_released(&self, inputs: impl IntoIterator<Item = T>) -> bool {
inputs.into_iter().any(|it| self.just_released(it))
}
pub fn clear_just_released(&mut self, input: T) -> bool {
self.just_released.remove(&input)
}
pub fn reset(&mut self, input: T) {
self.pressed.remove(&input);
self.just_pressed.remove(&input);
self.just_released.remove(&input);
}
pub fn reset_all(&mut self) {
self.pressed.clear();
self.just_pressed.clear();
self.just_released.clear();
}
pub fn clear(&mut self) {
self.just_pressed.clear();
self.just_released.clear();
}
pub fn get_pressed(&self) -> impl ExactSizeIterator<Item = &T> {
self.pressed.iter()
}
pub fn get_just_pressed(&self) -> impl ExactSizeIterator<Item = &T> {
self.just_pressed.iter()
}
pub fn get_just_released(&self) -> impl ExactSizeIterator<Item = &T> {
self.just_released.iter()
}
}
#[cfg(test)]
mod test {
use bevy_reflect::TypePath;
use crate::ButtonInput;
#[derive(TypePath, Copy, Clone, Eq, PartialEq, Hash)]
enum DummyInput {
Input1,
Input2,
}
#[test]
fn test_press() {
let mut input = ButtonInput::default();
assert!(!input.pressed.contains(&DummyInput::Input1));
assert!(!input.just_pressed.contains(&DummyInput::Input1));
input.press(DummyInput::Input1);
assert!(input.just_pressed.contains(&DummyInput::Input1));
assert!(input.pressed.contains(&DummyInput::Input1));
}
#[test]
fn test_pressed() {
let mut input = ButtonInput::default();
assert!(!input.pressed(DummyInput::Input1));
input.press(DummyInput::Input1);
assert!(input.pressed(DummyInput::Input1));
}
#[test]
fn test_any_pressed() {
let mut input = ButtonInput::default();
assert!(!input.any_pressed([DummyInput::Input1]));
assert!(!input.any_pressed([DummyInput::Input2]));
assert!(!input.any_pressed([DummyInput::Input1, DummyInput::Input2]));
input.press(DummyInput::Input1);
assert!(input.any_pressed([DummyInput::Input1]));
assert!(!input.any_pressed([DummyInput::Input2]));
assert!(input.any_pressed([DummyInput::Input1, DummyInput::Input2]));
}
#[test]
fn test_all_pressed() {
let mut input = ButtonInput::default();
assert!(!input.all_pressed([DummyInput::Input1]));
assert!(!input.all_pressed([DummyInput::Input2]));
assert!(!input.all_pressed([DummyInput::Input1, DummyInput::Input2]));
input.press(DummyInput::Input1);
assert!(input.all_pressed([DummyInput::Input1]));
assert!(!input.all_pressed([DummyInput::Input1, DummyInput::Input2]));
input.press(DummyInput::Input2);
assert!(input.all_pressed([DummyInput::Input1, DummyInput::Input2]));
}
#[test]
fn test_release() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
assert!(input.pressed.contains(&DummyInput::Input1));
assert!(!input.just_released.contains(&DummyInput::Input1));
input.release(DummyInput::Input1);
assert!(!input.pressed.contains(&DummyInput::Input1));
assert!(input.just_released.contains(&DummyInput::Input1));
}
#[test]
fn test_release_all() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.press(DummyInput::Input2);
input.release_all();
assert!(input.pressed.is_empty());
assert!(input.just_released.contains(&DummyInput::Input1));
assert!(input.just_released.contains(&DummyInput::Input2));
}
#[test]
fn test_just_pressed() {
let mut input = ButtonInput::default();
assert!(!input.just_pressed(DummyInput::Input1));
input.press(DummyInput::Input1);
assert!(input.just_pressed(DummyInput::Input1));
}
#[test]
fn test_any_just_pressed() {
let mut input = ButtonInput::default();
assert!(!input.any_just_pressed([DummyInput::Input1]));
assert!(!input.any_just_pressed([DummyInput::Input2]));
assert!(!input.any_just_pressed([DummyInput::Input1, DummyInput::Input2]));
input.press(DummyInput::Input1);
assert!(input.any_just_pressed([DummyInput::Input1]));
assert!(!input.any_just_pressed([DummyInput::Input2]));
assert!(input.any_just_pressed([DummyInput::Input1, DummyInput::Input2]));
}
#[test]
fn test_clear_just_pressed() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
assert!(input.just_pressed(DummyInput::Input1));
input.clear_just_pressed(DummyInput::Input1);
assert!(!input.just_pressed(DummyInput::Input1));
}
#[test]
fn test_just_released() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
assert!(!input.just_released(DummyInput::Input1));
input.release(DummyInput::Input1);
assert!(input.just_released(DummyInput::Input1));
}
#[test]
fn test_any_just_released() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
assert!(!input.any_just_released([DummyInput::Input1]));
assert!(!input.any_just_released([DummyInput::Input2]));
assert!(!input.any_just_released([DummyInput::Input1, DummyInput::Input2]));
input.release(DummyInput::Input1);
assert!(input.any_just_released([DummyInput::Input1]));
assert!(!input.any_just_released([DummyInput::Input2]));
assert!(input.any_just_released([DummyInput::Input1, DummyInput::Input2]));
}
#[test]
fn test_clear_just_released() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.release(DummyInput::Input1);
assert!(input.just_released(DummyInput::Input1));
input.clear_just_released(DummyInput::Input1);
assert!(!input.just_released(DummyInput::Input1));
}
#[test]
fn test_reset() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
assert!(input.pressed(DummyInput::Input1));
assert!(input.just_pressed(DummyInput::Input1));
assert!(!input.just_released(DummyInput::Input1));
input.reset(DummyInput::Input1);
assert!(!input.pressed(DummyInput::Input1));
assert!(!input.just_pressed(DummyInput::Input1));
assert!(!input.just_released(DummyInput::Input1));
input.press(DummyInput::Input1);
input.release(DummyInput::Input1);
assert!(!input.pressed(DummyInput::Input1));
assert!(input.just_pressed(DummyInput::Input1));
assert!(input.just_released(DummyInput::Input1));
input.reset(DummyInput::Input1);
assert!(!input.pressed(DummyInput::Input1));
assert!(!input.just_pressed(DummyInput::Input1));
assert!(!input.just_released(DummyInput::Input1));
}
#[test]
fn test_reset_all() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.press(DummyInput::Input2);
input.release(DummyInput::Input2);
assert!(input.pressed.contains(&DummyInput::Input1));
assert!(input.just_pressed.contains(&DummyInput::Input1));
assert!(input.just_released.contains(&DummyInput::Input2));
input.reset_all();
assert!(input.pressed.is_empty());
assert!(input.just_pressed.is_empty());
assert!(input.just_released.is_empty());
}
#[test]
fn test_clear() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
assert!(input.pressed(DummyInput::Input1));
assert!(input.just_pressed(DummyInput::Input1));
assert!(!input.just_released(DummyInput::Input1));
input.clear();
assert!(input.pressed(DummyInput::Input1));
assert!(!input.just_pressed(DummyInput::Input1));
assert!(!input.just_released(DummyInput::Input1));
input.press(DummyInput::Input1);
input.release(DummyInput::Input1);
assert!(!input.pressed(DummyInput::Input1));
assert!(!input.just_pressed(DummyInput::Input1));
assert!(input.just_released(DummyInput::Input1));
input.clear();
assert!(!input.pressed(DummyInput::Input1));
assert!(!input.just_pressed(DummyInput::Input1));
assert!(!input.just_released(DummyInput::Input1));
}
#[test]
fn test_get_pressed() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.press(DummyInput::Input2);
let pressed = input.get_pressed();
assert_eq!(pressed.len(), 2);
for pressed_input in pressed {
assert!(input.pressed.contains(pressed_input));
}
}
#[test]
fn test_get_just_pressed() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.press(DummyInput::Input2);
let just_pressed = input.get_just_pressed();
assert_eq!(just_pressed.len(), 2);
for just_pressed_input in just_pressed {
assert!(input.just_pressed.contains(just_pressed_input));
}
}
#[test]
fn test_get_just_released() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.press(DummyInput::Input2);
input.release(DummyInput::Input1);
input.release(DummyInput::Input2);
let just_released = input.get_just_released();
assert_eq!(just_released.len(), 2);
for just_released_input in just_released {
assert!(input.just_released.contains(just_released_input));
}
}
#[test]
fn test_general_input_handling() {
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.press(DummyInput::Input2);
assert!(input.just_pressed(DummyInput::Input1));
assert!(input.just_pressed(DummyInput::Input2));
assert!(input.pressed(DummyInput::Input1));
assert!(input.pressed(DummyInput::Input2));
input.clear();
assert!(!input.just_pressed(DummyInput::Input1));
assert!(!input.just_pressed(DummyInput::Input2));
assert!(input.pressed(DummyInput::Input1));
assert!(input.pressed(DummyInput::Input2));
input.release(DummyInput::Input1);
input.release(DummyInput::Input2);
assert!(input.just_released(DummyInput::Input1));
assert!(input.just_released(DummyInput::Input2));
assert!(!input.pressed(DummyInput::Input1));
assert!(!input.pressed(DummyInput::Input2));
input.clear();
assert!(!input.just_released(DummyInput::Input1));
assert!(!input.just_released(DummyInput::Input2));
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.release(DummyInput::Input2);
input.reset(DummyInput::Input1);
input.reset(DummyInput::Input2);
assert!(!input.just_pressed(DummyInput::Input1));
assert!(!input.pressed(DummyInput::Input1));
assert!(!input.just_released(DummyInput::Input2));
}
}