feat(core/input): extract virtualpads from the input controller
This commit is contained in:
parent
2e3fd587c0
commit
3e907b69d7
2 changed files with 123 additions and 68 deletions
|
@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
- **world:** extract map module from the world module
|
- **world:** extract map module from the world module
|
||||||
|
|
||||||
|
- **core/input:** extract virtualpads from the input controller
|
||||||
|
|
||||||
## [0.6.0] - 2019-07-20
|
## [0.6.0] - 2019-07-20
|
||||||
|
|
||||||
- Meta: Add proper crediting
|
- Meta: Add proper crediting
|
||||||
|
|
|
@ -22,7 +22,8 @@
|
||||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
]]
|
]]
|
||||||
|
|
||||||
local InputManager = Object:extend()
|
local InputManager = Object:extend()
|
||||||
|
local VirtualPad = Object:extend()
|
||||||
|
|
||||||
-- INIT FUNCTIONS
|
-- INIT FUNCTIONS
|
||||||
-- Initialize and configure the controller system
|
-- Initialize and configure the controller system
|
||||||
|
@ -31,42 +32,23 @@ function InputManager:new(controller)
|
||||||
self.controller = controller
|
self.controller = controller
|
||||||
self.data = self.controller.options:getInputData()
|
self.data = self.controller.options:getInputData()
|
||||||
|
|
||||||
self:initKeys()
|
self:initSources()
|
||||||
end
|
end
|
||||||
|
|
||||||
function InputManager:initKeys()
|
function InputManager:initSources()
|
||||||
self.fakekeys = self:getKeyList(1)
|
self.sources = {}
|
||||||
|
for sourceid, data in ipairs(self.data) do
|
||||||
self.sources = self:getSources()
|
local source = VirtualPad(self, sourceid, data)
|
||||||
self.fakesources = self:getSources()
|
table.insert(self.sources, source)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- INFO FUNCTIONS
|
-- INFO FUNCTIONS
|
||||||
-- Get functions from the controller object
|
-- Get functions from the controller object
|
||||||
|
|
||||||
function InputManager:isDown(sourceid, padkey)
|
function InputManager:isDown(sourceid, key)
|
||||||
local isdown = false
|
self.controller.debug:warning("core/input", "core.input:isDown is deprecated since 0.7.0 and will be removed in 0.8.0")
|
||||||
|
return self.sources[sourceid]:isDown(key)
|
||||||
if self.data[sourceid].type == "keyboard" then
|
|
||||||
local key = self.data[sourceid].keys[padkey]
|
|
||||||
isdown = love.keyboard.isDown(key)
|
|
||||||
else
|
|
||||||
local type = self.data[sourceid].type or "nil"
|
|
||||||
local warnstring = "unsupported input device " .. type .. " for source " .. sourceid
|
|
||||||
core.debug:warning("core/input", warnstring)
|
|
||||||
end
|
|
||||||
|
|
||||||
return isdown
|
|
||||||
end
|
|
||||||
|
|
||||||
function InputManager:getSources()
|
|
||||||
local sources = {}
|
|
||||||
for i,v in ipairs(self.data) do
|
|
||||||
sources[i] = {}
|
|
||||||
sources[i].keys = self:getKeyList(i)
|
|
||||||
end
|
|
||||||
|
|
||||||
return sources
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function InputManager:getKeyList(sourceid)
|
function InputManager:getKeyList(sourceid)
|
||||||
|
@ -77,7 +59,6 @@ function InputManager:getKeyList(sourceid)
|
||||||
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"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -92,60 +73,132 @@ function InputManager:getKey(sourceid, padkey)
|
||||||
return key
|
return key
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function InputManager:getSources()
|
||||||
|
return self.sources
|
||||||
|
end
|
||||||
|
|
||||||
-- KEY MANAGEMENT FUNCTIONS
|
-- KEY MANAGEMENT FUNCTIONS
|
||||||
-- Manage pressed keys
|
-- Manage pressed keys
|
||||||
|
|
||||||
function InputManager:flushKeys()
|
function InputManager:flushKeys()
|
||||||
for i,v in ipairs(self.sources) do
|
for i,v in ipairs(self.sources) do
|
||||||
self:flushSourceKeys(i)
|
source:flushKeys()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function InputManager:flushSourceKeys(sourceid)
|
function InputManager:flushSourceKeys(sourceid)
|
||||||
self.keys = {}
|
self.keys = {}
|
||||||
for k,v in pairs(self.sources[sourceid].keys) do
|
self.sources[sourceid]:flushKeys()
|
||||||
v = {}
|
|
||||||
v.isDown = false
|
|
||||||
v.isPressed = false
|
|
||||||
v.isReleased = false
|
|
||||||
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
|
end
|
||||||
|
|
||||||
-- UPDATE FUNCTIONS
|
-- UPDATE FUNCTIONS
|
||||||
-- Check every step pressed keys
|
-- Check every step pressed keys
|
||||||
|
|
||||||
function InputManager:update(dt)
|
function InputManager:update(dt)
|
||||||
for i,v in ipairs(self.sources) do
|
for i, source in ipairs(self.sources) do
|
||||||
self:checkKeys(i)
|
source:checkKeys()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
------------------------------------ VIRTUALPADS -------------------------------
|
||||||
|
-- Virtual representation of a pad
|
||||||
|
-- The role of the virtualpad is to return all the data a controller at any time
|
||||||
|
-- They can be flushed and deactivated for a while when needed
|
||||||
|
|
||||||
|
-- INIT FUNCTIONS
|
||||||
|
-- Initialize and configure the controller system
|
||||||
|
|
||||||
|
function VirtualPad:new(controller, id, data)
|
||||||
|
self.controller = controller
|
||||||
|
self.id = id
|
||||||
|
self.data = data
|
||||||
|
|
||||||
|
self.type = self.data.type or "nil"
|
||||||
|
|
||||||
|
self:initKeys()
|
||||||
|
end
|
||||||
|
|
||||||
|
function VirtualPad:initKeys()
|
||||||
|
local keys = {}
|
||||||
|
if (self.data ~= nil) then
|
||||||
|
for k,v in pairs(self.data.keys) do
|
||||||
|
keys[k] = {}
|
||||||
|
keys[k].isDown = false
|
||||||
|
keys[k].isPressed = false
|
||||||
|
keys[k].isReleased = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self.keys = keys
|
||||||
|
self.fakekeys = keys
|
||||||
|
end
|
||||||
|
|
||||||
|
function VirtualPad:isDown(key)
|
||||||
|
local isdown = false
|
||||||
|
|
||||||
|
if self.type == "keyboard" then
|
||||||
|
isdown = love.keyboard.isDown(self.data.keys[key])
|
||||||
|
else
|
||||||
|
local warnstring = "unsupported input device " .. self.type .. " for source " .. self.id
|
||||||
|
core.debug:warning("core/input", warnstring)
|
||||||
|
end
|
||||||
|
|
||||||
|
return isdown
|
||||||
|
end
|
||||||
|
|
||||||
|
function VirtualPad:checkKeys()
|
||||||
|
for key, keydata in pairs(self.keys) do
|
||||||
|
self:checkKey(key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function VirtualPad:checkKey(key)
|
||||||
|
local isDown = self:isDown(key)
|
||||||
|
if (isDown) then
|
||||||
|
if not (self.keys[key].isDown) then
|
||||||
|
core.debug:print("virtualpad", "key " .. key .. " is Pressed")
|
||||||
|
self.keys[key].isDown = true
|
||||||
|
self.keys[key].isPressed = true
|
||||||
|
self.keys[key].isReleased = false
|
||||||
|
else
|
||||||
|
if (self.keys[key].isPressed) then
|
||||||
|
core.debug:print("virtualpad", "key " .. key .. " is Down")
|
||||||
|
self.keys[key].isPressed = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if (self.keys[key].isDown) then
|
||||||
|
self.keys[key].isDown = false
|
||||||
|
self.keys[key].isPressed = false
|
||||||
|
self.keys[key].isReleased = true
|
||||||
|
else
|
||||||
|
if (self.keys[key].isReleased) then
|
||||||
|
core.debug:print("virtualpad", "key " .. key .. " is Released")
|
||||||
|
self.keys[key].isReleased = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function VirtualPad:getKeys()
|
||||||
|
return self.keys
|
||||||
|
end
|
||||||
|
|
||||||
|
function VirtualPad:getKey(key)
|
||||||
|
return self.keys[key]
|
||||||
|
end
|
||||||
|
|
||||||
|
function VirtualPad:flushKeys()
|
||||||
|
for key, _ in pairs(self.keys) do
|
||||||
|
self:flushKey(key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function VirtualPad:flushKey(key)
|
||||||
|
self.keys[key].isDown = false
|
||||||
|
self.keys[key].isPressed = false
|
||||||
|
self.keys[key].isReleased = false
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
return InputManager
|
return InputManager
|
||||||
|
|
Loading…
Reference in a new issue