Controllers.lua
Posted: Thu Jun 12, 2014 6:29 pm
Hi!
GitHub : https://github.com/Felix-Du/Controllers.lua
Download : https://raw.githubusercontent.com/Felix ... ollers.lua
ReadMe : https://github.com/Felix-Du/Controllers ... /README.md
So, this is a small project I've worked on, and I'm at this point where it seems to be improvable, but it's has basically all the features I need and a bit more, so I'm releasing it now.
Controllers.lua is a small lib(1 file!) that aim to replace, simplify, and enhance how Löve manage user-input. I think it's a really cool and simple.
You give it a list of "buttons" or "axes" (like "game_walk" or "menu_up"), you optionally define some "players" so that you don't have to copy-paste the same list of buttons each time you want to add a new place for a player. Then you place the needed callbacks and here you go! Then, if you want to know if a button is pressed, you can just use "Controllers:isDown("name_of_button")". You don't have to care about what is actually pressed(The keyboard, a button of a gamepad, an axis, the mouse). And if callbacks are your things, then you're in luck, because Controllers.lua allow you to set some callbacks (Once again, it's device-agnostic).
Here's a not actually runnable example, just so that you can get how it work:
GitHub : https://github.com/Felix-Du/Controllers.lua
Download : https://raw.githubusercontent.com/Felix ... ollers.lua
ReadMe : https://github.com/Felix-Du/Controllers ... /README.md
So, this is a small project I've worked on, and I'm at this point where it seems to be improvable, but it's has basically all the features I need and a bit more, so I'm releasing it now.
Controllers.lua is a small lib(1 file!) that aim to replace, simplify, and enhance how Löve manage user-input. I think it's a really cool and simple.
You give it a list of "buttons" or "axes" (like "game_walk" or "menu_up"), you optionally define some "players" so that you don't have to copy-paste the same list of buttons each time you want to add a new place for a player. Then you place the needed callbacks and here you go! Then, if you want to know if a button is pressed, you can just use "Controllers:isDown("name_of_button")". You don't have to care about what is actually pressed(The keyboard, a button of a gamepad, an axis, the mouse). And if callbacks are your things, then you're in luck, because Controllers.lua allow you to set some callbacks (Once again, it's device-agnostic).
Here's a not actually runnable example, just so that you can get how it work:
Code: Select all
require('controllers')
function love.load()
Controllers:init({
menu_up = {type="button", default={'kb_up','gpb_dpup','gpa_lefty_-'}}
,menu_down = {type="button", default={'kb_down','gpb_dpdown','gpa_lefty_+'}}
,menu_select = {type="button", default={'kb_return','kb_ ','gpb_a'}}
,menu_back = {type="button", default={'kb_escape','gpb_start'}}
,game_jump = {type="button", per_player=true, default={'kb_ ','gpb_a'}}
,game_walk = {type="axis", exclusive="press", per_player=true, default={'kb_left_-','kb_right_+','gpa_leftx'}}
})
Controllers:setPlayer('player_1','joystick',1)
end
function love.update(dt)
Controllers:pre_update(dt)
if pause then
for i=1, Controllers:isPressed('menu_up') do
menu_select = menu_select - 1
end
for i=1, Controllers:isPressed('menu_down') do
menu_select = menu_select + 1
end
end
if Controllers:isPressed('menu_back') then
pause = not pause
end
if Controllers:isDown('game_jump','player_1') and player:onGround() then
player:jump()
end
player.speedx = Controllers:getAxis('game_walk','player_1') * player.walk_speed
Controllers:post_update(dt)
end
function love.mousepressed(x,y,button)
Controllers:mousePressed(x,y,button)
end
function love.mousereleased(x,y,button)
Controllers:mouseReleased(x,y,button)
end
function love.keypressed(key,unicode)
Controllers:keyPressed(key)
end
function love.keyreleased(key,unicode)
Controllers:keyReleased(key)
end
function love.joystickpressed(joystick,key)
Controllers:joystickPressed(joystick,key)
end
function love.joystickreleased(joystick,key)
Controllers:joystickReleased(joystick,key)
end