use std::{collections::HashSet, hash::Hash};
#[derive(Clone, Debug)]
pub struct ButtonMap<T: Copy + Eq + Hash> {
pressed: HashSet<T>,
just_pressed: HashSet<T>,
just_released: HashSet<T>,
}
impl<T: Copy + Eq + Hash> Default for ButtonMap<T> {
fn default() -> Self {
Self {
pressed: HashSet::new(),
just_pressed: HashSet::new(),
just_released: HashSet::new(),
}
}
}
impl<T: Copy + Eq + Hash> ButtonMap<T> {
pub fn press(&mut self, input: T) {
if self.pressed.insert(input) {
self.just_pressed.insert(input);
}
}
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 reset(&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 pressed(&self, input: T) -> bool {
self.pressed.contains(&input)
}
pub fn any_pressed(&self, inputs: impl IntoIterator<Item = T>) -> bool {
inputs.into_iter().any(|t| self.pressed(t))
}
pub fn all_pressed(&self, inputs: impl IntoIterator<Item = T>) -> bool {
inputs.into_iter().all(|t| self.pressed(t))
}
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(|t| self.just_pressed(t))
}
pub fn all_just_pressed(&self, inputs: impl IntoIterator<Item = T>) -> bool {
inputs.into_iter().all(|t| self.just_pressed(t))
}
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(|t| self.just_released(t))
}
pub fn all_just_released(&self, inputs: impl IntoIterator<Item = T>) -> bool {
inputs.into_iter().all(|t| self.just_released(t))
}
}