inputs: make inputs support multiple sources
This commit is contained in:
parent
88d81fd39a
commit
0dce34e33a
3 changed files with 63 additions and 44 deletions
|
@ -26,19 +26,27 @@ local InputManager = Object:extend()
|
|||
|
||||
function InputManager:new(controller)
|
||||
self.controller = controller
|
||||
self.data = self.controller.options:getPlayerInputData(1)
|
||||
self.data = self.controller.options:getInputData()
|
||||
|
||||
self.keys = self:getKeyList()
|
||||
self.fakekeys = self:getKeyList()
|
||||
self:initKeys()
|
||||
end
|
||||
|
||||
function InputManager:isDown(padkey)
|
||||
local isdown = false
|
||||
if self.data.type == "keyboard" then
|
||||
local key = self.data.keys[padkey]
|
||||
isdown = love.keyboard.isDown(key)
|
||||
if isdown then
|
||||
function InputManager:initKeys()
|
||||
self.fakekeys = self:getKeyList(1)
|
||||
|
||||
self.sources = {}
|
||||
for i,v in ipairs(self.data) do
|
||||
self.sources[i] = {}
|
||||
self.sources[i].keys = self:getKeyList(i)
|
||||
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
|
||||
print("Warning: unsupported input device")
|
||||
end
|
||||
|
@ -46,39 +54,38 @@ function InputManager:isDown(padkey)
|
|||
return isdown
|
||||
end
|
||||
|
||||
function InputManager:getKeyList()
|
||||
function InputManager:getKeyList(sourceid)
|
||||
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].isDown = false
|
||||
keys[k].isPressed = false
|
||||
keys[k].isReleased = false
|
||||
keys[k].test = "ok"
|
||||
end
|
||||
end
|
||||
|
||||
return keys
|
||||
end
|
||||
|
||||
function InputManager:translateAction(key)
|
||||
--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)
|
||||
function InputManager:getKey(sourceid, 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
|
||||
end
|
||||
return key
|
||||
end
|
||||
|
||||
function InputManager:flushKeys()
|
||||
for i,v in ipairs(self.sources) do
|
||||
self:flushSourceKeys(sourceid)
|
||||
end
|
||||
end
|
||||
|
||||
function InputManager:flushSourceKeys(sourceid)
|
||||
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].isDown = false
|
||||
self.keys[k].isPressed = false
|
||||
|
@ -87,31 +94,38 @@ function InputManager:flushKeys()
|
|||
end
|
||||
end
|
||||
|
||||
function InputManager:update(dt)
|
||||
for k,v in pairs(self.keys) do
|
||||
local isDown = self:isDown(k)
|
||||
|
||||
function InputManager:checkKeys(sourceid)
|
||||
for k,v in pairs(self.sources[sourceid].keys) do
|
||||
local isDown = self:isDown(sourceid, k)
|
||||
if (isDown) then
|
||||
if not (self.keys[k].isDown) then
|
||||
self.keys[k].isDown = true
|
||||
self.keys[k].isPressed = true
|
||||
self.keys[k].isReleased = false
|
||||
if not (self.sources[sourceid].keys[k].isDown) then
|
||||
self.sources[sourceid].keys[k].isDown = true
|
||||
self.sources[sourceid].keys[k].isPressed = true
|
||||
self.sources[sourceid].keys[k].isReleased = false
|
||||
else
|
||||
if (self.keys[k].isPressed) then
|
||||
self.keys[k].isPressed = false
|
||||
if (self.sources[sourceid].keys[k].isPressed) then
|
||||
self.sources[sourceid].keys[k].isPressed = false
|
||||
end
|
||||
end
|
||||
else
|
||||
if (self.keys[k].isDown) then
|
||||
self.keys[k].isDown = false
|
||||
self.keys[k].isPressed = false
|
||||
self.keys[k].isReleased = true
|
||||
if (self.sources[sourceid].keys[k].isDown) then
|
||||
self.sources[sourceid].keys[k].isDown = false
|
||||
self.sources[sourceid].keys[k].isPressed = false
|
||||
self.sources[sourceid].keys[k].isReleased = true
|
||||
else
|
||||
if (self.keys[k].isReleased) then
|
||||
self.keys[k].isReleased = false
|
||||
if (self.sources[sourceid].keys[k].isReleased) then
|
||||
self.sources[sourceid].keys[k].isReleased = false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function InputManager:update(dt)
|
||||
for i,v in ipairs(self.sources) do
|
||||
self:checkKeys(i)
|
||||
end
|
||||
end
|
||||
|
||||
return InputManager
|
||||
|
|
|
@ -74,13 +74,14 @@ function Scene:setKeys()
|
|||
self.inputLocked = false
|
||||
end
|
||||
else
|
||||
self.keys = core.input.keys
|
||||
-- TODO: make the scene input system get all inputs
|
||||
self.keys = core.input.sources[1].keys
|
||||
end
|
||||
|
||||
self.menusystem.keys = self.keys
|
||||
end
|
||||
|
||||
function Scene:getKeys()
|
||||
function Scene:getKeys(sourceid)
|
||||
if (self.inputLocked) then
|
||||
return self.keys
|
||||
else
|
||||
|
|
|
@ -92,6 +92,10 @@ function OptionsManager:getPlayerInputData(id)
|
|||
return _playerInputData
|
||||
end
|
||||
|
||||
function OptionsManager:getInputData()
|
||||
return self.data.input
|
||||
end
|
||||
|
||||
function OptionsManager:write()
|
||||
local data = self:getData()
|
||||
|
||||
|
|
Loading…
Reference in a new issue