108 lines
3.2 KiB
Lua
108 lines
3.2 KiB
Lua
-- 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.data.input[1]
|
|
|
|
self.keys = {}
|
|
for k,v in pairs(self.data.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
|
|
|
|
self.fakekeys = {}
|
|
for k,v in pairs(self.data.keys) do
|
|
self.fakekeys[k] = {}
|
|
self.fakekeys[k].isDown = false
|
|
self.fakekeys[k].isPressed = false
|
|
self.fakekeys[k].isReleased = false
|
|
self.fakekeys[k].test = "ok"
|
|
end
|
|
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
|
|
end
|
|
else
|
|
print("Warning: unsupported input device")
|
|
end
|
|
|
|
return isdown
|
|
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)
|
|
local padkey = padkey
|
|
for k,v in pairs(self.data.keys) do
|
|
if (k == padkey) then key = v end
|
|
end
|
|
return key
|
|
end
|
|
|
|
function InputManager:update(dt)
|
|
for k,v in pairs(self.keys) do
|
|
local isDown = self:isDown(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
|
|
else
|
|
if (self.keys[k].isPressed) then
|
|
self.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
|
|
else
|
|
if (self.keys[k].isReleased) then
|
|
self.keys[k].isReleased = false
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
return InputManager
|