use vek::Vec2;
#[derive(Debug, Default)]
pub struct Input {
pub mouse_pos: Vec2<i32>,
pub left_mouse: ButtonState,
pub right_mouse: ButtonState,
pub up: ButtonState,
pub down: ButtonState,
pub left: ButtonState,
pub right: ButtonState,
pub space: ButtonState,
pub r: ButtonState,
pub g: ButtonState,
pub c: ButtonState,
pub o: ButtonState,
pub n: ButtonState,
pub x: ButtonState,
}
impl Input {
pub fn update(&mut self) {
self.left_mouse.update();
self.right_mouse.update();
self.up.update();
self.down.update();
self.left.update();
self.right.update();
self.space.update();
self.r.update();
self.g.update();
self.c.update();
self.o.update();
self.n.update();
self.x.update();
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ButtonState {
#[default]
None,
Released,
Pressed,
Down,
}
impl ButtonState {
pub fn is_pressed(&self) -> bool {
*self == Self::Pressed
}
pub fn is_down(&self) -> bool {
*self == Self::Pressed || *self == Self::Down
}
pub fn is_released(&self) -> bool {
*self == Self::Released
}
pub fn update(&mut self) {
if *self == Self::Released {
*self = Self::None;
} else if *self == Self::Pressed {
*self = Self::Down;
}
}
pub fn handle_bool(&mut self, pressed: bool) {
if (*self == Self::None || *self == Self::Released) && pressed {
*self = Self::Pressed;
} else if (*self == Self::Pressed || *self == Self::Down) && !pressed {
*self = Self::Released;
}
}
}