view Modules/BuffFrame.lua @ 70:1b0d7bcd252e

- anchors code
author Nenue
date Mon, 22 Aug 2016 20:38:43 -0400
parents ebc18a7412a1
children 6f8661094643
line wrap: on
line source
-- Veneer
-- BuffFrame.lua
-- Created: 7/27/2016 8:08 PM
-- %file-revision%
--[[
  Adds progress bars and cooldown swirls to buffbutton frames

  Known Limitations:
  - Individual BuffButton frames are created upon use, making it difficult to do any sort of securestate priming
  - TempEnchant info returns relative values only, and they don't synchronize with aura events
  - BuffButtons can only be hidden/shown by blizzcode, so functions doing that have to be accounted for
--]]

local BUFF_BUTTON_SIZE = 48
local BUFF_PROGRESS_SIZE = 4
local BUFF_PROGRESS_INSET = 1
local BUFF_BUTTON_ZOOM = .15
local BORDER_SIZE_L = 1
local BORDER_SIZE_R = 1
local BORDER_SIZE_U = 4
local BORDER_SIZE_D = 1


local plugin = CreateFrame('Frame', 'VeneerBuffFrame', UIParent)
local vn, print = LibStub("LibKraken").register(VeneerController, plugin)
local tprint = DEVIAN_WORKSPACE and function(...) _G.print('Timer', ...) end or function() end

local _G, UIParent = _G, UIParent
local tinsert, tremove, unpack, select, tconcat = table.insert, table.remove, unpack, select, table.concat
local floor, tonumber, format = math.floor, tonumber, string.format
local UnitAura, GetTime, CreateFrame =  UnitAura, GetTime, CreateFrame
local hooksecurefunc = hooksecurefunc

local buttons = {}
local buffTypes = {
  {
    name = 'buff',
    pattern = 'BuffButton(%d)',
    filters = 'HELPFUL',
  },
  {
    name = 'debuff',
    pattern = 'DebuffButton(%d)',
    filters = 'HARMFUL',
  },
  {
    name = 'tempenchant',
    pattern = 'TempEnchant(%d)',
    filters = 'TEMPENCHANT'
  }
}

local textureMapping = {
  [1] = 16,	--Main hand
  [2] = 17,	--Off-hand
  [3] = 18,	--Ranged
}

local tickCounter = {}
local aurasCache = {}
local skinnedFrames = {}
local pendingFrames = {}
local anchors = {}
local expirationCache = {}
local visibility = {}

local VeneerButton_OnHide = function(self)
  self:SetScript('OnDragStart', self.StartMoving)
  self:SetScript('OnDragStop', self.StopMovingOrSizing)
  self:SetMovable(false)
  self:EnableMouse(false)
  self:RegisterForDrag('LeftButton')
end
local VeneerButton_OnShow = function(self)
  self:SetScript('OnDragStart', self.StartMoving)
  self:SetScript('OnDragStop', self.StopMovingOrSizing)
  self:SetMovable(false)
  self:EnableMouse(false)
  self:RegisterForDrag('LeftButton')
end


