use {sfml::window::mouse, std::collections::HashSet};
use sfml::window::{Event, Key};
#[derive(Default)]
pub struct InputState {
keys_down: HashSet<Key>,
mouse_down: HashSet<mouse::Button>,
mouse_pressed: HashSet<mouse::Button>,
}
impl InputState {
pub fn start_frame(&mut self) {
self.mouse_pressed.clear();
}
pub fn update_from_event(&mut self, event: &Event) {
match event {
Event::KeyPressed { code, .. } => {
self.keys_down.insert(*code);
}
Event::KeyReleased { code, .. } => {
self.keys_down.remove(code);
}
Event::MouseButtonPressed { button, .. } => {
self.mouse_down.insert(*button);
self.mouse_pressed.insert(*button);
}
Event::MouseButtonReleased { button, .. } => {
self.mouse_down.remove(button);
}
_ => {}
}
}
pub fn key_down(&self, key: Key) -> bool {
self.keys_down.contains(&key)
}
pub fn mouse_down(&self, button: mouse::Button) -> bool {
self.mouse_down.contains(&button)
}
pub fn mouse_pressed(&self, button: mouse::Button) -> bool {
self.mouse_pressed.contains(&button)
}
}