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