Galactica/src/inputstatus.rs

49 lines
979 B
Rust
Raw Normal View History

2023-12-20 19:05:12 -08:00
use sdl2::{event::Event, keyboard::Keycode};
// TODO: no boolean modification (no pub)
pub struct InputStatus {
pub key_left: bool,
pub key_right: bool,
pub key_thrust: bool,
}
impl InputStatus {
pub fn new() -> Self {
InputStatus {
key_left: false,
key_right: false,
key_thrust: false,
}
}
fn handle_keyup(&mut self, keycode: Keycode) {
match keycode {
Keycode::Left => self.key_left = false,
Keycode::Right => self.key_right = false,
Keycode::Up => self.key_thrust = false,
_ => {}
}
}
fn handle_keydown(&mut self, keycode: Keycode) {
match keycode {
Keycode::Left => self.key_left = true,
Keycode::Right => self.key_right = true,
Keycode::Up => self.key_thrust = true,
_ => {}
}
}
pub fn update(&mut self, event: Event) {
match event {
Event::KeyDown {
keycode: Some(key), ..
} => self.handle_keydown(key),
Event::KeyUp {
keycode: Some(key), ..
} => self.handle_keyup(key),
_ => {}
}
}
}