rich_sdl2_rust/event/game_controller/
map.rs

1//! A definition of a mapping structure.
2
3use crate::bind;
4
5/// A mapping that the logical button is attached to the physical index.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum MapInput {
9    /// It is attached to the button.
10    Button {
11        /// The physical index of the button.
12        index: u32,
13    },
14    /// It is attached to the axis.
15    Axis {
16        /// The physical index of the axis.
17        index: u32,
18    },
19    /// It is attached to the hat.
20    Hat {
21        /// The physical index of the hat.
22        index: u32,
23        /// The mask of the hat.
24        mask: u32,
25    },
26}
27
28impl From<bind::SDL_GameControllerButtonBind> for MapInput {
29    fn from(raw: bind::SDL_GameControllerButtonBind) -> Self {
30        match raw.bindType {
31            bind::SDL_CONTROLLER_BINDTYPE_BUTTON => MapInput::Button {
32                index: unsafe { raw.value.button } as u32,
33            },
34            bind::SDL_CONTROLLER_BINDTYPE_AXIS => MapInput::Axis {
35                index: unsafe { raw.value.axis } as u32,
36            },
37            bind::SDL_CONTROLLER_BINDTYPE_HAT => MapInput::Hat {
38                index: unsafe { raw.value.hat.hat } as u32,
39                mask: unsafe { raw.value.hat.hat_mask } as u32,
40            },
41            _ => unreachable!(), // NONE does not occur on this wrapper
42        }
43    }
44}