All files / src/web gamepads.js

81.03% Statements 47/58
69.44% Branches 25/36
100% Functions 3/3
79.62% Lines 43/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171  1x 1x           1x                                                                         38x 38x 38x     38x   38x                     34x 1x     34x 34x 1x     33x 1x   32x 34x 32x   32x 32x               5x 5x 5x   5x       5x 4x 4x   5x   5x 4x 16x               16x       16x   16x     16x     16x                                       16x     4x 64x 64x   64x     3x                   64x            
/** Each stick direction as [axis, direction], in the browser's standard mapping. */
const LeftStick = { up: [1, -1], down: [1, 1], left: [0, -1], right: [0, 1] };
const RightStick = { up: [3, -1], down: [3, 1], left: [2, -1], right: [2, 1] };
 
/**
 * The buttons, by their index in the browser's standard mapping, and the stick directions each `GP.` control name
 * sets. A direction takes the D-pad and both sticks; `FIRE` takes every button that is neither a direction nor Start.
 */
const Controls = {
    A: { buttons: [0], axes: [] },
    B: { buttons: [1], axes: [] },
    X: { buttons: [2], axes: [] },
    Y: { buttons: [3], axes: [] },
    LB: { buttons: [4], axes: [] },
    RB: { buttons: [5], axes: [] },
    LT: { buttons: [6], axes: [] },
    RT: { buttons: [7], axes: [] },
    BACK: { buttons: [8], axes: [] },
    START: { buttons: [9], axes: [] },
    FIRE1: { buttons: [10], axes: [] },
    FIRE2: { buttons: [11], axes: [] },
    FIRE: { buttons: [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11], axes: [] },
    UP: { buttons: [12], axes: [LeftStick.up, RightStick.up] },
    DOWN: { buttons: [13], axes: [LeftStick.down, RightStick.down] },
    LEFT: { buttons: [14], axes: [LeftStick.left, RightStick.left] },
    RIGHT: { buttons: [15], axes: [LeftStick.right, RightStick.right] },
    UP1: { buttons: [], axes: [LeftStick.up] },
    DOWN1: { buttons: [], axes: [LeftStick.down] },
    LEFT1: { buttons: [], axes: [LeftStick.left] },
    RIGHT1: { buttons: [], axes: [LeftStick.right] },
    UP2: { buttons: [], axes: [RightStick.up] },
    DOWN2: { buttons: [], axes: [RightStick.down] },
    LEFT2: { buttons: [], axes: [RightStick.left] },
    RIGHT2: { buttons: [], axes: [RightStick.right] },
    UP3: { buttons: [0], axes: [] },
    DOWN3: { buttons: [2], axes: [] },
    LEFT3: { buttons: [3], axes: [] },
    RIGHT3: { buttons: [1], axes: [] },
};
 
export class GamePad {
    /**
     * @param {object} model - the emulated machine, whose keys the controls press
     */
    constructor(model) {
        this.gamepad0 = null;
        this.keys = model.keys;
        this.gamepadMapping = Object.assign([], model.gamepadKeys);
 
        // Gamepad joysticks
        this.gamepadAxisMapping = [[], [], [], []];
        /** @type {Object<string, string>} the key names the URL gave each control, by the control's name */
        this.remapped = {};
    }
 
    /**
     * Maps a gamepad button or stick direction to a key on the emulated machine.
     * @param {string} gamepadKey - the gamepad control, eg `FIRE2`
     * @param {string} machineKey - the key to press, eg `RETURN`
     * @returns {?string} a description of the problem, or null if the mapping was applied
     */
    remap(gamepadKey, machineKey) {
        // convert "1" into "K1"
        if (machineKey.length === 1 && machineKey >= "0" && machineKey <= "9") {
            machineKey = "K" + machineKey;
        }
 
        const mappedKey = this.keys[machineKey];
        if (!mappedKey) {
            return `"${machineKey}" is not a key on the emulated machine.`;
        }
 
        if (!Object.hasOwn(Controls, gamepadKey)) {
            return `unknown gamepad control "${gamepadKey}".`;
        }
        const { buttons, axes } = Controls[gamepadKey];
        for (const button of buttons) this.gamepadMapping[button] = mappedKey;
        for (const [axis, direction] of axes) this.gamepadAxisMapping[axis][direction] = mappedKey;
 
        this.remapped[gamepadKey] = machineKey;
        return null;
    }
 
    /**
     * Reads the pads, once a frame: presses the keys mapped to their controls and latches the FIRE buttons.
     * @param {object} processor - the emulated machine
     */
    update(processor) {
        const keyboard = processor.keyboardInterface;
        const pads = processor.config.getGamepads();
        const pad = pads[0];
        // FIRE1 on a second pad is the second fire button.
        processor.sysvia.setGamepadFire(
            !!pad?.buttons[10]?.pressed,
            !!pad && (!!pad.buttons[11]?.pressed || !!pads[1]?.buttons[10]?.pressed),
        );
        if (pad && !this.gamepad0) {
            this.gamepadButtons = new Array(16).fill(false);
            this.gamepadAxes = [0, 0, 0, 0];
        }
        this.gamepad0 = pad;
 
        if (this.gamepad0) {
            for (let i = 0; i < 4; i++) {
                const axisRaw = this.gamepad0.axes[i];
 
                // Mike's XBox 360 controller, zero positions
                // console.log(i, axisRaw, axis);
                //0 -0.03456169366836548 -1
                //1 -0.037033677101135254 -1
                //2 0.055374979972839355 1
                //3 0.06575113534927368 1
                const threshold = 0.15;
 
                // normalize to -1, 0, 1
                let axis;
                Iif (axisRaw < -threshold) {
                    axis = -1;
                } else Iif (axisRaw > threshold) {
                    axis = 1;
                } else {
                    axis = 0;
                }
 
                Iif (axis !== this.gamepadAxes[i]) {
                    // tricky because transition can be
                    // -1 to 0
                    // -1 to 1
                    // 0 to 1
                    // 0 to -1
                    // 1 to 0
                    // 1 to -1
                    const oldKey = this.gamepadAxisMapping[i][this.gamepadAxes[i]];
                    if (oldKey) {
                        keyboard.keyUpRaw(oldKey);
                    }
 
                    const newKey = this.gamepadAxisMapping[i][axis];
                    if (newKey) {
                        keyboard.keyDownRaw(newKey);
                    }
                }
 
                // store new state
                this.gamepadAxes[i] = axis;
            }
 
            for (let i = 0; i < 16; i++) {
                Eif (this.gamepad0.buttons[i]) {
                    const button = this.gamepad0.buttons[i];
 
                    if (button.pressed !== this.gamepadButtons[i]) {
                        // different to last time
 
                        Iif (this.gamepadMapping[i]) {
                            if (button.pressed) {
                                keyboard.keyDownRaw(this.gamepadMapping[i]);
                            } else {
                                keyboard.keyUpRaw(this.gamepadMapping[i]);
                            }
                        }
                    }
 
                    // store new state
                    this.gamepadButtons[i] = button.pressed;
                }
            }
        }
    }
}