View Single Post
04-24-17, 02:02 PM   #3
syncrow
A Flamescale Wyrmkin
 
syncrow's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 149
You're code re-creates your buttons constantly, which would cause massive error junk when you loot beacons right in combat....

It would be much better to pre-create all necessary buttons and only handle the count text upon bag updates.

Here is a little example on how it would look like:
Lua Code:
  1. local buttonSize = 36
  2. local padding = 0;
  3. local beacons = {
  4.     -- rares
  5.     [146923] = ""-- petrification
  6.     [147355] = ""-- bloodstrike
  7.     [146922] = ""-- fel growth
  8.     [146915] = ""-- greater Torment
  9.     [146912] = ""-- greater carnage
  10.     [146914] = ""-- greater engineering
  11.     [146910] = ""-- greater dominance
  12.     [146913] = ""-- greater warbeasts
  13.     [146911] = "",  -- greater firestorm
  14.    
  15.     -- uncommons
  16.     [146906] = "",  -- carnage
  17.     [146907] = "",  -- warbeasts
  18.     [146909] = "",  -- torment
  19.     [146908] = "",  -- engineering
  20.     [146903] = "",  -- dominance
  21.     [146905] = ""-- firestorm
  22. }
  23.  
  24. local function CreateButtons()
  25.     local index = 1;
  26.    
  27.     for itemID in pairs(beacons) do
  28.         local itemName, itemLink = GetItemInfo(itemID)
  29.        
  30.         if itemName then
  31.             local texture = select(10, GetItemInfo(itemID));
  32.             local button = CreateFrame("Button", "BMUDButton"..index, UIParent, "SecureActionButtonTemplate")
  33.             local xPos = -300 + ( (buttonSize + padding) * (index-1))
  34.            
  35.             button:SetSize(buttonSize, buttonSize)
  36.             button:SetPoint("CENTER", xPos, 0)
  37.             button:RegisterForClicks("AnyUp", "AnyDown")
  38.             button:SetAttribute("type", "item")
  39.             button:SetAttribute("item", itemName)
  40.  
  41.             button.fs = button:CreateFontString(nil, "OVERLAY", "GameTooltipText")
  42.             button.fs:SetPoint("BOTTOMRIGHT")
  43.             button.fs:SetFont("Fonts\\FRIZQT__.TTF", 14, "THINOUTLINE")
  44.             button.fs:SetTextColor(1, 1, 1)
  45.  
  46.             button:SetNormalTexture(texture)
  47.             button:SetPushedTexture(nil,"ARTWORK")
  48.             button:SetHighlightTexture(nil,"ARTWORK")
  49.            
  50.             button:HookScript("OnEnter", function(self)
  51.                 GameTooltip:SetOwner(self, "ANCHOR_CURSOR")
  52.                 GameTooltip:SetHyperlink(itemLink)
  53.                 GameTooltip:Show()
  54.             end)
  55.             button:HookScript("OnLeave", GameTooltip_Hide)
  56.            
  57.             -- overwrite empty string with the button as a reference here
  58.             beacons[itemID] = button;
  59.         end
  60.        
  61.         index = index + 1;
  62.     end    
  63. end
  64.  
  65. local function UpdateCount()
  66.     for itemID, button in pairs(beacons) do
  67.         local itemName = GetItemInfo(itemID);
  68.         local count = 0;
  69.        
  70.         for bag = 0, 4 do
  71.             for slot = 1, GetContainerNumSlots(bag) do
  72.                 local item = GetContainerItemID(bag, slot);
  73.                
  74.                 -- found a beacon in your bag
  75.                 if itemID == item then
  76.                     count = count + 1
  77.                 end
  78.             end
  79.         end
  80.        
  81.         -- handle display
  82.         if count > 0 then
  83.             button.fs:SetText(count);
  84.             button:GetNormalTexture():SetDesaturated(false);
  85.         else
  86.             button.fs:SetText("");
  87.             button:GetNormalTexture():SetDesaturated(true);
  88.         end
  89.     end
  90. end
  91.  
  92. local frame = CreateFrame("Frame", "BeamMeUpDejaInitFrame", BeamMeUpDejaDragFrame)
  93.  
  94. frame:RegisterEvent("PLAYER_LOGIN")
  95. frame:SetScript("OnEvent", function(self, event, ...)
  96.     if event == "PLAYER_LOGIN" then
  97.         CreateButtons()
  98.         UpdateCount()
  99.         self:RegisterEvent("BAG_UPDATE")
  100.     end
  101.    
  102.     if event == "BAG_UPDATE" then
  103.         UpdateCount()
  104.     end
  105. end)

The buttons in this example are static and do not hide / show themselves properly, but this is further secure wrapping....

PS: beacons are not stackable, as far as i'm aware
__________________

Last edited by syncrow : 04-24-17 at 02:16 PM.
  Reply With Quote