knucklebones/utils/input.lua

41 lines
1.0 KiB
Lua
Raw Permalink Normal View History

2023-08-12 11:36:14 +02:00
-- manage clicks, since tic-80 only returns a "pressed" state
Input = {
--- @type boolean
lclick_prev_state = false,
--- @type 'none'|'pressed'|'released'
lclick_state = "none"
}
function Input.update_mouse(self)
local x, y, left = mouse()
-- left btn down
if left then
-- was not pressed before
if not self.lclick_prev_state then
self.lclick_state = "pressed"
self.lclick_prev_state = true
-- was already pressed
elseif self.lclick_prev_state then
self.lclick_state = "none"
end
-- left btn up
elseif not left then
if self.lclick_prev_state then
self.lclick_state = "released"
self.lclick_prev_state = false
elseif not self.lclick_prev_state then
self.lclick_state = "none"
end
end
end
function Input.mouse_pressed(self)
return self.lclick_state == "pressed"
end
function Input.mouse_released(self)
return self.lclick_state == "released"
end