WoWInterface SVN Broker_WoDCurrency

[/] [trunk/] [Broker_WoDCurrency/] [Broker_WoDCurrency.lua] - Rev 4

Compare with Previous | Blame | View Log

local f = CreateFrame("Frame")
local strformat = string.format
local tinsert = table.insert
local tremove = table.remove
local tlbsort = table.sort
local db, _
local labelString = "|T%s:0|t %s"
local separatedBankString = "%d (%d | %d)"

local ids = {
        {id = 823, type = "currency"},  --apexis crystal
        {id = 824, type = "currency"},  --garrison resources
        {id = 944, type = "currency"},  --artifact fragment
        {id = 980, type = "currency"},  --dingy iron coins
        {id = 994, type = "currency"},  --seal of tempered fate
        {id = 1101, type = "currency"}, --oil
        {id = 116395, type = "item"},   --comprehensive outpost construction guide
        {id = 116053, type = "item"},   --draenic seeds
        {id = 116394, type = "item"},   --outpost building assembly notes
        {id = 116415, type = "item"},   --pet charm
        {id = 120945, type = "item"},   --primal spirit
        {id = 118700, type = "item"},   --secret of draenor alchemy
        {id = 118720, type = "item"},   --secret of draenor blacksmithing
        {id = 119293, type = "item"},   --secret of draenor enchanting
        {id = 119299, type = "item"},   --secret of draenor engineering
        {id = 119297, type = "item"},   --secret of draenor inscription
        {id = 118723, type = "item"},   --secret of draenor jewelcrafting
        {id = 118721, type = "item"},   --secret of draenor leatherworking
        {id = 118722, type = "item"},   --secret of draenor tailoring
}

f.buttons = {}
local i = 1
local function AddCheckbox(setting, name)
        f.buttons[i] = CreateFrame("CheckButton", nil, f, "UICheckButtonTemplate")
        local box = f.buttons[i]
        if i == 1 then
                box:SetPoint("TOPLEFT", 20, -85)
        elseif i == 2 then
                box:SetPoint("TOPLEFT", f.buttons[1], "TOPLEFT", 0, -80)
        elseif i == 13 then
                box:SetPoint("TOPLEFT", f.buttons[2], "TOPRIGHT", 250, 0)
        else
                box:SetPoint("TOPLEFT", f.buttons[i-1], "TOPLEFT", 0, -30)
        end
        box:SetChecked(WoDCurrencyDBPC[setting])
        box:SetScript("OnClick", function(self)
                        self:SetChecked(not WoDCurrencyDBPC[setting])
                        WoDCurrencyDBPC[setting] = not WoDCurrencyDBPC[setting]
                end)
        --labels
        box.text = box:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
        box.text:SetPoint("LEFT", box, "RIGHT", 5, 0)
        box.text:SetText(name)
        
        i = i+1
        
        return box
end

local function BuildOptions()
        --[[local OnEnter = function(self, title, text)
                        GameTooltip:SetOwner(self, "ANCHOR_TOPRIGHT")
                        GameTooltip:SetText(title)
                        GameTooltip:AddLine(text,1,1,1,true)
                        GameTooltip:Show()
                end]]

        local header = f:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
                header:SetParent(f)
                header:SetPoint("TOPLEFT", 20, -15)
                header:SetText("Broker_WoDCurrency")

        local subheader = f:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
                subheader:SetParent(f)
                subheader:SetPoint("TOPLEFT", 20, -32)
                subheader:SetPoint("RIGHT", -20, -32)
                subheader:SetHeight(30)
                subheader:SetNonSpaceWrap(true)
                subheader:SetJustifyH("LEFT")
                subheader:SetText("An LDB plugin to display currencies for WoD.")

        local box = AddCheckbox("separateBank", "Separate items in bank from total count:   "..TOTAL.." ("..BAGSLOT.." | "..BANK..")")
        
        local selectToTrack = f:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
                selectToTrack:SetParent(f)
                selectToTrack:SetPoint("TOPLEFT", box, "BOTTOMLEFT", 0, -15)
                selectToTrack:SetHeight(30)
                selectToTrack:SetNonSpaceWrap(true)
                selectToTrack:SetJustifyH("LEFT")
                selectToTrack:SetText("Select which currencies and items to track.")
end

