Hazel icon indicating copy to clipboard operation
Hazel copied to clipboard

Added Windows gamepad support

Open AmelieHeinrich opened this issue 3 years ago • 6 comments

Gamepad support for Hazel

This pull request contains the basic implementation of a gamepad input system. It only supports regular gamepads, so it isn't possible to connect a guitar hero controller for example.

Why?

Having an interface for gamepad input will be useful if Hazel ever gets ported to console. It's also very important for an engine to support gamepad input, as it is one of the most popular input methods in the industry.

How does it work?

Additional functions have been added to the input class to poll button state, trigger state, joystick state and vibration state. Some additional enum values have been added to map ABXY buttons to the Dualshock's square, cross, rectangle and triangle button. The input system handles deadzone values as well. You may also notice that there is three new functions in the input class: Init, SetEventCallback and Update. Init is used to initialise input polling libraries (XInput on Windows for example) SetEventCallback is used so that the input system can send GamepadConnected and GamepadDisconnected events. Update is used to actually update the state of the gamepads.

Example code

if (Input::IsGamepadButtonPressed(0, Gamepad::A))
    Input::SetGamepadVibration(0, glm::vec2(1.0f)); // Full speed on both motors

glm::vec2 leftJoystick = Input::GetGamepadLeftJoystick(0);
playerPosition.x += leftJoystick.x * dt;
playerPosition.y += leftJoystick.y * dt;

What you might ask

  • Why the fuck didn't you use GLFW for gamepad polling you absolute idiot? Because GLFW doesn't support gamepad vibration.
  • Why is gamepad connect and disconnect an event? Can't you just add a Input::IsGamepadConnected function? No, because gamepad connection and disconnection should be handled as notifications : not just a plain old boolean state
  • Without GLFW, it's not cross-platform, dude... LOL as if Hazel was cross-platform KEKW (on a serious note, when Hazel will be able to build on both Mac and Linux, I'll add the necessary backends for Apple's GameController framework and Linux joystick file descriptor)

What could be added

  • Swap A & B buttons if the controller is a Nintendo Switch gamepad
  • Get the battery state of the gamepad (XInputGetBatteryInformation)

AmelieHeinrich avatar Aug 23 '22 19:08 AmelieHeinrich

Nice. I wasn't aware that glfw doesn't support gamepad vibration

gamecoder-nz avatar Aug 30 '22 23:08 gamecoder-nz

Have you implemented this for mac and linux already?

gamecoder-nz avatar Aug 30 '22 23:08 gamecoder-nz

Have you implemented this for mac and linux already?

Already adressed in the QNA:

LOL as if Hazel was cross-platform KEKW (on a serious note, when Hazel will be able to build on both Mac and Linux, I'll add the necessary backends for Apple's GameController framework and Linux joystick file descriptor)

AmelieHeinrich avatar Aug 31 '22 17:08 AmelieHeinrich

Any specific reason for moving the EventCallbackFn?

drsnuggles8 avatar Nov 21 '22 22:11 drsnuggles8

Any specific reason for moving the EventCallbackFn?

I think the OP chose to move it so it was easier to use: static void SetEventCallback(const EventCallbackFn& callback); inside of Input.h and virtual void SetEventCallback(const EventCallbackFn& callback) = 0; inside of window.h, but maybe it won't even work if not moved, i haven't delved too much into it 🙂

VagueLobster avatar Nov 22 '22 08:11 VagueLobster

I think the OP chose to move it so it was easier to use: static void SetEventCallback(const EventCallbackFn& callback);

inside of Input.h and virtual void SetEventCallback(const EventCallbackFn& callback) = 0; inside of window.h

Correct. It's just so that the input system can also access the EventCallbackFn type. No reason not to move it for clarity.

AmelieHeinrich avatar Nov 22 '22 08:11 AmelieHeinrich