mirror of
https://github.com/airstruck/luigi.git
synced 2025-12-19 02:16:43 +00:00
initial commit
This commit is contained in:
49
luigi/event.lua
Normal file
49
luigi/event.lua
Normal file
@@ -0,0 +1,49 @@
|
||||
local ROOT = (...):gsub('[^.]*$', '')
|
||||
|
||||
local Base = require(ROOT .. 'base')
|
||||
|
||||
local Event = Base:extend({ name = 'Event' })
|
||||
|
||||
function Event:emit (observer, data, defaultAction)
|
||||
local callbacks = self.registry[observer]
|
||||
if not callbacks then
|
||||
if defaultAction then defaultAction() end
|
||||
return
|
||||
end
|
||||
for i, callback in ipairs(callbacks) do
|
||||
local result = callback(data or {})
|
||||
if result ~= nil then return result end
|
||||
end
|
||||
if defaultAction then defaultAction() end
|
||||
end
|
||||
|
||||
function Event:bind (observer, callback)
|
||||
local registry = self.registry
|
||||
if not registry[observer] then
|
||||
registry[observer] = {}
|
||||
end
|
||||
table.insert(registry[observer], callback)
|
||||
end
|
||||
|
||||
local eventNames = {
|
||||
'Display', 'Keyboard', 'Motion', 'Mouse', 'Reshape', 'Enter', 'Leave',
|
||||
'Press', 'PressStart', 'PressDrag', 'PressMove', 'PressLeave', 'PressEnter',
|
||||
'PressEnd'
|
||||
}
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user