local GetVeneer = function(frame)
  local name = frame:GetName()
  if not (_G[name..'Veneer']) then
    print('|cFF88FF00Creating', name,'Veneer')
    local veneer = CreateFrame('Frame', name..'Veneer', UIParent)
    local id = frame:GetID()
    veneer:SetAllPoints(frame)
    veneer:SetParent(frame)
    veneer.bg = veneer:CreateTexture()
    veneer.bg:SetColorTexture(1,1,1,0)
    veneer.bg:SetAllPoints(veneer)
    veneer.bg:Show()
    veneer:Hide()
    veneer:EnableMouse(false)

    veneer:SetScript('OnShow', VeneerButton_OnShow)
    veneer:SetScript('OnHide', VeneerButton_OnHide)

    local position = tonumber(name:match("%d"))
    if position == 1 then
      veneer:Show()
    end

    veneer.progress = CreateFrame('Frame', name .. 'VeneerProgress', veneer)
    veneer.progress:Hide()
    veneer.progress:SetPoint('BOTTOMLEFT', veneer, 'BOTTOMLEFT', 3, -6)
    veneer.progress:SetPoint('TOPRIGHT', veneer, 'BOTTOMRIGHT', -3, -1)
    veneer.progress:SetHeight(BUFF_PROGRESS_SIZE + (BUFF_PROGRESS_INSET * 2))

    veneer.progress.bg = veneer.progress:CreateTexture(nil, 'BACKGROUND')
    veneer.progress.bg:SetColorTexture(0,0,0,1)
    veneer.progress.bg:SetAllPoints(veneer.progress)

    veneer.progress.fg = veneer.progress:CreateTexture(nil, 'ARTWORK')
    veneer.progress.fg:SetColorTexture(0,1,0,1)
    veneer.progress.fg:SetPoint('BOTTOMLEFT', BUFF_PROGRESS_INSET,BUFF_PROGRESS_INSET)
    veneer.progress.fg:SetPoint('TOP', 0, -BUFF_PROGRESS_INSET)

    veneer.progress.status = veneer.progress:CreateFontString()
    veneer.progress.status:SetFontObject(VeneerNumberFont)
    veneer.progress.status:SetPoint('TOP')


    veneer.cooldown = CreateFrame('Cooldown', name ..'VeneerCooldown', veneer, 'CooldownFrameTemplate')
    veneer.cooldown:SetAllPoints(frame)
    veneer.cooldown:SetReverse(true)

    local overlay = CreateFrame('Frame', name .. 'VeneerOverlay', UIParent)

    overlay:Show()
    overlay:SetFrameStrata('MEDIUM')
    local n = frame:GetNumPoints()
    for i = 1, n do
      overlay:SetPoint(frame:GetPoint(n))
    end


    local underlay = CreateFrame('Frame', name..'VeneerUnderlay', UIParent)
    underlay:Show()
    underlay:SetFrameStrata('BACKGROUND')
    local n = frame:GetNumPoints()
    for i = 1, n do
      underlay:SetPoint(frame:GetPoint(n))
    end

    veneer.duration = overlay:CreateFontString(name..'VeneerDuration', 'OVERLAY')
    veneer.duration:SetFontObject(VeneerNumberFont)
    veneer.duration:SetPoint('TOP', frame, 'BOTTOM', 0, -8)

    veneer.count = overlay:CreateFontString(name..'VeneerCount', 'OVERLAY')
    veneer.count:SetFontObject(VeneerNumberFont)
    veneer.count:SetPoint('BOTTOMRIGHT', frame, 'BOTTOMRIGHT', -3, 3)



    veneer.border = underlay:CreateTexture(name..'VeneerBorder', 'BACKGROUND')
    veneer.border:SetPoint('TOPLEFT', veneer, 'TOPLEFT', -BORDER_SIZE_L, BORDER_SIZE_U)
    veneer.border:SetPoint('BOTTOMRIGHT', veneer, 'BOTTOMRIGHT', BORDER_SIZE_R, -BORDER_SIZE_D)
    veneer.border:Show()


    veneer.overlay = overlay
    veneer.underlay = underlay
  end


  return _G[name..'Veneer']
end


-- Associates skinning elements with said button
local SkinFrame = function(name)
  local frame = _G[name ]
  if skinnedFrames[frame] then
    print('|cFFFF4400Attempting to skin a frame that already went through.|r')
    return
  end
  print('|cFFFFFF00Adopting', name)

  local icon = _G[name .. 'Icon']
  local border = _G[name .. 'Border']
  local count = _G[name .. 'Count']
  local duration = _G[name .. 'Duration']
  local slot = frame:GetID() or 0
  local veneer = GetVeneer(frame)
  local underlay = veneer.underlay
  local overlay = veneer.overlay

  skinnedFrames[frame] = frame
  frame:SetSize(BUFF_BUTTON_SIZE,BUFF_BUTTON_SIZE)

  local offset = BUFF_BUTTON_ZOOM/2
  icon:SetTexCoord(offset, 1 - offset, offset, 1 - offset)
  if border then
    border:Hide()
    hooksecurefunc(border, 'SetVertexColor', function(frame, r, g, b, a)
      frame:Hide()
      print('|cFF0088FFborder:SetVertexColor|r', r,g,b,a)
      veneer.border:SetColorTexture(r,g,b,a)
    end)

    local color = DebuffTypeColor["none"]
    if aurasCache[frame] and aurasCache[frame][5] then
      color = DebuffTypeColor[aurasCache[frame][5]]
    end

    veneer.border:SetColorTexture(color.r,color.g,color.b)
  end
  if duration then
    duration:ClearAllPoints()
    --duration:SetPoint('TOP', frame, 'BOTTOM', 0, -8)
    --duration:SetFontObject(VeneerNumberFont)
    --duration:SetDrawLayer('OVERLAY')

    hooksecurefunc(duration, 'Hide', function(self, text)
      veneer.duration:Hide()
    end)
    hooksecurefunc(duration, 'Show', function(self, text)
      veneer.duration:Show()
    end)
  end
  if count then
    count:ClearAllPoints()
    hooksecurefunc(count, 'SetText', function(self, text)
      self:Hide()
      veneer.count:SetText(text)
    end)
    hooksecurefunc(count, 'Hide', function(self, text)
      veneer.count:Hide()
    end)
    hooksecurefunc(count, 'Show', function(self, text)
      veneer.count:Show()
    end)
  end


  hooksecurefunc(frame, "Hide", function(self)
    local isVisible = self:IsVisible()
    if isVisible ~= visibility[self] then
      visibility[self] = isVisible
    end
    veneer:Hide()
    veneer.count:Hide()
    underlay:Hide()
  end)

  hooksecurefunc(frame, 'Show', function(self)
    veneer:Show()
    veneer.count:Show()
    veneer.border:Show()
    underlay:Show()
    local isVisible = self:IsVisible()
    if isVisible ~= visibility[self] then
      print('|cFFFFFF00SHOW|r', self:GetName())
      visibility[self] = isVisible
    end
  end)

  anchors[frame] = veneer
