annotate Export.lua @ 61:cf2b6b9a8337 v23

6.2 update, shopping list bug fixes, ui scale option
author yellowfive
date Tue, 23 Jun 2015 00:27:21 -0700
parents 01b63b8ed811
children 69db1c3025ac
rev   line source
yellowfive@57 1 local Amr = LibStub("AceAddon-3.0"):GetAddon("AskMrRobot")
yellowfive@57 2 local L = LibStub("AceLocale-3.0"):GetLocale("AskMrRobot", true)
yellowfive@57 3 local AceGUI = LibStub("AceGUI-3.0")
yellowfive@57 4
yellowfive@57 5 local _lastExport = nil
yellowfive@57 6 local _txt = nil
yellowfive@57 7
yellowfive@57 8 local function createLabel(container, text, width)
yellowfive@57 9 local lbl = AceGUI:Create("AmrUiLabel")
yellowfive@57 10 lbl:SetWidth(width or 800)
yellowfive@57 11 lbl:SetText(text)
yellowfive@57 12 lbl:SetFont(Amr.CreateFont("Regular", 14, Amr.Colors.Text))
yellowfive@57 13 container:AddChild(lbl)
yellowfive@57 14 return lbl
yellowfive@57 15 end
yellowfive@57 16
yellowfive@57 17 local function onSplashClose()
yellowfive@57 18 Amr:HideCover()
yellowfive@57 19 Amr.db.char.FirstUse = false
yellowfive@57 20 end
yellowfive@57 21
yellowfive@57 22 -- render a splash screen with first-time help
yellowfive@57 23 local function renderSplash(container)
yellowfive@57 24 local panel = Amr:RenderCoverChrome(container, 700, 450)
yellowfive@57 25
yellowfive@57 26 local lbl = createLabel(panel, L.ExportSplashTitle, 650)
yellowfive@57 27 lbl:SetJustifyH("CENTER")
yellowfive@57 28 lbl:SetFont(Amr.CreateFont("Bold", 24, Amr.Colors.TextHeaderActive))
yellowfive@57 29 lbl:SetPoint("TOP", panel.content, "TOP", 0, -10)
yellowfive@57 30
yellowfive@57 31 local lbl2 = createLabel(panel, L.ExportSplashSubtitle, 650)
yellowfive@57 32 lbl2:SetJustifyH("CENTER")
yellowfive@57 33 lbl2:SetFont(Amr.CreateFont("Bold", 18, Amr.Colors.TextTan))
yellowfive@57 34 lbl2:SetPoint("TOP", lbl.frame, "BOTTOM", 0, -20)
yellowfive@57 35
yellowfive@57 36 lbl = createLabel(panel, L.ExportSplash1, 650)
yellowfive@57 37 lbl:SetFont(Amr.CreateFont("Regular", 14, Amr.Colors.Text))
yellowfive@57 38 lbl:SetPoint("TOPLEFT", lbl2.frame, "BOTTOMLEFT", 0, -70)
yellowfive@57 39
yellowfive@57 40 lbl2 = createLabel(panel, L.ExportSplash2, 650)
yellowfive@57 41 lbl2:SetFont(Amr.CreateFont("Regular", 14, Amr.Colors.Text))
yellowfive@57 42 lbl2:SetPoint("TOPLEFT", lbl.frame, "BOTTOMLEFT", 0, -15)
yellowfive@57 43
yellowfive@57 44 lbl = createLabel(panel, L.ExportSplash3, 650)
yellowfive@57 45 lbl:SetFont(Amr.CreateFont("Regular", 14, Amr.Colors.Text))
yellowfive@57 46 lbl:SetPoint("TOPLEFT", lbl2.frame, "BOTTOMLEFT", 0, -15)
yellowfive@57 47
yellowfive@57 48 local btn = AceGUI:Create("AmrUiButton")
yellowfive@57 49 btn:SetText(L.ExportSplashClose)
yellowfive@57 50 btn:SetBackgroundColor(Amr.Colors.Green)
yellowfive@57 51 btn:SetFont(Amr.CreateFont("Bold", 16, Amr.Colors.White))
yellowfive@57 52 btn:SetWidth(120)
yellowfive@57 53 btn:SetHeight(28)
yellowfive@57 54 btn:SetPoint("BOTTOM", panel.content, "BOTTOM", 0, 20)
yellowfive@57 55 btn:SetCallback("OnClick", onSplashClose)
yellowfive@57 56 panel:AddChild(btn)
yellowfive@57 57 end
yellowfive@57 58
yellowfive@57 59 -- renders the main UI for the Export tab
yellowfive@57 60 function Amr:RenderTabExport(container)
yellowfive@57 61
yellowfive@57 62 local lbl = createLabel(container, L.ExportTitle)
yellowfive@57 63 lbl:SetFont(Amr.CreateFont("Bold", 24, Amr.Colors.TextHeaderActive))
yellowfive@57 64 lbl:SetPoint("TOPLEFT", container.content, "TOPLEFT", 0, -40)
yellowfive@57 65
yellowfive@57 66 local lbl2 = createLabel(container, L.ExportHelp1)
yellowfive@57 67 lbl2:SetPoint("TOPLEFT", lbl.frame, "BOTTOMLEFT", 0, -10)
yellowfive@57 68
yellowfive@57 69 lbl = createLabel(container, L.ExportHelp2)
yellowfive@57 70 lbl:SetPoint("TOPLEFT", lbl2.frame, "BOTTOMLEFT", 0, -10)
yellowfive@57 71
yellowfive@57 72 lbl2 = createLabel(container, L.ExportHelp3)
yellowfive@57 73 lbl2:SetPoint("TOPLEFT", lbl.frame, "BOTTOMLEFT", 0, -10)
yellowfive@57 74
yellowfive@57 75 lbl = createLabel(container, L.ExportHelp4)
yellowfive@57 76 lbl:SetPoint("TOPLEFT", lbl2.frame, "BOTTOMLEFT", 0, -10)
yellowfive@57 77
yellowfive@57 78 _txt = AceGUI:Create("AmrUiTextarea")
yellowfive@57 79 _txt:SetWidth(800)
yellowfive@57 80 _txt:SetHeight(300)
yellowfive@57 81 _txt:SetPoint("TOP", lbl.frame, "BOTTOM", 0, -20)
yellowfive@57 82 _txt:SetFont(Amr.CreateFont("Regular", 12, Amr.Colors.Text))
yellowfive@57 83 container:AddChild(_txt)
yellowfive@57 84
yellowfive@57 85 local data = self:ExportCharacter()
yellowfive@57 86 local txt = Amr.Serializer:SerializePlayerData(data, true)
yellowfive@57 87 _txt:SetText(txt)
yellowfive@57 88 _txt:SetFocus(true)
yellowfive@57 89
yellowfive@57 90 -- update shopping list data
yellowfive@57 91 Amr:UpdateShoppingData(data)
yellowfive@57 92
yellowfive@57 93 -- show help splash if first time a user is using this
yellowfive@57 94 if Amr.db.char.FirstUse then
yellowfive@57 95 Amr:ShowCover(renderSplash)
yellowfive@57 96 AceGUI:ClearFocus()
yellowfive@57 97 end
yellowfive@57 98 end
yellowfive@57 99
yellowfive@57 100 function Amr:ReleaseTabExport()
yellowfive@57 101 end
yellowfive@57 102
yellowfive@57 103 function Amr:GetExportText()
yellowfive@57 104 return _txt:GetText()
yellowfive@57 105 end
yellowfive@57 106
yellowfive@57 107
yellowfive@57 108 -- use some local variables to deal with the fact that a user can close the bank before a scan completes
yellowfive@57 109 local _lastBankBagId = nil
yellowfive@57 110 local _lastBankSlotId = nil
yellowfive@57 111
yellowfive@57 112 local function scanBag(bagId, isBank, bagTable, bagItemsWithCount)
yellowfive@57 113 local numSlots = GetContainerNumSlots(bagId)
yellowfive@57 114 for slotId = 1, numSlots do
yellowfive@57 115 local _, itemCount, _, _, _, _, itemLink = GetContainerItemInfo(bagId, slotId)
yellowfive@57 116 if itemLink ~= nil then
yellowfive@57 117 local itemData = Amr.Serializer.ParseItemLink(itemLink)
yellowfive@57 118 if itemData ~= nil then
yellowfive@57 119 -- only add equippable items to bag data
yellowfive@57 120 if IsEquippableItem(itemLink) or Amr.SetTokenIds[itemData.id] then
yellowfive@57 121 if isBank then
yellowfive@57 122 _lastBankBagId = bagId
yellowfive@57 123 _lastBankSlotId = slotId
yellowfive@57 124 end
yellowfive@57 125
yellowfive@57 126 table.insert(bagTable, itemLink)
yellowfive@57 127 end
yellowfive@57 128
yellowfive@57 129 -- all items and counts, used for e.g. shopping list and reagents, etc.
yellowfive@57 130 if bagItemsWithCount then
yellowfive@57 131 if bagItemsWithCount[itemData.id] then
yellowfive@57 132 bagItemsWithCount[itemData.id] = bagItemsWithCount[itemData.id] + itemCount
yellowfive@57 133 else
yellowfive@57 134 bagItemsWithCount[itemData.id] = itemCount
yellowfive@57 135 end
yellowfive@57 136 end
yellowfive@57 137 end
yellowfive@57 138 end
yellowfive@57 139 end
yellowfive@57 140 end
yellowfive@57 141
yellowfive@57 142 -- get the player's current gear and save it, also returns the data from GetPlayerData for efficiency
yellowfive@57 143 local function getEquipped()
yellowfive@57 144 local data = Amr.Serializer:GetPlayerData(Amr.db.char.SubSpecs)
yellowfive@57 145 local spec = GetActiveSpecGroup()
yellowfive@57 146
yellowfive@57 147 Amr.db.char.Equipped[spec] = data.Equipped[spec]
yellowfive@57 148 Amr.db.char.SubSpecs[spec] = data.SubSpecs[spec]
yellowfive@57 149
yellowfive@57 150 return data
yellowfive@57 151 end
yellowfive@57 152
yellowfive@57 153 local function scanBags()
yellowfive@57 154
yellowfive@57 155 local bagItems = {}
yellowfive@57 156 local itemsAndCounts = {}
yellowfive@57 157
yellowfive@57 158 scanBag(BACKPACK_CONTAINER, false, bagItems, itemsAndCounts) -- backpack
yellowfive@57 159 for bagId = 1, NUM_BAG_SLOTS do
yellowfive@57 160 scanBag(bagId, false, bagItems, itemsAndCounts)
yellowfive@57 161 end
yellowfive@57 162
yellowfive@57 163 Amr.db.char.BagItems = bagItems
yellowfive@57 164 Amr.db.char.BagItemsAndCounts = itemsAndCounts
yellowfive@57 165 end
yellowfive@57 166
yellowfive@57 167 -- scan the player's bank and save the contents, must be at the bank
yellowfive@57 168 local function scanBank()
yellowfive@57 169
yellowfive@57 170 local bankItems = {}
yellowfive@57 171 local itemsAndCounts = {}
yellowfive@57 172
yellowfive@57 173 scanBag(BANK_CONTAINER, true, bankItems, itemsAndCounts)
yellowfive@57 174 scanBag(REAGENTBANK_CONTAINER, true, bankItems, itemsAndCounts)
yellowfive@57 175 for bagId = NUM_BAG_SLOTS + 1, NUM_BAG_SLOTS + NUM_BANKBAGSLOTS do
yellowfive@57 176 scanBag(bagId, true, bankItems, itemsAndCounts)
yellowfive@57 177 end
yellowfive@57 178
yellowfive@57 179 -- see if the scan completed before the window closed, otherwise we don't overwrite with partial data
yellowfive@57 180 if _lastBankBagId ~= nil then
yellowfive@57 181 local itemLink = GetContainerItemLink(_lastBankBagId, _lastBankSlotId)
yellowfive@57 182 if itemLink ~= nil then --still open
yellowfive@57 183 Amr.db.char.BankItems = bankItems
yellowfive@57 184 Amr.db.char.BankItemsAndCounts = itemsAndCounts
yellowfive@57 185 end
yellowfive@57 186 end
yellowfive@57 187
yellowfive@57 188 end
yellowfive@57 189
yellowfive@57 190 -- scan the player's void storage and save the contents, must be at void storage
yellowfive@57 191 local function scanVoid()
yellowfive@57 192
yellowfive@57 193 if IsVoidStorageReady() then
yellowfive@57 194 local voidItems = {}
yellowfive@57 195 local VOID_STORAGE_MAX = 80
yellowfive@57 196 local VOID_STORAGE_PAGES = 2
yellowfive@57 197
yellowfive@57 198 for page = 1,VOID_STORAGE_PAGES do
yellowfive@57 199 for i = 1,VOID_STORAGE_MAX do
yellowfive@57 200 local itemId = GetVoidItemInfo(page, i)
yellowfive@57 201 if itemId then
yellowfive@57 202 local itemLink = GetVoidItemHyperlinkString(((page - 1) * VOID_STORAGE_MAX) + i);
yellowfive@57 203 if itemLink then
yellowfive@57 204 tinsert(voidItems, itemLink)
yellowfive@57 205 end
yellowfive@57 206 end
yellowfive@57 207 end
yellowfive@57 208 end
yellowfive@57 209
yellowfive@57 210 Amr.db.char.VoidItems = voidItems
yellowfive@57 211 end
yellowfive@57 212
yellowfive@57 213 end
yellowfive@57 214
yellowfive@57 215 local function getRepStanding(factionId)
yellowfive@57 216 local name, description, standingId, _ = GetFactionInfoByID(factionId)
yellowfive@57 217 return standingId - 1; -- our rep enum correspond to what the armory returns, are 1 less than what the game returns
yellowfive@57 218 end
yellowfive@57 219
yellowfive@57 220 local function getReputations()
yellowfive@57 221 local reps = {}
yellowfive@57 222
yellowfive@57 223 local repList = {1375,1376,1270,1269,1341,1337,1387,1388,1435}
yellowfive@57 224 for i, repId in pairs(repList) do
yellowfive@57 225 local standing = getRepStanding(repId)
yellowfive@57 226 if standing >= 0 then
yellowfive@57 227 reps[repId] = standing
yellowfive@57 228 end
yellowfive@57 229 end
yellowfive@57 230
yellowfive@57 231 return reps
yellowfive@57 232 end
yellowfive@57 233
yellowfive@57 234 -- Returns a data object containing all information about the current player needed for an export:
yellowfive@57 235 -- gear, spec, reputations, bag, bank, and void storage items.
yellowfive@57 236 function Amr:ExportCharacter()
yellowfive@57 237
yellowfive@57 238 local data = getEquipped()
yellowfive@57 239 scanBags()
yellowfive@57 240
yellowfive@57 241 -- get extra data that is not necessary for the base serializer, but that we add here for completeness
yellowfive@57 242 data.Equipped = Amr.db.char.Equipped
yellowfive@57 243 data.Reputations = getReputations()
yellowfive@57 244 data.BagItems = Amr.db.char.BagItems
yellowfive@57 245 data.BankItems = Amr.db.char.BankItems
yellowfive@57 246 data.VoidItems = Amr.db.char.VoidItems
yellowfive@57 247
yellowfive@57 248 return data
yellowfive@57 249 end
yellowfive@57 250
yellowfive@57 251 function Amr:InitializeExport()
yellowfive@57 252 Amr:AddEventHandler("UNIT_INVENTORY_CHANGED", function(unitID)
yellowfive@57 253 if unitID and unitID ~= "player" then return end
yellowfive@57 254 getEquipped()
yellowfive@57 255 end)
yellowfive@57 256 end
yellowfive@57 257
yellowfive@57 258 Amr:AddEventHandler("BANKFRAME_OPENED", scanBank)
yellowfive@57 259 Amr:AddEventHandler("PLAYERBANKSLOTS_CHANGED", scanBank)
yellowfive@57 260
yellowfive@57 261 Amr:AddEventHandler("VOID_STORAGE_OPEN", scanVoid)
yellowfive@57 262 Amr:AddEventHandler("VOID_STORAGE_CONTENTS_UPDATE", scanVoid)
yellowfive@57 263 Amr:AddEventHandler("VOID_STORAGE_DEPOSIT_UPDATE", scanVoid)
yellowfive@57 264 Amr:AddEventHandler("VOID_STORAGE_UPDATE", scanVoid)