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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use devices::{HatSwitch, JoystickState};

/// State of a Key or Button
#[derive(Eq, PartialEq, Clone, Debug)]
pub enum State {
    Pressed,
    Released,
}

/// Key Identifier (UK Keyboard Layout)
#[derive(Eq, PartialEq, Hash, Clone, Debug)]
pub enum KeyId {
    Escape,
    Return,
    Backspace,
    Left,
    Right,
    Up,
    Down,
    Space,
    A,
    B,
    C,
    D,
    E,
    F,
    G,
    H,
    I,
    J,
    K,
    L,
    M,
    N,
    O,
    P,
    Q,
    R,
    S,
    T,
    U,
    V,
    W,
    X,
    Y,
    Z,
    F1,
    F2,
    F3,
    F4,
    F5,
    F6,
    F7,
    F8,
    F9,
    F10,
    F11,
    F12,
    Zero,
    One,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Shift,
    LeftCtrl,
    RightCtrl,
    LeftAlt,
    RightAlt,
    CapsLock,
    Pause,
    PageUp,
    PageDown,
    PrintScreen,
    Insert,
    End,
    Home,
    Delete,
    Add,
    Subtract,
    Multiply,
    Separator,
    Decimal,
    Divide,
    BackTick,
    BackSlash,
    ForwardSlash,
    Plus,
    Minus,
    FullStop,
    Comma,
    Tab,
    Numlock,
    LeftSquareBracket,
    RightSquareBracket,
    SemiColon,
    Apostrophe,
    Hash,
}

/// Mouse Buttons
#[derive(Eq, PartialEq, Hash, Clone, Debug)]
pub enum MouseButton {
    Left,
    Right,
    Middle,
    Button4,
    Button5,
}

#[derive(Eq, PartialEq, Hash, Clone, Debug)]
pub enum Axis {
    X,
    Y,
    Z,
    RX,
    RY,
    RZ,
    SLIDER,
}

/// Event types
///
/// The usize entry acts as a device ID unique to each DeviceType (Mouse, Keyboard, Hid).
/// Keyboard press events repeat when a key is held down.
#[derive(Clone, Debug)]
pub enum RawEvent {
    MouseButtonEvent(usize, MouseButton, State),
    MouseMoveEvent(usize, i32, i32),
    MouseWheelEvent(usize, f32),
    KeyboardEvent(usize, KeyId, State),
    JoystickButtonEvent(usize, usize, State),
    JoystickAxisEvent(usize, Axis, f64),
    JoystickHatSwitchEvent(usize, HatSwitch),
}

impl JoystickState {
    pub fn compare_states(&self, other_state: JoystickState, id: usize) -> Vec<RawEvent> {
        let mut output: Vec<RawEvent> = Vec::new();
        for (index, (&press_state, _)) in self
            .button_states
            .iter()
            .zip(other_state.button_states.iter())
            .enumerate()
            .filter(|&(_, (&a, &b))| a != b)
        {
            output.push(RawEvent::JoystickButtonEvent(
                id,
                index,
                if press_state {
                    State::Released
                } else {
                    State::Pressed
                },
            ));
        }
        if self.raw_axis_states.x != other_state.raw_axis_states.x {
            if let Some(value) = other_state.axis_states.x {
                output.push(RawEvent::JoystickAxisEvent(id, Axis::X, value));
            }
        }
        if self.raw_axis_states.y != other_state.raw_axis_states.y {
            if let Some(value) = other_state.axis_states.y {
                output.push(RawEvent::JoystickAxisEvent(id, Axis::Y, value));
            }
        }
        if self.raw_axis_states.z != other_state.raw_axis_states.z {
            if let Some(value) = other_state.axis_states.z {
                output.push(RawEvent::JoystickAxisEvent(id, Axis::Z, value));
            }
        }
        if self.raw_axis_states.rx != other_state.raw_axis_states.rx {
            if let Some(value) = other_state.axis_states.rx {
                output.push(RawEvent::JoystickAxisEvent(id, Axis::RX, value));
            }
        }
        if self.raw_axis_states.ry != other_state.raw_axis_states.ry {
            if let Some(value) = other_state.axis_states.ry {
                output.push(RawEvent::JoystickAxisEvent(id, Axis::RY, value));
            }
        }
        if self.raw_axis_states.rz != other_state.raw_axis_states.rz {
            if let Some(value) = other_state.axis_states.rz {
                output.push(RawEvent::JoystickAxisEvent(id, Axis::RZ, value));
            }
        }
        if self.raw_axis_states.slider != other_state.raw_axis_states.slider {
            if let Some(value) = other_state.axis_states.slider {
                output.push(RawEvent::JoystickAxisEvent(id, Axis::SLIDER, value));
            }
        }
        if let Some(value_other) = other_state.hatswitch {
            if let Some(value_self) = self.hatswitch.clone() {
                if value_self != value_other {
                    output.push(RawEvent::JoystickHatSwitchEvent(id, value_other));
                }
            }
        }
        output
    }
}