end

local Aura_SetBorderColor = function(self, r,g,b,a) end
local Aura_OnShow = function(self) end
local Aura_OnHide = function(self) end

--- Set widgets to reflect the passed parameters
local UpdateVeneer = function (frame, duration, expires)
  local veneer = GetVeneer(frame)
  -- is it a new button?
  if not skinnedFrames[frame] then
    SkinFrame(frame:GetName())
  end

  if frame.filter == 'HARMFUL' then

    veneer.border:Show()
  end


  if expires and duration then
    if duration ~= 0 then
      local startTime = (expires - duration)
      local endTime = expires or 0
      print('|cFF0088FF'..frame:GetName()..'|r', duration, expires)
      veneer.progress:Show()
      veneer.elapsed = 0
      veneer.progress:SetScript('OnUpdate', function(self, elapsed)
        veneer.elapsed = veneer.elapsed + elapsed

        local w = floor(veneer.progress:GetWidth()+.5) - (BUFF_PROGRESS_INSET*2)
        local t = GetTime()
        local progress = (t - startTime) / duration

        local nw = (w - (w * progress))
        if veneer.elapsed >= 0.25 then

          tprint(t, startTime, floor(progress*100), w * progress, nw, w)
          veneer.elapsed = 0.25 - veneer.elapsed
        end
        if (progress >= 1) or not frame:IsVisible() then
          veneer.startTime = nil
          self:Hide()
          self:SetScript('OnUpdate', nil)
        else
          self.fg:SetWidth(nw)
        end
      end)

      veneer.cooldown:Show()
      veneer.cooldown:SetCooldown(startTime, duration)
    else
      print('|cFF00FF88'..frame:GetName()..'|r', 'duration zero')
      veneer.progress:SetScript('OnUpdate', nil)
      veneer.progress:Hide()
      veneer.cooldown:Hide()
    end
  else
    veneer.progress:Hide()
    veneer.cooldown:SetCooldown(0,0)
    veneer.cooldown:Hide()
    print('|cFF88FF00'..frame:GetName()..'|r', 'nil duration')
  end
  veneer:Show()
end


--- Provides the number of changed indices for use in deciding between partial and full veneer updates
local CacheCheck = function(frame, ...)
  aurasCache[frame] = aurasCache[frame] or {}
  local hasChange = 0
  local numVals = select('#',...)
  for i = 1,   numVals do
    local arg = select(i, ...)
    if aurasCache[frame][i] ~= arg then
      hasChange = hasChange + 1
    end
    aurasCache[frame][i] = arg
  end
  return hasChange
end

local AuraButton_Update = function(name, index, filter)
  local bName = name..index
  local frame = _G[bName]
  if frame and frame:IsVisible() then
    tickCounter[frame] = (tickCounter[frame] or 0) + 1



    local cacheDiff = CacheCheck(frame, UnitAura(frame.unit, frame:GetID(), frame.filter))

    -- if the name or expirationTime changed
    if (cacheDiff >= 1) then
      print('|cFFFF4400', frame:GetName(), 'diff:', cacheDiff)
      if not skinnedFrames[frame] then
        tinsert(pendingFrames, frame)
      end
      expirationCache[name] = frame.expirationTime
      print(unpack(aurasCache[frame]))



      UpdateVeneer(frame, aurasCache[frame][6], aurasCache[frame][7])
    end

  end
end

local BuffFrame_UpdateAllBuffAnchors = function()
  local todo = {}
  if #pendingFrames >= 1 then

    print('|cFFBBFF00AllBuffAnchors|r', #pendingFrames)
    while pendingFrames[1] do
      local frame = tremove(pendingFrames)
      tinsert(todo, frame:GetName())

      -- re-apply custom anchors
    end
    print(tconcat(todo, ', '))
  end
  --BuffButton1
  --DebuffButton1
  --todo: separate frame groups and iterate over them at appropriate times
  if BuffButton1 then
    TempEnchant1:SetPoint('TOPRIGHT', BuffButton1, 'TOPRIGHT', BuffButton1:GetWidth()+4, 0)
  end

  local lastBuff, topBuff
  for i = 1, BUFF_ACTUAL_DISPLAY do
    local buff = _G['BuffButton'..i]
    if buff then
      if i == 1 then
        buff:SetPoint('TOPRIGHT', UIParent, 'TOPRIGHT', -120, -6)
        topBuff = buff
      elseif mod(i,12) == 1 then
        buff:SetPoint('TOPRIGHT', topBuff, 'BOTTOMRIGHT', 0, -14)
        topBuff = buff
      else
        buff:SetPoint('TOPRIGHT', lastBuff, 'TOPLEFT')
      end
      lastBuff = buff
    end
  end

  for i = 1, DEBUFF_ACTUAL_DISPLAY do
    local debuff = _G['DebuffButton'..i]
    if debuff then
      if i == 1 then
        if topBuff then
          debuff:SetPoint('TOPRIGHT', topBuff, 'BOTTOMRIGHT', 0, -14)
        else
          debuff:SetPoint('TOPRIGHT', UIParent, 'TOPRIGHT', -120, -6)
        end
        topBuff = debuff
      elseif mod(i, 12) == 1 then
        debuff:SetPoint('TOPRIGHT', topBuff, 'BOTTOMRIGHT', 0, -14)
        topBuff = debuff
      else
        debuff:SetPoint('TOPRIGHT', lastBuff, 'TOPLEFT')
      end
      lastBuff = debuff
    end
  end

end

local AuraButton_UpdateDuration = function(frame, timeLeft)
  local veneer = GetVeneer(frame)
  local hours =  floor(timeLeft/3600)
  local minutes = floor(mod(timeLeft, 3600)/60)
  local seconds = floor(mod(timeLeft, 60))
  local timeString = '%ds'
  if timeLeft > 3600 then
    timeString = format('%d:%02d', hours, minutes)
  elseif timeLeft > 60 then
    timeString = format('%d:%02d', minutes, seconds)
  else
    timeString = format('%d', seconds)
  end


  veneer.duration:SetText(timeString)
  veneer.duration:SetVertexColor(1,1,1)
end


-- Obtains the first instance of Tenchant use

local TemporaryEnchantFrame_Update = function(...)
  local numVals = select('#', ...)
  local numItems = numVals / 4
  if numItems >= 1 then
    for itemIndex = numItems, 1, -1 do
      local frame = _G['TempEnchant'..itemIndex]
      local hasEnchant, timeRemaining, enchantCharges = select((4 * (itemIndex -1)) + 1, ...)


      if hasEnchant then
        local endTime = floor(GetTime()*1000) + timeRemaining


        --print(endTime)
        if endTime ~= expirationCache[frame] then
          if expirationCache[frame] then
            print(endTime, expirationCache[frame], endTime - expirationCache[frame])
          end
          expirationCache[frame] = endTime
          print('push tempenchant timer update', timeRemaining / 1000, GetTime()+(timeRemaining/1000))
          UpdateVeneer(frame, timeRemaining/1000, GetTime()+(timeRemaining/1000))
        end
      else
        GetVeneer(frame):Hide()
      end

    end

  end

end

local BuffFrame_Update = function(...)

end


hooksecurefunc("BuffFrame_Update", BuffFrame_Update)
hooksecurefunc("AuraButton_UpdateDuration", AuraButton_UpdateDuration)
hooksecurefunc("AuraButton_Update", AuraButton_Update)
hooksecurefunc("BuffFrame_UpdateAllBuffAnchors", BuffFrame_UpdateAllBuffAnchors)
hooksecurefunc("TemporaryEnchantFrame_Update", TemporaryEnchantFrame_Update)

-- The TempEnchant frames are hardcoded in the base FrameXML, so get them now
for i = 1, 3 do

  SkinFrame('TempEnchant'..i)
  _G['TempEnchant'..i..'Border']:SetVertexColor(0.5,0,1,1)

end

plugin.init = function ()



  plugin.db = vn.db[PLUGIN_NAME]
end