use std::{hash::Hash, ops::Index};
use fnv::FnvHashMap;
use super::Button;
pub struct ButtonState<T>(FnvHashMap<T, Button>);
impl<T> ButtonState<T>
where
T: Eq + Hash,
{
pub fn new(buttons: impl Iterator<Item = (T, bool)>) -> Self {
Self(buttons.map(|(b, state)| (b, Button::new(state))).collect())
}
pub fn update(&mut self, buttons: impl Iterator<Item = (T, bool)>) {
for (button, state) in buttons {
if let Some(prev_state) = self.0.get_mut(&button) {
prev_state.update(state);
} else {
self.0.insert(button, Button::new(state));
}
}
}
pub fn get(&self, button: T) -> &Button {
self.0.get(&button).unwrap()
}
pub fn pressed(&self, button: T) -> bool {
self.0.get(&button).unwrap().pressed
}
pub fn released(&self, button: T) -> bool {
self.0.get(&button).unwrap().released
}
pub fn held(&self, button: T) -> bool {
self.0.get(&button).unwrap().held
}
}
impl<T> Index<T> for ButtonState<T>
where
T: Eq + Hash,
{
type Output = Button;
fn index(&self, button: T) -> &Self::Output {
self.get(button)
}
}