mirror of
https://github.com/airstruck/luigi.git
synced 2025-12-19 10:26:43 +00:00
46 lines
1.2 KiB
Lua
46 lines
1.2 KiB
Lua
local ROOT = (...):gsub('[^.]*$', '')
|
|
|
|
local Base = require(ROOT .. 'base')
|
|
local Hooker = require(ROOT .. 'hooker')
|
|
|
|
local Event = Base:extend({ name = 'Event' })
|
|
|
|
function Event:emit (target, data, defaultAction)
|
|
local callbacks = self.registry[target]
|
|
if not callbacks then
|
|
if defaultAction then defaultAction() end
|
|
return
|
|
end
|
|
local result = callbacks(data or {})
|
|
if result ~= nil then return result end
|
|
if defaultAction then defaultAction() end
|
|
end
|
|
|
|
function Event:bind (target, callback)
|
|
local registry = self.registry
|
|
return Hooker.hook(registry, target, callback)
|
|
end
|
|
|
|
local eventNames = {
|
|
'Reshape', 'Display', 'Keyboard', 'TextInput', 'Motion',
|
|
'Enter', 'Leave', 'PressEnter', 'PressLeave',
|
|
'PressStart', 'PressEnd', 'PressDrag', 'PressMove', 'Press',
|
|
}
|
|
|
|
local weakKeyMeta = { __mode = 'k' }
|
|
|
|
for i, name in ipairs(eventNames) do
|
|
Event[name] = Event:extend({
|
|
name = name,
|
|
registry = setmetatable({}, weakKeyMeta),
|
|
})
|
|
end
|
|
|
|
function Event.injectBinders (t)
|
|
for i, name in ipairs(eventNames) do
|
|
t['on' .. name] = function (...) return Event[name]:bind(...) end
|
|
end
|
|
end
|
|
|
|
return Event
|