view 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
line wrap: on
line source
--
-- Created by IntelliJ IDEA.
-- User: Nick
-- Date: 3/25/2017
-- Time: 7:07 PM
-- To change this template use File | Settings | File Templates.
--

local print = DEVIAN_WORKSPACE and function(...) print('Currency', ...) end or nop
local profileUpdate, needsUpdate


local itemBlock =  {}
local currencyBlock = {}


local zoneEvents = {
  "ZONE_CHANGED_NEW_AREA", "ZONE_CHANGED_INDOORS", "ZONE_CHANGED"
}
local currencyEvents = {
  'CURRENCY_DISPLAY_UPDATE', 'CHAT_MSG_CURRENCY'
}
local itemEvents = {
  'CHAT_MSG_LOOT', 'BAG_UPDATE'
}

local blocks = {
  ["Ancient Mana"] = {
    currencyID = 1155,
    showMax = true,
    zones = {
      ['Suramar'] = true,
    },
  },
  ["Blood of Sargeras"] = {
    itemID = 124124,
  },
  ["Legionfall War Supplies"] = {
    currencyID = 1342,
    zones = {
      ["The Broken Shore"] = true
    }
  },
  ["Nethershard"] = {
    currencyID = 1226,
    zones = {
      ["The Broken Shore"] = true
    }
  },
  ["Veiled Argunite"] = {
    currencyID = 1508,
    zones = {
      ["Antoran Wastes"] = true,
      ["Krokuun"] = true,
      ["MacAree"] = true,
    }
  },
}
local items, currencies = {}, {}


VeneerCurrencyMixin = {
  Blocks = {},
  HideCombat = true,
  EventList = {'PLAYER_ENTERING_WORLD'},
  moduleName = 'Currency Watch',
  anchorPoint = 'TOP',
  anchorPriority = 2,
}
VeneerCurrencyBlockMixin = {}
local module = VeneerCurrencyMixin
local block = VeneerCurrencyBlockMixin



local function RegisterEvents (frame, events)
  for _, event in ipairs(events) do
    print('|cFFFF0088'..(frame.name or frame:GetName())..'|r', 'listening to', event)
    frame:RegisterEvent(event)
  end
end

function module:Setup()
  self:Update()
end

function module:OnLoad ()
  Veneer:AddHandler(self)

  for name, info in pairs(blocks) do
    local block = CreateFrame('Frame', nil, self, 'VeneerCurrencyTemplate')
    block.name = name
    for k,v in pairs(info) do
      print(name, k, '=', v)
      block[k] = v
    end

    block.debug = function(...)
      print('|cFF0088FF<'..block.name..'>|r', ...)
    end
    block.id = info.itemID or info.currencyID


    if info.itemID then
      local itemID = info.itemID
      items[itemID] = {
        count = 0,
        frame = block
      }
      block.Update = itemBlock.Update
      RegisterEvents(self, itemEvents)
    elseif info.currencyID then
      local currencyID = info.currencyID
      block.Update = currencyBlock.Update
      RegisterEvents(block, currencyEvents)
    end



    if info.zones then
      RegisterEvents(block, zoneEvents)
      local zones = info.zones
      local of = block.Update
      function block:Update(zone, subZone)

        local zone = GetRealZoneText()
        local subZone = GetSubZoneText()
        block.debug('Zone Check', zone, subZone)
        local canShow = ((zone and self.zones[zone]) or (subZone and self.zones[subZone])) and true or false
        if of then
          canShow = canShow and of(self)
        end
        return canShow
      end
    end
  end
end

function module:OnEvent (event, arg)
  print(self:GetName(), 'OnEvent', event, arg)
end

function module:Update(isBatchUpdate)
  print(self:GetName(), 'Update()')
  if InCombatLockdown() then
    self:SetShown(false)
    return
  end


  for itemID in pairs(items) do
    items[itemID].count = 0
  end

  local canShow = false

  for i = 0, NUM_BAG_SLOTS do
    local numSlots = GetContainerNumSlots(i)
    for j = 1, numSlots do
      local itemID = GetContainerItemID(i, j)
      local texture, count = GetContainerItemInfo(i,j)
      if items[itemID] then
        items[itemID].count = items[itemID].count + (count or 1)
        if not items[itemID].texture then
          items[itemID].texture = texture
          print('tracked currency tally', items[itemID].count, '|T'..texture..':16:16|t')
          items[itemID].frame.Icon:SetTexture(texture)
        end
      end
    end
  end



  local lastBlock
  local totalWidth = 0
  for _, block in ipairs(self.Blocks) do
    local blockIsShown = block:Update(self.realZoneText, self.subZoneText) or false
    block:SetShown(blockIsShown)
    canShow = canShow or blockIsShown


    if blockIsShown then
      block:ClearAllPoints()
      if lastBlock then
        block:SetPoint('TOPLEFT', lastBlock, 'TOPRIGHT')
      else
        block:SetPoint('TOPLEFT', self, 'TOPLEFT')
      end
      lastBlock = block

      block:SetHeight(24)
      block:SetWidth(block.Icon:GetWidth() + block.Label:GetStringWidth()+4)
      totalWidth = totalWidth + block:GetWidth()
    end
    print('|cFF0088FF'..block.name..'|r', block:IsShown(), block:GetSize())
  end

  self:UpdateProfile()
  self:SetWidth(totalWidth)

  needsUpdate = nil
  print(self:IsShown(), '|cFF00FF88'..self:GetName()..'|r', self:GetSize())
  self:SetShown(canShow)
  Veneer:DynamicReanchor()
end

function module:UpdateProfile()
  if not self.profile then
    profileUpdate = true
    return
  end

  for itemID, info in pairs(items) do
    self.profile.Items = self.profile.Items or {}
    self.profile.Items[itemID] = info
  end
end

function module:OnUpdate()
  if needsUpdate then
    self:Update(GetRealZoneText(), GetSubZoneText())
  elseif profileUpdate then
    self:UpdateProfile()
  end

end

function block:OnEnter()
  if not InCombatLockdown() then
    GameTooltip:SetOwner(self, "ANCHOR_BOTTOMRIGHT");
    if self.currencyID then
      GameTooltip:SetCurrencyTokenByID(self.currencyID);
    else
      GameTooltip:SetItemByID(self.itemID);
    end
    GameTooltip:Show();
  end
end
function block:OnLeave()
  if GameTooltip:IsOwned(self) then
    GameTooltip_Hide()
  end
end

function block:OnEvent(event, ...)
  self.debug('OnEvent', event, ...)
  self:Update()
end

function block:Setup()
end

function itemBlock:Update()
  if items[self.itemID].count >= 1 then
    self.Icon:SetTexture(GetItemIcon(self.id))
    self.Label:SetFormattedText("%d", items[self.id].count)
    return true
  end
  return false
end

function currencyBlock:Update()
  local name, earned, texture, earnedThisWeek, weeklyMax, totalMax = GetCurrencyInfo(self.id)

  self.Icon:SetTexture(texture)
  if self.showMax then
    self.Label:SetFormattedText("%d / %d", earned, totalMax)
  else
    self.Label:SetFormattedText("%d", earned)
  end
  return true
end