active device detection

This commit is contained in:
Andrew Minnich
2018-03-08 23:01:45 -05:00
parent 0cb0175add
commit 375a4e1441
2 changed files with 43 additions and 1 deletions

View File

@@ -1,5 +1,23 @@
local baton = {} local baton = {}
local function parseSource(source)
return source:match '(.+):(.+)'
end
local sf = {kbm = {}, joy = {}}
function sf.kbm.key(key)
return love.keyboard.isDown(key) and 1 or 0
end
function sf.joy.button(joystick, button)
if tonumber(button) then
return joystick:isDown(tonumber(button)) and 1 or 0
else
return joystick:isGamepadDown(button) and 1 or 0
end
end
local Player = {} local Player = {}
Player.__index = Player Player.__index = Player
@@ -50,6 +68,24 @@ function Player:_init(config)
self:_initPairs() self:_initPairs()
end end
function Player:_setActiveDevice()
for _, control in pairs(self._controls) do
for _, source in ipairs(control.sources) do
local type, value = parseSource(source)
if sf.kbm[type] and sf.kbm[type](value) > self.config.deadzone then
self._activeDevice = 'kbm'
return
elseif self.config.joystick and sf.joy[type] and sf.joy[type](self.config.joystick, value) > self.config.deadzone then
self._activeDevice = 'joy'
end
end
end
end
function Player:update()
self:_setActiveDevice()
end
function baton.new(config) function baton.new(config)
local player = setmetatable({}, Player) local player = setmetatable({}, Player)
player:_init(config) player:_init(config)

View File

@@ -15,4 +15,10 @@ local input = baton.new {
joystick = love.joystick.getJoysticks()[1], joystick = love.joystick.getJoysticks()[1],
} }
print(inspect(input)) function love.update(dt)
input:update()
end
function love.draw()
love.graphics.print(tostring(input._activeDevice))
end