Ability to bind hat as buttons as well

This commit is contained in:
Erkki Seppälä 2014-10-24 09:43:42 +03:00
parent 53ac2c612c
commit 2af0378bb7
3 changed files with 49 additions and 13 deletions

View file

@ -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

View file

@ -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

View file

@ -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