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    /// USB vendor and product ids of the pad, `0` where the platform reports
14    /// none. Enough to tell a model apart, e.g. a GameCube adapter.
15    pub vendor_id: u16,
16    pub product_id: u16,
17    pub left_stick: Vec2,
18    pub right_stick: Vec2,
19    pub left_trigger_held: bool,
20    pub left_trigger_pressed: bool,
21    pub right_trigger_held: bool,
22    pub right_trigger_pressed: bool,
23    pub south_held: bool,
24    pub south_pressed: bool,
25    pub east_held: bool,
26    pub east_pressed: bool,
27    pub west_held: bool,
28    pub west_pressed: bool,
29    pub north_held: bool,
30    pub north_pressed: bool,
31    pub dpad_left_held: bool,
32    pub dpad_left_pressed: bool,
33    pub dpad_up_held: bool,
34    pub dpad_up_pressed: bool,
35    pub dpad_right_held: bool,
36    pub dpad_right_pressed: bool,
37    pub dpad_down_held: bool,
38    pub dpad_down_pressed: bool,
39    pub left_shoulder_held: bool,
40    pub left_shoulder_pressed: bool,
41    pub right_shoulder_held: bool,
42    pub right_shoulder_pressed: bool,
43    pub start_held: bool,
44    pub start_pressed: bool,
45    pub select_held: bool,
46    pub select_pressed: bool,
47    pub left_stick_click_held: bool,
48    pub left_stick_click_pressed: bool,
49    pub right_stick_click_held: bool,
50    pub right_stick_click_pressed: bool,
51}
52
53#[derive(Clone, Copy, Debug, Default)]
54pub struct TouchContact {
55    pub id: u64,
56    pub start: Vec2,
57    pub pos: Vec2,
58    pub just_pressed: bool,
59}
60
61pub fn dead_zone(value: Vec2) -> Vec2 {
62    const DEAD_ZONE: f32 = 0.22;
63
64    let length = value.length();
65    if length <= DEAD_ZONE {
66        return Vec2::ZERO;
67    }
68
69    let scaled = ((length - DEAD_ZONE) / (1.0 - DEAD_ZONE)).clamp(0.0, 1.0);
70    value.normalize_or_zero() * scaled
71}
72
73pub fn apply_stick(raw: Vec2) -> Vec2 {
74    dead_zone(raw).clamp_length_max(1.0)
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn dead_zone_kills_rest() {
83        assert_eq!(dead_zone(Vec2::new(0.1, 0.0)), Vec2::ZERO);
84    }
85
86    #[test]
87    fn full_deflection_survives() {
88        let out = apply_stick(Vec2::new(1.0, 0.0));
89        assert!((out.length() - 1.0).abs() < 1e-6);
90    }
91}