Skip to content

Latest commit

 

History

History
95 lines (66 loc) · 3.08 KB

README.md

File metadata and controls

95 lines (66 loc) · 3.08 KB

Requires love2d 11.3+ Lint status

GamepadGuesser

Guesses what a gamepad should look like and provides a default set of art that matches the input gamepad.

Not an input mapping library. If you want to define input bindings, I recommend baton.

Simplify showing gamepad icons with gamepadguesser. It loads the extensive SDL_GameControllerDB for better gamepad support, maps joysticks to appearance (console-specific buttons like Xbox, 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:

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

GamepadGuesser caches each image it creates, so you can call getImage in draw without worrying about garbage.

See main.lua for a more extensive example.

Disabling Autodetection

Autodetection may be imperfect, so you can set an override to force a specific console visual:

joy:overrideConsole(joystick, "nintendo")

You can use gamepadguesser.CONSOLES to provide a list of options in your settings menu (also include an "auto" option that passes nil to overrideConsole.

Low-level operation

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.

You can also get the name of the console associated with the joystick ("xbox", "playstation", "nintendo"):

function love.gamepadpressed(joystick, button)
    text = gamepadguesser.joystickToConsole(joystick)
end

function love.draw()
    love.graphics.printf(text or "", 0, 0, 100)
end

Calling gamepadguesser functions directly doesn't automatically load SDL_GameControllerDB. You can call gamepadguesser.loadMappings(path_to_gamepadguesser) to load them yourself.

Caveats

lovejs returns nil for getGamepadMappingString() and getName() produces generic names, so all gamepads appear to be xbox gamepads in web builds.

Requires love2d 11.3+ because getting gamepad names depends on getGamepadMappingString.

License