@pmndrs/xr
Version:
VR/AR for threejs
45 lines (44 loc) • 1.72 kB
JavaScript
export function bindXRInputSourceEvent(session, inputSource, event, fn) {
const filterFn = (event) => {
if (inputSource != 'all' && event.inputSource != inputSource) {
return;
}
fn(event);
};
session.addEventListener(event, filterFn);
return () => session.removeEventListener(event, filterFn);
}
export function bindPointerXRInputSourceEvent(pointer, session, inputSource, event, missingEvents, options = {}) {
const downListener = (e) => {
if (e.inputSource === inputSource) {
pointer.down(Object.assign(e, { button: options.button ?? 0 }));
}
};
const upListener = (e) => {
if (e.inputSource === inputSource) {
pointer.up(Object.assign(e, { button: options.button ?? 0 }));
}
};
const downEventName = `${event}start`;
const upEventName = `${event}end`;
//missing events are required for transient pointers when the input source is registered asynchrounously
//so that events directly emitted on initialization are still processed once the input source is created
const length = missingEvents.length;
for (let i = 0; i < length; i++) {
const event = missingEvents[i];
switch (event.type) {
case downEventName:
downListener(event);
break;
case upEventName:
upListener(event);
break;
}
}
session.addEventListener(downEventName, downListener);
session.addEventListener(upEventName, upListener);
return () => {
session.removeEventListener(downEventName, downListener);
session.removeEventListener(upEventName, upListener);
};
}