/Tukui/modules/unitframes/plugins/oUF_Experience/oUF_Experience.lua

http://github.com/Asphyxia/Tukui · Lua · 152 lines · 109 code · 28 blank · 15 comment · 44 complexity · 78917a1132a3fae6f95ac12228c8f717 MD5 · raw file

  1. local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2. if C.unitframes.enable ~= true then return end
  3. local _, ns = ...
  4. local oUF = ns.oUF or oUF
  5. if not oUF then return end
  6. --[[
  7. Elements handled:
  8. .Experience [statusbar]
  9. .Experience.Rested [statusbar] (optional, must be parented to Experience)
  10. .Experience.Text [fontstring] (optional)
  11. Booleans:
  12. - noTooltip
  13. Functions that can be overridden from within a layout:
  14. - PostUpdate(element unit, min, max)
  15. --]]
  16. local _, ns = ...
  17. local oUF = ns.oUF or oUF
  18. assert(oUF, 'oUF Experience was unable to locate oUF install')
  19. local hunterPlayer = select(2, UnitClass('player')) == 'HUNTER'
  20. local function GetXP(unit)
  21. if(unit == 'pet') then
  22. return GetPetExperience()
  23. else
  24. return UnitXP(unit), UnitXPMax(unit)
  25. end
  26. end
  27. local function SetTooltip(self)
  28. local unit = self:GetParent().unit
  29. local min, max = GetXP(unit)
  30. local bars = unit == 'pet' and 6 or 20
  31. GameTooltip:SetOwner(self, 'ANCHOR_BOTTOM', 0, -5)
  32. GameTooltip:AddLine(string.format(XP..": %d / %d (%d%% - %d/%d)", min, max, min/max * 100, bars - (bars * (max - min) / max), bars))
  33. GameTooltip:AddLine(string.format(LEVEL_ABBR..": %d (%d%% - %d/%d)", max - min, (max - min) / max * 100, 1 + bars * (max - min) / max, bars))
  34. if(self.rested) then
  35. GameTooltip:AddLine(string.format("|cff0090ff"..TUTORIAL_TITLE26..": +%d (%d%%)", self.rested, self.rested / max * 100))
  36. end
  37. GameTooltip:Show()
  38. end
  39. local function Update(self, event, owner)
  40. if(event == 'UNIT_PET' and owner ~= 'player') then return end
  41. local experience = self.Experience
  42. -- Conditional hiding
  43. if(self.unit == 'player') then
  44. if(UnitLevel('player') == MAX_PLAYER_LEVEL) then
  45. return experience:Hide()
  46. end
  47. elseif(self.unit == 'pet') then
  48. local _, hunterPet = HasPetUI()
  49. if(not self.disallowVehicleSwap and UnitHasVehicleUI('player')) then
  50. return experience:Hide()
  51. elseif(not hunterPet or (UnitLevel('pet') == UnitLevel('player'))) then
  52. return experience:Hide()
  53. end
  54. else
  55. return experience:Hide()
  56. end
  57. local unit = self.unit
  58. local min, max = GetXP(unit)
  59. experience:SetMinMaxValues(0, max)
  60. experience:SetValue(min)
  61. experience:Show()
  62. if(experience.Text) then
  63. experience.Text:SetFormattedText('%d / %d', min, max)
  64. end
  65. if(experience.Rested) then
  66. local rested = GetXPExhaustion()
  67. if(unit == 'player' and rested and rested > 0) then
  68. experience.Rested:SetMinMaxValues(0, max)
  69. experience.Rested:SetValue(math.min(min + rested, max))
  70. experience.rested = rested
  71. else
  72. experience.Rested:SetMinMaxValues(0, 1)
  73. experience.Rested:SetValue(0)
  74. experience.rested = nil
  75. end
  76. end
  77. if(experience.PostUpdate) then
  78. return experience:PostUpdate(unit, min, max)
  79. end
  80. end
  81. local function Enable(self, unit)
  82. local experience = self.Experience
  83. if(experience) then
  84. local Update = experience.Update or Update
  85. self:RegisterEvent('PLAYER_XP_UPDATE', Update)
  86. self:RegisterEvent('PLAYER_LEVEL_UP', Update)
  87. self:RegisterEvent('UNIT_PET', Update)
  88. if(experience.Rested) then
  89. self:RegisterEvent('UPDATE_EXHAUSTION', Update)
  90. end
  91. if(hunterPlayer) then
  92. self:RegisterEvent('UNIT_PET_EXPERIENCE', Update)
  93. end
  94. if(not experience.noTooltip) then
  95. experience:EnableMouse()
  96. experience:HookScript('OnLeave', GameTooltip_Hide)
  97. experience:HookScript('OnEnter', SetTooltip)
  98. end
  99. if(not experience:GetStatusBarTexture()) then
  100. experience:SetStatusBarTexture([=[Interface\TargetingFrame\UI-StatusBar]=])
  101. end
  102. return true
  103. end
  104. end
  105. local function Disable(self)
  106. local experience = self.Experience
  107. if(experience) then
  108. local Update = experience.Update or Update
  109. self:UnregisterEvent('PLAYER_XP_UPDATE', Update)
  110. self:UnregisterEvent('PLAYER_LEVEL_UP', Update)
  111. self:UnregisterEvent('UNIT_PET', Update)
  112. if(experience.Rested) then
  113. self:UnregisterEvent('UPDATE_EXHAUSTION', Update)
  114. end
  115. if(hunterPlayer) then
  116. self:UnregisterEvent('UNIT_PET_EXPERIENCE', Update)
  117. end
  118. end
  119. end
  120. oUF:AddElement('Experience', Update, Enable, Disable)