mirror of
https://github.com/TangentFoxy/baton.git
synced 2025-07-28 11:02:19 +00:00
active device detection
This commit is contained in:
36
baton.lua
36
baton.lua
@@ -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)
|
||||||
|
Reference in New Issue
Block a user