veloren_voxygen/settings/
gamepad.rs

1use hashbrown::HashMap;
2use serde::{Deserialize, Serialize};
3
4#[derive(Clone, Debug, Serialize, Deserialize)]
5#[serde(default)]
6pub struct GamepadSettings {
7    pub game_buttons: con_settings::GameButtons,
8    pub menu_buttons: con_settings::MenuButtons,
9    pub game_axis: con_settings::GameAxis,
10    pub menu_axis: con_settings::MenuAxis,
11    pub game_analog_buttons: con_settings::GameAnalogButton,
12    pub menu_analog_buttons: con_settings::MenuAnalogButton,
13    pub game_layer_buttons: con_settings::GameLayerEntries,
14    pub pan_sensitivity: u32,
15    pub pan_invert_y: bool,
16    pub axis_deadzones: HashMap<crate::controller::Axis, f32>,
17    pub button_deadzones: HashMap<crate::controller::AnalogButton, f32>,
18    pub mouse_emulation_sensitivity: u32,
19    pub inverted_axes: Vec<crate::controller::Axis>,
20}
21
22impl Default for GamepadSettings {
23    fn default() -> Self {
24        Self {
25            game_buttons: con_settings::GameButtons::default(),
26            menu_buttons: con_settings::MenuButtons::default(),
27            game_axis: con_settings::GameAxis::default(),
28            menu_axis: con_settings::MenuAxis::default(),
29            game_analog_buttons: con_settings::GameAnalogButton::default(),
30            menu_analog_buttons: con_settings::MenuAnalogButton::default(),
31            game_layer_buttons: con_settings::GameLayerEntries::default(),
32            pan_sensitivity: 10,
33            pan_invert_y: false,
34            axis_deadzones: HashMap::new(),
35            button_deadzones: HashMap::new(),
36            mouse_emulation_sensitivity: 12,
37            inverted_axes: Vec::new(),
38        }
39    }
40}
41
42pub mod con_settings {
43    use crate::controller::*;
44    use gilrs::{Axis as GilAxis, Button as GilButton};
45    use i18n::Localization;
46    use serde::{Deserialize, Serialize};
47
48    // represents a controller button to fire a GameInput on
49    // includes two modifier buttons to determine what layer is active
50    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
51    #[serde(default)]
52    pub struct LayerEntry {
53        pub button: Button,
54        pub mod1: Button,
55        pub mod2: Button,
56    }
57
58    impl Default for LayerEntry {
59        fn default() -> Self {
60            // binding to unknown = getting skipped from processing
61            Self {
62                button: Button::Simple(GilButton::Unknown),
63                mod1: Button::Simple(GilButton::Unknown),
64                mod2: Button::Simple(GilButton::Unknown),
65            }
66        }
67    }
68
69    impl LayerEntry {
70        pub fn display_string(&self, localized_strings: &Localization) -> String {
71            use self::Button::*;
72
73            let mod1: Option<String> = match self.mod1 {
74                Simple(GilButton::Unknown) => None,
75                _ => self
76                    .mod1
77                    .try_shortened()
78                    .map_or(Some(self.mod1.display_string(localized_strings)), Some),
79            };
80            let mod2: Option<String> = match self.mod2 {
81                Simple(GilButton::Unknown) => None,
82                _ => self
83                    .mod2
84                    .try_shortened()
85                    .map_or(Some(self.mod2.display_string(localized_strings)), Some),
86            };
87
88            format!(
89                "{}{}{} {}",
90                mod1.map_or("".to_owned(), |m1| format!("{} + ", m1)),
91                mod2.map_or("".to_owned(), |m2| format!("{} + ", m2)),
92                self.button.display_string(localized_strings),
93                self.button
94                    .try_shortened()
95                    .map_or("".to_owned(), |short| format!("({})", short))
96            )
97        }
98    }
99
100    // struct to associate each available GameInput with a LayerEntry
101    // similar in function to the GameButtons struct
102    // nothing prevents mapping a GameInput in both GameLayerEntries and GameButtons
103    // it's likely not desirable to double map a GameInput
104    #[derive(Clone, Debug, Serialize, Deserialize)]
105    #[serde(default)]
106    pub struct GameLayerEntries {
107        pub primary: LayerEntry,
108        pub secondary: LayerEntry,
109        pub block: LayerEntry,
110        pub slot1: LayerEntry,
111        pub slot2: LayerEntry,
112        pub slot3: LayerEntry,
113        pub slot4: LayerEntry,
114        pub slot5: LayerEntry,
115        pub slot6: LayerEntry,
116        pub slot7: LayerEntry,
117        pub slot8: LayerEntry,
118        pub slot9: LayerEntry,
119        pub slot10: LayerEntry,
120        pub toggle_cursor: LayerEntry,
121        pub escape: LayerEntry,
122        pub enter: LayerEntry,
123        pub command: LayerEntry,
124        pub move_forward: LayerEntry,
125        pub move_left: LayerEntry,
126        pub move_back: LayerEntry,
127        pub move_right: LayerEntry,
128        pub jump: LayerEntry,
129        pub sit: LayerEntry,
130        pub dance: LayerEntry,
131        pub glide: LayerEntry,
132        pub swimup: LayerEntry,
133        pub swimdown: LayerEntry,
134        pub sneak: LayerEntry,
135        pub toggle_lantern: LayerEntry,
136        pub mount: LayerEntry,
137        pub map: LayerEntry,
138        pub bag: LayerEntry,
139        pub quest_log: LayerEntry,
140        pub character_window: LayerEntry,
141        pub social: LayerEntry,
142        pub crafting: LayerEntry,
143        pub diary: LayerEntry,
144        pub settings: LayerEntry,
145        pub controls: LayerEntry,
146        pub toggle_interface: LayerEntry,
147        pub toggle_debug: LayerEntry,
148        #[cfg(feature = "egui-ui")]
149        pub toggle_egui_debug: LayerEntry,
150        pub toggle_chat: LayerEntry,
151        pub fullscreen: LayerEntry,
152        pub screenshot: LayerEntry,
153        pub toggle_ingame_ui: LayerEntry,
154        pub roll: LayerEntry,
155        pub respawn: LayerEntry,
156        pub interact: LayerEntry,
157        pub toggle_wield: LayerEntry,
158        pub swap_loadout: LayerEntry,
159    }
160
161    impl Default for GameLayerEntries {
162        fn default() -> Self {
163            Self {
164                primary: LayerEntry::default(),
165                secondary: LayerEntry::default(),
166                block: LayerEntry::default(),
167                slot1: LayerEntry {
168                    button: Button::Simple(GilButton::DPadRight),
169                    mod1: Button::Simple(GilButton::RightTrigger),
170                    mod2: Button::Simple(GilButton::Unknown),
171                },
172                slot2: LayerEntry {
173                    button: Button::Simple(GilButton::DPadDown),
174                    mod1: Button::Simple(GilButton::RightTrigger),
175                    mod2: Button::Simple(GilButton::Unknown),
176                },
177                slot3: LayerEntry {
178                    button: Button::Simple(GilButton::DPadUp),
179                    mod1: Button::Simple(GilButton::LeftTrigger),
180                    mod2: Button::Simple(GilButton::Unknown),
181                },
182                slot4: LayerEntry {
183                    button: Button::Simple(GilButton::DPadLeft),
184                    mod1: Button::Simple(GilButton::LeftTrigger),
185                    mod2: Button::Simple(GilButton::Unknown),
186                },
187                slot5: LayerEntry {
188                    button: Button::Simple(GilButton::DPadRight),
189                    mod1: Button::Simple(GilButton::LeftTrigger),
190                    mod2: Button::Simple(GilButton::Unknown),
191                },
192                slot6: LayerEntry {
193                    button: Button::Simple(GilButton::DPadDown),
194                    mod1: Button::Simple(GilButton::LeftTrigger),
195                    mod2: Button::Simple(GilButton::Unknown),
196                },
197                slot7: LayerEntry {
198                    button: Button::Simple(GilButton::DPadUp),
199                    mod1: Button::Simple(GilButton::RightTrigger),
200                    mod2: Button::Simple(GilButton::LeftTrigger),
201                },
202                slot8: LayerEntry {
203                    button: Button::Simple(GilButton::DPadLeft),
204                    mod1: Button::Simple(GilButton::RightTrigger),
205                    mod2: Button::Simple(GilButton::LeftTrigger),
206                },
207                slot9: LayerEntry {
208                    button: Button::Simple(GilButton::DPadRight),
209                    mod1: Button::Simple(GilButton::RightTrigger),
210                    mod2: Button::Simple(GilButton::LeftTrigger),
211                },
212                slot10: LayerEntry {
213                    button: Button::Simple(GilButton::DPadDown),
214                    mod1: Button::Simple(GilButton::RightTrigger),
215                    mod2: Button::Simple(GilButton::LeftTrigger),
216                },
217                toggle_cursor: LayerEntry {
218                    button: Button::Simple(GilButton::Start),
219                    mod1: Button::Simple(GilButton::RightTrigger),
220                    mod2: Button::Simple(GilButton::LeftTrigger),
221                },
222                escape: LayerEntry {
223                    button: Button::Simple(GilButton::Start),
224                    mod1: Button::Simple(GilButton::Unknown),
225                    mod2: Button::Simple(GilButton::Unknown),
226                },
227                enter: LayerEntry::default(),
228                command: LayerEntry::default(),
229                move_forward: LayerEntry::default(),
230                move_left: LayerEntry::default(),
231                move_back: LayerEntry::default(),
232                move_right: LayerEntry::default(),
233                jump: LayerEntry::default(),
234                sit: LayerEntry {
235                    button: Button::Simple(GilButton::Select),
236                    mod1: Button::Simple(GilButton::RightTrigger),
237                    mod2: Button::Simple(GilButton::LeftTrigger),
238                },
239                dance: LayerEntry {
240                    button: Button::Simple(GilButton::Select),
241                    mod1: Button::Simple(GilButton::LeftTrigger),
242                    mod2: Button::Simple(GilButton::Unknown),
243                },
244                glide: LayerEntry {
245                    button: Button::Simple(GilButton::DPadUp),
246                    mod1: Button::Simple(GilButton::Unknown),
247                    mod2: Button::Simple(GilButton::Unknown),
248                },
249                swimup: LayerEntry::default(),
250                swimdown: LayerEntry::default(),
251                sneak: LayerEntry::default(),
252                toggle_lantern: LayerEntry {
253                    button: Button::Simple(GilButton::DPadLeft),
254                    mod1: Button::Simple(GilButton::RightTrigger),
255                    mod2: Button::Simple(GilButton::Unknown),
256                },
257                mount: LayerEntry::default(),
258                map: LayerEntry {
259                    button: Button::Simple(GilButton::DPadLeft),
260                    mod1: Button::Simple(GilButton::Unknown),
261                    mod2: Button::Simple(GilButton::Unknown),
262                },
263                bag: LayerEntry::default(),
264                quest_log: LayerEntry {
265                    button: Button::Simple(GilButton::DPadRight),
266                    mod1: Button::Simple(GilButton::Unknown),
267                    mod2: Button::Simple(GilButton::Unknown),
268                },
269                character_window: LayerEntry::default(),
270                social: LayerEntry {
271                    button: Button::Simple(GilButton::DPadUp),
272                    mod1: Button::Simple(GilButton::RightTrigger),
273                    mod2: Button::Simple(GilButton::Unknown),
274                },
275                crafting: LayerEntry {
276                    button: Button::Simple(GilButton::Select),
277                    mod1: Button::Simple(GilButton::RightTrigger),
278                    mod2: Button::Simple(GilButton::Unknown),
279                },
280                diary: LayerEntry {
281                    button: Button::Simple(GilButton::Select),
282                    mod1: Button::Simple(GilButton::Unknown),
283                    mod2: Button::Simple(GilButton::Unknown),
284                },
285                settings: LayerEntry {
286                    button: Button::Simple(GilButton::Start),
287                    mod1: Button::Simple(GilButton::RightTrigger),
288                    mod2: Button::Simple(GilButton::Unknown),
289                },
290                controls: LayerEntry {
291                    button: Button::Simple(GilButton::Start),
292                    mod1: Button::Simple(GilButton::LeftTrigger),
293                    mod2: Button::Simple(GilButton::Unknown),
294                },
295                toggle_interface: LayerEntry::default(),
296                toggle_debug: LayerEntry::default(),
297                #[cfg(feature = "egui-ui")]
298                toggle_egui_debug: LayerEntry::default(),
299                toggle_chat: LayerEntry::default(),
300                fullscreen: LayerEntry::default(),
301                screenshot: LayerEntry::default(),
302                toggle_ingame_ui: LayerEntry::default(),
303                roll: LayerEntry::default(),
304                respawn: LayerEntry::default(),
305                interact: LayerEntry::default(),
306                toggle_wield: LayerEntry::default(),
307                swap_loadout: LayerEntry {
308                    button: Button::Simple(GilButton::DPadDown),
309                    mod1: Button::Simple(GilButton::Unknown),
310                    mod2: Button::Simple(GilButton::Unknown),
311                },
312            }
313        }
314    }
315
316    #[derive(Clone, Debug, Serialize, Deserialize)]
317    #[serde(default)]
318    pub struct GameButtons {
319        pub primary: Button,
320        pub secondary: Button,
321        pub block: Button,
322        pub slot1: Button,
323        pub slot2: Button,
324        pub slot3: Button,
325        pub slot4: Button,
326        pub slot5: Button,
327        pub slot6: Button,
328        pub slot7: Button,
329        pub slot8: Button,
330        pub slot9: Button,
331        pub slot10: Button,
332        pub toggle_cursor: Button,
333        pub escape: Button,
334        pub enter: Button,
335        pub command: Button,
336        pub move_forward: Button,
337        pub move_left: Button,
338        pub move_back: Button,
339        pub move_right: Button,
340        pub jump: Button,
341        pub sit: Button,
342        pub dance: Button,
343        pub glide: Button,
344        pub swimup: Button,
345        pub swimdown: Button,
346        pub sneak: Button,
347        pub toggle_lantern: Button,
348        pub mount: Button,
349        pub stayfollow: Button,
350        pub map: Button,
351        pub bag: Button,
352        pub quest_log: Button,
353        pub character_window: Button,
354        pub social: Button,
355        pub crafting: Button,
356        pub diary: Button,
357        pub settings: Button,
358        pub controls: Button,
359        pub toggle_interface: Button,
360        pub toggle_debug: Button,
361        #[cfg(feature = "egui-ui")]
362        pub toggle_egui_debug: Button,
363        pub toggle_chat: Button,
364        pub fullscreen: Button,
365        pub screenshot: Button,
366        pub toggle_ingame_ui: Button,
367        pub roll: Button,
368        pub respawn: Button,
369        pub interact: Button,
370        pub toggle_wield: Button,
371        pub swap_loadout: Button,
372    }
373
374    #[derive(Clone, Debug, Serialize, Deserialize)]
375    #[serde(default)]
376    pub struct MenuButtons {
377        pub up: Button,
378        pub down: Button,
379        pub left: Button,
380        pub right: Button,
381        pub scroll_up: Button,
382        pub scroll_down: Button,
383        pub scroll_left: Button,
384        pub scroll_right: Button,
385        pub home: Button,
386        pub end: Button,
387        pub apply: Button,
388        pub back: Button,
389        pub exit: Button,
390    }
391
392    #[derive(Clone, Debug, Serialize, Deserialize)]
393    #[serde(default)]
394    pub struct GameAxis {
395        pub movement_x: Axis,
396        pub movement_y: Axis,
397        pub camera_x: Axis,
398        pub camera_y: Axis,
399    }
400
401    #[derive(Clone, Debug, Serialize, Deserialize)]
402    #[serde(default)]
403    pub struct MenuAxis {
404        pub move_x: Axis,
405        pub move_y: Axis,
406        pub scroll_x: Axis,
407        pub scroll_y: Axis,
408    }
409
410    #[derive(Clone, Debug, Default, Serialize, Deserialize)]
411    #[serde(default)]
412    pub struct GameAnalogButton {}
413
414    #[derive(Clone, Debug, Default, Serialize, Deserialize)]
415    #[serde(default)]
416    pub struct MenuAnalogButton {}
417
418    impl Default for GameButtons {
419        fn default() -> Self {
420            // binding to unknown = getting skipped from processing
421            Self {
422                primary: Button::Simple(GilButton::RightTrigger2),
423                secondary: Button::Simple(GilButton::LeftTrigger2),
424                block: Button::Simple(GilButton::North),
425                slot1: Button::Simple(GilButton::Unknown),
426                slot2: Button::Simple(GilButton::Unknown),
427                slot3: Button::Simple(GilButton::Unknown),
428                slot4: Button::Simple(GilButton::Unknown),
429                slot5: Button::Simple(GilButton::Unknown),
430                slot6: Button::Simple(GilButton::Unknown),
431                slot7: Button::Simple(GilButton::Unknown),
432                slot8: Button::Simple(GilButton::Unknown),
433                slot9: Button::Simple(GilButton::Unknown),
434                slot10: Button::Simple(GilButton::Unknown),
435                toggle_cursor: Button::Simple(GilButton::Unknown),
436                escape: Button::Simple(GilButton::Unknown),
437                enter: Button::Simple(GilButton::Unknown),
438                command: Button::Simple(GilButton::Unknown),
439                move_forward: Button::Simple(GilButton::Unknown),
440                move_left: Button::Simple(GilButton::Unknown),
441                move_back: Button::Simple(GilButton::Unknown),
442                move_right: Button::Simple(GilButton::Unknown),
443                jump: Button::Simple(GilButton::South),
444                sit: Button::Simple(GilButton::Unknown),
445                dance: Button::Simple(GilButton::Unknown),
446                glide: Button::Simple(GilButton::Unknown),
447                swimup: Button::Simple(GilButton::South),
448                swimdown: Button::Simple(GilButton::West),
449                sneak: Button::Simple(GilButton::LeftThumb),
450                toggle_lantern: Button::Simple(GilButton::Unknown),
451                mount: Button::Simple(GilButton::South),
452                stayfollow: Button::Simple(GilButton::Unknown),
453                map: Button::Simple(GilButton::Unknown),
454                bag: Button::Simple(GilButton::East),
455                quest_log: Button::Simple(GilButton::Unknown),
456                character_window: Button::Simple(GilButton::Unknown),
457                social: Button::Simple(GilButton::Unknown),
458                crafting: Button::Simple(GilButton::Unknown),
459                diary: Button::Simple(GilButton::Unknown),
460                settings: Button::Simple(GilButton::Unknown),
461                controls: Button::Simple(GilButton::Unknown),
462                toggle_interface: Button::Simple(GilButton::Unknown),
463                toggle_debug: Button::Simple(GilButton::Unknown),
464                #[cfg(feature = "egui-ui")]
465                toggle_egui_debug: Button::Simple(GilButton::Unknown),
466                toggle_chat: Button::Simple(GilButton::Unknown),
467                fullscreen: Button::Simple(GilButton::Unknown),
468                screenshot: Button::Simple(GilButton::Unknown),
469                toggle_ingame_ui: Button::Simple(GilButton::Unknown),
470                roll: Button::Simple(GilButton::RightThumb),
471                respawn: Button::Simple(GilButton::South),
472                interact: Button::Simple(GilButton::West),
473                toggle_wield: Button::Simple(GilButton::Unknown),
474                swap_loadout: Button::Simple(GilButton::Unknown),
475            }
476        }
477    }
478
479    impl Default for MenuButtons {
480        fn default() -> Self {
481            Self {
482                up: Button::Simple(GilButton::DPadUp),
483                down: Button::Simple(GilButton::DPadDown),
484                left: Button::Simple(GilButton::DPadLeft),
485                right: Button::Simple(GilButton::DPadRight),
486                scroll_up: Button::Simple(GilButton::Unknown),
487                scroll_down: Button::Simple(GilButton::Unknown),
488                scroll_left: Button::Simple(GilButton::Unknown),
489                scroll_right: Button::Simple(GilButton::Unknown),
490                home: Button::Simple(GilButton::Unknown),
491                end: Button::Simple(GilButton::Unknown),
492                apply: Button::Simple(GilButton::South),
493                back: Button::Simple(GilButton::East),
494                exit: Button::Simple(GilButton::Mode),
495            }
496        }
497    }
498
499    impl Default for GameAxis {
500        fn default() -> Self {
501            Self {
502                movement_x: Axis::Simple(GilAxis::LeftStickX),
503                movement_y: Axis::Simple(GilAxis::LeftStickY),
504                camera_x: Axis::Simple(GilAxis::RightStickX),
505                camera_y: Axis::Simple(GilAxis::RightStickY),
506            }
507        }
508    }
509
510    impl Default for MenuAxis {
511        fn default() -> Self {
512            Self {
513                move_x: Axis::Simple(GilAxis::RightStickX),
514                move_y: Axis::Simple(GilAxis::RightStickY),
515                scroll_x: Axis::Simple(GilAxis::LeftStickX),
516                scroll_y: Axis::Simple(GilAxis::LeftStickY),
517            }
518        }
519    }
520}