stdweb/webapi/events/
gamepad.rs

1use webcore::value::Reference;
2use webcore::try_from::TryInto;
3
4use webapi::event::{IEvent, Event};
5use webapi::gamepad::Gamepad;
6
7/// A GamepadEvent is fired on the window object, when a gamepad is connected or disconnected to the system.
8///
9/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/GamepadEvent)
10// https://w3c.github.io/gamepad/#gamepadevent-interface
11pub trait IGamepadEvent: IEvent {
12
13    /// Returns the gamepad associated with this event.
14    #[inline]
15    fn gamepad( &self ) -> Gamepad {
16        js!(
17            return @{self.as_ref()}.gamepad;
18        ).try_into().unwrap()
19    }
20}
21
22/// A reference to a JavaScript object which implements the [IGamepadEvent](trait.IGamepadEvent.html)
23/// interface.
24///
25/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/GamepadEvent)
26// https://w3c.github.io/gamepad/#gamepadevent-interface
27#[derive(Clone, Debug, Eq, PartialEq, ReferenceType)]
28#[reference(instance_of = "GamepadEvent")]
29#[reference(subclass_of(Event))]
30pub struct GamepadEvent( Reference );
31
32impl IEvent for GamepadEvent {}
33impl IGamepadEvent for GamepadEvent {}
34
35/// The `GamepadConnected` event is fired on the window object, when the first input is received for a gamepad.
36///
37/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/gamepadconnected)
38// https://w3c.github.io/gamepad/#event-gamepadconnected
39#[derive(Clone, Debug, Eq, PartialEq, ReferenceType)]
40#[reference(instance_of = "GamepadEvent")]
41#[reference(event = "gamepadconnected")]
42#[reference(subclass_of(Event, GamepadEvent))]
43pub struct GamepadConnectedEvent( Reference );
44
45impl IEvent for GamepadConnectedEvent {}
46impl IGamepadEvent for GamepadConnectedEvent {}
47
48/// The `GamepadDisconnected` event is fired on the window object, when a gamepad is disconnected.
49///
50/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/gamepaddisconnected)
51// https://w3c.github.io/gamepad/#event-gamepaddisconnected
52#[derive(Clone, Debug, Eq, PartialEq, ReferenceType)]
53#[reference(instance_of = "GamepadEvent")]
54#[reference(event = "gamepaddisconnected")]
55#[reference(subclass_of(Event, GamepadEvent))]
56pub struct GamepadDisconnectedEvent( Reference );
57
58impl IEvent for GamepadDisconnectedEvent {}
59impl IGamepadEvent for GamepadDisconnectedEvent {}
60
61#[cfg(all(test, feature = "web_test"))]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_gamepad_connected_event() {
67
68        let event: GamepadConnectedEvent = js!(
69            return new GamepadEvent("gamepadconnected");
70        ).try_into().unwrap();
71
72        assert_eq!(event.event_type(), "gamepadconnected");
73    }
74
75    #[test]
76    fn test_gamepad_disconnected_event() {
77
78        let event: GamepadDisconnectedEvent = js!(
79            return new GamepadEvent("gamepaddisconnected");
80        ).try_into().unwrap();
81
82        assert_eq!(event.event_type(), "gamepaddisconnected");
83    }
84}