View Single Post
04-25-17, 01:35 PM   #22
syncrow
A Flamescale Wyrmkin
 
syncrow's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 149
ok here is the code: (everything is tested, and should be properly debugged)

Lua Code:
  1. local buttonSize = 36;
  2. local padding = 2;
  3. local perRow = 6;
  4.  
  5. local function CreateButton(parent)
  6.     local button = CreateFrame("Frame", nil, parent)
  7.     local index = #parent.beacons + 1 or 1
  8.    
  9.     button:SetSize(buttonSize, buttonSize);
  10.     button.access = {}
  11.    
  12.     button.slot = CreateFrame("Button", nil, button, "ContainerFrameItemButtonTemplate")
  13.     button.slot:SetSize(buttonSize, buttonSize)
  14.     button.slot:SetPoint("CENTER")
  15.     button.slot:SetScript("OnEnter", function()
  16.         local bag = button:GetID()
  17.         local slot = button.slot:GetID()
  18.        
  19.         GameTooltip:SetOwner(button, "ANCHOR_CURSOR")
  20.         GameTooltip:SetBagItem(bag, slot);
  21.         GameTooltip:Show()
  22.     end)
  23.     button.slot:SetScript("OnLeave", GameTooltip_Hide)
  24.     button.slot:Show()
  25.    
  26.     button.slot.icon:SetTexCoord(0.075, 0.925, 0.075, 0.925);
  27.     button.slot.BattlepayItemTexture:Hide()
  28.    
  29.     return button
  30. end
  31.  
  32. local function UpdateButton(button)
  33.  
  34.     -- no access found: reset button
  35.     if #button.access == 0 then
  36.         button:Hide()
  37.         button.slot.Count:SetText("")
  38.         button.slot:SetNormalTexture("")
  39.         return
  40.     end
  41.  
  42.     local totalCount = 0;
  43.     local texture, bagID, slotID;
  44.    
  45.     for info, values in pairs(button.access) do
  46.         local bag, slot = unpack(values)
  47.         local icon, count = GetContainerItemInfo(bag, slot)
  48.        
  49.         if count then
  50.             totalCount = totalCount + count
  51.         end
  52.        
  53.         -- get the first possible identification for access later
  54.         if not bagID or not slotID or not tex then
  55.             texture = icon
  56.             bagID = bag
  57.             slotID = slot
  58.         end
  59.     end
  60.    
  61.     -- we do not need to show a count there is only 1 item existing
  62.     if totalCount == 1 then
  63.         totalCount = ""
  64.     end
  65.  
  66.     -- update count & texture
  67.     button.slot.Count:SetText(totalCount)
  68.     button.slot.icon:SetTexture(texture)
  69.        
  70.     -- update access
  71.     button:SetID(bagID)
  72.     button.slot:SetID(slotID)
  73.     button:Show()
  74. end
  75.  
  76. local function ResetButtons(self)
  77.     local table = self.beacons
  78.    
  79.     for _, button in pairs(table) do
  80.         button:Hide();
  81.         button.access = {}
  82.     end
  83. end
  84.  
  85. local function CacheAccess(self)
  86.     local table = self.beacons
  87.    
  88.     for bag = 0, 4 do
  89.         for slot = 1, GetContainerNumSlots(bag) do
  90.             local itemID = GetContainerItemID(bag, slot)
  91.            
  92.             if itemID then
  93.                 local name = GetItemInfo(itemID)
  94.                
  95.                 -- beacon found!!!
  96.                 if name and name:find("Sentinax Beacon") then
  97.                     local button = table[name]
  98.                    
  99.                     -- no button found: create it
  100.                     if not button then
  101.                         button = CreateButton(self)
  102.                         table[name] = button
  103.                     end
  104.                    
  105.                     -- store access
  106.                     if button then
  107.                         tinsert(button.access, {bag, slot})
  108.                     end
  109.                 end
  110.             end
  111.         end
  112.     end
  113. end
  114.  
  115. local function UpdateAllButtons(self)
  116.     local table = self.beacons
  117.     local index = 1;
  118.    
  119.     for _, button in pairs(table) do
  120.         if button then
  121.             UpdateButton(button)
  122.            
  123.             if button:IsShown() then
  124.                 local row = math.ceil( index / perRow)
  125.                 local col = (index-1) % perRow + 1
  126.                 local x = (buttonSize * (col-1) ) + ( padding * (col-1) )
  127.                 local y = (buttonSize * (row-1) ) + ( padding * (row-1) )
  128.                
  129.                 button:SetPoint("TOPLEFT", self, 0 + x, 0 - y)
  130.                
  131.                 index = index + 1
  132.             end
  133.         end
  134.     end
  135. end
  136.  
  137. local function UpdateFrameSize(self)
  138.     local table = self.beacons
  139.     local width, height, numRows, numCols
  140.     local shownBtns = 0
  141.    
  142.     for _, button in pairs(table) do
  143.         if button:IsShown() then
  144.             shownBtns = shownBtns + 1
  145.         end
  146.     end
  147.    
  148.     numRows = math.ceil( shownBtns / perRow )
  149.     numCols = (shownBtns < perRow) and shownBtns or perRow
  150.     width = (buttonSize * numCols) + (padding * (numCols - 1) )
  151.     height = numRows * buttonSize + (padding * (numRows - 1) )
  152.    
  153.     self:SetSize(width, height)
  154. end
  155.  
  156. local frame = CreateFrame("Frame", "BeamMeUpDejaInitFrame", UIParent)
  157.  
  158. frame.beacons = {}
  159. frame:SetPoint("CENTER");
  160. frame:RegisterEvent("PLAYER_LOGIN")
  161. frame:SetScript("OnEvent", function(self, event, ...)
  162.     if event == "PLAYER_LOGIN" then
  163.         self:RegisterEvent("BAG_UPDATE")
  164.        
  165.         -- pre call the event for initialization
  166.         ResetButtons(self)
  167.         CacheAccess(self)
  168.         UpdateAllButtons(self)
  169.         UpdateFrameSize(self)
  170.        
  171.         return
  172.     end
  173.  
  174.     ResetButtons(self)
  175.     CacheAccess(self)
  176.     UpdateAllButtons(self)
  177.     UpdateFrameSize(self)
  178. end)

Note:
- Buttons are ordered from left to right, and from top to bottom
(can be changed on line 129 - switch math operators for x / y)
__________________

Last edited by syncrow : 04-25-17 at 01:40 PM.
  Reply With Quote