annotate Modules/Currency.lua @ 126:3de635cda288 v7.3.17102017

- zone checking fix; moveed code into the zone-context pre-hook
author Nenue
date Tue, 17 Oct 2017 17:00:10 -0400
parents 3f4794dca91b
children 799ec6dce9c3
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@125 41 ["The Broken Shore"] = true
Nenue@125 42 }
Nenue@116 43 },
Nenue@116 44 ["Nethershard"] = {
Nenue@116 45 currencyID = 1226,
Nenue@125 46 zones = {
Nenue@125 47 ["The 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@125 84 self:Update()
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
Nick@113 111 RegisterEvents(self, itemEvents)
Nick@113 112 elseif info.currencyID then
Nick@113 113 local currencyID = info.currencyID
Nenue@125 114 block.Update = currencyBlock.Update
Nenue@125 115 RegisterEvents(block, currencyEvents)
Nick@113 116 end
Nenue@123 117
Nenue@123 118
Nenue@123 119
Nick@113 120 if info.zones then
Nenue@125 121 RegisterEvents(block, zoneEvents)
Nick@113 122 local zones = info.zones
Nenue@125 123 local of = block.Update
Nenue@125 124 function block:Update(zone, subZone)
Nenue@126 125
Nenue@126 126 local zone = GetRealZoneText()
Nenue@126 127 local subZone = GetSubZoneText()
Nenue@126 128 block.debug('Zone Check', zone, subZone)
Nenue@125 129 local canShow = ((zone and self.zones[zone]) or (subZone and self.zones[subZone])) and true or false
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)
Nick@113 141 end
Nenue@126 142
Nick@113 143 function module:Update(isBatchUpdate)
Nick@113 144 print(self:GetName(), 'Update()')
Nick@113 145 if InCombatLockdown() then
Nick@113 146 self:SetShown(false)
Nick@113 147 return
Nick@113 148 end
Nick@113 149
Nick@113 150
Nick@113 151 for itemID in pairs(items) do
Nick@113 152 items[itemID].count = 0
Nick@113 153 end
Nenue@126 154
Nick@113 155 local canShow = false
Nick@113 156
Nick@113 157 for i = 0, NUM_BAG_SLOTS do
Nick@113 158 local numSlots = GetContainerNumSlots(i)
Nick@113 159 for j = 1, numSlots do
Nick@113 160 local itemID = GetContainerItemID(i, j)
Nick@113 161 local texture, count = GetContainerItemInfo(i,j)
Nick@113 162 if items[itemID] then
Nick@113 163 items[itemID].count = items[itemID].count + (count or 1)
Nenue@115 164 if not items[itemID].texture then
Nenue@115 165 items[itemID].texture = texture
Nenue@115 166 print('tracked currency tally', items[itemID].count, '|T'..texture..':16:16|t')
Nenue@115 167 items[itemID].frame.Icon:SetTexture(texture)
Nenue@115 168 end
Nick@113 169 end
Nick@113 170 end
Nick@113 171 end
Nick@113 172
Nenue@115 173
Nick@113 174
Nick@113 175 local lastBlock
Nick@113 176 local totalWidth = 0
Nick@113 177 for _, block in ipairs(self.Blocks) do
Nenue@125 178 local blockIsShown = block:Update(self.realZoneText, self.subZoneText) or false
Nick@113 179 block:SetShown(blockIsShown)
Nick@113 180 canShow = canShow or blockIsShown
Nick@113 181
Nick@113 182
Nenue@125 183 if blockIsShown then
Nick@113 184 block:ClearAllPoints()
Nick@113 185 if lastBlock then
Nick@113 186 block:SetPoint('TOPLEFT', lastBlock, 'TOPRIGHT')
Nick@113 187 else
Nick@113 188 block:SetPoint('TOPLEFT', self, 'TOPLEFT')
Nick@113 189 end
Nick@113 190 lastBlock = block
Nick@113 191
Nick@113 192 block:SetHeight(24)
Nenue@123 193 block:SetWidth(block.Icon:GetWidth() + block.Label:GetStringWidth()+4)
Nick@113 194 totalWidth = totalWidth + block:GetWidth()
Nick@113 195 end
Nenue@126 196 print('|cFF0088FF'..block.name..'|r', block:IsShown(), block:GetSize())
Nick@113 197 end
Nick@113 198
Nenue@115 199 self:UpdateProfile()
Nick@113 200 self:SetWidth(totalWidth)
Nick@113 201
Nenue@115 202 needsUpdate = nil
Nick@113 203 print(self:IsShown(), '|cFF00FF88'..self:GetName()..'|r', self:GetSize())
Nick@113 204 self:SetShown(canShow)
Nenue@115 205 Veneer:DynamicReanchor()
Nenue@115 206 end
Nenue@115 207
Nenue@115 208 function module:UpdateProfile()
Nenue@115 209 if not self.profile then
Nenue@115 210 profileUpdate = true
Nenue@115 211 return
Nenue@115 212 end
Nenue@115 213
Nenue@115 214 for itemID, info in pairs(items) do
Nenue@115 215 self.profile.Items = self.profile.Items or {}
Nenue@115 216 self.profile.Items[itemID] = info
Nenue@115 217 end
Nick@113 218 end
Nick@113 219
Nick@113 220 function module:OnUpdate()
Nenue@115 221 if needsUpdate then
Nenue@125 222 self:Update(GetRealZoneText(), GetSubZoneText())
Nenue@115 223 elseif profileUpdate then
Nenue@115 224 self:UpdateProfile()
Nick@113 225 end
Nenue@115 226
Nick@113 227 end
Nick@113 228
Nick@114 229 function block:OnEnter()
Nick@114 230 if not InCombatLockdown() then
Nick@114 231 GameTooltip:SetOwner(self, "ANCHOR_BOTTOMRIGHT");
Nick@114 232 if self.currencyID then
Nick@114 233 GameTooltip:SetCurrencyTokenByID(self.currencyID);
Nick@114 234 else
Nick@114 235 GameTooltip:SetItemByID(self.itemID);
Nick@114 236 end
Nick@114 237 GameTooltip:Show();
Nick@114 238 end
Nick@114 239 end
Nick@114 240 function block:OnLeave()
Nick@114 241 if GameTooltip:IsOwned(self) then
Nick@114 242 GameTooltip_Hide()
Nick@114 243 end
Nick@114 244 end
Nick@114 245
Nick@113 246 function block:OnEvent(event, ...)
Nenue@126 247 self.debug('OnEvent', event, ...)
Nenue@126 248 self:Update()
Nick@113 249 end
Nick@113 250
Nick@113 251 function block:Setup()
Nenue@126 252 end
Nick@113 253
Nenue@126 254 function itemBlock:Update()
Nenue@123 255 if items[self.itemID].count >= 1 then
Nenue@123 256 self.Icon:SetTexture(GetItemIcon(self.id))
Nenue@123 257 self.Label:SetFormattedText("%d", items[self.id].count)
Nenue@123 258 return true
Nenue@123 259 end
Nenue@125 260 return false
Nenue@123 261 end
Nenue@123 262
Nenue@126 263 function currencyBlock:Update()
Nenue@123 264 local name, earned, texture, earnedThisWeek, weeklyMax, totalMax = GetCurrencyInfo(self.id)
Nenue@123 265
Nenue@123 266 self.Icon:SetTexture(texture)
Nenue@123 267 if self.showMax then
Nenue@123 268 self.Label:SetFormattedText("%d / %d", earned, totalMax)
Nenue@123 269 else
Nenue@123 270 self.Label:SetFormattedText("%d", earned)
Nenue@123 271 end
Nenue@123 272 return true
Nenue@123 273 end