Add keyboard input for camera movement

This commit is contained in:
crumblingstatue 2023-04-03 12:24:43 +02:00
parent 0aa2781c25
commit 91cd644b8c
3 changed files with 59 additions and 1 deletions

33
src/input.rs Normal file
View file

@ -0,0 +1,33 @@
use fnv::FnvHashSet;
use sfml::window::{Event, Key};
#[derive(Default)]
pub struct KbInput {
down: FnvHashSet<Key>,
pressed: FnvHashSet<Key>,
}
impl KbInput {
pub fn update_from_event(&mut self, ev: &Event) {
match ev {
Event::KeyPressed { code, .. } => {
self.pressed.insert(*code);
self.down.insert(*code);
}
Event::KeyReleased { code, .. } => {
self.down.remove(code);
}
_ => {}
}
}
/// Pressed event should be cleared every frame
pub fn clear_pressed(&mut self) {
self.pressed.clear();
}
pub fn down(&self, key: Key) -> bool {
self.down.contains(&key)
}
pub fn pressed(&self, key: Key) -> bool {
self.pressed.contains(&key)
}
}