local buttonSize = 36;
local padding = 2;
local perRow = 6;
local function CreateButton(parent)
local button = CreateFrame("Frame", nil, parent)
local index = #parent.beacons + 1 or 1
button:SetSize(buttonSize, buttonSize);
button.access = {}
button.slot = CreateFrame("Button", nil, button, "ContainerFrameItemButtonTemplate")
button.slot:SetSize(buttonSize, buttonSize)
button.slot:SetPoint("CENTER")
button.slot:SetScript("OnEnter", function()
local bag = button:GetID()
local slot = button.slot:GetID()
GameTooltip:SetOwner(button, "ANCHOR_CURSOR")
GameTooltip:SetBagItem(bag, slot);
GameTooltip:Show()
end)
button.slot:SetScript("OnLeave", GameTooltip_Hide)
button.slot:Show()
button.slot.icon:SetTexCoord(0.075, 0.925, 0.075, 0.925);
button.slot.BattlepayItemTexture:Hide()
return button
end
local function UpdateButton(button)
-- no access found: reset button
if #button.access == 0 then
button:Hide()
button.slot.Count:SetText("")
button.slot:SetNormalTexture("")
return
end
local totalCount = 0;
local texture, bagID, slotID;
for info, values in pairs(button.access) do
local bag, slot = unpack(values)
local icon, count = GetContainerItemInfo(bag, slot)
if count then
totalCount = totalCount + count
end
-- get the first possible identification for access later
if not bagID or not slotID or not tex then
texture = icon
bagID = bag
slotID = slot
end
end
-- we do not need to show a count there is only 1 item existing
if totalCount == 1 then
totalCount = ""
end
-- update count & texture
button.slot.Count:SetText(totalCount)
button.slot.icon:SetTexture(texture)
-- update access
button:SetID(bagID)
button.slot:SetID(slotID)
button:Show()
end
local function ResetButtons(self)
local table = self.beacons
for _, button in pairs(table) do
button:Hide();
button.access = {}
end
end
local function CacheAccess(self)
local table = self.beacons
for bag = 0, 4 do
for slot = 1, GetContainerNumSlots(bag) do
local itemID = GetContainerItemID(bag, slot)
if itemID then
local name = GetItemInfo(itemID)
-- beacon found!!!
if name and name:find("Sentinax Beacon") then
local button = table[name]
-- no button found: create it
if not button then
button = CreateButton(self)
table[name] = button
end
-- store access
if button then
tinsert(button.access, {bag, slot})
end
end
end
end
end
end
local function UpdateAllButtons(self)
local table = self.beacons
local index = 1;
for _, button in pairs(table) do
if button then
UpdateButton(button)
if button:IsShown() then
local row = math.ceil( index / perRow)
local col = (index-1) % perRow + 1
local x = (buttonSize * (col-1) ) + ( padding * (col-1) )
local y = (buttonSize * (row-1) ) + ( padding * (row-1) )
button:SetPoint("TOPLEFT", self, 0 + x, 0 - y)
index = index + 1
end
end
end
end
local function UpdateFrameSize(self)
local table = self.beacons
local width, height, numRows, numCols
local shownBtns = 0
for _, button in pairs(table) do
if button:IsShown() then
shownBtns = shownBtns + 1
end
end
numRows = math.ceil( shownBtns / perRow )
numCols = (shownBtns < perRow) and shownBtns or perRow
width = (buttonSize * numCols) + (padding * (numCols - 1) )
height = numRows * buttonSize + (padding * (numRows - 1) )
self:SetSize(width, height)
end
local frame = CreateFrame("Frame", "BeamMeUpDejaInitFrame", UIParent)
frame.beacons = {}
frame:SetPoint("CENTER");
frame:RegisterEvent("PLAYER_LOGIN")
frame:SetScript("OnEvent", function(self, event, ...)
if event == "PLAYER_LOGIN" then
self:RegisterEvent("BAG_UPDATE")
-- pre call the event for initialization
ResetButtons(self)
CacheAccess(self)
UpdateAllButtons(self)
UpdateFrameSize(self)
return
end
ResetButtons(self)
CacheAccess(self)
UpdateAllButtons(self)
UpdateFrameSize(self)
end)