diff --git a/CHANGELOG.md b/CHANGELOG.md index 14a36cb..b5de3de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 +- **core/input:** extract virtualpads from the input controller + ## [0.6.0] - 2019-07-20 - Meta: Add proper crediting diff --git a/gamecore/input.lua b/gamecore/input.lua index c0c0078..ee76baa 100644 --- a/gamecore/input.lua +++ b/gamecore/input.lua @@ -22,7 +22,8 @@ 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 -- Initialize and configure the controller system @@ -31,42 +32,23 @@ function InputManager:new(controller) self.controller = controller self.data = self.controller.options:getInputData() - self:initKeys() + self:initSources() end -function InputManager:initKeys() - self.fakekeys = self:getKeyList(1) - - self.sources = self:getSources() - self.fakesources = self:getSources() +function InputManager:initSources() + self.sources = {} + for sourceid, data in ipairs(self.data) do + local source = VirtualPad(self, sourceid, data) + table.insert(self.sources, source) + end end -- INFO FUNCTIONS -- Get functions from the controller object -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 - 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 +function InputManager:isDown(sourceid, key) + 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) end function InputManager:getKeyList(sourceid) @@ -77,7 +59,6 @@ function InputManager:getKeyList(sourceid) keys[k].isDown = false keys[k].isPressed = false keys[k].isReleased = false - keys[k].test = "ok" end end @@ -92,60 +73,132 @@ function InputManager:getKey(sourceid, padkey) return key end +function InputManager:getSources() + return self.sources +end + -- KEY MANAGEMENT FUNCTIONS -- Manage pressed keys function InputManager:flushKeys() for i,v in ipairs(self.sources) do - self:flushSourceKeys(i) + source:flushKeys() end end function InputManager:flushSourceKeys(sourceid) self.keys = {} - for k,v in pairs(self.sources[sourceid].keys) do - 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 + self.sources[sourceid]:flushKeys() end -- UPDATE FUNCTIONS -- Check every step pressed keys function InputManager:update(dt) - for i,v in ipairs(self.sources) do - self:checkKeys(i) + for i, source in ipairs(self.sources) do + source:checkKeys() 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