diff options
author | AUTOMATIC <16777216c@gmail.com> | 2023-05-13 05:16:37 +0000 |
---|---|---|
committer | AUTOMATIC <16777216c@gmail.com> | 2023-05-13 05:16:37 +0000 |
commit | b08500cec8a791ef20082628b49b17df833f5dda (patch) | |
tree | 2d09f3ca93139f082b88463f3a2a43a4ac45526f /javascript/imageviewerGamepad.js | |
parent | 5ab7f213bec2f816f9c5644becb32eb72c8ffb89 (diff) | |
parent | 231562ea13e4f697953bdbabd6b76b22a88c587b (diff) | |
download | stable-diffusion-webui-gfx803-b08500cec8a791ef20082628b49b17df833f5dda.tar.gz stable-diffusion-webui-gfx803-b08500cec8a791ef20082628b49b17df833f5dda.tar.bz2 stable-diffusion-webui-gfx803-b08500cec8a791ef20082628b49b17df833f5dda.zip |
Merge branch 'release_candidate'
Diffstat (limited to 'javascript/imageviewerGamepad.js')
-rw-r--r-- | javascript/imageviewerGamepad.js | 85 |
1 files changed, 53 insertions, 32 deletions
diff --git a/javascript/imageviewerGamepad.js b/javascript/imageviewerGamepad.js index 29bd7140..6297a12b 100644 --- a/javascript/imageviewerGamepad.js +++ b/javascript/imageviewerGamepad.js @@ -1,36 +1,57 @@ - let delay = 350//ms - window.addEventListener('gamepadconnected', (e) => { - console.log("Gamepad connected!") - const gamepad = e.gamepad; - setInterval(() => { - const xValue = gamepad.axes[0].toFixed(2); - if (xValue < -0.3) { - modalPrevImage(e); - } else if (xValue > 0.3) { - modalNextImage(e); - } - - }, delay); - }); - - - /* - 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 (isScrolling) return; - isScrolling = true; - - if (e.deltaX <= -0.6) { +window.addEventListener('gamepadconnected', (e) => { + const index = e.gamepad.index; + let isWaiting = false; + setInterval(async () => { + if (!opts.js_modal_lightbox_gamepad || isWaiting) return; + const gamepad = navigator.getGamepads()[index]; + const xValue = gamepad.axes[0]; + if (xValue <= -0.3) { modalPrevImage(e); - } else if (e.deltaX >= 0.6) { + isWaiting = true; + } else if (xValue >= 0.3) { modalNextImage(e); + isWaiting = true; } + if (isWaiting) { + await sleepUntil(() => { + const xValue = navigator.getGamepads()[index].axes[0] + if (xValue < 0.3 && xValue > -0.3) { + return true; + } + }, opts.js_modal_lightbox_gamepad_repeat); + isWaiting = false; + } + }, 10); +}); + +/* +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); + } - setTimeout(() => { - isScrolling = false; - }, delay); - });
\ No newline at end of file + setTimeout(() => { + isScrolling = false; + }, opts.js_modal_lightbox_gamepad_repeat); +}); + +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); + }); +} |