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
use serde::{Deserialize, Serialize};

use crate::{bindings::BindingTypes, event::InputEvent};

/// Controller axes matching SDL controller model
#[derive(Eq, PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
pub enum ControllerAxis {
    /// The X axis on the left stick
    LeftX,
    /// The Y axis on the left stick
    LeftY,
    /// The X axis on the right stick
    RightX,
    /// The Y axis on the right stick
    RightY,
    /// The analog left trigger, not to be confused with the left bumper.
    LeftTrigger,
    /// The analog right trigger, not to be confused with the right bumper.
    RightTrigger,
}

/// Controller buttons matching SDL controller model
#[derive(Eq, PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
pub enum ControllerButton {
    /// The A button, typically the lower button in the "diamond" of buttons on the right side
    /// of the controller.
    A,
    /// The B button, typically the right button in the "diamond" of buttons on the right side
    /// of the controller.
    B,
    /// The X button, typically the left button in the "diamond" of buttons on the right side
    /// of the controller.
    X,
    /// The Y button, typically the top button in the "diamond" of buttons on the right side
    /// of the controller.
    Y,
    /// The dpad button pointed towards the player
    DPadDown,
    /// The dpad button pointed to the player's left
    DPadLeft,
    /// The dpad button pointed to the player's right
    DPadRight,
    /// The dpad button pointed away from the player.
    DPadUp,
    /// The digital left shoulder bumper. Usually located above the left trigger.
    LeftShoulder,
    /// The digital right shoulder bumper. Usually located above the right trigger.
    RightShoulder,
    /// If your press the left analog stick into the controller this button is pressed.
    LeftStick,
    /// If your press the right analog stick into the controller this button is pressed.
    RightStick,
    /// The back button is typically a button slightly left of center with a leftward arrow on it.
    Back,
    /// The start button is typically a button slightly right of center with a rightward arrow on it.
    Start,
    /// The centermost button on the controller. Large and green on an Xbox controller.
    Guide,
}

/// Controller events generated by the SDL events system.
#[derive(PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
pub enum ControllerEvent {
    /// Movement event on a controller axis.
    ///
    /// Corresponds to [`SDL_CONTROLLERAXISMOTION`].
    ///
    /// [`SDL_CONTROLLERAXISMOTION`]: https://wiki.libsdl.org/SDL_ControllerAxisEvent
    ControllerAxisMoved {
        /// The joystick instance id.
        which: u32,
        /// The controller axis.
        axis: ControllerAxis,
        /// The axis value (range: -32768 to 32767).
        value: f32,
    },
    /// Button press event on a controller.
    ///
    /// Corresponds to [`SDL_CONTROLLERBUTTONDOWN`].
    ///
    /// [`SDL_CONTROLLERBUTTONDOWN`]: https://wiki.libsdl.org/SDL_ControllerButtonEvent
    ControllerButtonPressed {
        /// The joystick instance id.
        which: u32,
        /// The controller button.
        button: ControllerButton,
    },
    /// Button press event on a controller.
    ///
    /// Corresponds to [`SDL_CONTROLLERBUTTONUP`].
    ///
    /// [`SDL_CONTROLLERBUTTONUP`]: https://wiki.libsdl.org/SDL_ControllerButtonEvent
    ControllerButtonReleased {
        /// The joystick instance id.
        which: u32,
        /// The controller button.
        button: ControllerButton,
    },
    /// Controller disconnect event.
    ///
    /// Corresponds to [`SDL_CONTROLLERDEVICEREMOVED`].
    ///
    /// [`SDL_CONTROLLERDEVICEREMOVED`]: https://wiki.libsdl.org/SDL_ControllerDeviceEvent
    ControllerDisconnected {
        /// The joystick device index for the `SDL_CONTROLLERDEVICEADDED` event or instance id for
        /// the `SDL_CONTROLLERDEVICEREMOVED` or `SDL_CONTROLLERDEVICEREMAPPED` event
        which: u32,
    },
    /// Controller connected event.
    ///
    /// Corresponds to [`SDL_CONTROLLERDEVICEADDED`].
    ///
    /// [`SDL_CONTROLLERDEVICEADDED`]: https://wiki.libsdl.org/SDL_ControllerDeviceEvent
    ControllerConnected {
        /// The joystick device index for the `SDL_CONTROLLERDEVICEADDED` event or instance id for
        /// the `SDL_CONTROLLERDEVICEREMOVED` or `SDL_CONTROLLERDEVICEREMAPPED` event
        which: u32,
    },
}

impl<'a, T> Into<InputEvent<T>> for &'a ControllerEvent
where
    T: BindingTypes,
{
    fn into(self) -> InputEvent<T> {
        use self::ControllerEvent::*;
        match *self {
            ControllerAxisMoved { which, axis, value } => {
                InputEvent::ControllerAxisMoved { which, axis, value }
            }
            ControllerButtonPressed { which, button } => {
                InputEvent::ControllerButtonPressed { which, button }
            }
            ControllerButtonReleased { which, button } => {
                InputEvent::ControllerButtonReleased { which, button }
            }
            ControllerConnected { which } => InputEvent::ControllerConnected { which },
            ControllerDisconnected { which } => InputEvent::ControllerDisconnected { which },
        }
    }
}