https://github.com/idbrii/love-gamepadguesser
Requires love2d 11.3+ (for getGamepadMappingString).
I recently added SDL_GameControllerDB to a project, wrote some code to pretty print my control scheme, and used a CC0 button prompt art pack. I did these on separate projects, but thought it would be great to combine the three ideas to make it easy to show the correct art for your gamepad.
The result: gamepadguesser. It loads the extensive SDL_GameControllerDB for better gamepad support, maps joysticks to appearance (console-specific buttons like Xbox, PS, Nintendo), and builds images on demand for each GamepadButton and GamepadAxis.
You can start displaying the correct image for your buttons in only a few lines of code:
Code: Select all
local gamepadguesser = require "gamepadguesser"
local joy = gamepadguesser.createJoystickData("gamepadguesser")
function love.gamepadpressed(joystick, button)
gamepad = joystick
btn = button
end
function love.draw()
if btn then
love.graphics.draw(joy:getImage(gamepad, btn), 0, 0)
end
end
Tested with a DualSense, DualShock 4, Xbox 360 wired, Switch Pro.
See main.lua or GamepadGuesser-demo.love for a more extensive example.
You can also use gamepadguesser with your own art. The easiest way is to modify the art in gamepadguesser/assets/images/ to ensure the correct file names.
Alternatively, you could only use GamepadGuesser to get the name of the console associated with the joystick ("xbox", "playstation", "nintendo"):
Code: Select all
function love.gamepadpressed(joystick, button)
text = gamepadguesser.joystickToConsole(joystick)
end
function love.draw()
love.graphics.printf(text or "", 0, 0, 100)
end
Caveats
lovejs returns `nil` for `getGamepadMappingString()` and `getName()` produces generic names, so all gamepads appear to be xbox gamepads in web builds.
Licenses
- MIT
- Includes SDL_GameControllerDB: SDL License
- Includes gamepad art in gamepadguesser/assets/images/: CC0