annotate Modules/Currency.lua @ 130:67b90544a7b7

Added tag v7.3.0-20171022 for changeset 9f2cf5609420
author Nenue
date Sun, 22 Oct 2017 18:29:09 -0400
parents 799ec6dce9c3
children 15a7f27b11e6
rev   line source
Nick@113 1 --
Nick@113 2 -- Created by IntelliJ IDEA.
Nick@113 3 -- User: Nick
Nick@113 4 -- Date: 3/25/2017
Nick@113 5 -- Time: 7:07 PM
Nick@113 6 -- To change this template use File | Settings | File Templates.
Nick@113 7 --
Nick@113 8
Nenue@125 9 local print = DEVIAN_WORKSPACE and function(...) print('Currency', ...) end or nop
Nenue@115 10 local profileUpdate, needsUpdate
Nick@113 11
Nenue@123 12
Nenue@123 13 local itemBlock = {}
Nenue@123 14 local currencyBlock = {}
Nenue@123 15
Nenue@123 16
Nick@113 17 local zoneEvents = {
Nick@113 18 "ZONE_CHANGED_NEW_AREA", "ZONE_CHANGED_INDOORS", "ZONE_CHANGED"
Nick@113 19 }
Nick@113 20 local currencyEvents = {
Nick@113 21 'CURRENCY_DISPLAY_UPDATE', 'CHAT_MSG_CURRENCY'
Nick@113 22 }
Nick@113 23 local itemEvents = {
Nick@113 24 'CHAT_MSG_LOOT', 'BAG_UPDATE'
Nick@113 25 }
Nick@113 26
Nick@113 27 local blocks = {
Nick@113 28 ["Ancient Mana"] = {
Nick@113 29 currencyID = 1155,
Nenue@116 30 showMax = true,
Nick@113 31 zones = {
Nick@113 32 ['Suramar'] = true,
Nenue@116 33 },
Nick@113 34 },
Nick@113 35 ["Blood of Sargeras"] = {
Nick@113 36 itemID = 124124,
Nenue@116 37 },
Nenue@116 38 ["Legionfall War Supplies"] = {
Nenue@116 39 currencyID = 1342,
Nenue@125 40 zones = {
Nenue@128 41 ["Broken Shore"] = true
Nenue@125 42 }
Nenue@116 43 },
Nenue@116 44 ["Nethershard"] = {
Nenue@116 45 currencyID = 1226,
Nenue@125 46 zones = {
Nenue@128 47 ["Broken Shore"] = true
Nenue@125 48 }
Nenue@123 49 },
Nenue@123 50 ["Veiled Argunite"] = {
Nenue@123 51 currencyID = 1508,
Nenue@125 52 zones = {
Nenue@125 53 ["Antoran Wastes"] = true,
Nenue@125 54 ["Krokuun"] = true,
Nenue@125 55 ["MacAree"] = true,
Nenue@125 56 }
Nenue@123 57 },
Nick@113 58 }
Nenue@115 59 local items, currencies = {}, {}
Nick@113 60
Nick@113 61
Nenue@115 62 VeneerCurrencyMixin = {
Nenue@115 63 Blocks = {},
Nenue@115 64 HideCombat = true,
Nenue@115 65 EventList = {'PLAYER_ENTERING_WORLD'},
Nenue@115 66 moduleName = 'Currency Watch',
Nenue@115 67 anchorPoint = 'TOP',
Nenue@116 68 anchorPriority = 2,
Nenue@115 69 }
Nenue@115 70 VeneerCurrencyBlockMixin = {}
Nenue@115 71 local module = VeneerCurrencyMixin
Nenue@115 72 local block = VeneerCurrencyBlockMixin
Nick@113 73
Nick@113 74
Nick@113 75
Nick@113 76 local function RegisterEvents (frame, events)
Nick@113 77 for _, event in ipairs(events) do
Nick@113 78 print('|cFFFF0088'..(frame.name or frame:GetName())..'|r', 'listening to', event)
Nick@113 79 frame:RegisterEvent(event)
Nick@113 80 end
Nick@113 81 end
Nick@113 82
Nenue@125 83 function module:Setup()
Nenue@128 84 self:Reanchor()
Nenue@125 85 end
Nenue@125 86
Nick@113 87 function module:OnLoad ()
Nenue@115 88 Veneer:AddHandler(self)
Nick@113 89
Nick@113 90 for name, info in pairs(blocks) do
Nenue@125 91 local block = CreateFrame('Frame', nil, self, 'VeneerCurrencyTemplate')
Nenue@125 92 block.name = name
Nick@113 93 for k,v in pairs(info) do
Nick@113 94 print(name, k, '=', v)
Nenue@125 95 block[k] = v
Nick@113 96 end
Nick@113 97
Nenue@125 98 block.debug = function(...)
Nenue@125 99 print('|cFF0088FF<'..block.name..'>|r', ...)
Nick@113 100 end
Nenue@125 101 block.id = info.itemID or info.currencyID
Nenue@123 102
Nick@113 103
Nick@113 104 if info.itemID then
Nick@113 105 local itemID = info.itemID
Nick@113 106 items[itemID] = {
Nick@113 107 count = 0,
Nenue@125 108 frame = block
Nick@113 109 }
Nenue@125 110 block.Update = itemBlock.Update
Nenue@128 111 RegisterEvents(block, itemEvents)
Nick@113 112 elseif info.currencyID then
Nick@113 113 local currencyID = info.currencyID
Nenue@125 114 block.Update = currencyBlock.Update
Nenue@128 115 RegisterEvents(self, currencyEvents)
Nick@113 116 end
Nenue@123 117
Nenue@123 118
Nenue@123 119
Nick@113 120 if info.zones then
Nenue@128 121 RegisterEvents(self, zoneEvents)
Nick@113 122 local zones = info.zones
Nenue@125 123 local of = block.Update
Nenue@128 124 function block:Update()
Nenue@126 125
Nenue@126 126 local zone = GetRealZoneText()
Nenue@126 127 local subZone = GetSubZoneText()
Nenue@125 128 local canShow = ((zone and self.zones[zone]) or (subZone and self.zones[subZone])) and true or false
Nenue@128 129 block.debug('Zone Check', zone, subZone, canShow)
Nick@113 130 if of then
Nenue@125 131 canShow = canShow and of(self)
Nick@113 132 end
Nick@113 133 return canShow
Nick@113 134 end
Nick@113 135 end
Nick@113 136 end
Nick@113 137 end
Nick@113 138
Nick@113 139 function module:OnEvent (event, arg)
Nick@113 140 print(self:GetName(), 'OnEvent', event, arg)
Nenue@128 141 self:Update()
Nick@113 142 end
Nenue@126 143
Nenue@128 144 function module:Update()
Nenue@128 145 print(self:GetName(), 'Reanchor()')
Nick@113 146 if InCombatLockdown() then
Nick@113 147 self:SetShown(false)
Nick@113 148 return
Nick@113 149 end
Nick@113 150
Nick@113 151
Nick@113 152 for itemID in pairs(items) do
Nick@113 153 items[itemID].count = 0
Nick@113 154 end
Nenue@126 155
Nick@113 156 local canShow = false
Nick@113 157
Nenue@128 158 if BagBrother then
Nenue@128 159 for i, bag in pairs(BagBrother.Player) do
Nenue@128 160 if type(bag) == 'table' then
Nenue@128 161 --print(i)
Nenue@128 162 for j, item in pairs(bag) do
Nenue@128 163 local itemID, count = string.match(item ,"^(%d+):.+;(%d*)")
Nenue@128 164 itemID = tonumber(itemID)
Nenue@128 165 if itemID then
Nenue@128 166 --print(j, itemID)
Nenue@128 167 count = tonumber(count)
Nenue@128 168 if count and count >= 1 then
Nenue@128 169 local texture = GetItemIcon(itemID)
Nenue@128 170 --print(itemID)
Nenue@128 171
Nenue@128 172 if items[itemID] then
Nenue@128 173 items[itemID].count = items[itemID].count + (count or 1)
Nenue@128 174 if not items[itemID].texture then
Nenue@128 175 items[itemID].texture = texture
Nenue@128 176 --print('tracked currency tally', items[itemID].count, '|T'..texture..':16:16|t')
Nenue@128 177 items[itemID].frame.Icon:SetTexture(texture)
Nenue@128 178 end
Nenue@128 179 end
Nenue@128 180 end
Nenue@128 181 end
Nenue@128 182 end
Nenue@128 183 end
Nenue@128 184
Nenue@128 185 end
Nenue@128 186 else
Nenue@128 187 for i = 0, NUM_BAG_SLOTS do
Nenue@128 188 local numSlots = GetContainerNumSlots(i)
Nenue@128 189 for j = 1, numSlots do
Nenue@128 190 local itemID = GetContainerItemID(i, j)
Nenue@128 191 local texture, count = GetContainerItemInfo(i,j)
Nenue@128 192 if items[itemID] then
Nenue@128 193 items[itemID].count = items[itemID].count + (count or 1)
Nenue@128 194 if not items[itemID].texture then
Nenue@128 195 items[itemID].texture = texture
Nenue@128 196 print('tracked currency tally', items[itemID].count, '|T'..texture..':16:16|t')
Nenue@128 197 items[itemID].frame.Icon:SetTexture(texture)
Nenue@128 198 end
Nenue@115 199 end
Nick@113 200 end
Nick@113 201 end
Nick@113 202 end
Nick@113 203
Nenue@115 204
Nick@113 205
Nick@113 206 local lastBlock
Nick@113 207 local totalWidth = 0
Nick@113 208 for _, block in ipairs(self.Blocks) do
Nenue@128 209 local blockIsShown = block:Update()
Nenue@128 210 print(block.name, blockIsShown)
Nick@113 211 block:SetShown(blockIsShown)
Nick@113 212 canShow = canShow or blockIsShown
Nick@113 213
Nick@113 214
Nenue@125 215 if blockIsShown then
Nick@113 216 block:ClearAllPoints()
Nick@113 217 if lastBlock then
Nick@113 218 block:SetPoint('TOPLEFT', lastBlock, 'TOPRIGHT')
Nick@113 219 else
Nick@113 220 block:SetPoint('TOPLEFT', self, 'TOPLEFT')
Nick@113 221 end
Nick@113 222 lastBlock = block
Nick@113 223
Nick@113 224 block:SetHeight(24)
Nenue@123 225 block:SetWidth(block.Icon:GetWidth() + block.Label:GetStringWidth()+4)
Nick@113 226 totalWidth = totalWidth + block:GetWidth()
Nick@113 227 end
Nenue@126 228 print('|cFF0088FF'..block.name..'|r', block:IsShown(), block:GetSize())
Nick@113 229 end
Nick@113 230
Nenue@115 231 self:UpdateProfile()
Nick@113 232 self:SetWidth(totalWidth)
Nick@113 233
Nenue@115 234 needsUpdate = nil
Nick@113 235 print(self:IsShown(), '|cFF00FF88'..self:GetName()..'|r', self:GetSize())
Nick@113 236 self:SetShown(canShow)
Nenue@115 237 Veneer:DynamicReanchor()
Nenue@115 238 end
Nenue@115 239
Nenue@115 240 function module:UpdateProfile()
Nenue@115 241 if not self.profile then
Nenue@115 242 profileUpdate = true
Nenue@115 243 return
Nenue@115 244 end
Nenue@115 245
Nenue@115 246 for itemID, info in pairs(items) do
Nenue@115 247 self.profile.Items = self.profile.Items or {}
Nenue@115 248 self.profile.Items[itemID] = info
Nenue@115 249 end
Nick@113 250 end
Nick@113 251
Nick@113 252 function module:OnUpdate()
Nenue@115 253 if needsUpdate then
Nenue@125 254 self:Update(GetRealZoneText(), GetSubZoneText())
Nenue@115 255 elseif profileUpdate then
Nenue@115 256 self:UpdateProfile()
Nick@113 257 end
Nenue@115 258
Nick@113 259 end
Nick@113 260
Nick@114 261 function block:OnEnter()
Nick@114 262 if not InCombatLockdown() then
Nick@114 263 GameTooltip:SetOwner(self, "ANCHOR_BOTTOMRIGHT");
Nick@114 264 if self.currencyID then
Nick@114 265 GameTooltip:SetCurrencyTokenByID(self.currencyID);
Nick@114 266 else
Nick@114 267 GameTooltip:SetItemByID(self.itemID);
Nick@114 268 end
Nick@114 269 GameTooltip:Show();
Nick@114 270 end
Nick@114 271 end
Nick@114 272 function block:OnLeave()
Nick@114 273 if GameTooltip:IsOwned(self) then
Nick@114 274 GameTooltip_Hide()
Nick@114 275 end
Nick@114 276 end
Nick@114 277
Nick@113 278 function block:OnEvent(event, ...)
Nenue@126 279 self.debug('OnEvent', event, ...)
Nenue@126 280 self:Update()
Nick@113 281 end
Nick@113 282
Nick@113 283 function block:Setup()
Nenue@126 284 end
Nick@113 285
Nenue@126 286 function itemBlock:Update()
Nenue@123 287 if items[self.itemID].count >= 1 then
Nenue@123 288 self.Icon:SetTexture(GetItemIcon(self.id))
Nenue@123 289 self.Label:SetFormattedText("%d", items[self.id].count)
Nenue@123 290 return true
Nenue@123 291 end
Nenue@125 292 return false
Nenue@123 293 end
Nenue@123 294
Nenue@126 295 function currencyBlock:Update()
Nenue@123 296 local name, earned, texture, earnedThisWeek, weeklyMax, totalMax = GetCurrencyInfo(self.id)
Nenue@123 297
Nenue@123 298 self.Icon:SetTexture(texture)
Nenue@123 299 if self.showMax then
Nenue@123 300 self.Label:SetFormattedText("%d / %d", earned, totalMax)
Nenue@123 301 else
Nenue@123 302 self.Label:SetFormattedText("%d", earned)
Nenue@123 303 end
Nenue@123 304 return true
Nenue@123 305 end