2023-05-07 04:00:13 +00:00
|
|
|
let isWaiting = false;
|
2023-05-06 04:48:27 +00:00
|
|
|
window.addEventListener('gamepadconnected', (e) => {
|
2023-05-07 04:00:13 +00:00
|
|
|
setInterval(async () => {
|
|
|
|
if (!opts.js_modal_lightbox_gamepad || isWaiting) return;
|
2023-05-06 04:48:27 +00:00
|
|
|
const gamepad = navigator.getGamepads()[0];
|
|
|
|
const xValue = gamepad.axes[0];
|
2023-05-07 04:00:13 +00:00
|
|
|
if (xValue <= -0.3) {
|
2023-03-13 08:39:02 +00:00
|
|
|
modalPrevImage(e);
|
2023-05-07 04:00:13 +00:00
|
|
|
isWaiting = true;
|
|
|
|
} else if (xValue >= 0.3) {
|
2023-03-13 08:39:02 +00:00
|
|
|
modalNextImage(e);
|
2023-05-07 04:00:13 +00:00
|
|
|
isWaiting = true;
|
2023-03-13 08:39:02 +00:00
|
|
|
}
|
2023-05-07 04:00:13 +00:00
|
|
|
if (isWaiting) {
|
|
|
|
await sleepUntil(() => {
|
|
|
|
const xValue = navigator.getGamepads()[0].axes[0]
|
|
|
|
if (xValue < 0.3 && xValue > -0.3) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-05-07 04:16:51 +00:00
|
|
|
}, opts.js_modal_lightbox_gamepad_repeat);
|
2023-05-07 04:00:13 +00:00
|
|
|
isWaiting = false;
|
|
|
|
}
|
|
|
|
}, 10);
|
2023-05-06 04:48:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
Primarily for vr controller type pointer devices.
|
|
|
|
I use the wheel event because there's currently no way to do it properly with web xr.
|
|
|
|
*/
|
|
|
|
let isScrolling = false;
|
|
|
|
window.addEventListener('wheel', (e) => {
|
|
|
|
if (!opts.js_modal_lightbox_gamepad || isScrolling) return;
|
|
|
|
isScrolling = true;
|
|
|
|
|
|
|
|
if (e.deltaX <= -0.6) {
|
|
|
|
modalPrevImage(e);
|
|
|
|
} else if (e.deltaX >= 0.6) {
|
|
|
|
modalNextImage(e);
|
|
|
|
}
|
2023-03-13 08:39:02 +00:00
|
|
|
|
2023-05-06 04:48:27 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
isScrolling = false;
|
2023-05-07 04:16:51 +00:00
|
|
|
}, opts.js_modal_lightbox_gamepad_repeat);
|
2023-05-06 04:48:27 +00:00
|
|
|
});
|
2023-05-07 04:00:13 +00:00
|
|
|
|
|
|
|
function sleepUntil(f, timeout) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const timeStart = new Date();
|
|
|
|
const wait = setInterval(function() {
|
|
|
|
if (f() || new Date() - timeStart > timeout) {
|
|
|
|
clearInterval(wait);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}, 20);
|
|
|
|
});
|
|
|
|
}
|