Path: blob/master/javascript/imageviewerGamepad.js
3055 views
let gamepads = [];12window.addEventListener('gamepadconnected', (e) => {3const index = e.gamepad.index;4let isWaiting = false;5gamepads[index] = setInterval(async() => {6if (!opts.js_modal_lightbox_gamepad || isWaiting) return;7const gamepad = navigator.getGamepads()[index];8const xValue = gamepad.axes[0];9if (xValue <= -0.3) {10modalPrevImage(e);11isWaiting = true;12} else if (xValue >= 0.3) {13modalNextImage(e);14isWaiting = true;15}16if (isWaiting) {17await sleepUntil(() => {18const xValue = navigator.getGamepads()[index].axes[0];19if (xValue < 0.3 && xValue > -0.3) {20return true;21}22}, opts.js_modal_lightbox_gamepad_repeat);23isWaiting = false;24}25}, 10);26});2728window.addEventListener('gamepaddisconnected', (e) => {29clearInterval(gamepads[e.gamepad.index]);30});3132/*33Primarily for vr controller type pointer devices.34I use the wheel event because there's currently no way to do it properly with web xr.35*/36let isScrolling = false;37window.addEventListener('wheel', (e) => {38if (!opts.js_modal_lightbox_gamepad || isScrolling) return;39isScrolling = true;4041if (e.deltaX <= -0.6) {42modalPrevImage(e);43} else if (e.deltaX >= 0.6) {44modalNextImage(e);45}4647setTimeout(() => {48isScrolling = false;49}, opts.js_modal_lightbox_gamepad_repeat);50});5152function sleepUntil(f, timeout) {53return new Promise((resolve) => {54const timeStart = new Date();55const wait = setInterval(function() {56if (f() || new Date() - timeStart > timeout) {57clearInterval(wait);58resolve();59}60}, 20);61});62}636465