Unverified Commit 66428667 authored by AUTOMATIC1111's avatar AUTOMATIC1111 Committed by GitHub
Browse files

Merge pull request #10146 from missionfloyd/gamepad-option

Fix gamepad navigation
parents 6ac33fe9 85bd9b3d
Loading
Loading
Loading
Loading
+53 −32
Original line number Diff line number Diff line
    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) {
    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 (xValue > 0.3) {
            isWaiting = true;
        } else if (xValue >= 0.3) {
            modalNextImage(e);
            isWaiting = true;
        }

        }, delay);
        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 (isScrolling) return;
    if (!opts.js_modal_lightbox_gamepad || isScrolling) return;
    isScrolling = true;

    if (e.deltaX <= -0.6) {
@@ -32,5 +41,17 @@

    setTimeout(() => {
        isScrolling = false;
        }, delay);
    }, 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);
    });
}
+2 −0
Original line number Diff line number Diff line
@@ -400,6 +400,8 @@ options_templates.update(options_section(('ui', "User interface"), {
    "font": OptionInfo("", "Font for image grids that have text"),
    "js_modal_lightbox": OptionInfo(True, "Enable full page image viewer"),
    "js_modal_lightbox_initially_zoomed": OptionInfo(True, "Show images zoomed in by default in full page image viewer"),
    "js_modal_lightbox_gamepad": OptionInfo(True, "Navigate image viewer with gamepad"),
    "js_modal_lightbox_gamepad_repeat": OptionInfo(250, "Gamepad repeat period, in milliseconds"),
    "show_progress_in_title": OptionInfo(True, "Show generation progress in window title."),
    "samplers_in_dropdown": OptionInfo(True, "Use dropdown for sampler selection instead of radio group"),
    "dimensions_and_batch_together": OptionInfo(True, "Show Width/Height and Batch sliders in same row"),