Skip to main content

script/dom/gamepad/
gamepadpose.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;
6use js::typedarray::{Float32, HeapFloat32Array};
7use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
8use script_bindings::trace::RootedTraceableBox;
9
10use crate::dom::bindings::buffer_source::HeapBufferSource;
11use crate::dom::bindings::codegen::Bindings::GamepadPoseBinding::GamepadPoseMethods;
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::globalscope::GlobalScope;
14
15#[dom_struct]
16pub(crate) struct GamepadPose {
17    reflector_: Reflector,
18    #[ignore_malloc_size_of = "mozjs"]
19    position: HeapBufferSource<Float32>,
20    #[ignore_malloc_size_of = "mozjs"]
21    orientation: HeapBufferSource<Float32>,
22    #[ignore_malloc_size_of = "mozjs"]
23    linear_vel: HeapBufferSource<Float32>,
24    #[ignore_malloc_size_of = "mozjs"]
25    angular_vel: HeapBufferSource<Float32>,
26    #[ignore_malloc_size_of = "mozjs"]
27    linear_acc: HeapBufferSource<Float32>,
28    #[ignore_malloc_size_of = "mozjs"]
29    angular_acc: HeapBufferSource<Float32>,
30}
31
32// TODO: support gamepad discovery
33#[expect(dead_code)]
34impl GamepadPose {
35    fn new_inherited() -> GamepadPose {
36        GamepadPose {
37            reflector_: Reflector::new(),
38            position: HeapBufferSource::default(),
39            orientation: HeapBufferSource::default(),
40            linear_vel: HeapBufferSource::default(),
41            angular_vel: HeapBufferSource::default(),
42            linear_acc: HeapBufferSource::default(),
43            angular_acc: HeapBufferSource::default(),
44        }
45    }
46
47    pub(crate) fn new(
48        cx: &mut js::context::JSContext,
49        global: &GlobalScope,
50    ) -> DomRoot<GamepadPose> {
51        reflect_dom_object_with_cx(Box::new(GamepadPose::new_inherited()), global, cx)
52    }
53}
54
55impl GamepadPoseMethods<crate::DomTypeHolder> for GamepadPose {
56    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-position>
57    fn GetPosition(&self) -> Option<RootedTraceableBox<HeapFloat32Array>> {
58        self.position.typed_array_to_option()
59    }
60
61    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-hasposition>
62    fn HasPosition(&self) -> bool {
63        self.position.is_initialized()
64    }
65
66    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-linearvelocity>
67    fn GetLinearVelocity(&self) -> Option<RootedTraceableBox<HeapFloat32Array>> {
68        self.linear_vel.typed_array_to_option()
69    }
70
71    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-linearacceleration>
72    fn GetLinearAcceleration(&self) -> Option<RootedTraceableBox<HeapFloat32Array>> {
73        self.linear_acc.typed_array_to_option()
74    }
75
76    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-orientation>
77    fn GetOrientation(&self) -> Option<RootedTraceableBox<HeapFloat32Array>> {
78        self.orientation.typed_array_to_option()
79    }
80
81    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-orientation>
82    fn HasOrientation(&self) -> bool {
83        self.orientation.is_initialized()
84    }
85
86    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-angularvelocity>
87    fn GetAngularVelocity(&self) -> Option<RootedTraceableBox<HeapFloat32Array>> {
88        self.angular_vel.typed_array_to_option()
89    }
90
91    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-angularacceleration>
92    fn GetAngularAcceleration(&self) -> Option<RootedTraceableBox<HeapFloat32Array>> {
93        self.angular_acc.typed_array_to_option()
94    }
95}