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