1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use event::InputEvent;

/// Controller axes matching SDL controller model
#[derive(Eq, PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
pub enum ControllerAxis {
    LeftX,
    LeftY,
    RightX,
    RightY,
    LeftTrigger,
    RightTrigger,
}

/// Controller buttons matching SDL controller model
#[derive(Eq, PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
pub enum ControllerButton {
    A,
    B,
    X,
    Y,
    DPadDown,
    DPadLeft,
    DPadRight,
    DPadUp,
    LeftShoulder,
    RightShoulder,
    LeftStick,
    RightStick,
    Back,
    Start,
    Guide,
}

#[derive(PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
pub enum ControllerEvent {
    ControllerAxisMoved {
        which: u32,
        axis: ControllerAxis,
        value: f64,
    },
    ControllerButtonPressed {
        which: u32,
        button: ControllerButton,
    },
    ControllerButtonReleased {
        which: u32,
        button: ControllerButton,
    },
    ControllerDisconnected {
        which: u32,
    },
    ControllerConnected {
        which: u32,
    },
}

impl<'a, T> Into<InputEvent<T>> for &'a ControllerEvent {
    fn into(self) -> InputEvent<T> {
        use self::ControllerEvent::*;
        match *self {
            ControllerAxisMoved { which, axis, value } => {
                InputEvent::ControllerAxisMoved { which, axis, value }
            }
            ControllerButtonPressed { which, button } => {
                InputEvent::ControllerButtonPressed { which, button }
            }
            ControllerButtonReleased { which, button } => {
                InputEvent::ControllerButtonReleased { which, button }
            }
            ControllerConnected { which } => InputEvent::ControllerConnected { which },
            ControllerDisconnected { which } => InputEvent::ControllerDisconnected { which },
        }
    }
}