here something to easy configure key combination on which a function is called. supports down, pressed and released behaviours
joyp.lua :
--[[
joyp api by Xyrio/muxum
allows easy configuration of key combinations on which a function is called
version 0.1
lua 5.1+
]]
KEYSTATE = { UP=nil, DOWN=1, PRESSED=2, RELEASED=3 } --UP is so u know that all key reulting to nil r interpreted as having to be UP
--used to check all available keys
KEYLIST = { L=0, R=1, A=2, B=3, X=4, Y=5, start=6, select=7, up=8, down=9, left=10, right=11 }
--- joypad states with up=nil and notup = not nil joypad states (total 5 joypads)
keysold = { [1]={}, [2]={}, [3]={}, [4]={}, [5]={} }
keysnew = { [1]={}, [2]={}, [3]={}, [4]={}, [5]={} }
--- key combination list and their action
--- list item is a table like: { padnr=1, action=function() end, keys={ L=KEYSTATE.DOWN, R=KEYSTATE.PRESSED } } -- hold L and press R to trigger
--- note: index of keycombs can be anything
keycombs = {}
joyp = {}
--- key is down in current frame
--- returns nil on false else true
joyp.isDown = function(keymap,padnr,key)
if padnr ~= nil and keymap[padnr][key] ~= nil then
return true
end
end
--- key is down in current frame
--- returns nil on false else true
joyp.isUp = function(keymap,padnr,key)
if padnr ~= nil and keymap[padnr][key] == nil then
return true
end
end
--- key down this frame and was up last frame
--- returns nil on false else true
joyp.isPressed = function(for_interface_but_unused,padnr,key)
if joyp.isDown(keysnew,padnr,key) and joyp.isUp(keysold,padnr,key) then
return true
end
end
--- key up this frame and was down last frame
--- returns nil on false else true
joyp.isReleased = function(for_interface_but_unused,padnr,key)
if joyp.isUp(keysnew,padnr,key) and joyp.isDown(keysold,padnr,key) then
return true
end
end
joyp.getAllPadsKeys = function()
local keys = {}
keys[1] = joypad.read(1)
keys[2] = joypad.read(2)
keys[3] = joypad.read(3)
keys[4] = joypad.read(4)
keys[5] = joypad.read(5)
return keys
end
--- to be called in mainloop
joyp.handleInput = function()
local switch_keystate = { [KEYSTATE.DOWN]=joyp.isDown, [KEYSTATE.PRESSED]=joyp.isPressed, [KEYSTATE.RELEASED]=joyp.isReleased }
keysnew = joyp.getAllPadsKeys()
for i,kcomb in pairs(keycombs) do --for all key combinations
local alltrue = true
for key in pairs(KEYLIST) do -- for all possible keys
if kcomb.keys[key] ~= nil then
if switch_keystate[kcomb.keys[key]](keysnew,kcomb.padnr,key) == nil then
alltrue = false
end
else
if joyp.isUp(keysnew,kcomb.padnr,key) == nil then
alltrue = false
end
end
end
if alltrue == true and kcomb.action ~= nil then
kcomb.action()
end
end
keysold = keysnew
end
and here an example of how you could use it:
show mana - hold start and press select
next weapon of same type (needs reequip to apply weapon stats) - hold start and press up
prev weapon of same type (needs reequip to apply weapon stats) - hold start and press down
weapon has polymorph status (changed enemy type on hit) - hold start and press L
infinite stamina/ at least 100% weapon attacks - hold start and press R
(script for that behaviour is in the download)
one line from a file inside the download as an example how u config key combinations:
...
keycombs["showmana"] = { padnr=1, action=function() ui.flags.mana = toggle(ui.flags.mana) end, keys={ start=KEYSTATE.DOWN, select=KEYSTATE.PRESSED } } -- show mana values on screen
...
here everything for download:
http://www.mediafire.com/?bbkwjy3xjy5