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