view Modules/PaperDoll.lua @ 69:ebc18a7412a1

- use secure hook functions to manage duration and count text
author Nenue
date Sun, 21 Aug 2016 10:28:09 -0400
parents 6ccacc927ef6
children 6f8661094643
line wrap: on
line source
-- Veneer
-- PaperDoll.lua
-- Created: 8/16/2016 8:18 AM
-- %file-revision%
-- Displays item levels by equipment slots
-- Requires tooltip scraping to obtain these numbers, meaning any currently active tooltip will be cleared.

local plugin = CreateFrame('Frame', 'VeneerPaper', UIParent)
local vn, print = LibStub("LibKraken").register(VeneerController, plugin)

local slot_anchors = {
  [1] = 'TOPLEFT',
  [2] = 'TOPLEFT',
  [3] = 'TOPLEFT',
  [15] = 'TOPLEFT',
  [5] = 'TOPLEFT',
  [9] = 'TOPLEFT',

  [10] = 'TOPRIGHT',
  [6] = 'TOPRIGHT',
  [7] = 'TOPRIGHT',
  [8] = 'TOPRIGHT',
  [11] = 'TOPRIGHT',
  [12] = 'TOPRIGHT',
  [13] = 'TOPRIGHT',
  [14] = 'TOPRIGHT',
  
  [16] = 'BOTTOMRIGHT',
  [17] = 'BOTTOMLEFT',
}

local slot_relative = {
  [1] = 'TOPRIGHT',
  [2] = 'TOPRIGHT',
  [3] = 'TOPRIGHT',
  [15] = 'TOPRIGHT',
  [5] = 'TOPRIGHT',
  [9] = 'TOPRIGHT',

  [10] = 'TOPLEFT',
  [6] = 'TOPLEFT',
  [7] = 'TOPLEFT',
  [8] = 'TOPLEFT',
  [11] = 'TOPLEFT',
  [12] = 'TOPLEFT',
  [13] = 'TOPLEFT',
  [14] = 'TOPLEFT',

  [16] = 'TOPRIGHT',
  [17] = 'TOPLEFT',
}
local ticker
local vnslot = {}
local pendingSlots = {}



local UpdateVeneer = function(itemslot, frame)
  local slot = itemslot:GetID()
  if itemslot.hasItem then
    frame.link = GetInventoryItemLink('player', slot)
    --print(frame.link)
    local name, link, something, ilevel = GetItemInfo(frame.link)
    frame.label:SetText(ilevel)
    -- todo: test GetExtendedItemInfo()

    GameTooltip:SetOwner(plugin)
    GameTooltip:SetInventoryItem('player', slot)
    GameTooltip:Hide()

    for i = 1, 3 do
      local gname = 'gem'..i
      if frame[gname] then
        frame.gemslot[i] = frame.gemslot[i] or frame:CreateTexture(nil, 'ARTWORK')
        print(frame[gname])
      end
    end
    frame:Show()
  else
    frame:Hide()
  end
end

local UpdateAll = function()
  for index, frame in pairs(vnslot) do
    if frame:IsVisible() then
      print('forcing', index, frame:GetName())
      tinsert(pendingSlots, frame)
    end
  end
  plugin.ticker()
end

local UpdateNext = function()

  local frame = tremove(pendingSlots)
  if frame and frame:IsVisible() then
    print('updating', frame:GetName())
    UpdateVeneer(frame:GetParent(), frame)
  else

    ticker:Cancel()
    ticker = nil
  end

end

-- PaperDollFrame is separate from InspectUI handlers
local PaperDollItemSlotButton_Update = function(self)
  local name = self:GetName()
  local slot = self:GetID()
  if not slot_anchors[slot] then
    return
  end


  local frame = _G[name .. 'Veneer']

  if not frame then

    frame = CreateFrame('Frame', name..'Veneer', self)
    vnslot[slot] = frame

    frame.label = frame:CreateFontString(nil, 'OVERLAY', 'VeneerNumberFont')
    frame.gemslot = {}

    local anchor, relative, x, y = slot_anchors[slot], slot_relative[slot], 8, -4
    if anchor:match('RIGHT') then
      x = -x
    end
    if anchor:match('BOTTOM') then
      y = 4
    end


    frame:SetPoint(anchor, self, relative, x, y)
    frame:SetSize(200,24)
    frame.label:ClearAllPoints()
    frame.label:SetPoint(slot_anchors[slot], frame, slot_anchors[slot])

    tinsert(pendingSlots, frame)
  end

  plugin.ticker()
end


plugin.ticker = function()
  if (not ticker) and #pendingSlots >= 1 then
    ticker = C_Timer.NewTicker(0, UpdateNext)
  end
end


plugin.event = function(self, event, ...)
  print(self, event, flag, slot)

  if event == 'PLAYER_EQUIPMENT_CHANGED' then
    local slot, hasItem = ...
    if vnslot[slot] then
      UpdateVeneer(vnslot[slot]:GetParent(), vnslot[slot])
      plugin.ticker()
    end

  elseif event == 'PLAYER_ENTERING_WORLD' then
    UpdateAll()

  end

end

plugin:SetScript('OnEvent', plugin.event)
plugin:RegisterEvent('PLAYER_EQUIPMENT_CHANGED')
plugin:RegisterEvent('PLAYER_ENTERING_WORLD')


hooksecurefunc("PaperDollItemSlotButton_Update", PaperDollItemSlotButton_Update)

hooksecurefunc(GameTooltip, "SetInventoryItem", function(self, unit, slot)
  if self:GetOwner() == plugin then
  if unit == 'player' and vnslot[slot] then

    local text
    for i = 1, 3 do
      text = _G['GameTooltipTextLeft'..i]:GetText()
      text = text:match('Item Level (%d+)')
      if text then
        break
      end
    end
    vnslot[slot].label:SetText(text or '???')
  end
  end
end)