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

http://github.com/Asphyxia/Tukui · Lua · 81 lines · 73 code · 7 blank · 1 comment · 21 complexity · 1e393e906ed77044877beae1a0986456 MD5 · raw file

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