mirror of
https://github.com/TangentFoxy/baton.git
synced 2025-07-28 02:52:19 +00:00
readd source functions and demo
This commit is contained in:
37
baton.lua
37
baton.lua
@@ -6,6 +6,14 @@ local function parseSource(source)
|
||||
return source:match '(.+):(.+)'
|
||||
end
|
||||
|
||||
local function parseAxis(value)
|
||||
return value:match '(.+)([%+%-])'
|
||||
end
|
||||
|
||||
local function parseHat(value)
|
||||
return value:match '(%d)(.+)'
|
||||
end
|
||||
|
||||
-- source functions --
|
||||
|
||||
local sf = {kbm = {}, joy = {}}
|
||||
@@ -14,6 +22,25 @@ function sf.kbm.key(key)
|
||||
return love.keyboard.isDown(key) and 1 or 0
|
||||
end
|
||||
|
||||
function sf.kbm.sc(sc)
|
||||
return love.keyboard.isScancodeDown(sc) and 1 or 0
|
||||
end
|
||||
|
||||
function sf.kbm.mouse(button)
|
||||
return love.mouse.isDown(tonumber(button)) and 1 or 0
|
||||
end
|
||||
|
||||
function sf.joy.axis(joystick, value)
|
||||
local axis, direction = parseAxis(value)
|
||||
if tonumber(axis) then
|
||||
value = joystick:getAxis(tonumber(axis))
|
||||
else
|
||||
value = joystick:getGamepadAxis(axis)
|
||||
end
|
||||
if direction == '-' then value = -value end
|
||||
return value > 0 and value or 0
|
||||
end
|
||||
|
||||
function sf.joy.button(joystick, button)
|
||||
if tonumber(button) then
|
||||
return joystick:isDown(tonumber(button)) and 1 or 0
|
||||
@@ -22,6 +49,11 @@ function sf.joy.button(joystick, button)
|
||||
end
|
||||
end
|
||||
|
||||
function sf.joy.hat(joystick, value)
|
||||
local hat, direction = parseHat(value)
|
||||
return joystick:getHat(hat) == direction and 1 or 0
|
||||
end
|
||||
|
||||
-- player class - internal functions --
|
||||
|
||||
local Player = {}
|
||||
@@ -72,6 +104,7 @@ function Player:_init(config)
|
||||
self:_loadConfig(config)
|
||||
self:_initControls()
|
||||
self:_initPairs()
|
||||
self._activeDevice = 'none'
|
||||
end
|
||||
|
||||
function Player:_setActiveDevice()
|
||||
@@ -214,6 +247,10 @@ function Player:released(name)
|
||||
end
|
||||
end
|
||||
|
||||
function Player:getActiveDevice()
|
||||
return self._activeDevice
|
||||
end
|
||||
|
||||
-- baton functions --
|
||||
|
||||
function baton.new(config)
|
||||
|
Reference in New Issue
Block a user