Skip to main content

script/dom/webxr/
xrhittestresult.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::context::JSContext;
7use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
8use webxr_api::HitTestResult;
9
10use crate::dom::bindings::codegen::Bindings::XRHitTestResultBinding::XRHitTestResultMethods;
11use crate::dom::bindings::reflector::DomGlobal;
12use crate::dom::bindings::root::{Dom, DomRoot};
13use crate::dom::window::Window;
14use crate::dom::xrframe::XRFrame;
15use crate::dom::xrpose::XRPose;
16use crate::dom::xrspace::XRSpace;
17
18#[dom_struct]
19pub(crate) struct XRHitTestResult {
20    reflector_: Reflector,
21    #[no_trace]
22    result: HitTestResult,
23    frame: Dom<XRFrame>,
24}
25
26impl XRHitTestResult {
27    fn new_inherited(result: HitTestResult, frame: &XRFrame) -> XRHitTestResult {
28        XRHitTestResult {
29            reflector_: Reflector::new(),
30            result,
31            frame: Dom::from_ref(frame),
32        }
33    }
34
35    pub(crate) fn new(
36        cx: &mut JSContext,
37        window: &Window,
38        result: HitTestResult,
39        frame: &XRFrame,
40    ) -> DomRoot<XRHitTestResult> {
41        reflect_dom_object_with_cx(
42            Box::new(XRHitTestResult::new_inherited(result, frame)),
43            window,
44            cx,
45        )
46    }
47}
48
49impl XRHitTestResultMethods<crate::DomTypeHolder> for XRHitTestResult {
50    /// <https://immersive-web.github.io/hit-test/#dom-xrhittestresult-getpose>
51    fn GetPose(&self, cx: &mut JSContext, base: &XRSpace) -> Option<DomRoot<XRPose>> {
52        let base = self.frame.get_pose(base)?;
53        let pose = self.result.space.then(&base.inverse());
54        Some(XRPose::new(cx, self.global().as_window(), pose.cast_unit()))
55    }
56}