src/index.js
function Unswitch(settings) {
  const buttonState = {};
  let axesPosition = 8;

  for (let i = buttonMappings.length - 1; i >= 0; i -= 1) {
    buttonState[buttonMappings[i]] = { pressed: false };
  }

  this.update = () => {
    const gamepads = navigator.getGamepads();
    for (let i = Object.keys(gamepads).length - 1; i >= 0; i -= 1) {
      if (gamepads[i] && gamepads[i].id && gamepads[i].id.indexOf(settings.side) !== -1) {
        this.observe(gamepads[i]);
        break;
      }
    }
  };

  this.observe = (pad) => {
    const { buttons, axes } = pad;
    for (let j = buttonMappings.length - 1; j >= 0; j -= 1) {
      const button = buttonMappings[j];
      if (buttonState[button].pressed !== buttons[j].pressed) {
        buttonState[button].pressed = buttons[j].pressed;
        if (settings[button]) {
          settings[button](buttonState[button].pressed);
        }

        if (settings.buttons) {
          settings.buttons(button, buttonState[button].pressed, settings.side);
        }
      }
    }
    if (settings.axes) {
      const position = getAxesPosition(axes, buttons);
      if (position !== axesPosition) {
        settings.axes(position);
        axesPosition = position;
      }
    }
  };
}
function Unswitch(settings) {
  const buttonState = {};
  let axesPosition = 8;

  for (let i = buttonMappings.length - 1; i >= 0; i -= 1) {
    buttonState[buttonMappings[i]] = { pressed: false };
  }

  this.update = () => {
    const gamepads = navigator.getGamepads();
    for (let i = Object.keys(gamepads).length - 1; i >= 0; i -= 1) {
      if (gamepads[i] && gamepads[i].id && gamepads[i].id.indexOf(settings.side) !== -1) {
        this.observe(gamepads[i]);
        break;
      }
    }
  };

  this.observe = (pad) => {
    const { buttons, axes } = pad;
    for (let j = buttonMappings.length - 1; j >= 0; j -= 1) {
      const button = buttonMappings[j];
      if (buttonState[button].pressed !== buttons[j].pressed) {
        buttonState[button].pressed = buttons[j].pressed;
        if (settings[button]) {
          settings[button](buttonState[button].pressed);
        }

        if (settings.buttons) {
          settings.buttons(button, buttonState[button].pressed, settings.side);
        }
      }
    }
    if (settings.axes) {
      const position = getAxesPosition(axes, buttons);
      if (position !== axesPosition) {
        settings.axes(position);
        axesPosition = position;
      }
    }
  };
}
function Unswitch ( settings ) { const buttonState = { } ; let axesPosition = 8 ; for ( let i = buttonMappings . length - 1 ; i >= 0 ; i -= 1 ) { buttonState [ buttonMappings [ i ] ] = { pressed : false } ; } this . update = ( ) => { const gamepads = navigator . getGamepads ( ) ; for ( let i = Object . keys ( gamepads ) . length - 1 ; i >= 0 ; i -= 1 ) { if ( gamepads [ i ] && gamepads [ i ] . id && gamepads [ i ] . id . indexOf ( settings . side ) !== - 1 ) { this . observe ( gamepads [ i ] ) ; break ; } } } ; this . observe = ( pad ) => { const { buttons , axes } = pad ; for ( let j = buttonMappings . length - 1 ; j >= 0 ; j -= 1 ) { const button = buttonMappings [ j ] ; if ( buttonState [ button ] . pressed !== buttons [ j ] . pressed ) { buttonState [ button ] . pressed = buttons [ j ] . pressed ; if ( settings [ button ] ) { settings [ button ] ( buttonState [ button ] . pressed ) ; } if ( settings . buttons ) { settings . buttons ( button , buttonState [ button ] . pressed , settings . side ) ; } } } if ( settings . axes ) { const position = getAxesPosition ( axes , buttons ) ; if ( position !== axesPosition ) { settings . axes ( position ) ; axesPosition = position ; } } } ; }
Create an instance of Unswitch.
@param {object} settings
Create an instance of Unswitch .