epervier-old/gamecore/input.lua

132 lines
3.8 KiB
Lua
Raw Normal View History

-- core/input.lua :: The input system. This object take care of transforming the
-- differents inputs source in a virtual controller for the game.
--[[
Copyright © 2019 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local InputManager = Object:extend()
function InputManager:new(controller)
self.controller = controller
self.data = self.controller.options:getInputData()
self:initKeys()
end
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
return isdown
end
function InputManager:getKeyList(sourceid)
local keys = {}
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:getKey(sourceid, padkey)
local padkey = padkey
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[sourceid].keys) do
self.keys[k] = {}
self.keys[k].isDown = false
self.keys[k].isPressed = false
self.keys[k].isReleased = false
self.keys[k].test = "ok"
end
end
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.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.sources[sourceid].keys[k].isPressed) then
self.sources[sourceid].keys[k].isPressed = false
end
end
else
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.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