Ability to bind hat as buttons as well
This commit is contained in:
parent
53ac2c612c
commit
2af0378bb7
3 changed files with 49 additions and 13 deletions
|
@ -12,6 +12,7 @@ require 'entities/debug'
|
||||||
require 'chaingunmode'
|
require 'chaingunmode'
|
||||||
require 'entities/plane'
|
require 'entities/plane'
|
||||||
require 'machinegun'
|
require 'machinegun'
|
||||||
|
require 'utils'
|
||||||
|
|
||||||
level_state = {}
|
level_state = {}
|
||||||
|
|
||||||
|
@ -111,10 +112,7 @@ function level_state:update(dt)
|
||||||
for player = 1, #players, 1 do
|
for player = 1, #players, 1 do
|
||||||
local j = joysticks[player]
|
local j = joysticks[player]
|
||||||
if j then
|
if j then
|
||||||
local buttons = {}
|
local buttons = getJoystickButtons(j)
|
||||||
for i = 1, j:getButtonCount(), 1 do
|
|
||||||
buttons[i] = j:isDown(i)
|
|
||||||
end
|
|
||||||
if lastJoystickButtons[player] ~= nil then
|
if lastJoystickButtons[player] ~= nil then
|
||||||
for i = 1, #buttons, 1 do
|
for i = 1, #buttons, 1 do
|
||||||
if buttons[i] ~= lastJoystickButtons[player][i] then
|
if buttons[i] ~= lastJoystickButtons[player][i] then
|
||||||
|
|
|
@ -3,6 +3,7 @@ require 'level_state'
|
||||||
require 'settings'
|
require 'settings'
|
||||||
require 'entities/label'
|
require 'entities/label'
|
||||||
require 'entities/valuecontroller'
|
require 'entities/valuecontroller'
|
||||||
|
require 'utils'
|
||||||
|
|
||||||
menu_state = {}
|
menu_state = {}
|
||||||
|
|
||||||
|
@ -91,6 +92,37 @@ end
|
||||||
function menu_state:update(dt)
|
function menu_state:update(dt)
|
||||||
menuTime = menuTime + 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
|
for key, entity in pairs(menu_state.entity_list) do
|
||||||
if entity.update then
|
if entity.update then
|
||||||
entity:update(dt)
|
entity:update(dt)
|
||||||
|
@ -139,15 +171,6 @@ function menu_state:keyreleased(key, unicode)
|
||||||
end
|
end
|
||||||
|
|
||||||
function menu_state:joystickpressed(key, button)
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
15
utils.lua
15
utils.lua
|
@ -87,3 +87,18 @@ end
|
||||||
function colorNAKKIVENE(color100, color0, ratio)
|
function colorNAKKIVENE(color100, color0, ratio)
|
||||||
return ratio * color100 + (1 - ratio) * color0
|
return ratio * color100 + (1 - ratio) * color0
|
||||||
end
|
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
|
||||||
|
|
Loading…
Reference in a new issue