cc-utilities/gfx/event.lua

35 lines
872 B
Lua

local Event = {}
function Event.isClickEvent(evt)
return evt[1] == "monitor_touch" or evt[1] == "mouse_click"
end
function Event.isKeyEvent(evt, key)
return evt[1] == "key" and (key == nil or evt[2] == key)
end
function Event.getClickParams(evt)
return evt[3], evt[4], evt[2]
end
function Event.repositionEvent(evt, dX, dY)
if Event.isClickEvent(evt) then
return evt[1], evt[2], evt[3] + dX, evt[4] + dY
else
return evt
end
end
function Event.toElementLocalPosition(evt, element)
return {Event.repositionEvent(evt, 1 - element:getX(), 1 - element:getY())}
end
function Event.containsClick(element, evt, dX, dY)
local x, y = Event.getClickParams(evt)
x = x + (dX or 0)
y = y + (dY or 0)
local eX, eY = element:getPos()
return x >= eX and x < (eX + element:getWidth()) and y >= eY and y < (eY + element:getHeight())
end
return Event