From 2af0378bb74b821eb720a307e9b84147e724ce05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erkki=20Sepp=C3=A4l=C3=A4?= Date: Fri, 24 Oct 2014 09:43:42 +0300 Subject: [PATCH] Ability to bind hat as buttons as well --- level_state.lua | 6 ++---- menu_state.lua | 41 ++++++++++++++++++++++++++++++++--------- utils.lua | 15 +++++++++++++++ 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/level_state.lua b/level_state.lua index 9dca20b..df9b03f 100644 --- a/level_state.lua +++ b/level_state.lua @@ -12,6 +12,7 @@ require 'entities/debug' require 'chaingunmode' require 'entities/plane' require 'machinegun' +require 'utils' level_state = {} @@ -111,10 +112,7 @@ function level_state:update(dt) for player = 1, #players, 1 do local j = joysticks[player] if j then - local buttons = {} - for i = 1, j:getButtonCount(), 1 do - buttons[i] = j:isDown(i) - end + local buttons = getJoystickButtons(j) if lastJoystickButtons[player] ~= nil then for i = 1, #buttons, 1 do if buttons[i] ~= lastJoystickButtons[player][i] then diff --git a/menu_state.lua b/menu_state.lua index 98f7ce7..a6360e4 100644 --- a/menu_state.lua +++ b/menu_state.lua @@ -3,6 +3,7 @@ require 'level_state' require 'settings' require 'entities/label' require 'entities/valuecontroller' +require 'utils' menu_state = {} @@ -91,6 +92,37 @@ end function menu_state:update(dt) menuTime = menuTime + dt + local buttons + for i, joystick in ipairs(love.joystick.getJoysticks()) do + local jButtons = getJoystickButtons(joystick) + if buttons == nil then + buttons = jButtons + else + for j = 1, #buttons, 1 do + buttons[j] = buttons[j] or jButtons[i] + end + end + end + + if currentlyChosen ~= nil then + local button = nil + for i = 1, #buttons, 1 do + if buttons[i] then + button = i + end + end + + if button ~= nil then + local key = string.format("button%d", button) + KEYMAP[currentlyChosen.data.player][currentlyChosen.data.key][currentlyChosen.data.bindingIdx] = key + currentlyChosen.data.label.label = key + currentlyChosen:delete() + currentlyChosen = nil + save_settings() + end + end + + for key, entity in pairs(menu_state.entity_list) do if entity.update then entity:update(dt) @@ -139,15 +171,6 @@ function menu_state:keyreleased(key, unicode) end function menu_state:joystickpressed(key, button) - if currentlyChosen == nil then - else - local key = string.format("button%d", button) - KEYMAP[currentlyChosen.data.player][currentlyChosen.data.key][currentlyChosen.data.bindingIdx] = key - currentlyChosen.data.label.label = key - currentlyChosen:delete() - currentlyChosen = nil - save_settings() - end end diff --git a/utils.lua b/utils.lua index 586e508..bc16ddd 100644 --- a/utils.lua +++ b/utils.lua @@ -87,3 +87,18 @@ end function colorNAKKIVENE(color100, color0, ratio) return ratio * color100 + (1 - ratio) * color0 end + +function getJoystickButtons(j) + local buttons = {} + for i = 1, j:getButtonCount(), 1 do + buttons[#buttons + 1] = j:isDown(i) + end + local hatDirs = {"u", "r", "d", "l"} + for i = 1, j:getHatCount(), 1 do + local hat = j:getHat(i) + for j = 1, #hatDirs, 1 do + buttons[#buttons + 1] = hatDirs[j] == hat + end + end + return buttons +end