script/dom/gamepad/
gamepadbuttonlist.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6
7use crate::dom::bindings::codegen::Bindings::GamepadButtonListBinding::GamepadButtonListMethods;
8use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
9use crate::dom::bindings::root::{Dom, DomRoot, DomSlice};
10use crate::dom::gamepad::gamepadbutton::GamepadButton;
11use crate::dom::window::Window;
12use crate::script_runtime::CanGc;
13
14// https://w3c.github.io/gamepad/#gamepadbutton-interface
15#[dom_struct]
16pub(crate) struct GamepadButtonList {
17    reflector_: Reflector,
18    list: Vec<Dom<GamepadButton>>,
19}
20
21impl GamepadButtonList {
22    fn new_inherited(list: &[&GamepadButton]) -> GamepadButtonList {
23        GamepadButtonList {
24            reflector_: Reflector::new(),
25            list: list.iter().map(|button| Dom::from_ref(*button)).collect(),
26        }
27    }
28
29    pub(crate) fn new(
30        window: &Window,
31        list: &[&GamepadButton],
32        can_gc: CanGc,
33    ) -> DomRoot<GamepadButtonList> {
34        reflect_dom_object(
35            Box::new(GamepadButtonList::new_inherited(list)),
36            window,
37            can_gc,
38        )
39    }
40}
41
42impl GamepadButtonListMethods<crate::DomTypeHolder> for GamepadButtonList {
43    /// <https://w3c.github.io/gamepad/#dom-gamepad-buttons>
44    fn Length(&self) -> u32 {
45        self.list.len() as u32
46    }
47
48    /// <https://w3c.github.io/gamepad/#dom-gamepad-buttons>
49    fn Item(&self, index: u32) -> Option<DomRoot<GamepadButton>> {
50        self.list
51            .get(index as usize)
52            .map(|button| DomRoot::from_ref(&**button))
53    }
54
55    /// <https://w3c.github.io/gamepad/#dom-gamepad-buttons>
56    fn IndexedGetter(&self, index: u32) -> Option<DomRoot<GamepadButton>> {
57        self.Item(index)
58    }
59}
60
61impl GamepadButtonList {
62    /// Initialize the number of buttons in the "standard" gamepad mapping.
63    /// <https://www.w3.org/TR/gamepad/#dfn-initializing-buttons>
64    pub(crate) fn init_buttons(window: &Window, can_gc: CanGc) -> DomRoot<GamepadButtonList> {
65        let standard_buttons = &[
66            GamepadButton::new(window, false, false, can_gc), // Bottom button in right cluster
67            GamepadButton::new(window, false, false, can_gc), // Right button in right cluster
68            GamepadButton::new(window, false, false, can_gc), // Left button in right cluster
69            GamepadButton::new(window, false, false, can_gc), // Top button in right cluster
70            GamepadButton::new(window, false, false, can_gc), // Top left front button
71            GamepadButton::new(window, false, false, can_gc), // Top right front button
72            GamepadButton::new(window, false, false, can_gc), // Bottom left front button
73            GamepadButton::new(window, false, false, can_gc), // Bottom right front button
74            GamepadButton::new(window, false, false, can_gc), // Left button in center cluster
75            GamepadButton::new(window, false, false, can_gc), // Right button in center cluster
76            GamepadButton::new(window, false, false, can_gc), // Left stick pressed button
77            GamepadButton::new(window, false, false, can_gc), // Right stick pressed button
78            GamepadButton::new(window, false, false, can_gc), // Top button in left cluster
79            GamepadButton::new(window, false, false, can_gc), // Bottom button in left cluster
80            GamepadButton::new(window, false, false, can_gc), // Left button in left cluster
81            GamepadButton::new(window, false, false, can_gc), // Right button in left cluster
82            GamepadButton::new(window, false, false, can_gc), // Center button in center cluster
83        ];
84        rooted_vec!(let buttons <- standard_buttons.iter().map(DomRoot::as_traced));
85        Self::new(window, buttons.r(), can_gc)
86    }
87}