LuanRoger / xinput_gamepad

🎮 Add support to XInput controllers with Win32 API.
https://pub.dev/packages/xinput_gamepad
MIT License
6 stars 2 forks source link

_buttonReact() can't handle multiple buttons at once #2

Closed ASGAlex closed 2 years ago

ASGAlex commented 2 years ago

For example, when I push "fire" button while "moving", xinput returns bitmask like (XINPUT_GAMEPAD_DPAD_UP | XINPUT_GAMEPAD_A), and InputBitmaskConverter.convertButton return null.

In my case the easiest way was to bring access to raw bitmask and work with it an "old-shcool way":

 onRawButtonEvent(int bitmask) {
    useController = true;
    keysPressed.clear();
    if (bitmask & XINPUT_GAMEPAD_DPAD_UP > 0) {
      keysPressed.add(LogicalKeyboardKey.keyW);
    } else if (bitmask & XINPUT_GAMEPAD_DPAD_DOWN > 0) {
      keysPressed.add(LogicalKeyboardKey.keyS);
    } else if (bitmask & XINPUT_GAMEPAD_DPAD_LEFT > 0) {
      keysPressed.add(LogicalKeyboardKey.keyA);
    } else if (bitmask & XINPUT_GAMEPAD_DPAD_RIGHT > 0) {
      keysPressed.add(LogicalKeyboardKey.keyD);
    }

    if (bitmask & (XINPUT_GAMEPAD_X | XINPUT_GAMEPAD_RIGHT_SHOULDER) > 0) {
      keysPressed.add(LogicalKeyboardKey.space);
      spaceHold = true;
    } else {
      spaceHold = false;
    }
    if (bitmask & XINPUT_GAMEPAD_BACK > 0) {
      keysPressed.add(LogicalKeyboardKey.escape);
    }
    if (bitmask & XINPUT_GAMEPAD_START > 0) {
      keysPressed.add(LogicalKeyboardKey.backquote);
    }
    if (keysPressed.isNotEmpty) {
      notifyListeners();
    }
  }

With bitmask I can handle each button both individually and in combinations. I thing, this functionality should exists while more modern solution is not implemented.

LuanRoger commented 2 years ago

Hello, thanks for the contribution. ❤️

This problem was already known but I couldn't think of a solution until then, creating a callback to access the raw bitmask doesn't seem to be an ideal solution yet, but I'll leave it implemented because some controls can return bitmasks that the converter doesn't recognize, besides to support combinations.

About the problem, I was inspired by your example to make the converter recognize more than one button per state, but it only works in buttonMode: ButtonMode.HOLD for now. If you want to try to make it work with the other mode it will be greatly appreciated.