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

http://github.com/Asphyxia/Tukui · Lua · 151 lines · 123 code · 25 blank · 3 comment · 45 complexity · de23f0924e0825f966ffda946c72466d MD5 · raw file

  1. local WoW41 = select(4, GetBuildInfo()) == 40100
  2. local parent, ns = ...
  3. local oUF = ns.oUF
  4. oUF.colors.power = {}
  5. for power, color in next, PowerBarColor do
  6. if(type(power) == 'string') then
  7. oUF.colors.power[power] = {color.r, color.g, color.b}
  8. end
  9. end
  10. local Update = function(self, event, unit)
  11. if(self.unit ~= unit) then return end
  12. local power = self.Power
  13. if(power.PreUpdate) then power:PreUpdate(unit) end
  14. local min, max = UnitPower(unit), UnitPowerMax(unit)
  15. local disconnected = not UnitIsConnected(unit)
  16. power:SetMinMaxValues(0, max)
  17. if(disconnected) then
  18. power:SetValue(max)
  19. else
  20. power:SetValue(min)
  21. end
  22. power.disconnected = disconnected
  23. local r, g, b, t
  24. if(power.colorTapping and UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit)) then
  25. t = self.colors.tapped
  26. elseif(power.colorDisconnected and not UnitIsConnected(unit)) then
  27. t = self.colors.disconnected
  28. elseif(power.colorPower) then
  29. local ptype, ptoken, altR, altG, altB = UnitPowerType(unit)
  30. t = self.colors.power[ptoken]
  31. if(not t and altR) then
  32. r, g, b = altR, altG, altB
  33. end
  34. elseif(power.colorClass and UnitIsPlayer(unit)) or
  35. (power.colorClassNPC and not UnitIsPlayer(unit)) or
  36. (power.colorClassPet and UnitPlayerControlled(unit) and not UnitIsPlayer(unit)) then
  37. local _, class = UnitClass(unit)
  38. t = self.colors.class[class]
  39. elseif(power.colorReaction and UnitReaction(unit, 'player')) then
  40. t = self.colors.reaction[UnitReaction(unit, "player")]
  41. elseif(power.colorSmooth) then
  42. r, g, b = self.ColorGradient(min / max, unpack(power.smoothGradient or self.colors.smooth))
  43. end
  44. if(t) then
  45. r, g, b = t[1], t[2], t[3]
  46. end
  47. if(b) then
  48. power:SetStatusBarColor(r, g, b)
  49. local bg = power.bg
  50. if(bg) then
  51. local mu = bg.multiplier or 1
  52. bg:SetVertexColor(r * mu, g * mu, b * mu)
  53. end
  54. end
  55. if(power.PostUpdate) then
  56. return power:PostUpdate(unit, min, max)
  57. end
  58. end
  59. local Path = function(self, ...)
  60. return (self.Power.Override or Update) (self, ...)
  61. end
  62. local UNIT_HAPPINESS = function(self, event, unit, powerType, ...)
  63. if(powerType == 'HAPPINESS') then
  64. return Path(self, event, unit, powerType, ...)
  65. end
  66. end
  67. local ForceUpdate = function(element)
  68. return Path(element.__owner, 'ForceUpdate', element.__owner.unit)
  69. end
  70. local OnPowerUpdate
  71. do
  72. local UnitPower = UnitPower
  73. OnPowerUpdate = function(self)
  74. if(self.disconnected) then return end
  75. local unit = self.__owner.unit
  76. local power = UnitPower(unit)
  77. if(power ~= self.min) then
  78. self.min = power
  79. return Path(self.__owner, 'OnPowerUpdate', unit)
  80. end
  81. end
  82. end
  83. local Enable = function(self, unit)
  84. local power = self.Power
  85. if(power) then
  86. power.__owner = self
  87. power.ForceUpdate = ForceUpdate
  88. if(power.frequentUpdates and (unit == 'player' or unit == 'pet')) then
  89. power:SetScript("OnUpdate", OnPowerUpdate)
  90. -- Without this happiness won't really update corretly when using
  91. -- frequentUpdates
  92. if(unit == 'pet') then
  93. power:RegisterEvent('UNIT_POWER', UNIT_HAPPINESS)
  94. end
  95. else
  96. self:RegisterEvent('UNIT_POWER', Path)
  97. end
  98. self:RegisterEvent('UNIT_CONNECTION', Path)
  99. self:RegisterEvent('UNIT_MAXPOWER', Path)
  100. -- For tapping.
  101. self:RegisterEvent('UNIT_FACTION', Path)
  102. if(not power:GetStatusBarTexture()) then
  103. power:SetStatusBarTexture[[Interface\TargetingFrame\UI-StatusBar]]
  104. end
  105. return true
  106. end
  107. end
  108. local Disable = function(self)
  109. local power = self.Power
  110. if(power) then
  111. if(power:GetScript'OnUpdate') then
  112. power:SetScript("OnUpdate", nil)
  113. self:UnregisterEvent('UNIT_POWER', UNIT_HAPPINESS)
  114. else
  115. self:UnregisterEvent('UNIT_POWER', Path)
  116. end
  117. self:UnregisterEvent('UNIT_CONNECTION', Path)
  118. self:UnregisterEvent('UNIT_MAXPOWER', Path)
  119. self:UnregisterEvent('UNIT_FACTION', Path)
  120. end
  121. end
  122. oUF:AddElement('Power', Path, Enable, Disable)