Navigation Menu

Skip to content

Commit

Permalink
Add XRHand[index]
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Apr 28, 2020
1 parent 89fac8b commit ad6da05
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 6 deletions.
33 changes: 32 additions & 1 deletion components/script/dom/webidls/XRHand.webidl
Expand Up @@ -6,5 +6,36 @@

[SecureContext, Exposed=Window, Pref="dom.webxr.hands.enabled"]
interface XRHand {
// getter XRJoint(unsigned short jointIndex);
readonly attribute long length;
getter XRJointSpace(unsigned long index);

const unsigned long WRIST = 0;
const unsigned long THUMB_METACARPAL = 1;
const unsigned long THUMB_PHALANX_PROXIMAL = 2;
const unsigned long THUMB_PHALANX_DISTAL = 3;
const unsigned long THUMB_PHALANX_TIP = 4;

const unsigned long INDEX_METACARPAL = 5;
const unsigned long INDEX_PHALANX_PROXIMAL = 6;
const unsigned long INDEX_PHALANX_INTERMEDIATE = 7;
const unsigned long INDEX_PHALANX_DISTAL = 8;
const unsigned long INDEX_PHALANX_TIP = 9;

const unsigned long MIDDLE_METACARPAL = 10;
const unsigned long MIDDLE_PHALANX_PROXIMAL = 11;
const unsigned long MIDDLE_PHALANX_INTERMEDIATE = 12;
const unsigned long MIDDLE_PHALANX_DISTAL = 13;
const unsigned long MIDDLE_PHALANX_TIP = 14;

const unsigned long RING_METACARPAL = 15;
const unsigned long RING_PHALANX_PROXIMAL = 16;
const unsigned long RING_PHALANX_INTERMEDIATE = 17;
const unsigned long RING_PHALANX_DISTAL = 18;
const unsigned long RING_PHALANX_TIP = 19;

const unsigned long LITTLE_METACARPAL = 20;
const unsigned long LITTLE_PHALANX_PROXIMAL = 21;
const unsigned long LITTLE_PHALANX_INTERMEDIATE = 22;
const unsigned long LITTLE_PHALANX_DISTAL = 23;
const unsigned long LITTLE_PHALANX_TIP = 24;
};
65 changes: 60 additions & 5 deletions components/script/dom/xrhand.rs
Expand Up @@ -2,32 +2,87 @@
* 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 crate::dom::bindings::codegen::Bindings::XRHandBinding::{XRHandConstants, XRHandMethods};
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;
use dom_struct::dom_struct;
use webxr_api::Hand;
use webxr_api::{FingerJoint, Hand, Joint};

#[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"]
support: Hand<()>,
spaces: Hand<Dom<XRJointSpace>>,
}

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

pub fn new(global: &GlobalScope, source: &XRInputSource, support: Hand<()>) -> DomRoot<XRHand> {
reflect_dom_object(Box::new(XRHand::new_inherited(source, support)), global)
let id = source.id();
let session = source.session();
let spaces = support
.map(|field, joint| field.map(|_| XRJointSpace::new(global, session, id, 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 Length(&self) -> i32 {
XRHandConstants::LITTLE_PHALANX_TIP as i32 + 1
}

/// https://github.com/immersive-web/webxr-hands-input/blob/master/explainer.md
fn IndexedGetter(&self, joint_index: u32) -> Option<DomRoot<XRJointSpace>> {
let joint = match joint_index {
XRHandConstants::WRIST => Joint::Wrist,
XRHandConstants::THUMB_METACARPAL => Joint::ThumbMetacarpal,
XRHandConstants::THUMB_PHALANX_PROXIMAL => Joint::ThumbPhalanxProximal,
XRHandConstants::THUMB_PHALANX_DISTAL => Joint::ThumbPhalanxDistal,
XRHandConstants::THUMB_PHALANX_TIP => Joint::ThumbPhalanxTip,
XRHandConstants::INDEX_METACARPAL => Joint::Index(FingerJoint::Metacarpal),
XRHandConstants::INDEX_PHALANX_PROXIMAL => Joint::Index(FingerJoint::PhalanxProximal),
XRHandConstants::INDEX_PHALANX_INTERMEDIATE => {
Joint::Index(FingerJoint::PhalanxIntermediate)
},
XRHandConstants::INDEX_PHALANX_DISTAL => Joint::Index(FingerJoint::PhalanxDistal),
XRHandConstants::INDEX_PHALANX_TIP => Joint::Index(FingerJoint::PhalanxTip),
XRHandConstants::MIDDLE_METACARPAL => Joint::Middle(FingerJoint::Metacarpal),
XRHandConstants::MIDDLE_PHALANX_PROXIMAL => Joint::Middle(FingerJoint::PhalanxProximal),
XRHandConstants::MIDDLE_PHALANX_INTERMEDIATE => {
Joint::Middle(FingerJoint::PhalanxIntermediate)
},
XRHandConstants::MIDDLE_PHALANX_DISTAL => Joint::Middle(FingerJoint::PhalanxDistal),
XRHandConstants::MIDDLE_PHALANX_TIP => Joint::Middle(FingerJoint::PhalanxTip),
XRHandConstants::RING_METACARPAL => Joint::Ring(FingerJoint::Metacarpal),
XRHandConstants::RING_PHALANX_PROXIMAL => Joint::Ring(FingerJoint::PhalanxProximal),
XRHandConstants::RING_PHALANX_INTERMEDIATE => {
Joint::Ring(FingerJoint::PhalanxIntermediate)
},
XRHandConstants::RING_PHALANX_DISTAL => Joint::Ring(FingerJoint::PhalanxDistal),
XRHandConstants::RING_PHALANX_TIP => Joint::Ring(FingerJoint::PhalanxTip),
XRHandConstants::LITTLE_METACARPAL => Joint::Little(FingerJoint::Metacarpal),
XRHandConstants::LITTLE_PHALANX_PROXIMAL => Joint::Little(FingerJoint::PhalanxProximal),
XRHandConstants::LITTLE_PHALANX_INTERMEDIATE => {
Joint::Little(FingerJoint::PhalanxIntermediate)
},
XRHandConstants::LITTLE_PHALANX_DISTAL => Joint::Little(FingerJoint::PhalanxDistal),
XRHandConstants::LITTLE_PHALANX_TIP => Joint::Little(FingerJoint::PhalanxTip),
// XXXManishearth should this be a TypeError?
_ => return None,
};
self.spaces.get(joint).map(|j| DomRoot::from_ref(&**j))
}
}
4 changes: 4 additions & 0 deletions components/script/dom/xrinputsource.rs
Expand Up @@ -69,6 +69,10 @@ impl XRInputSource {
pub fn id(&self) -> InputId {
self.info.id
}

pub fn session(&self) -> &XRSession {
&self.session
}
}

impl XRInputSourceMethods for XRInputSource {
Expand Down

0 comments on commit ad6da05

Please sign in to comment.