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::trace::RootedTraceableBox;
8
9use crate::dom::bindings::buffer_source::HeapBufferSource;
10use crate::dom::bindings::codegen::Bindings::GamepadPoseBinding::GamepadPoseMethods;
11use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::globalscope::GlobalScope;
14use crate::script_runtime::{CanGc, 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(global: &GlobalScope, can_gc: CanGc) -> DomRoot<GamepadPose> {
49        reflect_dom_object(Box::new(GamepadPose::new_inherited()), global, can_gc)
50    }
51}
52
53impl GamepadPoseMethods<crate::DomTypeHolder> for GamepadPose {
54    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-position>
55    fn GetPosition(&self, _cx: JSContext) -> Option<RootedTraceableBox<HeapFloat32Array>> {
56        self.position.typed_array_to_option()
57    }
58
59    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-hasposition>
60    fn HasPosition(&self) -> bool {
61        self.position.is_initialized()
62    }
63
64    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-linearvelocity>
65    fn GetLinearVelocity(&self, _cx: JSContext) -> Option<RootedTraceableBox<HeapFloat32Array>> {
66        self.linear_vel.typed_array_to_option()
67    }
68
69    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-linearacceleration>
70    fn GetLinearAcceleration(
71        &self,
72        _cx: JSContext,
73    ) -> Option<RootedTraceableBox<HeapFloat32Array>> {
74        self.linear_acc.typed_array_to_option()
75    }
76
77    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-orientation>
78    fn GetOrientation(&self, _cx: JSContext) -> Option<RootedTraceableBox<HeapFloat32Array>> {
79        self.orientation.typed_array_to_option()
80    }
81
82    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-orientation>
83    fn HasOrientation(&self) -> bool {
84        self.orientation.is_initialized()
85    }
86
87    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-angularvelocity>
88    fn GetAngularVelocity(&self, _cx: JSContext) -> Option<RootedTraceableBox<HeapFloat32Array>> {
89        self.angular_vel.typed_array_to_option()
90    }
91
92    /// <https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-angularacceleration>
93    fn GetAngularAcceleration(
94        &self,
95        _cx: JSContext,
96    ) -> Option<RootedTraceableBox<HeapFloat32Array>> {
97        self.angular_acc.typed_array_to_option()
98    }
99}