View Single Post
10-03-16, 02:46 PM   #8
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Lua Code:
  1. AssignedArtifactPower = LibStub("AceAddon-3.0"):NewAddon("Assigned Artifact Power", "AceEvent-3.0")
  2.  
  3. function AssignedArtifactPower:calc_ap()
  4.     local c, ge, gc, xp, ps, n, _ = _G.C_ArtifactUI
  5.     _, _, n, _, xp, ps = c.GetEquippedArtifactInfo()
  6.     for i=1,ps-1 do
  7.         xp = xp + c.GetCostForPointAtRank(i)
  8.     end
  9.     AP = xp
  10. end
  11.  
  12. function AssignedArtifactPower:OnEnable()
  13.     self:RegisterEvent("ARTIFACT_UPDATE")
  14.     self:RegisterEvent("ARTIFACT_XP_UPDATE")
  15.     self:RegisterEvent("ARTIFACT_MAX_RANKS_UPDATE")
  16.     self:Refresh()
  17. end
  18.  
  19. function AssignedArtifactPower:ARTIFACT_UPDATE()
  20.     self:Refresh()
  21. end
  22.  
  23. function AssignedArtifactPower:ARTIFACT_XP_UPDATE()
  24.     self:Refresh()
  25. end
  26.  
  27. function AssignedArtifactPower:ARTIFACT_MAX_RANKS_UPDATE()
  28.     self:Refresh()
  29. end
  30.  
  31. function AssignedArtifactPower:Overlay()
  32.     AFP = ArtifactFrame.PerksTab
  33.     if not AFP.AssignedArtifactPower then
  34.         AF = AFP.TitleContainer
  35.         AFPoints = AF.PointsRemainingLabel
  36.         AFLabel = AF.ArtifactPower
  37.         --New Frame for available Artifactpower
  38.         AFP.ArtifactPower = CreateFrame("Frame", "ArtifactPower", AFP)
  39.         AFP.ArtifactPower:SetPoint("TOP", -75, -120, "CENTER")
  40.         AFP.ArtifactPower:SetWidth(150)
  41.         AFP.ArtifactPower:SetHeight(40)
  42.  
  43.         AFLabel:ClearAllPoints()
  44.         AFLabel:SetPoint("TOP", AFP.ArtifactPower, 0, 0, "CENTER")
  45.         AFPoints:ClearAllPoints()
  46.         AFPoints:SetPoint("BOTTOM", AFLabel, 0, -15, "CENTER")
  47.  
  48.         --New Frame for assigned Artifactpower
  49.         AFP.AssignedArtifactPower = CreateFrame("Frame", "AssignedArtifactPower", AFP)
  50.         AFP.AssignedArtifactPower:SetPoint("TOP", 75, -120, "CENTER")
  51.         AFP.AssignedArtifactPower:SetWidth(150)
  52.         AFP.AssignedArtifactPower:SetHeight(40)
  53.  
  54.         tAPL = AFP.AssignedArtifactPower:CreateFontString("ArtifactPowerLabel", "ARTWORK")
  55.         tAPL:SetFont(AFLabel:GetFont())
  56.         tAPL:SetPoint("TOP", AFP.AssignedArtifactPower, 0, 0, "CENTER")
  57.         tAPL:SetTextColor(AFLabel:GetTextColor())
  58.         tAPL:SetText("Assigned")
  59.  
  60.         tAP = AFP.AssignedArtifactPower:CreateFontString("ArtifactPowerPoints", "ARTWORK")
  61.         Mixin(tAP, AnimatedNumericFontStringMixin)
  62.         tAP:SetFont(AFPoints:GetFont())
  63.         tAP:SetPoint("BOTTOM", tAPL, 0, -15, "CENTER")
  64.         tAP:SetTextColor(AFPoints:GetTextColor())
  65.         self:calc_ap()
  66.         tAP:SetAnimatedValue(0) -- for extra prettiness :p
  67.         tAP:SetAnimatedValue(AP)
  68.  
  69.         -- TBH, it's not the best of setting OnUpdate script, but style is style :p
  70.         -- Read [url]https://www.wowinterface.com/forums/showpost.php?p=257783&postcount=2[/url]
  71.         -- (2) Defining a function inside another function
  72.         AFP.AssignedArtifactPower:SetScript("OnUpdate", function(self, elapsed)
  73.             tAP:UpdateAnimatedValue(elapsed)
  74.         end)
  75.  
  76.         AFP.AssignedArtifactPower:SetScript("OnEnter", function(self, motion)
  77.             GameTooltip:SetOwner(self, "ANCHOR_CURSOR")
  78.             GameTooltip:ClearLines()
  79.             GameTooltip:SetText("Total assigned artifact power")
  80.             GameTooltip:AddLine("Resetting your weapons artifact power will reset this counter!", 1, 1, 1)
  81.             GameTooltip:Show()
  82.         end)
  83.  
  84.         AFP.AssignedArtifactPower:SetScript("OnLeave", function(self, motion)
  85.             GameTooltip:Hide()
  86.         end)
  87.  
  88.     else
  89.         self:calc_ap()
  90.         tAP:SetAnimatedValue(AP)
  91.     end
  92. end
  93.  
  94. function AssignedArtifactPower:Refresh()
  95.     if not ArtifactFrame or not ArtifactFrame.PerksTab then return end
  96.     self:Overlay()
  97. end

As I noted in code comments, setting "OnUpdate" the way I did isn't optimal, like at all... Read this post for more info.

I also hope that tAPL, and many other variables are locals...
__________________

Last edited by lightspark : 10-03-16 at 02:57 PM.
  Reply With Quote