Galactica/src/inputstatus.rs

61 lines
1.4 KiB
Rust
Raw Normal View History

2023-12-22 19:18:19 -08:00
use winit::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
2023-12-20 19:05:12 -08:00
pub struct InputStatus {
2023-12-22 19:18:19 -08:00
scroll_speed: f32,
2023-12-20 19:05:12 -08:00
pub key_left: bool,
pub key_right: bool,
pub key_thrust: bool,
2023-12-27 20:14:53 -08:00
pub key_guns: bool,
2023-12-22 19:18:19 -08:00
pub v_scroll: f32,
2023-12-20 19:05:12 -08:00
}
impl InputStatus {
pub fn new() -> Self {
InputStatus {
key_left: false,
key_right: false,
key_thrust: false,
2023-12-27 20:14:53 -08:00
key_guns: false,
2023-12-22 19:18:19 -08:00
v_scroll: 0.0,
scroll_speed: 10.0,
2023-12-20 19:05:12 -08:00
}
}
2023-12-25 18:36:05 -08:00
pub fn release_all(&mut self) {
self.key_left = false;
self.key_right = false;
self.key_thrust = false;
2023-12-27 20:14:53 -08:00
self.key_guns = false;
2023-12-25 18:36:05 -08:00
}
2023-12-22 19:18:19 -08:00
pub fn process_key(&mut self, state: &ElementState, key: &VirtualKeyCode) {
2023-12-22 16:51:21 -08:00
let down = state == &ElementState::Pressed;
match key {
VirtualKeyCode::Left => self.key_left = down,
VirtualKeyCode::Right => self.key_right = down,
VirtualKeyCode::Up => self.key_thrust = down,
2023-12-27 20:14:53 -08:00
VirtualKeyCode::Space => self.key_guns = down,
2023-12-20 19:05:12 -08:00
_ => {}
}
}
2023-12-22 19:18:19 -08:00
pub fn process_click(&mut self, state: &ElementState, key: &MouseButton) {
let _down = state == &ElementState::Pressed;
match key {
_ => {}
}
}
pub fn process_scroll(&mut self, delta: &MouseScrollDelta, _phase: &TouchPhase) {
match delta {
MouseScrollDelta::LineDelta(_h, v) => {
self.v_scroll -= self.scroll_speed * v;
}
// TODO: handle this better
MouseScrollDelta::PixelDelta(v) => {
self.v_scroll -= v.x as f32;
}
}
}
2023-12-20 19:05:12 -08:00
}