-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathGridRosterUnitEvents.lua
More file actions
95 lines (85 loc) · 2.87 KB
/
GridRosterUnitEvents.lua
File metadata and controls
95 lines (85 loc) · 2.87 KB
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
--===================================================================
-- More efficient AceEvents replacement, these events are only fired
-- for units in roster so no extra checks are required to filter
-- non-roster units like nameplates.
-- These register/unregister functions are embeded by default in all
-- statuses, the "method_name" defaults to "event":
-- status:RegisterRosterUnitEvent(event, method_name)
-- status:RegisterRosterUnitEvent(event, function)
-- status:RegisterRosterUnitEvent(event)
-- status:UnregisterRosterUnitEvent(event)
-- Register/unregister examples:
-- status:RegisterRosterUnitEvent("UNIT_AURA")
-- status:RegisterRosterUnitEvent("UNIT_AURA", function)
-- status:RegisterRosterUnitEvent("UNIT_AURA", "UpdateAuras")
-- Direct call examples:
-- Grid2.UnregisterRosterUnitEvent(table, "UNIT_HEALTH")
-- Grid2.RegisterRosterUnitEvent(table, "UNIT_HEALTH", function)
-- Event handling function/method example:
-- function status:UpdateAuras(event, unit, ...)
-- self:UpdateIndicators(unit)
-- end
-- If a string is passed as method_name a direct function reference
-- is saved (not the method name) so if the function/method body is
-- changed a new RegisterRosterUnitEvent() call is required to
-- register the new function callback.
--===================================================================
local Grid2 = Grid2
local next = next
local type = type
local events = {}
local frames = setmetatable( {}, {__index = function(t, unit)
local f = CreateFrame('Frame')
f:Hide()
f:SetScript('OnEvent', function(_, event, ...)
for obj, func in next, events[event] do
func(obj, event, ...)
end
end)
t[unit] = f
return f
end} )
local Messages = LibStub("AceEvent-3.0"):Embed({})
function Messages:Grid_UnitLeft(_, unit)
frames[unit]:UnregisterAllEvents()
end
function Messages:Grid_UnitUpdated(_, unit, joined)
if joined then
local frame = frames[unit]
for eventname in next, events do
frame:RegisterUnitEvent(eventname, unit)
end
end
end
-- Public Functions
function Grid2.RegisterRosterUnitEvent(object, event, method)
if not next(events) then
Messages:RegisterMessage('Grid_UnitUpdated')
Messages:RegisterMessage('Grid_UnitLeft')
end
local objects = events[event]
if objects == nil then
objects = {}
events[event] = objects
for unit in Grid2:IterateRosterUnits() do
frames[unit]:RegisterUnitEvent(event, unit)
end
end
objects[object] = type(method)=='function' and method or object[method or event]
end
function Grid2.UnregisterRosterUnitEvent(object, event)
local objects = events[event]
if objects then
objects[object] = nil
if not next(objects) then
events[event] = nil
for unit in Grid2:IterateRosterUnits() do
frames[unit]:UnregisterEvent(event)
end
if not next(events) then
Messages:UnregisterMessage('Grid_UnitUpdated')
Messages:UnregisterMessage('Grid_UnitLeft')
end
end
end
end