AzeriteBar.lua

AzeriteBar.lua (8.1.5.29981; unchanged since 8.1.0.28724)
  1. local AZERITE_XP_BAR_EVENTS = {
  2.   "PLAYER_ENTERING_WORLD",
  3.   "AZERITE_ITEM_EXPERIENCE_CHANGED",
  4.   "CVAR_UPDATE",
  5.   "BAG_UPDATE",
  6. };
  7. AzeriteBarMixin = CreateFromMixins(StatusTrackingBarMixin);
  8.  
  9. function AzeriteBarMixin:ShouldBeVisible()
  10.   return C_AzeriteItem.HasActiveAzeriteItem();
  11. end
  12.  
  13. function AzeriteBarMixin:Update()
  14.   local azeriteItemLocation = C_AzeriteItem.FindActiveAzeriteItem();
  15.  
  16.   if (not azeriteItemLocation) then
  17.     return;
  18.   end
  19.  
  20.   local azeriteItem = Item:CreateFromItemLocation(azeriteItemLocation);
  21.  
  22.   if AzeriteUtil.IsAzeriteItemLocationBankBag(azeriteItemLocation) then
  23.     self.xp, self.totalLevelXP = 0, 1;
  24.     self.currentLevel = -1;
  25.   else
  26.     self.xp, self.totalLevelXP = C_AzeriteItem.GetAzeriteItemXPInfo(azeriteItemLocation);
  27.     self.currentLevel = C_AzeriteItem.GetPowerLevel(azeriteItemLocation);
  28.   end
  29.   self.xpToNextLevel = self.totalLevelXP - self.xp;
  30.  
  31.   self:SetBarValues(self.xp, 0, self.totalLevelXP);
  32.   self:UpdatePointsTooltip();
  33.   self:Show();
  34. end
  35.  
  36. function AzeriteBarMixin:UpdateOverlayFrameText()
  37.   if ( self.OverlayFrame.Text:IsShown() ) then
  38.     local xp = self.StatusBar:GetAnimatedValue();
  39.     local _, totalLevelXP = self.StatusBar:GetMinMaxValues();
  40.     self.OverlayFrame.Text:SetFormattedText(AZERITE_POWER_BAR, FormatPercentage(xp / totalLevelXP, true));
  41.   end
  42. end
  43.  
  44. function AzeriteBarMixin:AnimatedValueChangedCallback()
  45.   self:UpdateOverlayFrameText();
  46.   self:UpdatePointsTooltip();
  47. end
  48.  
  49. function AzeriteBarMixin:OnLoad()
  50.   self:SetBarColor(ARTIFACT_BAR_COLOR:GetRGB());
  51.   self.StatusBar:SetOnAnimatedValueChangedCallback(function() self:AnimatedValueChangedCallback(); end)
  52.   self.priority = 0;
  53. end
  54.  
  55. function AzeriteBarMixin:OnEvent(event, ...)
  56.   if ( self:IsVisible() ) then
  57.     if ( event == "PLAYER_ENTERING_WORLD" or event == "AZERITE_ITEM_EXPERIENCE_CHANGED") then
  58.       self:Update();
  59.     elseif ( event == "CVAR_UPDATE" ) then
  60.       local name, value = ...;
  61.       if ( name == "XP_BAR_TEXT" ) then
  62.         self:UpdateTextVisibility();
  63.       end
  64.     elseif ( event == "BAG_UPDATE" ) then
  65.       local bagID = ...;
  66.       if bagID > NUM_BAG_SLOTS then
  67.         self:Update();
  68.       end
  69.     end
  70.   end
  71. end
  72.  
  73. function AzeriteBarMixin:OnShow()
  74.   FrameUtil.RegisterFrameForEvents(self, AZERITE_XP_BAR_EVENTS);
  75.   self:UpdateTextVisibility();
  76. end
  77.  
  78. function AzeriteBarMixin:OnHide()
  79.   FrameUtil.UnregisterFrameForEvents(self, AZERITE_XP_BAR_EVENTS);
  80. end
  81.  
  82. function AzeriteBarMixin:OnEnter()
  83.   self:ShowText();
  84.   self:UpdateOverlayFrameText();
  85.   if self.currentLevel == -1 then
  86.     GameTooltip_SetDefaultAnchor(GameTooltip, UIParent);
  87.     GameTooltip:SetText(HEART_OF_AZEROTH_MISSING_ERROR, HIGHLIGHT_FONT_COLOR:GetRGB());
  88.   else
  89.     self:SetupPointsTooltip();
  90.   end
  91. end
  92.  
  93. function AzeriteBarMixin:OnLeave()
  94.   self:HideText();
  95.   GameTooltip_Hide();
  96.   if self.currentLevel ~= -1 then
  97.     self:CancelItemLoadCallback();
  98.   end
  99. end
  100.  
  101. function AzeriteBarMixin:CancelItemLoadCallback()
  102.   if self.itemDataLoadedCancelFunc then
  103.     self.itemDataLoadedCancelFunc();
  104.     self.itemDataLoadedCancelFunc = nil;
  105.   end
  106. end
  107.  
  108. function AzeriteBarMixin:SetupPointsTooltip()
  109.   local azeriteItemLocation = C_AzeriteItem.FindActiveAzeriteItem();
  110.   local azeriteItem = Item:CreateFromItemLocation(azeriteItemLocation);
  111.  
  112.   self.itemDataLoadedCancelFunc = azeriteItem:ContinueWithCancelOnItemLoad(function()
  113.     local azeriteItemName = azeriteItem:GetItemName();
  114.     GameTooltip_SetDefaultAnchor(GameTooltip, UIParent);
  115.     GameTooltip:SetText(AZERITE_POWER_TOOLTIP_TITLE:format(self.currentLevel, self.xpToNextLevel), HIGHLIGHT_FONT_COLOR:GetRGB());
  116.     GameTooltip:AddLine(AZERITE_POWER_TOOLTIP_BODY:format(azeriteItemName));
  117.     GameTooltip:Show();
  118.   end);
  119. end
  120.  
  121. function AzeriteBarMixin:UpdatePointsTooltip()
  122.   if ( GameTooltip:IsOwned(self) ) then
  123.     self:SetupPointsTooltip();
  124.   end
  125. end