2020-06-05 06:34:21 +00:00
|
|
|
use std::{collections::HashSet, hash::Hash};
|
2020-06-05 05:48:53 +00:00
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
pub struct Input<T> {
|
|
|
|
pressed: HashSet<T>,
|
|
|
|
just_pressed: HashSet<T>,
|
|
|
|
just_released: HashSet<T>,
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
impl<T> Default for Input<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
pressed: Default::default(),
|
|
|
|
just_pressed: Default::default(),
|
|
|
|
just_released: Default::default(),
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-05 06:34:21 +00:00
|
|
|
}
|
2020-06-05 05:48:53 +00:00
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
impl<T> Input<T>
|
|
|
|
where
|
|
|
|
T: Copy + Eq + Hash,
|
|
|
|
{
|
|
|
|
pub fn press(&mut self, input: T) {
|
|
|
|
if !self.pressed(input) {
|
|
|
|
self.just_pressed.insert(input);
|
|
|
|
}
|
2020-06-05 05:48:53 +00:00
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
self.pressed.insert(input);
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
pub fn pressed(&self, input: T) -> bool {
|
|
|
|
self.pressed.contains(&input)
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
pub fn release(&mut self, input: T) {
|
|
|
|
self.pressed.remove(&input);
|
|
|
|
self.just_released.insert(input);
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
pub fn just_pressed(&self, input: T) -> bool {
|
|
|
|
self.just_pressed.contains(&input)
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
pub fn just_released(&self, input: T) -> bool {
|
|
|
|
self.just_released.contains(&input)
|
|
|
|
}
|
2020-06-05 05:48:53 +00:00
|
|
|
|
2020-06-05 06:34:21 +00:00
|
|
|
pub fn update(&mut self) {
|
|
|
|
self.just_pressed.clear();
|
|
|
|
self.just_released.clear();
|
2020-06-05 05:48:53 +00:00
|
|
|
}
|
|
|
|
}
|