view Modules/Currency.lua @ 114:6748c98a6c6c

- Reworked OrderHallHandler triggers - OrderHallHandler minimializes hud info - WorldState currency headers have a unified font
author Nick@Zahhak
date Mon, 27 Mar 2017 00:39:29 -0400
parents 2105b6e5095f
children 8c94bee4fdfc
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('VnWorldState', ...) end or nop

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,
    zones = {
      ['Suramar'] = true,
      ["Sashj'tar Ruins"] = true,
      ["Faronaar Ruins"] = true
    }
  },
  ["Blood of Sargeras"] = {
    itemID = 124124,
  }
}
local items = {}


VeneerWorldStateCurrencyMixin = { Blocks = {} }
VeneerWorldStateCurrencyBlockMixin = {}
local module = VeneerWorldStateCurrencyMixin
local block = VeneerWorldStateCurrencyBlockMixin



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:OnLoad ()
  self:RegisterEvent("PLAYER_ENTERING_WORLD");
  self:RegisterEvent('PLAYER_REGEN_ENABLED')
  self:RegisterEvent('PLAYER_REGEN_DISABLED')

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

    local debug = function(...)
      print('|cFF0088FF<'..frame.name..'>|r', ...)
    end

    if info.itemID then
      local itemID = info.itemID
      items[itemID] = {
        count = 0,
        frame = frame
      }
      frame.Update = function(block)
        debug('Update [Item]')
        if items[itemID].count >= 1 then
          block.Icon:SetTexture(GetItemIcon(itemID))
          block.Label:SetFormattedText("%d", items[itemID].count)
          return true
        end
      end
      RegisterEvents(self, itemEvents)
    elseif info.currencyID then
      local currencyID = info.currencyID
      frame.Update = function (block)
        debug('Update [Currency]')
        local name, earned, texture, earnedThisWeek, weeklyMax, totalMax = GetCurrencyInfo(currencyID)
        
        block.Icon:SetTexture(texture)
        block.Label:SetFormattedText("%d / %d", earned, totalMax)
        block:SetWidth(block.Icon:GetWidth() + block.Label:GetStringWidth() + 6)
        return true
      end

      RegisterEvents(frame, currencyEvents)
    end
    if info.zones then
      RegisterEvents(frame, zoneEvents)
      local zones = info.zones
      local of = frame.Update
      frame.Update = function(block)
        debug('Update [Zone]')
        local zone = self.zoneText
        local canShow = (zone and block.zones[zone]) and true or false
        if of then
          canShow = canShow and of(frame)
        end
        return canShow
      end
    end
  end
end

function module:OnEvent (event, arg)
  print(self:GetName(), 'OnEvent', event, arg)
  self:Update()
end
local toUpdate = {}
local wipe = table.wipe
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
  self.zoneText = GetRealZoneText()
  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)
        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

  for itemID, info in pairs(items) do
  end

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


    if block:IsShown() 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:GetWidth()+4)
      totalWidth = totalWidth + block:GetWidth()
    end
    print(block:IsShown(), '|cFF0088FF'..block.name..'|r', block:GetSize())

  end

  self:SetWidth(totalWidth)

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

function module:OnUpdate()
  if self.needsUpdate then
    self:Update()
  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, ...)
  print('|cFF0088FF<'..self.name..'>|r', 'OnEvent', event, ...)
  self:Update()
end

function block:Setup()

end