-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathcombatevents.lua
116 lines (93 loc) · 2.84 KB
/
combatevents.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
local _, ns = ...
local oUF = ns.oUF
local Private = oUF.Private
local argcheck = Private.argcheck
local frame_metatable = Private.frame_metatable
local CombatLogGetCurrentEventInfo = _G.CombatLogGetCurrentEventInfo
local event_metatable = {
__call = function(funcs, ...)
for self, func in next, funcs do
if (self:IsVisible()) then
func(self, ...)
end
end
end,
}
local self_metatable = {
__call = function(funcs, self, ...)
for _, func in next, funcs do
func(self, ...)
end
end
}
local listener = CreateFrame('Frame')
listener.activeEvents = 0
local function filter(_, event, ...)
if(listener[event]) then
listener[event](event, ...)
end
end
listener:SetScript('OnEvent', function(self, event)
filter(CombatLogGetCurrentEventInfo())
end)
--[[ CombatEvents: frame:RegisterCombatEvent(event, handler)
Used to register a frame for a combat log event and add an event handler.
* self - frame that will be registered for the given event
* event - name of the combat log event to register (string)
* handler - function which will be executed when the combat log event fires. Multiple handlers can be added for the
same frame and event (function)
--]]
function frame_metatable.__index:RegisterCombatEvent(event, handler)
argcheck(event, 2, 'string')
argcheck(handler, 3, 'function')
if(not listener[event]) then
listener[event] = setmetatable({}, event_metatable)
listener.activeEvents = listener.activeEvents + 1
end
local current = listener[event][self]
if(current) then
for _, func in next, current do
if(func == handler) then return end
end
table.insert(current, handler)
else
-- even with a single handler we want to make sure the frame is visible
listener[event][self] = setmetatable({handler}, self_metatable)
end
if(listener.activeEvents > 0) then
listener:RegisterEvent('COMBAT_LOG_EVENT_UNFILTERED')
end
end
--[[ CombatEvents: frame:UnregisterCombatEvent(event, handler)
Used to remove a function from the event handler list for a combat log event.
* self - the frame registered for the event
* event - name of the registered combat log event (string)
* handler - function to be removed from the list of event handlers
--]]
function frame_metatable.__index:UnregisterCombatEvent(event, handler)
argcheck(event, 2, 'string')
if(not listener[event]) then return end
local cleanUp = false
local current = listener[event][self]
if(current) then
for i, func in next, current do
if(func == handler) then
current[i] = nil
break
end
end
if(not next(current)) then
cleanUp = true
end
end
if(cleanUp) then
listener[event][self] = nil
if(not next(listener[event])) then
listener[event] = nil
listener.activeEvents = listener.activeEvents - 1
if(listener.activeEvents <= 0) then
listener:UnregisterEvent('COMBAT_LOG_EVENT_UNFILTERED')
end
end
end
end