winit_input_map/
input_code.rs

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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#[cfg(feature = "mice-keyboard")]
use winit::keyboard::{ KeyCode, PhysicalKey };
#[cfg(feature = "mice-keyboard")]
use winit::event::*;
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
/// Enum that specifies an input
pub enum InputCode {
    #[cfg(feature = "mice-keyboard")]
    Device { id: SpecifyDevice, input: DeviceInput },
    #[cfg(feature = "gamepad")]
    Gamepad { id: SpecifyGamepad, input: GamepadInput }
}
impl InputCode {
    /// sets `SpecifyGamepad` or `SpecifyDevice` to any
    pub fn set_any(self) -> Self {
        match self {
            #[cfg(feature = "gamepad")]
            Self::Gamepad { input, .. } => input.into(),
            #[cfg(feature = "mice-keyboard")]
            Self::Device  { input, .. } => input.into(),
        }
    }
    #[cfg(feature = "gamepad")]
    #[allow(irrefutable_let_patterns)]
    /// sets the gamepad id. if its a device it does nothing.
    pub fn set_gamepad_id(self, id: gilrs::GamepadId) -> Self {
        if let Self::Gamepad { input, .. } = self { input.with_id(id) }
        else { self }
    }
    #[cfg(feature = "mice-keyboard")]
    #[allow(irrefutable_let_patterns)]
    /// sets the device id. if its a gamepad it does nothing.
    pub fn set_device_id(self, id: DeviceId) -> Self {
        if let Self::Device { input, .. } = self { input.with_id(id) }
        else { self }
    }
}
#[cfg(feature = "mice-keyboard")]
impl From<DeviceInput> for InputCode {
    fn from(value: DeviceInput) -> Self {
        Self::Device { id: SpecifyDevice::Any, input: value }
    }
}
#[cfg(feature = "mice-keyboard")]
impl From<MouseButton> for InputCode {
    fn from(value: MouseButton) -> Self {
        Self::Device { id: SpecifyDevice::Any, input: value.into() }
    }
}
#[cfg(feature = "mice-keyboard")]
impl From<PhysicalKey> for InputCode {
    fn from(value: PhysicalKey) -> Self {
        Self::Device { id: SpecifyDevice::Any, input: value.into() }
    }
}
#[cfg(feature = "mice-keyboard")]
impl From<KeyCode> for InputCode {
    fn from(value: KeyCode) -> Self {
        Self::Device { id: SpecifyDevice::Any, input: value.into() }
    }
}
/// imports everything needed to reduce boilerplate when creating an input_map
pub mod base_input_codes {
    #![allow(ambiguous_glob_reexports)]
    use crate::input_code::*;

    #[cfg(feature = "gamepad")]
    pub use GamepadInput::*;
    #[cfg(feature = "mice-keyboard")]
    pub use DeviceInput::*;

    #[cfg(feature = "mice-keyboard")]
    pub use winit::{
        keyboard::{KeyCode::*, PhysicalKey::*},
        event::MouseButton
    };
}
#[cfg(feature = "mice-keyboard")]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum DeviceInput {
    Button(MouseButton),
    Key(PhysicalKey),
    MouseMoveLeft,
    MouseMoveRight,
    MouseMoveUp,
    MouseMoveDown,
    MouseScrollUp,
    MouseScrollDown,
    MouseScrollLeft,
    MouseScrollRight
}
#[cfg(feature = "mice-keyboard")]
impl DeviceInput {
    pub fn with_id(self, id: DeviceId) -> InputCode {
        InputCode::Device { id: SpecifyDevice::Id(id), input: self }
    }
    pub fn with_sid(self, id: SpecifyDevice) -> InputCode {
        InputCode::Device { id, input: self }
    }
}
#[cfg(feature = "mice-keyboard")]
impl From<MouseButton> for DeviceInput {
    fn from(value: MouseButton) -> Self {
        Self::Button(value) 
    }
}
#[cfg(feature = "mice-keyboard")]
impl From<KeyCode> for DeviceInput {
    fn from(value: KeyCode) -> Self {
        Self::Key(value.into()) 
    }
}
#[cfg(feature = "mice-keyboard")]
impl From<PhysicalKey> for DeviceInput {
    fn from(value: PhysicalKey) -> Self {
        Self::Key(value)
    }
}
/// specify device to listen to. defaults to any and can be specified later on at runtime
#[cfg(feature = "mice-keyboard")]
#[derive(Default, Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum SpecifyDevice {
    /// cant be set at compile time. use `Any` as default and then let the user select a specific
    /// gamepad at runtime
    Id(DeviceId),
    /// use as default
    #[default]
    Any
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum AxisSign { Pos, Neg }
#[cfg(feature = "gamepad")]
pub use gamepad::*;
#[cfg(feature = "gamepad")]
mod gamepad {
    use crate::InputCode;
    use gilrs::{Axis, Button};
    #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
    pub enum GamepadInput {
        LeftStickLeft,
        /// the left stick, moved to the right
        LeftStickRight,
        LeftStickUp,
        LeftStickDown,
        LeftStickPress,

