Merge pull request #16 from SashLilac/hat_handling

Fixed the handling of joystick hats.
pull/17/head
Ishaan Bhardwaj 2021-03-07 21:17:59 -05:00 committed by GitHub
commit 502a50d004
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 12 deletions

View File

@ -242,6 +242,14 @@ function love.joystickaxis(joystick, axis, value)
end end
end end
local last_hat_direction = ""
local directions = {
["u"] = "up",
["d"] = "down",
["l"] = "left",
["r"] = "right",
}
function love.joystickhat(joystick, hat, direction) function love.joystickhat(joystick, hat, direction)
local input_pressed = nil local input_pressed = nil
local has_hat = false local has_hat = false
@ -258,31 +266,47 @@ function love.joystickhat(joystick, hat, direction)
has_hat = true has_hat = true
end end
if input_pressed then if input_pressed then
dir = direction for i = 1, #direction do
for i, direction in ipairs{"d", "l", "u", "r"} do local char = direction:sub(i, i)
if dir:sub(1, 1) == direction or dir:sub(2) == direction then local _, count = last_hat_direction:gsub(char, char)
scene:onInputPress({input=input_pressed, type="joyhat", name=joystick:getName(), hat=hat, direction=direction}) if count == 0 then
else scene:onInputPress({input=config.input.joysticks[joystick:getName()].hats[hat][char], type="joyhat", name=joystick:getName(), hat=hat, direction=char})
scene:onInputRelease({input=input_pressed, type="joyhat", name=joystick:getName(), hat=hat, direction=direction})
end end
end end
for i = 1, #last_hat_direction do
local char = last_hat_direction:sub(i, i)
local _, count = direction:gsub(char, char)
if count == 0 then
scene:onInputRelease({input=config.input.joysticks[joystick:getName()].hats[hat][char], type="joyhat", name=joystick:getName(), hat=hat, direction=char})
end
end
last_hat_direction = direction
elseif has_hat then elseif has_hat then
for i, direction in ipairs{"d", "l", "ld", "lu", "r", "rd", "ru", "u"} do for i, direction in ipairs{"d", "l", "ld", "lu", "r", "rd", "ru", "u"} do
scene:onInputRelease({input=config.input.joysticks[joystick:getName()].hats[hat][direction], type="joyhat", name=joystick:getName(), hat=hat, direction=direction}) scene:onInputRelease({input=config.input.joysticks[joystick:getName()].hats[hat][direction], type="joyhat", name=joystick:getName(), hat=hat, direction=direction})
end end
last_hat_direction = ""
elseif direction ~= "c" then elseif direction ~= "c" then
dir = direction for i = 1, #direction do
for i, direction in ipairs{"d", "l", "u", "r"} do local char = direction:sub(i, i)
if dir:sub(1, 1) == direction or dir:sub(2) == direction then local _, count = last_hat_direction:gsub(char, char)
scene:onInputPress({input=nil, type="joyhat", name=joystick:getName(), hat=hat, direction=direction}) if count == 0 then
else scene:onInputPress({input=directions[char], type="joyhat", name=joystick:getName(), hat=hat, direction=char})
scene:onInputRelease({input=nil, type="joyhat", name=joystick:getName(), hat=hat, direction=direction})
end end
end end
for i = 1, #last_hat_direction do
local char = last_hat_direction:sub(i, i)
local _, count = direction:gsub(char, char)
if count == 0 then
scene:onInputRelease({input=directions[char], type="joyhat", name=joystick:getName(), hat=hat, direction=char})
end
end
last_hat_direction = direction
else else
for i, direction in ipairs{"d", "l", "ld", "lu", "r", "rd", "ru", "u"} do for i, direction in ipairs{"d", "l", "ld", "lu", "r", "rd", "ru", "u"} do
scene:onInputRelease({input=nil, type="joyhat", name=joystick:getName(), hat=hat, direction=direction}) scene:onInputRelease({input=nil, type="joyhat", name=joystick:getName(), hat=hat, direction=direction})
end end
last_hat_direction = ""
end end
end end