PageRenderTime 182ms CodeModel.GetById 80ms app.highlight 4ms RepoModel.GetById 97ms app.codeStats 0ms

/Tukui/modules/unitframes/core/oUF/events.lua

http://github.com/Asphyxia/Tukui
Lua | 81 lines | 73 code | 7 blank | 1 comment | 14 complexity | 1e393e906ed77044877beae1a0986456 MD5 | raw file
 1local parent, ns = ...
 2local oUF = ns.oUF
 3local Private = oUF.Private
 4
 5local argcheck = Private.argcheck
 6local error = Private.error
 7local frame_metatable = Private.frame_metatable
 8
 9-- Events
10Private.OnEvent = function(self, event, ...)
11	if(not self:IsShown()) then return end
12	return self[event](self, event, ...)
13end
14
15local event_metatable = {
16	__call = function(funcs, self, ...)
17		for _, func in next, funcs do
18			func(self, ...)
19		end
20	end,
21}
22
23local RegisterEvent = frame_metatable.__index.RegisterEvent
24function frame_metatable.__index:RegisterEvent(event, func)
25	argcheck(event, 2, 'string')
26
27	if(type(func) == 'string' and type(self[func]) == 'function') then
28		func = self[func]
29	end
30
31	local curev = self[event]
32	local kind = type(curev)
33	if(curev and func) then
34		if(kind == 'function' and curev ~= func) then
35			self[event] = setmetatable({curev, func}, event_metatable)
36		elseif(kind == 'table') then
37			for _, infunc in next, curev do
38				if(infunc == func) then return end
39			end
40
41			table.insert(curev, func)
42		end
43	elseif(self:IsEventRegistered(event)) then
44		return
45	else
46		if(type(func) == 'function') then
47			self[event] = func
48		elseif(not self[event]) then
49			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')
50		end
51
52		RegisterEvent(self, event)
53	end
54end
55
56local UnregisterEvent = frame_metatable.__index.UnregisterEvent
57function frame_metatable.__index:UnregisterEvent(event, func)
58	argcheck(event, 2, 'string')
59
60	local curev = self[event]
61	if(type(curev) == 'table' and func) then
62		for k, infunc in next, curev do
63			if(infunc == func) then
64				table.remove(curev, k)
65
66				local n = #curev
67				if(n == 1) then
68					local _, handler = next(curev)
69					self[event] = handler
70				elseif(n == 0) then
71					UnregisterEvent(self, event)
72				end
73
74				break
75			end
76		end
77	elseif(curev == func) then
78		self[event] = nil
79		UnregisterEvent(self, event)
80	end
81end