        /// the right stick, moved to the left
        RightStickLeft,
        RightStickRight,
        RightStickUp,
        RightStickDown,
        RightStickPress,

        DPadLeft,
        DPadRight,
        DPadUp,
        DPadDown,

        LeftZ,
        RightZ,

        South,
        East,
        North,
        West,

        LeftTrigger,
        LeftTrigger2,
        RightTrigger,
        RightTrigger2,

        Select,
        Start,
        Mode,
        /// unfortunately gilrs doesnt give enough infomation to have multiple 'Other' input binds
        Other
    }
    impl GamepadInput {
        pub fn with_id(self, id: gilrs::GamepadId) -> InputCode {
            InputCode::Gamepad { id: SpecifyGamepad::Id(id), input: self }
        }
        pub fn with_sid(self, id: SpecifyGamepad) -> InputCode {
            InputCode::Gamepad { id, input: self }
        }
    }
    pub fn axis_neg(axis: Axis) -> GamepadInput {
        match axis {
            Axis::LeftStickX => GamepadInput::LeftStickRight,
            Axis::LeftStickY => GamepadInput::LeftStickDown,
            Axis::RightStickX => GamepadInput::RightStickRight,
            Axis::RightStickY => GamepadInput::RightStickDown,
            Axis::LeftZ => GamepadInput::LeftZ,
            Axis::RightZ => GamepadInput::RightZ,
            Axis::DPadX => GamepadInput::DPadRight,
            Axis::DPadY => GamepadInput::DPadDown,
            Axis::Unknown => GamepadInput::Other
        }
    }
    pub fn axis_pos(axis: Axis) -> GamepadInput {
        match axis {
            Axis::LeftStickX => GamepadInput::LeftStickLeft,
            Axis::LeftStickY => GamepadInput::LeftStickUp,
            Axis::RightStickX => GamepadInput::RightStickLeft,
            Axis::RightStickY => GamepadInput::RightStickUp,
            Axis::LeftZ => GamepadInput::LeftZ,
            Axis::RightZ => GamepadInput::RightZ,
            Axis::DPadX => GamepadInput::DPadLeft,
            Axis::DPadY => GamepadInput::DPadUp,
            Axis::Unknown => GamepadInput::Other,
        }
    }
    impl From<Button> for GamepadInput {
        fn from(value: Button) -> Self {
            match value {
                Button::South => GamepadInput::South,
                Button::East => GamepadInput::East,
                Button::North => GamepadInput::North,
                Button::West => GamepadInput::West,
                Button::LeftTrigger => GamepadInput::LeftTrigger,
                Button::LeftTrigger2 => GamepadInput::LeftTrigger2,
                Button::RightTrigger2 => GamepadInput::RightTrigger2,
                Button::RightTrigger => GamepadInput::RightTrigger,
                Button::DPadUp => GamepadInput::DPadUp,
                Button::DPadDown => GamepadInput::DPadDown,
                Button::DPadLeft => GamepadInput::DPadLeft,
                Button::DPadRight => GamepadInput::DPadRight,
                Button::Z => GamepadInput::RightZ,
                Button::C => GamepadInput::LeftZ,
                Button::Select => GamepadInput::Select,
                Button::Start => GamepadInput::Start,
                Button::Mode => GamepadInput::Mode,
                Button::RightThumb => GamepadInput::RightStickPress,
                Button::LeftThumb  => GamepadInput::LeftStickPress,
                Button::Unknown => GamepadInput::Other 
            }
        }
    }
    impl From<GamepadInput> for InputCode {
        fn from(value: GamepadInput) -> InputCode {
            Self::Gamepad { input: value, id: Default::default() }
        }
    }
    /// Specify gamepad to listen to. defaults to any and can be specified later on at runtime
    #[derive(Debug, PartialEq, Eq, Clone, Copy, Default, Hash)]
    pub enum SpecifyGamepad {
        /// cant be set at compile time. use `Any` as default and then let the user select a specific
        /// gamepad at runtime
        Id(gilrs::GamepadId),
        /// use as default
        #[default]
        Any
    }
}