You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.6 KiB
71 lines
1.6 KiB
-- Create an EventManager class
|
|
---@class EventManager
|
|
local EventManager = {
|
|
events = {},
|
|
eventHandlers = {},
|
|
wowEventHandlers = {},
|
|
frame = nil
|
|
}
|
|
|
|
EventManager.__index = EventManager
|
|
|
|
-- Constructor
|
|
---@return EventManager
|
|
function EventManager:New()
|
|
local self = setmetatable({}, EventManager)
|
|
self.events = {}
|
|
self.eventHandlers = {}
|
|
self.wowEventHandlers = {}
|
|
|
|
-- Frame for wow events
|
|
self.frame = CreateFrame("Frame")
|
|
|
|
self.frame:SetScript('OnEvent', function(f, event, ...)
|
|
if self.wowEventHandlers[event] then
|
|
for _, callback in ipairs(self.wowEventHandlers[event]) do
|
|
callback(...)
|
|
end
|
|
end
|
|
end)
|
|
|
|
return self
|
|
end
|
|
|
|
-- Register an event
|
|
---@param event string
|
|
---@param handler fun(...)
|
|
---@return nil
|
|
function EventManager:RegisterEvent(event, handler)
|
|
if not self.events[event] then
|
|
self.events[event] = {}
|
|
end
|
|
|
|
table.insert(self.events[event], handler)
|
|
end
|
|
|
|
-- Register a wow event
|
|
---@param event string
|
|
---@param handler fun(...)
|
|
---@return nil
|
|
function EventManager:RegisterWoWEvent(event, handler)
|
|
if not self.wowEventHandlers[event] then
|
|
self.wowEventHandlers[event] = {}
|
|
self.frame:RegisterEvent(event)
|
|
end
|
|
|
|
table.insert(self.wowEventHandlers[event], handler)
|
|
end
|
|
|
|
-- Trigger an event
|
|
---@param event string
|
|
---@param ... any
|
|
---@return nil
|
|
function EventManager:TriggerEvent(event, ...)
|
|
if self.events[event] then
|
|
for _, handler in pairs(self.events[event]) do
|
|
handler(...)
|
|
end
|
|
end
|
|
end
|
|
|
|
return EventManager
|
|
|