/Tukui/modules/unitframes/core/oUF/elements/health.lua

http://github.com/Asphyxia/Tukui · Lua · 112 lines · 89 code · 19 blank · 4 comment · 34 complexity · f7d64d577909789e28b6d9a7f766ae09 MD5 · raw file

  1. local WoW41 = select(4, GetBuildInfo()) == 40100
  2. local parent, ns = ...
  3. local oUF = ns.oUF
  4. oUF.colors.health = {49/255, 207/255, 37/255}
  5. local Update = function(self, event, unit, powerType)
  6. if(self.unit ~= unit or (event == 'UNIT_POWER' and powerType ~= 'HAPPINESS')) then return end
  7. local health = self.Health
  8. if(health.PreUpdate) then health:PreUpdate(unit) end
  9. local min, max = UnitHealth(unit), UnitHealthMax(unit)
  10. local disconnected = not UnitIsConnected(unit)
  11. health:SetMinMaxValues(0, max)
  12. if(disconnected) then
  13. health:SetValue(max)
  14. else
  15. health:SetValue(min)
  16. end
  17. health.disconnected = disconnected
  18. local r, g, b, t
  19. if(health.colorTapping and UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit)) then
  20. t = self.colors.tapped
  21. elseif(health.colorDisconnected and not UnitIsConnected(unit)) then
  22. t = self.colors.disconnected
  23. elseif(health.colorClass and UnitIsPlayer(unit)) or
  24. (health.colorClassNPC and not UnitIsPlayer(unit)) or
  25. (health.colorClassPet and UnitPlayerControlled(unit) and not UnitIsPlayer(unit)) then
  26. local _, class = UnitClass(unit)
  27. t = self.colors.class[class]
  28. elseif(health.colorReaction and UnitReaction(unit, 'player')) then
  29. t = self.colors.reaction[UnitReaction(unit, "player")]
  30. elseif(health.colorSmooth) then
  31. r, g, b = self.ColorGradient(min / max, unpack(health.smoothGradient or self.colors.smooth))
  32. elseif(health.colorHealth) then
  33. t = self.colors.health
  34. end
  35. if(t) then
  36. r, g, b = t[1], t[2], t[3]
  37. end
  38. if(b) then
  39. health:SetStatusBarColor(r, g, b)
  40. local bg = health.bg
  41. if(bg) then local mu = bg.multiplier or 1
  42. bg:SetVertexColor(r * mu, g * mu, b * mu)
  43. end
  44. end
  45. if(health.PostUpdate) then
  46. return health:PostUpdate(unit, min, max)
  47. end
  48. end
  49. local Path = function(self, ...)
  50. return (self.Health.Override or Update) (self, ...)
  51. end
  52. local ForceUpdate = function(element)
  53. return Path(element.__owner, 'ForceUpdate', element.__owner.unit)
  54. end
  55. local Enable = function(self, unit)
  56. local health = self.Health
  57. if(health) then
  58. health.__owner = self
  59. health.ForceUpdate = ForceUpdate
  60. if(health.frequentUpdates and not self:GetScript'OnUpdate') then
  61. self:RegisterEvent('UNIT_HEALTH_FREQUENT', Path)
  62. end
  63. -- XXX: 4.0.6: They overlap, but they don't! So we'll have to eat some double
  64. -- updates. This will probably cost us less than actually running an OnUpdate
  65. -- again.
  66. self:RegisterEvent('UNIT_HEALTH', Path)
  67. self:RegisterEvent("UNIT_MAXHEALTH", Path)
  68. self:RegisterEvent('UNIT_CONNECTION', Path)
  69. self:RegisterEvent('UNIT_POWER', Path)
  70. -- For tapping.
  71. self:RegisterEvent('UNIT_FACTION', Path)
  72. if(not health:GetStatusBarTexture()) then
  73. health:SetStatusBarTexture[[Interface\TargetingFrame\UI-StatusBar]]
  74. end
  75. return true
  76. end
  77. end
  78. local Disable = function(self)
  79. local health = self.Health
  80. if(health) then
  81. self:UnregisterEvent('UNIT_HEALTH_FREQUENT', Path)
  82. self:UnregisterEvent('UNIT_HEALTH', Path)
  83. self:UnregisterEvent('UNIT_MAXHEALTH', Path)
  84. self:UnregisterEvent('UNIT_CONNECTION', Path)
  85. self:UnregisterEvent('UNIT_POWER', Path)
  86. self:UnregisterEvent('UNIT_FACTION', Path)
  87. end
  88. end
  89. oUF:AddElement('Health', Path, Enable, Disable)