GamepadGuesser: display the right art for gamepads

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
idbrii
Prole
Posts: 34
Joined: Sat Jun 12, 2021 5:07 am

GamepadGuesser: display the right art for gamepads

Post by idbrii »

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

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
GamepadGuesser caches each image it creates, so you can call getImage in draw without worrying about garbage.

Tested with a DualSense, DualShock 4, Xbox 360 wired, Switch Pro.

See main.lua or GamepadGuesser-demo.love for a more extensive example.
GamepadGuesser-demo.love
v0.1
(761.57 KiB) Downloaded 197 times


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
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.


Licenses
User avatar
idbrii
Prole
Posts: 34
Joined: Sat Jun 12, 2021 5:07 am

Re: GamepadGuesser: display the right art for gamepads

Post by idbrii »

This is my first library posted here, so let me know if I can improve how to bundle up a module.

I hope this library is helpful!
User avatar
Gunroar:Cannon()
Party member
Posts: 1139
Joined: Thu Dec 10, 2020 1:57 am

Re: GamepadGuesser: display the right art for gamepads

Post by Gunroar:Cannon() »

This is pretty cool! Is it to be used for something like control tutorials (the image it makes)?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: GamepadGuesser: display the right art for gamepads

Post by ReFreezed »

A note to anyone using this: It's a good idea to have an option in-game to let the player choose icon style. Only because someone has a controller that identifies itself as an Xbox controller (including in love.js builds, as noted by the caveat) doesn't mean the player wants Xbox icons.

Maybe the library should have a function for overriding the guessed icon style (with saving/loading in mind), or a way to get the button image by console instead of by joystick (e.g. joy:getImage("playstation",btn) ).
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
idbrii
Prole
Posts: 34
Joined: Sat Jun 12, 2021 5:07 am

Re: GamepadGuesser: display the right art for gamepads

Post by idbrii »

Gunroar:Cannon() wrote: Tue Jan 04, 2022 7:56 am This is pretty cool! Is it to be used for something like control tutorials (the image it makes)?
Yeah, or button hints in UI (show B button next to a back label to indicate how to leave a menu or A button below dialogue to show you can advance), or quick time events, or in-world UI for interactions.
ReFreezed wrote: Tue Jan 04, 2022 12:47 pm ... It's a good idea to have an option in-game to let the player choose icon style. ...

Maybe the library should have a function for overriding the guessed icon style (with saving/loading in mind), ...
That's a good idea! Maybe joy: overrideConsole(joystick, "playstation") and if you have saved data, you call that in love.joystickadded. Is that what you meant by "with saving/loading in mind"?

I wonder how local multiplayer games handle saving button style? Would they save some id for the gamepad? Maybe save by player number and rely on players to activate their gamepads in the same order? I think I've only seen it in single player.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: GamepadGuesser: display the right art for gamepads

Post by ReFreezed »

idbrii wrote: Tue Jan 04, 2022 5:43 pm Maybe joy: overrideConsole(joystick, "playstation") and if you have saved data, you call that in love.joystickadded. Is that what you meant by "with saving/loading in mind"?
I just meant so it's simple to use the library when saving and loading settings when the program eventually gets restarted. I actually like the latter of my suggestions more though (where the library doesn't even know about the concept of overrides), because you can write code like this:

Code: Select all

image = joy:getImage((gamepadStyleOverride or joystick), btn)
-- gamepadStyleOverride would be either a string (e.g. "playstation") or nil.
Having one override per controller seem silly. If I prefer PlayStation icons then it shouldn't matter what controller I'm using - the game should always show PlayStation icons.

(By the way, I'm just throwing out random suggestions here - I don't actually intend to use the library.)
idbrii wrote: Tue Jan 04, 2022 5:43 pm I wonder how local multiplayer games handle saving button style? Would they save some id for the gamepad? Maybe save by player number and rely on players to activate their gamepads in the same order?
Keep things simple. Either make all players share the same global setting, or have a setting per player together with whatever other per-player settings there are (this seems incredibly niche and is probably not worth even thinking about).
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
pauljessup
Party member
Posts: 356
Joined: Wed Jul 03, 2013 4:06 am

Re: GamepadGuesser: display the right art for gamepads

Post by pauljessup »

Oh hey, I did something similar with Emberglass! A handy thing for players.
glitchapp
Party member
Posts: 256
Joined: Tue Oct 05, 2021 10:34 am
Contact:

Re: GamepadGuesser: display the right art for gamepads

Post by glitchapp »

I implemented it in my game manager, I attached a screenshot.

In case anyone want to test it: https://github.com/glitchapp/lovegamelauncher

I think some of you may be interested in the technical part. The reason why everything look so beautiful (at least to me) is because there are several shaders working simultaneously:

At the background a beautiful shadertoy shader reminiscing some playstation style backround. At the front (the layer where the text and graphics are shown) several moonshine shader effects (crt which curves the graphics like and old crt screen, scanlines, and a subtle glow effect that make it looks a little shiny. I hope you like it!
You can find the link to all of them in the github repository in case you are interested!
Attachments
gamepadguesser.jpg
gamepadguesser.jpg (63.96 KiB) Viewed 5628 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests