diff --git a/baton.lua b/baton.lua index c43f463..f263d3f 100644 --- a/baton.lua +++ b/baton.lua @@ -1,5 +1,23 @@ 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 = {} Player.__index = Player @@ -50,6 +68,24 @@ function Player:_init(config) self:_initPairs() 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) local player = setmetatable({}, Player) player:_init(config) diff --git a/main.lua b/main.lua index 4e996ce..0f769f3 100644 --- a/main.lua +++ b/main.lua @@ -15,4 +15,10 @@ local input = baton.new { joystick = love.joystick.getJoysticks()[1], } -print(inspect(input)) \ No newline at end of file +function love.update(dt) + input:update() +end + +function love.draw() + love.graphics.print(tostring(input._activeDevice)) +end \ No newline at end of file