local itemsFound = 0
local checkAgain = {}
local function ReQuery()
        for k, currency in pairs(checkAgain) do
                if currency.type == "currency" then
                        currency.name, _, currency.texture = GetCurrencyInfo(currency.id)
                else
                        currency.name, _, _, _, _, _, _, _, _, currency.texture = GetItemInfo(currency.id)
                end
                if currency.name then
                        tremove(checkAgain, k)
                        itemsFound = itemsFound + 1
                end
        end
        
        if itemsFound == #ids then      --if all of our items have been found by the client...
                f:UnregisterEvent("GET_ITEM_INFO_RECEIVED")
                --then, alphabetize based on locale
                tlbsort(ids, function(a, b) return a.name < b.name end)
                --now let's add our checkboxes in alphabetical order
                for _, currency in pairs(ids) do
                        AddCheckbox(currency.id, currency.name)
                end
        end
end

local function Initialize()
        WoDCurrencyDBPC = WoDCurrencyDBPC or {}
        db = WoDCurrencyDBPC
        db.separateBank = db.separateBank or false
        
        for i = 1, #ids do
                local currency = ids[i]
                
                --first, store localized name (and texture, while we're at it)
                if currency.type == "currency" then
                        currency.name, _, currency.texture = GetCurrencyInfo(currency.id)
                else
                        currency.name, _, _, _, _, _, _, _, _, currency.texture = GetItemInfo(currency.id)
                end
                if not currency.name then       --if this item isn't in the client's cache, try again when it's found
                        currency.idsKey = i
                        tinsert(checkAgain, currency)
                        f:RegisterEvent("GET_ITEM_INFO_RECEIVED")
                else
                        itemsFound = itemsFound + 1
                end
                
                --also, while we're looping through, let's double-check our db
                if db[currency.id] == nil then
                        db[currency.id] = true
                end
        end
        
        
        f.name = "Broker_WoDCurrency"
        InterfaceOptions_AddCategory(f)
        BuildOptions()

        if itemsFound == #ids then      --if all of our items have been found by the client...
                f:UnregisterEvent("GET_ITEM_INFO_RECEIVED")
                --then, alphabetize based on locale
                tlbsort(ids, function(a, b) return a.name < b.name end)
                --now let's add our checkboxes in alphabetical order
                for _, currency in pairs(ids) do
                        AddCheckbox(currency.id, currency.name)
                end
        end
end

local function PollCurrencyData(tt)
        for _, currency in pairs(ids) do
                if db[currency.id] then
                        if currency.type == "currency" then
                                local name, amount, texture = GetCurrencyInfo(currency.id)
                                tt:AddDoubleLine(strformat(labelString, texture, name), amount)
                        else
                                local amount = GetItemCount(currency.id, true)
                                if db.separateBank then
                                        local justBags = GetItemCount(currency.id)
                                        local justBank = amount - justBags
                                        tt:AddDoubleLine(strformat(labelString, currency.texture, currency.name), strformat(separatedBankString, amount, justBags, justBank))
                                else
                                        tt:AddDoubleLine(strformat(labelString, currency.texture, currency.name), amount)
                                end
                        end
                end
        end
end

local BWC = LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject("Broker_WoDCurrency", {
        type = "launcher",
        label = "WoD Currency",
        icon = "Interface\\Icons\\INV_Garrison_Resource",
        OnClick = function(self, btn)
                        if btn == "RightButton" then
                                InterfaceOptionsFrame_OpenToCategory("Broker_WoDCurrency")
                                InterfaceOptionsFrame_OpenToCategory("Broker_WoDCurrency")
                        else
                                ToggleCharacter("TokenFrame")
                        end
                end,
})

function BWC.OnTooltipShow(self)
        self:AddLine("Broker_WoDCurrency",1,1,1)
        self:AddLine(" ")
        PollCurrencyData(self)
        self:AddLine(" ")
        self:AddLine("Left-click to toggle the Currency frame.")
        self:AddLine("Right-click to open the WoDCurrency options.")
end

f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:SetScript("OnEvent", function(self, event)
        if event == "GET_ITEM_INFO_RECEIVED" then
                ReQuery()
        else
                C_Timer.After(5, Initialize)
                --Initialize()
                f:UnregisterEvent("PLAYER_ENTERING_WORLD")
        end
end)

Compare with Previous | Blame