Go to most recent revision | Details | Compare with Previous | View Log
Line No. | Rev | Author | Line |
---|---|---|---|
1 | 2 | Blooblahguy-207965 | local parent, ns = ... |
2 | Blooblahguy-207965 | local oUF = ns.oUF |
|
3 | Blooblahguy-207965 | local Private = oUF.Private |
|
4 | Blooblahguy-207965 | ||
5 | Blooblahguy-207965 | local argcheck = Private.argcheck |
|
6 | Blooblahguy-207965 | local error = Private.error |
|
7 | Blooblahguy-207965 | local frame_metatable = Private.frame_metatable |
|
8 | Blooblahguy-207965 | ||
9 | Blooblahguy-207965 | -- Original event methods |
|
10 | Blooblahguy-207965 | local RegisterEvent = frame_metatable.__index.RegisterEvent |
|
11 | Blooblahguy-207965 | local RegisterUnitEvent = frame_metatable.__index.RegisterUnitEvent |
|
12 | Blooblahguy-207965 | local UnregisterEvent = frame_metatable.__index.UnregisterEvent |
|
13 | Blooblahguy-207965 | local IsEventRegistered = frame_metatable.__index.IsEventRegistered |
|
14 | Blooblahguy-207965 | ||
15 | Blooblahguy-207965 | local unitEvents = {} |
|
16 | Blooblahguy-207965 | ||
17 | Blooblahguy-207965 | Private.UpdateUnits = function(frame, unit, realUnit) |
|
18 | Blooblahguy-207965 | if unit == realUnit then |
|
19 | Blooblahguy-207965 | realUnit = nil |
|
20 | Blooblahguy-207965 | end |
|
21 | Blooblahguy-207965 | if frame.unit ~= unit or frame.realUnit ~= realUnit then |
|
22 | Blooblahguy-207965 | for event in next, unitEvents do |
|
23 | Blooblahguy-207965 | -- IsEventRegistered returns the units in case of an event |
|
24 | Blooblahguy-207965 | -- registered with RegisterUnitEvent |
|
25 | Blooblahguy-207965 | local registered, unit1 = IsEventRegistered(frame, event) |
|
26 | Blooblahguy-207965 | if registered and unit1 ~= unit then |
|
27 | Blooblahguy-207965 | -- RegisterUnitEvent erases previously registered units so |
|
28 | Blooblahguy-207965 | -- do not bother to unregister it |
|
29 | Blooblahguy-207965 | RegisterUnitEvent(frame, event, unit, realUnit) |
|
30 | Blooblahguy-207965 | end |
|
31 | Blooblahguy-207965 | end |
|
32 | Blooblahguy-207965 | frame.unit = unit |
|
33 | Blooblahguy-207965 | frame.realUnit = realUnit |
|
34 | Blooblahguy-207965 | frame.id = unit:match'^.-(%d+)' |
|
35 | Blooblahguy-207965 | return true |
|
36 | Blooblahguy-207965 | end |
|
37 | Blooblahguy-207965 | end |
|
38 | Blooblahguy-207965 | ||
39 | Blooblahguy-207965 | local OnEvent = function(self, event, ...) |
|
40 | Blooblahguy-207965 | if self:IsVisible() then |
|
41 | Blooblahguy-207965 | return self[event](self, event, ...) |
|
42 | Blooblahguy-207965 | end |
|
43 | Blooblahguy-207965 | end |
|
44 | Blooblahguy-207965 | ||
45 | Blooblahguy-207965 | local event_metatable = { |
|
46 | Blooblahguy-207965 | __call = function(funcs, self, ...) |
|
47 | Blooblahguy-207965 | for _, func in next, funcs do |
|
48 | Blooblahguy-207965 | func(self, ...) |
|
49 | Blooblahguy-207965 | end |
|
50 | Blooblahguy-207965 | end, |
|
51 | Blooblahguy-207965 | } |
|
52 | Blooblahguy-207965 | ||
53 | Blooblahguy-207965 | function frame_metatable.__index:RegisterEvent(event, func, unitless) |
|
54 | Blooblahguy-207965 | -- Block OnUpdate polled frames from registering events. |
|
55 | Blooblahguy-207965 | if(self.__eventless) then return end |
|
56 | Blooblahguy-207965 | ||
57 | Blooblahguy-207965 | argcheck(event, 2, 'string') |
|
58 | Blooblahguy-207965 | ||
59 | Blooblahguy-207965 | if(type(func) == 'string' and type(self[func]) == 'function') then |
|
60 | Blooblahguy-207965 | func = self[func] |
|
61 | Blooblahguy-207965 | end |
|
62 | Blooblahguy-207965 | ||
63 | Blooblahguy-207965 | -- TODO: should warn the user. |
|
64 | Blooblahguy-207965 | if not unitless and not (unitEvents[event] or event:match'^UNIT_') then |
|
65 | Blooblahguy-207965 | unitless = true |
|
66 | Blooblahguy-207965 | end |
|
67 | Blooblahguy-207965 | ||
68 | Blooblahguy-207965 | local curev = self[event] |
|
69 | Blooblahguy-207965 | local kind = type(curev) |
|
70 | Blooblahguy-207965 | if(curev and func) then |
|
71 | Blooblahguy-207965 | if(kind == 'function' and curev ~= func) then |
|
72 | Blooblahguy-207965 | self[event] = setmetatable({curev, func}, event_metatable) |
|
73 | Blooblahguy-207965 | elseif(kind == 'table') then |
|
74 | Blooblahguy-207965 | for _, infunc in next, curev do |
|
75 | Blooblahguy-207965 | if(infunc == func) then return end |
|
76 | Blooblahguy-207965 | end |
|
77 | Blooblahguy-207965 | ||
78 | Blooblahguy-207965 | table.insert(curev, func) |
|
79 | Blooblahguy-207965 | end |
|
80 | Blooblahguy-207965 | elseif(IsEventRegistered(self, event)) then |
|
81 | Blooblahguy-207965 | return |
|
82 | Blooblahguy-207965 | else |
|
83 | Blooblahguy-207965 | if(type(func) == 'function') then |
|
84 | Blooblahguy-207965 | self[event] = func |
|
85 | Blooblahguy-207965 | elseif(not self[event]) then |
|
86 | Blooblahguy-207965 | return error("Style [%s] attempted to register event [%s] on unit [%s] with a handler that doesn't exist.", self.style, event, self.unit or 'unknown') |
|
87 | Blooblahguy-207965 | end |
|
88 | Blooblahguy-207965 | ||
89 | Blooblahguy-207965 | if not self:GetScript('OnEvent') then |
|
90 | Blooblahguy-207965 | self:SetScript('OnEvent', OnEvent) |
|
91 | Blooblahguy-207965 | end |
|
92 | Blooblahguy-207965 | ||
93 | Blooblahguy-207965 | if unitless then |
|
94 | Blooblahguy-207965 | RegisterEvent(self, event) |
|
95 | Blooblahguy-207965 | else |
|
96 | Blooblahguy-207965 | unitEvents[event] = true |
|
97 | Blooblahguy-207965 | RegisterUnitEvent(self, event, self.unit) |
|
98 | Blooblahguy-207965 | end |
|
99 | Blooblahguy-207965 | end |
|
100 | Blooblahguy-207965 | end |
|
101 | Blooblahguy-207965 | ||
102 | Blooblahguy-207965 | function frame_metatable.__index:UnregisterEvent(event, func) |
|
103 | Blooblahguy-207965 | argcheck(event, 2, 'string') |
|
104 | Blooblahguy-207965 | ||
105 | Blooblahguy-207965 | local curev = self[event] |
|
106 | Blooblahguy-207965 | if(type(curev) == 'table' and func) then |
|
107 | Blooblahguy-207965 | for k, infunc in next, curev do |
|
108 | Blooblahguy-207965 | if(infunc == func) then |
|
109 | Blooblahguy-207965 | table.remove(curev, k) |
|
110 | Blooblahguy-207965 | ||
111 | Blooblahguy-207965 | local n = #curev |
|
112 | Blooblahguy-207965 | if(n == 1) then |
|
113 | Blooblahguy-207965 | local _, handler = next(curev) |
|
114 | Blooblahguy-207965 | self[event] = handler |
|
115 | Blooblahguy-207965 | elseif(n == 0) then |
|
116 | Blooblahguy-207965 | -- This should not happen |
|
117 | Blooblahguy-207965 | UnregisterEvent(self, event) |
|
118 | Blooblahguy-207965 | end |
|
119 | Blooblahguy-207965 | ||
120 | Blooblahguy-207965 | break |
|
121 | Blooblahguy-207965 | end |
|
122 | Blooblahguy-207965 | end |
|
123 | Blooblahguy-207965 | elseif(curev == func) then |
|
124 | Blooblahguy-207965 | self[event] = nil |
|
125 | Blooblahguy-207965 | UnregisterEvent(self, event) |
|
126 | Blooblahguy-207965 | end |
|
127 | Blooblahguy-207965 | end |