Advertisement
Ylaana

GlyphList.lua

May 12th, 2021 (edited)
25
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local glyphViewer, glyphData = ...
  2.  
  3. local glyphList = {}
  4. local playerLoc = PlayerLocation:CreateFromUnit("player")
  5. local _, _, playerClassID = C_PlayerInfo.GetClass(playerLoc)
  6. local conflicts = glyphData.Conflicts[playerClassID]
  7.  
  8. local function GetGlyphedSpells()
  9.     local spellsWithGlyphs = {}
  10.     for i = 1, GetNumSpellTabs() do
  11.         local _, _, offset, numSlots = GetSpellTabInfo(i)
  12.         for j = offset+1, offset+numSlots do
  13.             local _, spellID = GetSpellBookItemInfo(j, BOOKTYPE_SPELL)
  14.             if HasAttachedGlyph(spellID) then
  15.                 spellsWithGlyphs[#spellsWithGlyphs+1] = GetCurrentGlyphNameForSpell(spellID)
  16.             end
  17.         end
  18.     end
  19.     return spellsWithGlyphs
  20. end
  21.  
  22. local function IsGlyphActive (tab, val)
  23.     if (#tab > 0) then
  24.         for i = 1, #tab do
  25.             if val == tab[i] then
  26.                 return true
  27.             end
  28.         end
  29.     end
  30.     return false
  31. end
  32.  
  33. local glyphedSpells = GetGlyphedSpells()
  34. local wait = {}
  35. local cache_writer = CreateFrame('Frame')
  36. cache_writer:RegisterEvent('GET_ITEM_INFO_RECEIVED')
  37. cache_writer:SetScript('OnEvent', function(self, event, ...)
  38.     if event == 'GET_ITEM_INFO_RECEIVED' then
  39.         local itemID = ...
  40.         if wait[itemID] then
  41.             local itemName, itemLink = GetItemInfo(itemID)
  42.             --print("wait, ".. itemLink)
  43.             local isActive = IsGlyphActive(glyphedSpells, itemName)
  44.             glyphList[#glyphList+1] = {
  45.                 itemID=itemID,
  46.                 itemIcon=GetItemIcon(itemID),
  47.                 itemName=itemName,
  48.                 itemLink=itemLink,
  49.                 isActive=isActive,
  50.             }
  51.             wait[itemID] = nil
  52.         end
  53.     end
  54. end)
  55.  
  56. local function CreateGlyphList()
  57.     for i = 1, #glyphData.Glyphs[playerClassID] do
  58.         local itemID = glyphData.Glyphs[playerClassID][i]
  59.         local itemName, itemLink = GetItemInfo(itemID)
  60.         if itemName then
  61.             --print("normal, ".. itemLink)
  62.             local isActive = IsGlyphActive(glyphedSpells, itemName)
  63.             glyphList[#glyphList+1] = {
  64.                 itemID=itemID,
  65.                 itemIcon=GetItemIcon(itemID),
  66.                 itemName=itemName,
  67.                 itemLink=itemLink,
  68.                 isActive=isActive,
  69.             }
  70.         else
  71.             wait[itemID] = true
  72.         end
  73.     end
  74.     return glyphList
  75. end
  76.  
  77. local function GetGlyphInfo(itemID)
  78.     for i = 1, #glyphList do
  79.         if itemID == glyphList[i].itemID then
  80.             return glyphList[i]
  81.         end
  82.     end
  83. end
  84.  
  85. local function IsGlyphExclusive(item)
  86.     local exclusive = false
  87.     local active = false
  88.     local foundIndex = 0
  89.     if (#glyphedSpells > 0 and #conflicts > 0) then
  90.         for i = 1, #conflicts do
  91.             for j = 1, #conflicts[i] do
  92.                 if item.itemID == conflicts[i][j] then
  93.                     exclusive = true
  94.                     foundIndex = i
  95.                     break
  96.                 end
  97.             end
  98.             if exclusive == true then
  99.                 local matchedSet = conflicts[foundIndex]
  100.                 for k = 1, #matchedSet do
  101.                     local glyph = GetGlyphInfo(matchedSet[k])
  102.                     if glyph and glyph.isActive then
  103.                         active = true
  104.                         break
  105.                     end
  106.                 end
  107.             end
  108.         end
  109.     end
  110.     return exclusive, active
  111. end
  112.  
  113. GlyphListMixin = {};
  114.  
  115. function GlyphListMixin:OnLoad()
  116.     -- Set properties
  117.     self:EnableMouse(true)
  118.     self:SetMovable(true)
  119.     self:SetUserPlaced(true)
  120.     self:RegisterForDrag("LeftButton")
  121.     self:RegisterEvent("GET_ITEM_INFO_RECEIVED")
  122.  
  123.     self.items = CreateGlyphList();
  124.  
  125.     self.ListScrollFrame.update = function() self:RefreshLayout(); end
  126.  
  127.     HybridScrollFrame_SetDoNotHideScrollBar(self.ListScrollFrame, true);
  128. end
  129.  
  130. function GlyphListMixin:OnShow()
  131.     HybridScrollFrame_CreateButtons(self.ListScrollFrame, "GlyphListItemTemplate");
  132.  
  133.     self:RefreshLayout();
  134. end
  135.  
  136. function GlyphListMixin:RefreshLayout()
  137.     local items = self.items
  138.     local buttons = HybridScrollFrame_GetButtons(self.ListScrollFrame)
  139.     local offset = HybridScrollFrame_GetOffset(self.ListScrollFrame)
  140.  
  141.     for buttonIndex = 1, #buttons do
  142.         local button = buttons[buttonIndex]
  143.         local itemIndex = buttonIndex + offset
  144.  
  145.         button.GlyphActive:SetTexture("Interface\\Tooltips\\UI-Tooltip-Background")
  146.         button.GlyphActive:SetGradientAlpha("HORIZONTAL", unpack({0.15,0.6,0.15,1, 0.15,0.6,0.15,0}))
  147.         button.GlyphActive:Hide()
  148.  
  149.         button.GlyphConflict:SetTexture("Interface\\Tooltips\\UI-Tooltip-Background")
  150.         button.GlyphConflict:SetGradientAlpha("HORIZONTAL", unpack({0.6,0.15,0.15,1, 0.6,0.15,0.15,0}))
  151.         button.GlyphConflict:Hide()
  152.  
  153.         if itemIndex <= #items then
  154.             local item = items[itemIndex];
  155.             local isExclusive, isActive = IsGlyphExclusive(item)
  156.             button:SetID(itemIndex);
  157.             button.Icon:SetTexture(item.itemIcon or nil);
  158.             button.Text:SetText(item.itemLink or "");
  159.  
  160.             if item.isActive then
  161.                 button.GlyphActive:Show()
  162.             elseif isExclusive and isActive then
  163.                 button.GlyphConflict:Show()
  164.             end
  165.  
  166.             button:SetScript("OnEnter",function()
  167.                 GameTooltip:SetOwner(self,"ANCHOR_CURSOR")
  168.                 GameTooltip:SetHyperlink(item.itemLink)
  169.                 GameTooltip:Show()
  170.             end)
  171.             button:SetScript("OnLeave",function()
  172.                 GameTooltip:Hide()
  173.             end)
  174.  
  175.             button:SetWidth(self.ListScrollFrame.scrollChild:GetWidth());
  176.             button:Show();
  177.         else
  178.             button:Hide();
  179.         end
  180.     end
  181.  
  182.     local buttonHeight = self.ListScrollFrame.buttonHeight;
  183.     local totalHeight = #items * buttonHeight;
  184.     local shownHeight = #buttons * buttonHeight;
  185.  
  186.     HybridScrollFrame_Update(self.ListScrollFrame, totalHeight, shownHeight);
  187. end
  188.  
  189. _G["SLASH_GlyphList1"] = "/gl"
  190. SlashCmdList["GlyphList"] = function(msg)
  191.     GlyphListFrame:SetShown(not GlyphListFrame:IsShown())
  192. end
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement