script/dom/webxr/
xrjointspace.rs1use dom_struct::dom_struct;
6use euclid::RigidTransform3D;
7use webxr_api::{BaseSpace, Frame, InputId, Joint, JointFrame, Space};
8
9use crate::dom::bindings::codegen::Bindings::XRHandBinding::XRHandJoint;
10use crate::dom::bindings::codegen::Bindings::XRJointSpaceBinding::XRJointSpaceMethods;
11use crate::dom::bindings::reflector::reflect_dom_object;
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::globalscope::GlobalScope;
14use crate::dom::xrsession::{ApiPose, XRSession};
15use crate::dom::xrspace::XRSpace;
16use crate::script_runtime::CanGc;
17
18#[dom_struct]
19pub(crate) struct XRJointSpace {
20 xrspace: XRSpace,
21 #[ignore_malloc_size_of = "defined in rust-webxr"]
22 #[no_trace]
23 input: InputId,
24 #[ignore_malloc_size_of = "defined in rust-webxr"]
25 #[no_trace]
26 joint: Joint,
27 hand_joint: XRHandJoint,
28}
29
30impl XRJointSpace {
31 pub(crate) fn new_inherited(
32 session: &XRSession,
33 input: InputId,
34 joint: Joint,
35 hand_joint: XRHandJoint,
36 ) -> XRJointSpace {
37 XRJointSpace {
38 xrspace: XRSpace::new_inherited(session),
39 input,
40 joint,
41 hand_joint,
42 }
43 }
44
45 pub(crate) fn new(
46 global: &GlobalScope,
47 session: &XRSession,
48 input: InputId,
49 joint: Joint,
50 hand_joint: XRHandJoint,
51 can_gc: CanGc,
52 ) -> DomRoot<XRJointSpace> {
53 reflect_dom_object(
54 Box::new(Self::new_inherited(session, input, joint, hand_joint)),
55 global,
56 can_gc,
57 )
58 }
59
60 pub(crate) fn space(&self) -> Space {
61 let base = BaseSpace::Joint(self.input, self.joint);
62 let offset = RigidTransform3D::identity();
63 Space { base, offset }
64 }
65
66 pub(crate) fn frame<'a>(&self, frame: &'a Frame) -> Option<&'a JointFrame> {
67 frame
68 .inputs
69 .iter()
70 .find(|i| i.id == self.input)
71 .and_then(|i| i.hand.as_ref())
72 .and_then(|h| h.get(self.joint))
73 }
74
75 pub(crate) fn get_pose(&self, frame: &Frame) -> Option<ApiPose> {
76 self.frame(frame).map(|f| f.pose).map(|t| t.cast_unit())
77 }
78}
79
80impl XRJointSpaceMethods<crate::DomTypeHolder> for XRJointSpace {
81 fn JointName(&self) -> XRHandJoint {
83 self.hand_joint
84 }
85}