Add detection of short and long press on buttons

This commit is contained in:
2022-09-10 14:02:25 +02:00
parent f5befe340f
commit bcb7929e69

View File

@ -3,37 +3,65 @@ version 37
__lua__
-- maths attacks --
-- by djib --
btn_stat = false
btnp_stat = false
held_stat = false
switch = false
function _init()
init_btn_status()
end
function _update()
btn_stat = false
if btn(➡️) then
btn_stat = true
end
btnp_stat = false
if btnp(➡️) then
btnp_stat = true
end
if btnp(➡️) and btn(➡️) then
if switch then
held_stat = true
else
switch=true
end
else
held_stat = false
end
update_btn_status(❎)
update_btn_status(🅾️)
end
function _draw()
print(tostr(btn_stat) .. "-" .. tostr(btnp_stat) .. "-" tostr(held_stat))
cls()
print("❎ - pressed:"..tostr(btn_pressed(❎)).." - held:"..tostr(btn_held(❎)),1,1)
print("🅾️ - pressed:"..tostr(btn_pressed(🅾️)).." - held:"..tostr(btn_held(🅾️)),1,10)
end
-->8
-- buttons --
function init_btn_status()
-- no repeating delay for keyboard
poke(0x5f5d,255)
-- true when pressed
btn_down_status={}
-- true when quickly released
btn_up_status={}
-- true when held
btn_held_status={}
end
function update_btn_status(button)
-- button was already pressed --
if btn_down_status[button] then
-- button was held
if btnp(button) then
btn_held_status[button]=true
btn_down_status[button]=false
end
-- button no longer pressed
if not btn(button) then
btn_up_status[button]=true
btn_down_status[button]=false
end
else
if btnp(button) then
btn_down_status[button]=true
end
if not btn(button) then
btn_held_status[button]=false
btn_down_status[button]=false
btn_up_status[button]=false
end
end
end
function btn_held(button)
return btn_held_status[button]
end
function btn_pressed(button)
return btn_up_status[button]
end
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000