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

//! Back-end agnostic controller events.

use { Event, Input, Motion };

/// Components of a controller button event. Not guaranteed consistent across
/// backends.
#[derive(Copy, Clone, RustcDecodable, RustcEncodable, PartialEq, Eq, Debug, Hash)]
pub struct ControllerButton {
    /// Which controller was the button on.
    pub id: i32,
    /// Which button was pressed.
    pub button: u8,
}

impl ControllerButton {
    /// Create a new ControllerButton object. Intended for use by backends when
    /// emitting events.
    pub fn new(id: i32, button: u8) -> Self {
        ControllerButton {
            id: id,
            button: button,
        }
    }
}

/// Components of a controller axis move event. Not guaranteed consistent across
/// backends.
#[derive(Copy, Clone, RustcDecodable, RustcEncodable, PartialEq, Debug)]
pub struct ControllerAxisArgs {
    /// Which controller moved.
    pub id: i32,
    /// The axis that moved.
    pub axis: u8,
    /// Position of the controller. Usually [-1.0, 1.0], though backends may use
    /// a different range for various devices.
    pub position: f64
}

impl ControllerAxisArgs {
    /// Create a new ControllerAxisArgs object. Intended for use by backends when
    /// emitting events.
    pub fn new(id: i32, axis: u8, position: f64) -> Self {
        ControllerAxisArgs {
            id: id,
            axis: axis,
            position: position,
        }
    }
}

/// The position of a controller axis changed.
pub trait ControllerAxisEvent: Sized {
    /// Creates a controller axis event.
    fn from_controller_axis_args(
        args: ControllerAxisArgs,
        old_event: &Self
    ) -> Option<Self>;
    /// Calls closure if this is a controller axis event.
    fn controller_axis<U, F>(&self, f: F) -> Option<U>
        where F: FnMut(ControllerAxisArgs) -> U;
    /// Returns controller axis arguments.
    fn controller_axis_args(&self) -> Option<ControllerAxisArgs> {
        self.controller_axis(|args| args)
    }
}

/* TODO: Enable when specialization gets stable.
impl<T: GenericEvent> ControllerAxisEvent for T {
    fn from_controller_axis_args(
        args: ControllerAxisArgs,
        old_event: &Self
    ) -> Option<Self> {
        GenericEvent::from_args(CONTROLLER_AXIS, &args as &Any, old_event)
    }

    fn controller_axis<U, F>(&self, mut f: F) -> Option<U>
        where F: FnMut(ControllerAxisArgs) -> U
    {
        if self.event_id() != CONTROLLER_AXIS {
            return None;
        }
        self.with_args(|any| {
            if let Some(&args) = any.downcast_ref::<ControllerAxisArgs>() {
                Some(f(args))
            } else {
                panic!("Expected ControllerAxisArgs")
            }
        })
    }
}
*/

impl ControllerAxisEvent for Input {
    fn from_controller_axis_args(
        args: ControllerAxisArgs,
        _old_event: &Self
    ) -> Option<Self> {
        Some(Input::Move(Motion::ControllerAxis(args)))
    }

    fn controller_axis<U, F>(&self, mut f: F) -> Option<U>
        where F: FnMut(ControllerAxisArgs) -> U
    {
        match *self {
            Input::Move(Motion::ControllerAxis(args)) => Some(f(args)),
            _ => None
        }
    }
}

impl<I: ControllerAxisEvent> ControllerAxisEvent for Event<I> {
    fn from_controller_axis_args(
        args: ControllerAxisArgs,
        old_event: &Self
    ) -> Option<Self> {
        if let &Event::Input(ref old_input) = old_event {
            <I as ControllerAxisEvent>::from_controller_axis_args(args, old_input)
                .map(|x| Event::Input(x))
        } else {
            None
        }
    }

    fn controller_axis<U, F>(&self, f: F) -> Option<U>
        where F: FnMut(ControllerAxisArgs) -> U
    {
        match *self {
            Event::Input(ref x) => x.controller_axis(f),
            _ => None
        }
    }
}

#[cfg(test)]
mod controller_axis_tests {
    use super::*;

    #[test]
    fn test_input_controller_axis() {
        use super::super::{ Input, Motion };

        let e = Input::Move(Motion::ControllerAxis(
            ControllerAxisArgs::new(0, 1, 0.9)));
        let a: Option<Input> = ControllerAxisEvent::from_controller_axis_args(
            ControllerAxisArgs::new(0, 1, 0.9), &e);
        let b: Option<Input> = a.clone().unwrap().controller_axis(|cnt|
            ControllerAxisEvent::from_controller_axis_args(
                ControllerAxisArgs::new(cnt.id, cnt.axis, cnt.position),
                a.as_ref().unwrap())).unwrap();
        assert_eq!(a, b);
    }

    #[test]
    fn test_event_controller_axis() {
        use Event;
        use super::super::{ Input, Motion };

        let e = Event::Input(Input::Move(Motion::ControllerAxis(
            ControllerAxisArgs::new(0, 1, 0.9))));
        let a: Option<Event> = ControllerAxisEvent::from_controller_axis_args(
            ControllerAxisArgs::new(0, 1, 0.9), &e);
        let b: Option<Event> = a.clone().unwrap().controller_axis(|cnt|
            ControllerAxisEvent::from_controller_axis_args(
                ControllerAxisArgs::new(cnt.id, cnt.axis, cnt.position),
                a.as_ref().unwrap())).unwrap();
        assert_eq!(a, b);
    }
}