winit_input_map/
input_code.rs

1#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
2/// Enum that specifies an input
3pub enum InputCode {
4    #[cfg(feature = "mice-keyboard")]
5    Device { id: SpecifyDevice, input: DeviceInput },
6    #[cfg(feature = "gamepad")]
7    Gamepad { id: SpecifyGamepad, input: GamepadInput }
8}
9impl InputCode {
10    /// sets `SpecifyGamepad` or `SpecifyDevice` to any
11    pub fn set_any(self) -> Self {
12        match self {
13            #[cfg(feature = "gamepad")]
14            Self::Gamepad { input, .. } => input.into(),
15            #[cfg(feature = "mice-keyboard")]
16            Self::Device  { input, .. } => input.into(),
17        }
18    }
19    pub fn is_any(self) -> bool {
20        match self {
21            #[cfg(feature = "gamepad")]
22            Self::Gamepad { id, .. } => id == SpecifyGamepad::Any,
23            #[cfg(feature = "mice-keyboard")]
24            Self::Device  { id, .. } => id == SpecifyDevice::Any,
25        }
26    }
27    #[cfg(feature = "mice-keyboard")]
28    pub fn has_device_id(&self, id: winit::event::DeviceId) -> bool {
29        match self {
30            Self::Device { id: SpecifyDevice::Id(cid), .. } => *cid == id,
31            Self::Device { id: SpecifyDevice::Any,     .. } => true,
32            _ => false
33        }
34    }
35    #[cfg(feature = "gamepad")]
36    pub fn has_gamepad_id(&self, id: gilrs::GamepadId) -> bool {
37        match self {
38            Self::Gamepad { id: SpecifyGamepad::Id(cid), .. } => *cid == id,
39            Self::Gamepad { id: SpecifyGamepad::Any,     .. } => true,
40            _ => false
41        }
42    }
43    #[cfg(feature = "gamepad")]
44    #[allow(irrefutable_let_patterns)]
45    /// sets the gamepad id. if its a device it does nothing.
46    pub fn set_gamepad_id(self, id: gilrs::GamepadId) -> Self {
47        if let Self::Gamepad { input, .. } = self { input.with_id(id) }
48        else { self }
49    }
50    #[cfg(feature = "mice-keyboard")]
51    #[allow(irrefutable_let_patterns)]
52    /// sets the device id. if its a gamepad it does nothing.
53    pub fn set_device_id(self, id: winit::event::DeviceId) -> Self {
54        if let Self::Device { input, .. } = self { input.with_id(id) }
55        else { self }
56    }
57}
58/// imports everything needed to reduce boilerplate when creating an input_map
59pub mod base_input_codes {
60    #![allow(ambiguous_glob_reexports)]
61    use crate::input_code::*;
62
63    #[cfg(feature = "gamepad")]
64    pub use gamepad::GamepadInput::{*, self};
65
66    #[cfg(feature = "mice-keyboard")]
67    pub use mice_keyboard::DeviceInput::{*, self};
68
69    #[cfg(feature = "mice-keyboard")]
70    pub use winit::{
71        keyboard::{KeyCode::{*, self}, PhysicalKey::{*, self}},
72        event::MouseButton
73    };
74}
75
76#[cfg(feature = "mice-keyboard")]
77pub use mice_keyboard::*;
78#[cfg(feature = "mice-keyboard")]
79mod mice_keyboard {
80    use winit::keyboard::{ KeyCode, PhysicalKey };
81    use winit::event::*;
82    use crate::InputCode;
83    #[cfg(feature = "mice-keyboard")]
84    #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
85    pub enum DeviceInput {
86        Button(MouseButton),
87        Key(PhysicalKey),
88        MouseMoveLeft,
89        MouseMoveRight,
90        MouseMoveUp,
91        MouseMoveDown,
92        MouseScrollUp,
93        MouseScrollDown,
94        MouseScrollLeft,
95        MouseScrollRight,
96    }
97    #[cfg(feature = "mice-keyboard")]
98    impl DeviceInput {
99        pub fn with_id(self, id: DeviceId) -> InputCode {
100            InputCode::Device { id: SpecifyDevice::Id(id), input: self }
101        }
102        pub fn with_sid(self, id: SpecifyDevice) -> InputCode {
103            InputCode::Device { id, input: self }
104        }
105    }
106    #[cfg(feature = "mice-keyboard")]
107    impl From<MouseButton> for DeviceInput {
108        fn from(value: MouseButton) -> Self {
109            Self::Button(value) 
110        }
111    }
112    #[cfg(feature = "mice-keyboard")]
113    impl From<KeyCode> for DeviceInput {
114        fn from(value: KeyCode) -> Self {
115            Self::Key(value.into()) 
116        }
117    }
118    #[cfg(feature = "mice-keyboard")]
119    impl From<PhysicalKey> for DeviceInput {
120        fn from(value: PhysicalKey) -> Self {
121            Self::Key(value)
122        }
123    }
124    /// specify device to listen to. defaults to any and can be specified later on at runtime
125    #[cfg(feature = "mice-keyboard")]
126    #[derive(Default, Debug, PartialEq, Eq, Clone, Copy, Hash)]
127    pub enum SpecifyDevice {
128        /// cant be set at compile time. use `Any` as default and then let the user select a specific
129        /// gamepad at runtime
130        Id(DeviceId),
131        /// use as default
132        #[default]
133        Any
134    }
135    impl From<DeviceInput> for InputCode {
136        fn from(value: DeviceInput) -> Self {
137            Self::Device { id: SpecifyDevice::Any, input: value }
138        }
139    }
140    impl From<MouseButton> for InputCode {
141        fn from(value: MouseButton) -> Self {
142            Self::Device { id: SpecifyDevice::Any, input: value.into() }
143        }
144    }
145    impl From<PhysicalKey> for InputCode {
146        fn from(value: PhysicalKey) -> Self {
147            Self::Device { id: SpecifyDevice::Any, input: value.into() }
148        }
149    }
150    impl From<KeyCode> for InputCode {
151        fn from(value: KeyCode) -> Self {
152            Self::Device { id: SpecifyDevice::Any, input: value.into() }
153        }
154    }
155}
156
157
158#[cfg(feature = "gamepad")]
159pub use gamepad::*;
160#[cfg(feature = "gamepad")]
161mod gamepad {
162    use crate::InputCode;
163    use gilrs::{Axis, Button};
164    #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
165    pub enum GamepadInput {
166        LeftStickLeft,
167        /// the left stick, moved to the right
168        LeftStickRight,
169        LeftStickUp,
170        LeftStickDown,
171        LeftStickPress,
172
173        /// the right stick, moved to the left
174        RightStickLeft,
175        RightStickRight,
176        RightStickUp,
177        RightStickDown,
178        RightStickPress,
179
180        DPadLeft,
181        DPadRight,
182        DPadUp,
183        DPadDown,
184
185        LeftZ,
186        RightZ,
187
188        South,
189        East,
190        North,
191        West,
192
193        LeftBumper,
194        LeftTrigger,
195        RightBumper,
196        RightTrigger,
197
198        Select,
199        Start,
200        Mode,
201        /// unfortunately gilrs doesnt give enough infomation to have multiple 'Other' input binds
202        Other
203    }
204    impl GamepadInput {
205        pub fn with_id(self, id: gilrs::GamepadId) -> InputCode {
206            InputCode::Gamepad { id: SpecifyGamepad::Id(id), input: self }
207        }
208        pub fn with_sid(self, id: SpecifyGamepad) -> InputCode {
209            InputCode::Gamepad { id, input: self }
210        }
211    }
212    pub fn axis_neg(axis: Axis) -> GamepadInput {
213        match axis {
214            Axis::LeftStickX => GamepadInput::LeftStickLeft,
215            Axis::LeftStickY => GamepadInput::LeftStickDown,
216            Axis::RightStickX => GamepadInput::RightStickLeft,
217            Axis::RightStickY => GamepadInput::RightStickDown,
218            Axis::LeftZ => GamepadInput::LeftZ,
219            Axis::RightZ => GamepadInput::RightZ,
220            Axis::DPadX => GamepadInput::DPadLeft,
221            Axis::DPadY => GamepadInput::DPadDown,
222            Axis::Unknown => GamepadInput::Other
223        }
224    }
225    pub fn axis_pos(axis: Axis) -> GamepadInput {
226        match axis {
227            Axis::LeftStickX => GamepadInput::LeftStickRight,
228            Axis::LeftStickY => GamepadInput::LeftStickUp,
229            Axis::RightStickX => GamepadInput::RightStickRight,
230            Axis::RightStickY => GamepadInput::RightStickUp,
231            Axis::LeftZ => GamepadInput::LeftZ,
232            Axis::RightZ => GamepadInput::RightZ,
233            Axis::DPadX => GamepadInput::DPadRight,
234            Axis::DPadY => GamepadInput::DPadUp,
235            Axis::Unknown => GamepadInput::Other,
236        }
237    }
238    impl From<Button> for GamepadInput {
239        fn from(value: Button) -> Self {
240            match value {
241                Button::South => GamepadInput::South,
242                Button::East => GamepadInput::East,
243                Button::North => GamepadInput::North,
244                Button::West => GamepadInput::West,
245                Button::LeftTrigger => GamepadInput::LeftBumper,
246                Button::LeftTrigger2 => GamepadInput::LeftTrigger,
247                Button::RightTrigger2 => GamepadInput::RightTrigger,
248                Button::RightTrigger => GamepadInput::RightBumper,
249                Button::DPadUp => GamepadInput::DPadUp,
250                Button::DPadDown => GamepadInput::DPadDown,
251                Button::DPadLeft => GamepadInput::DPadLeft,
252                Button::DPadRight => GamepadInput::DPadRight,
253                Button::Z => GamepadInput::RightZ,
254                Button::C => GamepadInput::LeftZ,
255                Button::Select => GamepadInput::Select,
256                Button::Start => GamepadInput::Start,
257                Button::Mode => GamepadInput::Mode,
258                Button::RightThumb => GamepadInput::RightStickPress,
259                Button::LeftThumb  => GamepadInput::LeftStickPress,
260                Button::Unknown => GamepadInput::Other 
261            }
262        }
263    }
264    impl From<GamepadInput> for InputCode {
265        fn from(value: GamepadInput) -> InputCode {
266            Self::Gamepad { input: value, id: Default::default() }
267        }
268    }
269    /// Specify gamepad to listen to. defaults to any and can be specified later on at runtime
270    #[derive(Debug, PartialEq, Eq, Clone, Copy, Default, Hash)]
271    pub enum SpecifyGamepad {
272        /// cant be set at compile time. use `Any` as default and then let the user select a specific
273        /// gamepad at runtime
274        Id(gilrs::GamepadId),
275        /// use as default
276        #[default]
277        Any
278    }
279}