Skip to main content

repame_input/
snapshot.rs

1use glam::Vec2;
2
3#[derive(Clone, Copy, Debug, Default)]
4pub struct MouseState {
5    pub left_held: bool,
6    pub left_pressed: bool,
7    pub right_held: bool,
8    pub right_pressed: bool,
9}
10
11#[derive(Clone, Copy, Debug, Default)]
12pub struct GamepadState {
13    pub left_stick: Vec2,
14    pub right_stick: Vec2,
15    pub right_trigger_held: bool,
16    pub right_trigger_pressed: bool,
17    pub left_trigger_held: bool,
18    pub left_trigger_pressed: bool,
19    pub south_pressed: bool,
20    pub east_pressed: bool,
21    pub west_pressed: bool,
22    pub dpad_left_pressed: bool,
23    pub dpad_up_pressed: bool,
24    pub dpad_right_pressed: bool,
25    pub dpad_down_pressed: bool,
26    pub north_pressed: bool,
27    pub left_shoulder_held: bool,
28    pub left_shoulder_pressed: bool,
29    pub right_shoulder_held: bool,
30    pub right_shoulder_pressed: bool,
31}
32
33#[derive(Clone, Copy, Debug, Default)]
34pub struct TouchContact {
35    pub id: u64,
36    pub start: Vec2,
37    pub pos: Vec2,
38    pub just_pressed: bool,
39}
40
41pub fn dead_zone(value: Vec2) -> Vec2 {
42    const DEAD_ZONE: f32 = 0.22;
43
44    let length = value.length();
45    if length <= DEAD_ZONE {
46        return Vec2::ZERO;
47    }
48
49    let scaled = ((length - DEAD_ZONE) / (1.0 - DEAD_ZONE)).clamp(0.0, 1.0);
50    value.normalize_or_zero() * scaled
51}
52
53pub fn apply_stick(raw: Vec2) -> Vec2 {
54    dead_zone(raw).clamp_length_max(1.0)
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn dead_zone_kills_rest() {
63        assert_eq!(dead_zone(Vec2::new(0.1, 0.0)), Vec2::ZERO);
64    }
65
66    #[test]
67    fn full_deflection_survives() {
68        let out = apply_stick(Vec2::new(1.0, 0.0));
69        assert!((out.length() - 1.0).abs() < 1e-6);
70    }
71}