1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use dom_struct::dom_struct;
use webxr_api::{FingerJoint, Hand, Joint};

use crate::dom::bindings::codegen::Bindings::XRHandBinding::{XRHandJoint, XRHandMethods};
use crate::dom::bindings::iterable::Iterable;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::globalscope::GlobalScope;
use crate::dom::xrinputsource::XRInputSource;
use crate::dom::xrjointspace::XRJointSpace;

const JOINT_SPACE_MAP: [(XRHandJoint, Joint); 25] = [
    (XRHandJoint::Wrist, Joint::Wrist),
    (XRHandJoint::Thumb_metacarpal, Joint::ThumbMetacarpal),
    (
        XRHandJoint::Thumb_phalanx_proximal,
        Joint::ThumbPhalanxProximal,
    ),
    (XRHandJoint::Thumb_phalanx_distal, Joint::ThumbPhalanxDistal),
    (XRHandJoint::Thumb_tip, Joint::ThumbPhalanxTip),
    (
        XRHandJoint::Index_finger_metacarpal,
        Joint::Index(FingerJoint::Metacarpal),
    ),
    (
        XRHandJoint::Index_finger_phalanx_proximal,
        Joint::Index(FingerJoint::PhalanxProximal),
    ),
    (XRHandJoint::Index_finger_phalanx_intermediate, {
        Joint::Index(FingerJoint::PhalanxIntermediate)
    }),
    (
        XRHandJoint::Index_finger_phalanx_distal,
        Joint::Index(FingerJoint::PhalanxDistal),
    ),
    (
        XRHandJoint::Index_finger_tip,
        Joint::Index(FingerJoint::PhalanxTip),
    ),
    (
        XRHandJoint::Middle_finger_metacarpal,
        Joint::Middle(FingerJoint::Metacarpal),
    ),
    (
        XRHandJoint::Middle_finger_phalanx_proximal,
        Joint::Middle(FingerJoint::PhalanxProximal),
    ),
    (XRHandJoint::Middle_finger_phalanx_intermediate, {
        Joint::Middle(FingerJoint::PhalanxIntermediate)
    }),
    (
        XRHandJoint::Middle_finger_phalanx_distal,
        Joint::Middle(FingerJoint::PhalanxDistal),
    ),
    (
        XRHandJoint::Middle_finger_tip,
        Joint::Middle(FingerJoint::PhalanxTip),
    ),
    (
        XRHandJoint::Ring_finger_metacarpal,
        Joint::Ring(FingerJoint::Metacarpal),
    ),
    (
        XRHandJoint::Ring_finger_phalanx_proximal,
        Joint::Ring(FingerJoint::PhalanxProximal),
    ),
    (XRHandJoint::Ring_finger_phalanx_intermediate, {
        Joint::Ring(FingerJoint::PhalanxIntermediate)
    }),
    (
        XRHandJoint::Ring_finger_phalanx_distal,
        Joint::Ring(FingerJoint::PhalanxDistal),
    ),
    (
        XRHandJoint::Ring_finger_tip,
        Joint::Ring(FingerJoint::PhalanxTip),
    ),
    (
        XRHandJoint::Pinky_finger_metacarpal,
        Joint::Little(FingerJoint::Metacarpal),
    ),
    (
        XRHandJoint::Pinky_finger_phalanx_proximal,
        Joint::Little(FingerJoint::PhalanxProximal),
    ),
    (XRHandJoint::Pinky_finger_phalanx_intermediate, {
        Joint::Little(FingerJoint::PhalanxIntermediate)
    }),
    (
        XRHandJoint::Pinky_finger_phalanx_distal,
        Joint::Little(FingerJoint::PhalanxDistal),
    ),
    (
        XRHandJoint::Pinky_finger_tip,
        Joint::Little(FingerJoint::PhalanxTip),
    ),
];

#[dom_struct]
pub struct XRHand {
    reflector_: Reflector,
    #[ignore_malloc_size_of = "defined in webxr"]
    source: Dom<XRInputSource>,
    #[ignore_malloc_size_of = "partially defind in webxr"]
    #[custom_trace]
    spaces: Hand<Dom<XRJointSpace>>,
}

impl XRHand {
    fn new_inherited(source: &XRInputSource, spaces: &Hand<DomRoot<XRJointSpace>>) -> XRHand {
        XRHand {
            reflector_: Reflector::new(),
            source: Dom::from_ref(source),
            spaces: spaces.map(|j, _| j.as_ref().map(|j| Dom::from_ref(&**j))),
        }
    }

    pub fn new(global: &GlobalScope, source: &XRInputSource, support: Hand<()>) -> DomRoot<XRHand> {
        let id = source.id();
        let session = source.session();
        let spaces = support.map(|field, joint| {
            let hand_joint = JOINT_SPACE_MAP
                .iter()
                .find(|&&(_, value)| value == joint)
                .map(|&(hand_joint, _)| hand_joint)
                .expect("Invalid joint name");
            field.map(|_| XRJointSpace::new(global, session, id, joint, hand_joint))
        });
        reflect_dom_object(Box::new(XRHand::new_inherited(source, &spaces)), global)
    }
}

impl XRHandMethods for XRHand {
    /// <https://github.com/immersive-web/webxr-hands-input/blob/master/explainer.md>
    fn Size(&self) -> u32 {
        XRHandJoint::Pinky_finger_tip as u32 + 1
    }

    /// <https://github.com/immersive-web/webxr-hands-input/blob/master/explainer.md>
    fn Get(&self, joint_name: XRHandJoint) -> DomRoot<XRJointSpace> {
        let joint = JOINT_SPACE_MAP
            .iter()
            .find(|&&(key, _)| key == joint_name)
            .map(|&(_, joint)| joint)
            .expect("Invalid joint name");
        self.spaces
            .get(joint)
            .map(|j| DomRoot::from_ref(&**j))
            .expect("Failed to get joint pose")
    }
}

impl Iterable for XRHand {
    type Key = XRHandJoint;
    type Value = DomRoot<XRJointSpace>;

    fn get_iterable_length(&self) -> u32 {
        JOINT_SPACE_MAP.len() as u32
    }

    fn get_value_at_index(&self, n: u32) -> DomRoot<XRJointSpace> {
        let joint = JOINT_SPACE_MAP[n as usize].1;
        self.spaces
            .get(joint)
            .map(|j| DomRoot::from_ref(&**j))
            .expect("Failed to get joint pose")
    }

    fn get_key_at_index(&self, n: u32) -> XRHandJoint {
        JOINT_SPACE_MAP[n as usize].0
    }
}