diff options
author | AUTOMATIC1111 <16777216c@gmail.com> | 2023-05-17 20:18:56 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-17 20:18:56 +0000 |
commit | 04b4508a66de58c9f3a422fdcad4dd2ec3ad39ce (patch) | |
tree | 1cb30a63099a69f678d4901b495203b765a6df59 /javascript/imageviewerGamepad.js | |
parent | 7201d940a4fe664beb9662fadbeade4ee1d788f7 (diff) | |
parent | b397f63e00bbfbe9087d80abb457aa9a593b181b (diff) | |
download | stable-diffusion-webui-gfx803-04b4508a66de58c9f3a422fdcad4dd2ec3ad39ce.tar.gz stable-diffusion-webui-gfx803-04b4508a66de58c9f3a422fdcad4dd2ec3ad39ce.tar.bz2 stable-diffusion-webui-gfx803-04b4508a66de58c9f3a422fdcad4dd2ec3ad39ce.zip |
Merge branch 'dev' into improve-frontend-responsiveness
Diffstat (limited to 'javascript/imageviewerGamepad.js')
-rw-r--r-- | javascript/imageviewerGamepad.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/javascript/imageviewerGamepad.js b/javascript/imageviewerGamepad.js new file mode 100644 index 00000000..6297a12b --- /dev/null +++ b/javascript/imageviewerGamepad.js @@ -0,0 +1,57 @@ +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); + 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; + }, 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); + }); +} |