inputs: make inputs support multiple sources

This commit is contained in:
Kazhnuz 2019-04-07 17:42:46 +02:00
parent 88d81fd39a
commit 0dce34e33a
3 changed files with 63 additions and 44 deletions

View file

@ -26,19 +26,27 @@ local InputManager = Object:extend()
function InputManager:new(controller) function InputManager:new(controller)
self.controller = controller self.controller = controller
self.data = self.controller.options:getPlayerInputData(1) self.data = self.controller.options:getInputData()
self.keys = self:getKeyList() self:initKeys()
self.fakekeys = self:getKeyList()
end end
function InputManager:isDown(padkey) function InputManager:initKeys()
local isdown = false self.fakekeys = self:getKeyList(1)
if self.data.type == "keyboard" then
local key = self.data.keys[padkey] self.sources = {}
isdown = love.keyboard.isDown(key) for i,v in ipairs(self.data) do
if isdown then self.sources[i] = {}
self.sources[i].keys = self:getKeyList(i)
end end
end
function InputManager:isDown(sourceid, padkey)
local isdown = false
if self.data[sourceid].type == "keyboard" then
local key = self.data[sourceid].keys[padkey]
isdown = love.keyboard.isDown(key)
else else
print("Warning: unsupported input device") print("Warning: unsupported input device")
end end
@ -46,39 +54,38 @@ function InputManager:isDown(padkey)
return isdown return isdown
end end
function InputManager:getKeyList() function InputManager:getKeyList(sourceid)
local keys = {} local keys = {}
for k,v in pairs(self.data.keys) do if self.data[sourceid] ~= nil then
for k,v in pairs(self.data[sourceid].keys) do
keys[k] = {} keys[k] = {}
keys[k].isDown = false keys[k].isDown = false
keys[k].isPressed = false keys[k].isPressed = false
keys[k].isReleased = false keys[k].isReleased = false
keys[k].test = "ok" keys[k].test = "ok"
end end
end
return keys return keys
end end
function InputManager:translateAction(key) function InputManager:getKey(sourceid, padkey)
--TODO:depreciated function
local padkey = ""
for k,v in pairs(self.data.keys) do
if v == key then padkey = k end
end
return padkey
end
function InputManager:getKey(padkey)
local padkey = padkey local padkey = padkey
for k,v in pairs(self.data.keys) do for k,v in pairs(self.data[sourceid].keys) do
if (k == padkey) then key = v end if (k == padkey) then key = v end
end end
return key return key
end end
function InputManager:flushKeys() function InputManager:flushKeys()
for i,v in ipairs(self.sources) do
self:flushSourceKeys(sourceid)
end
end
function InputManager:flushSourceKeys(sourceid)
self.keys = {} self.keys = {}
for k,v in pairs(self.data.keys) do for k,v in pairs(self.data[sourceid].keys) do
self.keys[k] = {} self.keys[k] = {}
self.keys[k].isDown = false self.keys[k].isDown = false
self.keys[k].isPressed = false self.keys[k].isPressed = false
@ -87,31 +94,38 @@ function InputManager:flushKeys()
end end
end end
function InputManager:update(dt)
for k,v in pairs(self.keys) do function InputManager:checkKeys(sourceid)
local isDown = self:isDown(k) for k,v in pairs(self.sources[sourceid].keys) do
local isDown = self:isDown(sourceid, k)
if (isDown) then if (isDown) then
if not (self.keys[k].isDown) then if not (self.sources[sourceid].keys[k].isDown) then
self.keys[k].isDown = true self.sources[sourceid].keys[k].isDown = true
self.keys[k].isPressed = true self.sources[sourceid].keys[k].isPressed = true
self.keys[k].isReleased = false self.sources[sourceid].keys[k].isReleased = false
else else
if (self.keys[k].isPressed) then if (self.sources[sourceid].keys[k].isPressed) then
self.keys[k].isPressed = false self.sources[sourceid].keys[k].isPressed = false
end end
end end
else else
if (self.keys[k].isDown) then if (self.sources[sourceid].keys[k].isDown) then
self.keys[k].isDown = false self.sources[sourceid].keys[k].isDown = false
self.keys[k].isPressed = false self.sources[sourceid].keys[k].isPressed = false
self.keys[k].isReleased = true self.sources[sourceid].keys[k].isReleased = true
else else
if (self.keys[k].isReleased) then if (self.sources[sourceid].keys[k].isReleased) then
self.keys[k].isReleased = false self.sources[sourceid].keys[k].isReleased = false
end end
end end
end end
end end
end end
function InputManager:update(dt)
for i,v in ipairs(self.sources) do
self:checkKeys(i)
end
end
return InputManager return InputManager

View file

@ -74,13 +74,14 @@ function Scene:setKeys()
self.inputLocked = false self.inputLocked = false
end end
else else
self.keys = core.input.keys -- TODO: make the scene input system get all inputs
self.keys = core.input.sources[1].keys
end end
self.menusystem.keys = self.keys self.menusystem.keys = self.keys
end end
function Scene:getKeys() function Scene:getKeys(sourceid)
if (self.inputLocked) then if (self.inputLocked) then
return self.keys return self.keys
else else

View file

@ -92,6 +92,10 @@ function OptionsManager:getPlayerInputData(id)
return _playerInputData return _playerInputData
end end
function OptionsManager:getInputData()
return self.data.input
end
function OptionsManager:write() function OptionsManager:write()
local data = self:getData() local data = self:getData()