Advertisement
SethAluma

ArtifactPathFinder.lua 1.4.2 (modified)

Oct 27th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.27 KB | None | 0 0
  1. --[[
  2.  
  3. Copyright (C) 2016 Yossa <yossa.addons@googlemail.com>
  4. All Rights Reserved unless otherwise explicitly stated.
  5.  
  6. ]]--
  7.  
  8. ArtifactPathFinder = LibStub("AceAddon-3.0"):NewAddon("ArtifactPathFinder", "AceEvent-3.0")
  9.  
  10. function ArtifactPathFinder:OnInitialize()
  11.     if not ArtifactPathFinderCDB then
  12.         ArtifactPathFinderCDB = {}
  13.     end
  14.     if not ArtifactPathFinderCDB.selectedPathBySpec then
  15.         ArtifactPathFinderCDB.selectedPathBySpec = {}
  16.     end
  17. end
  18.  
  19. function ArtifactPathFinder:OnEnable()
  20.     self:RegisterEvent("ARTIFACT_UPDATE");
  21.     self:RegisterEvent("ARTIFACT_MAX_RANKS_UPDATE");
  22.     self:RefreshPerkRanks();
  23. end
  24.  
  25. function ArtifactPathFinder:OnDisable()
  26.     self:UnregisterAllEvents()
  27.     local powers = C_ArtifactUI.GetPowers();
  28.     for i, powerID in ipairs(powers) do
  29.         local powerButton = ArtifactPerksMixin.powerIDToPowerButton[powerID]
  30.         if powerButton and powerButton.ArtifactPathIndex then
  31.             powerButton.ArtifactPathIndex:Hide();
  32.         end
  33.     end
  34. end
  35.  
  36. function ArtifactPathFinder:ARTIFACT_UPDATE()
  37.     self:RefreshPerkRanks();
  38. end
  39.  
  40. function ArtifactPathFinder:ARTIFACT_MAX_RANKS_UPDATE()
  41.     self:RefreshPerkRanks();
  42. end
  43.  
  44. function ArtifactPathFinder:GetCurrentPaths()
  45.     local _, className = UnitClass("player");
  46.     local artifactItemId = C_ArtifactUI.GetArtifactInfo()
  47.     local pathTable = self.Data
  48.  
  49.     local currentPath = nil
  50.     local currentPathName = nil
  51.  
  52.     if pathTable[className] and pathTable[className][artifactItemId] then
  53.         return pathTable[className][artifactItemId]
  54.     end
  55.     return nil
  56. end
  57.  
  58. local function GetPowerInfoMaps()
  59.     local powers = C_ArtifactUI.GetPowers();
  60.     if not powers then return nil end
  61.     local powerInfo, spellInfo = {}, {}
  62.     -- get powers info
  63.     for i, powerID in ipairs(powers) do
  64.         local spellID, _, currentRank, maxRank, bonusRanks, x, y = C_ArtifactUI.GetPowerInfo(powerID);
  65.         powerInfo[powerID] = {}
  66.         powerInfo[powerID].spellID = spellID
  67.         powerInfo[powerID].powerID = powerID
  68.         powerInfo[powerID].buttonIndex = i
  69.         powerInfo[powerID].currentRank = currentRank
  70.         powerInfo[powerID].maxRank = maxRank
  71.         powerInfo[powerID].bonusRanks = bonusRanks
  72.         powerInfo[powerID].x = x
  73.         powerInfo[powerID].y = y
  74.         powerInfo[powerID].isFinished = currentRank == maxRank;
  75.         spellInfo[spellID] = powerInfo[powerID]
  76.     end
  77.     return powers, powerInfo, spellInfo
  78. end
  79.  
  80. local brokenSpellId = {}
  81.  
  82. local function CreateIndexesForPowers(currentPath, spellInfo)
  83.     local selectionPath = {}
  84.     local isNextChecking = true
  85.     --set powers indexes and next selection
  86.     for i,spellID in ipairs(currentPath) do
  87.         -- Start multi-spellID fix (Added by SethAluma)
  88.         if (type(spellID) == "table") then
  89.             for j,subSpellID in ipairs(spellID) do
  90.                 if spellInfo[subSpellID] then
  91.                     spellID = subSpellID
  92.                     break
  93.                 end
  94.             end
  95.         end
  96.         -- End multi-spellID fix
  97.        
  98.         if not spellInfo[spellID] then
  99.             if not brokenSpellId[spellID] then
  100.                 local _, className = UnitClass("player");
  101.                 local artifactItemId = C_ArtifactUI.GetArtifactInfo()
  102.                 DEFAULT_CHAT_FRAME:AddMessage("Artifact spell not found. Please report a bug in Path data: "..tostring(spellID).." "..className.."/"..tostring(artifactItemId))
  103.                 brokenSpellId[spellID] = 1
  104.             end
  105.         else
  106.             local isNext = false
  107.             if isNextChecking then
  108.                 if not spellInfo[spellID].isFinished then
  109.                     isNext = true
  110.                     isNextChecking = false
  111.                 end
  112.             end
  113.             local powerID = spellInfo[spellID].powerID
  114.             selectionPath[powerID] = {}
  115.             selectionPath[powerID].index = i
  116.             selectionPath[powerID].isNext = isNext
  117.         end
  118.     end
  119.     return selectionPath
  120. end
  121.  
  122. local function CreateButtonOverlay(powerButton)
  123.     local button = powerButton:CreateFontString(nil, "OVERLAY")
  124.     button:SetFont("Fonts\\FRIZQT__.TTF", 22, "OUTLINE")
  125.     button:SetPoint("CENTER",powerButton.Icon,"CENTER",0,0)
  126.     return button
  127. end
  128.  
  129. function ArtifactPathFinder:RefreshSelectedPath()
  130.     local currentPaths = ArtifactPathFinder:GetCurrentPaths()
  131.     local pathCount = 0
  132.     if not currentPaths then return 0 end
  133.     for _ in pairs(currentPaths) do pathCount = pathCount + 1 end
  134.     local artifactItemId = C_ArtifactUI.GetArtifactInfo()
  135.  
  136.     if pathCount == 0 then
  137.         self.selectedName = nil
  138.         self.selectedPath = nil
  139.     elseif pathCount == 1 or not ArtifactPathFinderCDB.selectedPathBySpec[artifactItemId] then
  140.         self.selectedName, self.selectedPath = next(currentPaths)
  141.     else
  142.         self.selectedName = ArtifactPathFinderCDB.selectedPathBySpec[artifactItemId]
  143.         if currentPaths[self.selectedName] then
  144.             self.selectedPath = currentPaths[self.selectedName]
  145.         else
  146.             self.selectedName, self.selectedPath = next(currentPaths)
  147.         end
  148.     end
  149.     ArtifactPathFinderCDB.selectedPathBySpec[artifactItemId] = self.selectedName
  150.     return pathCount
  151. end
  152.  
  153. local function DropDown_Initialize(self, level)
  154.     local info = UIDropDownMenu_CreateInfo()
  155.     local num = 1
  156.     local selectedNum = nil
  157.     for k, v in pairs(ArtifactPathFinder:GetCurrentPaths()) do
  158.         info = UIDropDownMenu_CreateInfo()
  159.         info.text = k
  160.         info.value = num
  161.         info.func = function (self)
  162.             UIDropDownMenu_SetSelectedID(ArtifactFrame.PerksTab.ArtifactPathFinderFilter, self:GetID())
  163.             ArtifactPathFinder.selectedPath = v
  164.             ArtifactPathFinder.selectedName = k
  165.             ArtifactPathFinderCDB.selectedPathBySpec[C_ArtifactUI.GetArtifactInfo()] = k
  166.             ArtifactPathFinder:RefreshPerkRanks()
  167.         end
  168.         info.checked = ArtifactPathFinder.selectedName == k
  169.         if info.checked then selectedNum = num end
  170.         num = num + 1
  171.         UIDropDownMenu_AddButton(info, level)
  172.     end
  173.     if selectedNum then
  174.         UIDropDownMenu_SetSelectedID(ArtifactFrame.PerksTab.ArtifactPathFinderFilter, selectedNum)
  175.     end
  176. end
  177.  
  178. function ArtifactPathFinder:RefreshFilter()
  179.     local pathCount = ArtifactPathFinder:RefreshSelectedPath()
  180.     if pathCount < 2 then
  181.         if ArtifactFrame.PerksTab.ArtifactPathFinderFilter then
  182.             ArtifactFrame.PerksTab.ArtifactPathFinderFilter:Hide()
  183.         end
  184.         return
  185.     end
  186.  
  187.     if not ArtifactFrame.PerksTab.ArtifactPathFinderFilter then
  188.         ArtifactFrame.PerksTab.ArtifactPathFinderFilter = CreateFrame("Frame", "ArtifactPathFinderFilter", ArtifactFrame.PerksTab, "UIDropDownMenuTemplate")
  189.     else
  190.         ArtifactFrame.PerksTab.ArtifactPathFinderFilter:Show()
  191.     end
  192.     local FilterDropDown = ArtifactFrame.PerksTab.ArtifactPathFinderFilter
  193.     UIDropDownMenu_Initialize(FilterDropDown, DropDown_Initialize)
  194.     UIDropDownMenu_SetWidth(FilterDropDown, 200);
  195.     UIDropDownMenu_SetButtonWidth(FilterDropDown, 144)
  196.     UIDropDownMenu_JustifyText(FilterDropDown, "LEFT")
  197.     FilterDropDown:SetPoint("TOPRIGHT", ArtifactFrame.PerksTab, "TOPRIGHT", 0, -50)
  198. end
  199.  
  200. function ArtifactPathFinder:HideAll()
  201.     local powers = C_ArtifactUI.GetPowers();
  202.     for i, powerID in ipairs(powers) do
  203.         local powerButton = ArtifactFrame.PerksTab:GetOrCreatePowerButton(i)
  204.         if powerButton and powerButton.ArtifactPathIndex then
  205.             powerButton.ArtifactPathIndex:Hide()
  206.             powerButton.StarBurst:SetAlpha(0)
  207.         end
  208.     end
  209.     if ArtifactFrame.PerksTab.ArtifactPathFinderFilter then
  210.         ArtifactFrame.PerksTab.ArtifactPathFinderFilter:Hide()
  211.     end
  212. end
  213.  
  214. function ArtifactPathFinder:RefreshPerkRanks()
  215.     if not ArtifactFrame or not ArtifactFrame.PerksTab then return end
  216.  
  217.     self:RefreshFilter()
  218.  
  219.     local currentPath = self.selectedPath
  220.     if currentPath then
  221.         local powers, powerInfo, spellInfo = GetPowerInfoMaps();
  222.         if not powers then return end
  223.         local selectionPath = CreateIndexesForPowers(currentPath, spellInfo)
  224.  
  225.         --fill all buttons
  226.         for i, powerID in ipairs(powers) do
  227.             local powerButton = ArtifactFrame.PerksTab:GetOrCreatePowerButton(i)
  228.             if powerButton then
  229.                 if not powerButton.ArtifactPathIndex then
  230.                     powerButton.ArtifactPathIndex = CreateButtonOverlay(powerButton)
  231.                 else
  232.                     powerButton.ArtifactPathIndex:Show();
  233.                 end
  234.                 powerButton.StarBurst:SetAlpha(0)
  235.                 powerButton.ArtifactPathIndex:SetTextColor(1, 1, 1, 1)
  236.                 powerButton.ArtifactPathIndex:SetText("")
  237.  
  238.                 if selectionPath[powerID] then
  239.                     if (C_ArtifactUI.GetTotalPurchasedRanks() == 0 and selectionPath[powerID].index>2) or
  240.                        (selectionPath[powerID].index == 18 and not selectionPath[powerID].isNext)
  241.                     then
  242.                         powerButton.ArtifactPathIndex:Hide()
  243.                     else
  244.                         powerButton.ArtifactPathIndex:SetText(tostring(selectionPath[powerID].index));
  245.                         if selectionPath[powerID].isNext then
  246.                             powerButton.ArtifactPathIndex:SetTextColor(1, 0, 0, 1)
  247.                             powerButton.StarBurst:SetAlpha(0.9)
  248.                         end
  249.                     end
  250.                 end
  251.             end
  252.         end
  253.     end
  254. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement