Nenue@59: -- Veneer Nenue@59: -- BuffFrame.lua Nenue@59: -- Created: 7/27/2016 8:08 PM Nenue@59: -- %file-revision% Nenue@62: --[[ Nenue@62: Adds progress bars and cooldown swirls to buffbutton frames Nenue@60: Nenue@62: Known Limitations: Nenue@62: - Individual BuffButton frames are created upon use, making it difficult to do any sort of securestate priming Nenue@62: - TempEnchant info returns relative values only, and they don't synchronize with aura events Nenue@62: - BuffButtons can only be hidden/shown by blizzcode, so functions doing that have to be accounted for Nenue@62: --]] Nenue@62: Nenue@64: local BUFF_BUTTON_SIZE = 48 Nenue@64: local BUFF_PROGRESS_SIZE = 4 Nenue@64: local BUFF_PROGRESS_INSET = 1 Nenue@64: local BUFF_BUTTON_ZOOM = 0 Nenue@64: Nenue@62: Nenue@62: local plugin = CreateFrame('Frame', 'VeneerBuffFrame', UIParent) Nenue@59: local vn, print = LibStub("LibKraken").register(VeneerController, plugin) Nenue@59: Nenue@59: Nenue@59: local buttons = {} Nenue@59: local buffTypes = { Nenue@59: { Nenue@59: name = 'buff', Nenue@59: pattern = 'BuffButton(%d)', Nenue@59: filters = 'HELPFUL', Nenue@59: }, Nenue@59: { Nenue@59: name = 'debuff', Nenue@59: pattern = 'DebuffButton(%d)', Nenue@59: filters = 'HARMFUL', Nenue@59: }, Nenue@59: { Nenue@59: name = 'tempenchant', Nenue@59: pattern = 'TempEnchant(%d)', Nenue@59: filters = 'TEMPENCHANT' Nenue@59: } Nenue@59: } Nenue@59: Nenue@59: local textureMapping = { Nenue@59: [1] = 16, --Main hand Nenue@59: [2] = 17, --Off-hand Nenue@59: [3] = 18, --Ranged Nenue@59: } Nenue@59: Nenue@59: local tickCounter = {} Nenue@59: local aurasCache = {} Nenue@59: local skinnedFrames = {} Nenue@59: local pendingFrames = {} Nenue@59: local anchors = {} Nenue@59: local expirationCache = {} Nenue@59: Nenue@59: local VeneerButton_OnHide = function(self) Nenue@59: self:SetScript('OnDragStart', self.StartMoving) Nenue@59: self:SetScript('OnDragStop', self.StopMovingOrSizing) Nenue@60: self:SetMovable(false) Nenue@60: self:EnableMouse(false) Nenue@59: self:RegisterForDrag('LeftButton') Nenue@59: end Nenue@59: local VeneerButton_OnShow = function(self) Nenue@59: self:SetScript('OnDragStart', self.StartMoving) Nenue@59: self:SetScript('OnDragStop', self.StopMovingOrSizing) Nenue@60: self:SetMovable(false) Nenue@60: self:EnableMouse(false) Nenue@59: self:RegisterForDrag('LeftButton') Nenue@59: end Nenue@59: Nenue@59: Nenue@59: local GetVeneer = function(frame) Nenue@59: local name = frame:GetName() Nenue@59: if not _G[name..'Veneer'] then Nenue@59: Nenue@59: local veneer = CreateFrame('Frame', name..'Veneer', UIParent) Nenue@60: local id = frame:GetID() Nenue@59: veneer:SetAllPoints(frame) Nenue@60: veneer:SetParent(frame) Nenue@59: veneer.bg = veneer:CreateTexture() Nenue@61: veneer.bg:SetColorTexture(1,1,1,0) Nenue@59: veneer.bg:SetAllPoints(veneer) Nenue@60: veneer.bg:Show() Nenue@59: veneer:Hide() Nenue@60: veneer:EnableMouse(false) Nenue@59: Nenue@59: veneer:SetScript('OnShow', VeneerButton_OnShow) Nenue@59: veneer:SetScript('OnHide', VeneerButton_OnHide) Nenue@59: Nenue@59: local position = tonumber(name:match("%d")) Nenue@59: if position == 1 then Nenue@59: veneer:Show() Nenue@59: end Nenue@59: Nenue@59: veneer.progress = CreateFrame('Frame', name .. 'VeneerProgress', veneer) Nenue@59: veneer.progress:Hide() Nenue@59: veneer.progress:SetPoint('BOTTOMLEFT', veneer, 'BOTTOMLEFT', 3, -6) Nenue@59: veneer.progress:SetPoint('TOPRIGHT', veneer, 'BOTTOMRIGHT', -3, -1) Nenue@59: Nenue@59: veneer.progress.bg = veneer.progress:CreateTexture(nil, 'BACKGROUND') Nenue@61: veneer.progress.bg:SetColorTexture(0,0,0,1) Nenue@59: veneer.progress.bg:SetAllPoints(veneer.progress) Nenue@59: Nenue@59: veneer.progress.fg = veneer.progress:CreateTexture(nil, 'ARTWORK') Nenue@59: veneer.progress.fg:SetColorTexture(0,1,0,1) Nenue@59: veneer.progress.fg:SetPoint('BOTTOMLEFT', 1,1) Nenue@59: veneer.progress.fg:SetPoint('TOP', 0, -1) Nenue@59: Nenue@59: veneer.progress.status = veneer.progress:CreateFontString() Nenue@59: veneer.progress.status:SetFontObject(VeneerNumberFont) Nenue@59: veneer.progress.status:SetPoint('TOP') Nenue@59: Nenue@61: Nenue@61: veneer.cooldown = CreateFrame('Cooldown', name ..'VeneerCooldown', veneer, 'CooldownFrameTemplate') Nenue@61: veneer.cooldown:SetAllPoints(frame) Nenue@61: veneer.cooldown:SetReverse(true) Nenue@61: Nenue@59: end Nenue@59: Nenue@59: Nenue@59: return _G[name..'Veneer'] Nenue@59: end Nenue@59: Nenue@61: --- Set widgets to reflect the passed parameters Nenue@59: local UpdateVeneer = function (frame, duration, expires) Nenue@59: local veneer = GetVeneer(frame) Nenue@59: Nenue@61: if expires and duration then Nenue@59: Nenue@61: if duration ~= 0 then Nenue@61: local startTime = (expires - duration) Nenue@61: local endTime = expires or 0 Nenue@61: print('|cFF0088FF'..frame:GetName()..'|r', duration, expires) Nenue@61: veneer.progress:Show() Nenue@61: veneer.elapsed = 0 Nenue@61: veneer.progress:SetScript('OnUpdate', function(self, elapsed) Nenue@61: veneer.elapsed = veneer.elapsed + elapsed Nenue@60: Nenue@67: local w = floor(veneer.progress:GetWidth()+.5) - (BUFF_PROGRESS_INSET*2) Nenue@61: local t = GetTime() Nenue@61: local progress = (t - startTime) / duration Nenue@61: Nenue@67: local nw = (w - (w * progress)) Nenue@61: if veneer.elapsed >= 0.25 then Nenue@61: Nenue@67: print(t, startTime, floor(progress*100), w * progress, nw, w) Nenue@61: veneer.elapsed = 0.25 - veneer.elapsed Nenue@61: end Nenue@61: if (progress >= 1) or not frame:IsVisible() then Nenue@61: veneer.startTime = nil Nenue@61: self:Hide() Nenue@61: self:SetScript('OnUpdate', nil) Nenue@61: else Nenue@61: self.fg:SetWidth(nw) Nenue@61: end Nenue@61: end) Nenue@61: Nenue@61: veneer.cooldown:Show() Nenue@61: veneer.cooldown:SetCooldown(startTime, duration) Nenue@61: else Nenue@61: print('|cFF00FF88'..frame:GetName()..'|r', 'duration zero') Nenue@61: veneer.progress:SetScript('OnUpdate', nil) Nenue@61: veneer.progress:Hide() Nenue@61: veneer.cooldown:Hide() Nenue@61: end Nenue@61: else Nenue@61: veneer.progress:Hide() Nenue@61: veneer.cooldown:SetCooldown(0,0) Nenue@61: veneer.cooldown:Hide() Nenue@61: print('|cFF88FF00'..frame:GetName()..'|r', 'nil duration') Nenue@59: end Nenue@59: veneer:Show() Nenue@59: end Nenue@59: Nenue@59: Nenue@59: -- Associates skinning elements with said button Nenue@59: local SkinFrame = function(name) Nenue@59: local frame = _G[name ] Nenue@59: if skinnedFrames[frame] then Nenue@59: print('|cFFFF4400Attempting to skin a frame that already went through.|r') Nenue@59: return Nenue@59: end Nenue@59: Nenue@59: local icon = _G[name .. 'Icon'] Nenue@59: local border = _G[name .. 'Border'] Nenue@59: local duration = _G[name .. 'Duration'] Nenue@59: local slot = frame:GetID() or 0 Nenue@59: Nenue@59: tickCounter[frame] = (tickCounter[frame] or 0) + 1 Nenue@59: Nenue@59: Nenue@59: print(tickCounter[frame], frame:GetName(), '|cFFFFFF00'..slot..'|r') Nenue@59: skinnedFrames[frame] = frame Nenue@64: frame:SetSize(BUFF_BUTTON_SIZE,BUFF_BUTTON_SIZE) Nenue@64: Nenue@64: local offset = BUFF_BUTTON_ZOOM/2 Nenue@64: icon:SetTexCoord(offset, 1 - offset, offset, 1 - offset) Nenue@59: if border then Nenue@59: border:SetSize(50,50) Nenue@59: end Nenue@59: if duration then Nenue@59: duration:ClearAllPoints() Nenue@61: duration:SetPoint('TOP', frame, 'BOTTOM', 0, -8) Nenue@59: duration:SetFontObject(VeneerNumberFont) Nenue@59: duration:SetDrawLayer('OVERLAY') Nenue@59: Nenue@59: end Nenue@59: Nenue@59: GetVeneer(frame) Nenue@59: Nenue@59: anchors[frame] = veneer Nenue@59: print('Initializing', name) Nenue@59: end Nenue@59: Nenue@59: Nenue@59: --- Provides the number of changed indices for use in deciding between partial and full veneer updates Nenue@59: local CacheCheck = function(frame, ...) Nenue@59: aurasCache[frame] = aurasCache[frame] or {} Nenue@59: local hasChange = 0 Nenue@59: local numVals = select('#',...) Nenue@59: for i = 1, numVals do Nenue@59: local arg = select(i, ...) Nenue@59: if aurasCache[frame][i] ~= arg then Nenue@59: hasChange = hasChange + 1 Nenue@59: end Nenue@59: aurasCache[frame][i] = arg Nenue@59: end Nenue@59: return hasChange Nenue@59: end Nenue@59: Nenue@59: local AuraButton_Update = function(name, index, filter) Nenue@59: local bName = name..index Nenue@59: local frame = _G[bName] Nenue@59: if frame and frame:IsVisible() then Nenue@59: tickCounter[frame] = (tickCounter[frame] or 0) + 1 Nenue@59: local cacheDiff = CacheCheck(frame, UnitAura(frame.unit, frame:GetID(), frame.filter)) Nenue@61: -- if the name or expirationTime changed Nenue@61: if (cacheDiff >= 1) then Nenue@59: print(frame:GetName(), 'diff:', cacheDiff) Nenue@61: if not skinnedFrames[frame] then Nenue@61: tinsert(pendingFrames, frame) Nenue@61: end Nenue@59: expirationCache[name] = frame.expirationTime Nenue@59: print(unpack(aurasCache[frame])) Nenue@59: UpdateVeneer(frame, aurasCache[frame][6], aurasCache[frame][7]) Nenue@59: end Nenue@59: Nenue@59: -- is it a new button? Nenue@59: if not skinnedFrames[frame] then Nenue@59: SkinFrame(bName) Nenue@59: end Nenue@59: end Nenue@59: end Nenue@59: Nenue@59: local BuffFrame_UpdateAllBuffAnchors = function() Nenue@59: local todo = {} Nenue@59: if #pendingFrames >= 1 then Nenue@59: Nenue@59: print('|cFFBBFF00AllBuffAnchors|r', #pendingFrames) Nenue@59: while pendingFrames[1] do Nenue@59: local frame = tremove(pendingFrames) Nenue@59: tinsert(todo, frame:GetName()) Nenue@59: Nenue@61: -- re-apply custom anchors Nenue@59: end Nenue@59: print(table.concat(todo, ', ')) Nenue@59: end Nenue@59: --BuffButton1 Nenue@59: --DebuffButton1 Nenue@61: --todo: separate frame groups and iterate over them at appropriate times Nenue@60: if BuffButton1 then Nenue@61: --TempEnchant1:SetPoint('TOPRIGHT', BuffButton1, 'TOPRIGHT', BuffButton1:GetWidth()+4, 0) Nenue@60: end Nenue@60: Nenue@59: end Nenue@59: Nenue@59: local AuraButton_UpdateDuration = function(frame, timeLeft) Nenue@60: local hours = floor(timeLeft/3600) Nenue@60: local minutes = floor(mod(timeLeft, 3600)/60) Nenue@60: local seconds = floor(mod(timeLeft, 60)) Nenue@60: local timeString = '%ds' Nenue@59: if timeLeft > 3600 then Nenue@60: timeString = format('%d:%02d', hours, minutes) Nenue@60: elseif timeLeft > 60 then Nenue@60: timeString = format('%d:%02d', minutes, seconds) Nenue@61: else Nenue@60: timeString = format('%d', seconds) Nenue@59: end Nenue@59: Nenue@60: Nenue@60: frame.duration:SetText(timeString) Nenue@59: frame.duration:SetVertexColor(1,1,1) Nenue@59: end Nenue@59: Nenue@59: local visibility = {} Nenue@59: local TempEnchantButton_OnHide = function(self) Nenue@59: local isVisible = self:IsVisible() Nenue@59: if isVisible ~= visibility[self] then Nenue@59: print('|cFFFFFF00HIDE|r', self:GetName()) Nenue@59: visibility[self] = isVisible Nenue@59: end Nenue@59: end Nenue@59: Nenue@59: -- Obtains the first instance of Tenchant use Nenue@59: Nenue@59: local TemporaryEnchantFrame_Update = function(...) Nenue@59: local numVals = select('#', ...) Nenue@59: local numItems = numVals / 4 Nenue@59: if numItems >= 1 then Nenue@59: for itemIndex = numItems, 1, -1 do Nenue@59: local frame = _G['TempEnchant'..itemIndex] Nenue@59: local hasEnchant, timeRemaining, enchantCharges = select((4 * (itemIndex -1)) + 1, ...) Nenue@59: Nenue@59: Nenue@59: if hasEnchant then Nenue@59: local endTime = floor(GetTime()*1000) + timeRemaining Nenue@59: Nenue@59: Nenue@59: --print(endTime) Nenue@59: if endTime ~= expirationCache[frame] then Nenue@59: if expirationCache[frame] then Nenue@59: print(endTime, expirationCache[frame], endTime - expirationCache[frame]) Nenue@59: end Nenue@59: expirationCache[frame] = endTime Nenue@59: print('push tempenchant timer update', timeRemaining / 1000, GetTime()+(timeRemaining/1000)) Nenue@59: UpdateVeneer(frame, timeRemaining/1000, GetTime()+(timeRemaining/1000)) Nenue@59: end Nenue@59: else Nenue@59: GetVeneer(frame):Hide() Nenue@59: end Nenue@59: Nenue@59: end Nenue@59: Nenue@59: end Nenue@59: Nenue@59: end Nenue@59: Nenue@59: local BuffFrame_Update = function(...) Nenue@61: Nenue@59: end Nenue@59: Nenue@59: Nenue@59: hooksecurefunc("BuffFrame_Update", BuffFrame_Update) Nenue@59: hooksecurefunc("AuraButton_UpdateDuration", AuraButton_UpdateDuration) Nenue@59: hooksecurefunc("AuraButton_Update", AuraButton_Update) Nenue@59: hooksecurefunc("BuffFrame_UpdateAllBuffAnchors", BuffFrame_UpdateAllBuffAnchors) Nenue@59: hooksecurefunc("TemporaryEnchantFrame_Update", TemporaryEnchantFrame_Update) Nenue@59: Nenue@59: -- The TempEnchant frames are hardcoded in the base FrameXML, so get them now Nenue@59: for i = 1, 3 do Nenue@59: Nenue@59: SkinFrame('TempEnchant'..i) Nenue@59: hooksecurefunc(_G['TempEnchant'..i], "Hide", TempEnchantButton_OnHide) Nenue@59: Nenue@59: Nenue@59: end Nenue@59: Nenue@59: plugin.init = function () Nenue@61: Nenue@61: Nenue@61: Nenue@59: plugin.db = vn.db[PLUGIN_NAME] Nenue@59: end