annotate Modules/BuffFrame.lua @ 61:3f083d389c18

- don't send updates for BuffAnchors hook
author Nenue
date Mon, 15 Aug 2016 09:35:46 -0400
parents 2a636b00c31e
children ef4116179e2f
rev   line source
Nenue@59 1 -- Veneer
Nenue@59 2 -- BuffFrame.lua
Nenue@59 3 -- Created: 7/27/2016 8:08 PM
Nenue@59 4 -- %file-revision%
Nenue@61 5 -- Adds progress bars and cooldown swirls to buffbutton frames
Nenue@60 6
Nenue@59 7 local PLUGIN_NAME = 'BuffFrame'
Nenue@59 8 local plugin = {}
Nenue@59 9 local vn, print = LibStub("LibKraken").register(VeneerController, plugin)
Nenue@59 10
Nenue@59 11
Nenue@59 12 local buttons = {}
Nenue@59 13 local buffTypes = {
Nenue@59 14 {
Nenue@59 15 name = 'buff',
Nenue@59 16 pattern = 'BuffButton(%d)',
Nenue@59 17 filters = 'HELPFUL',
Nenue@59 18 },
Nenue@59 19 {
Nenue@59 20 name = 'debuff',
Nenue@59 21 pattern = 'DebuffButton(%d)',
Nenue@59 22 filters = 'HARMFUL',
Nenue@59 23 },
Nenue@59 24 {
Nenue@59 25 name = 'tempenchant',
Nenue@59 26 pattern = 'TempEnchant(%d)',
Nenue@59 27 filters = 'TEMPENCHANT'
Nenue@59 28 }
Nenue@59 29 }
Nenue@59 30
Nenue@59 31 local textureMapping = {
Nenue@59 32 [1] = 16, --Main hand
Nenue@59 33 [2] = 17, --Off-hand
Nenue@59 34 [3] = 18, --Ranged
Nenue@59 35 }
Nenue@59 36
Nenue@59 37 local tickCounter = {}
Nenue@59 38 local aurasCache = {}
Nenue@59 39 local skinnedFrames = {}
Nenue@59 40 local pendingFrames = {}
Nenue@59 41 local anchors = {}
Nenue@59 42 local expirationCache = {}
Nenue@59 43
Nenue@59 44 local VeneerButton_OnHide = function(self)
Nenue@59 45 self:SetScript('OnDragStart', self.StartMoving)
Nenue@59 46 self:SetScript('OnDragStop', self.StopMovingOrSizing)
Nenue@60 47 self:SetMovable(false)
Nenue@60 48 self:EnableMouse(false)
Nenue@59 49 self:RegisterForDrag('LeftButton')
Nenue@59 50 end
Nenue@59 51 local VeneerButton_OnShow = function(self)
Nenue@59 52 self:SetScript('OnDragStart', self.StartMoving)
Nenue@59 53 self:SetScript('OnDragStop', self.StopMovingOrSizing)
Nenue@60 54 self:SetMovable(false)
Nenue@60 55 self:EnableMouse(false)
Nenue@59 56 self:RegisterForDrag('LeftButton')
Nenue@59 57 end
Nenue@59 58
Nenue@59 59
Nenue@59 60 local GetVeneer = function(frame)
Nenue@59 61 local name = frame:GetName()
Nenue@59 62 if not _G[name..'Veneer'] then
Nenue@59 63
Nenue@59 64 local veneer = CreateFrame('Frame', name..'Veneer', UIParent)
Nenue@60 65 local id = frame:GetID()
Nenue@59 66 veneer:SetAllPoints(frame)
Nenue@60 67 veneer:SetParent(frame)
Nenue@59 68 veneer.bg = veneer:CreateTexture()
Nenue@61 69 veneer.bg:SetColorTexture(1,1,1,0)
Nenue@59 70 veneer.bg:SetAllPoints(veneer)
Nenue@60 71 veneer.bg:Show()
Nenue@59 72 veneer:Hide()
Nenue@60 73 veneer:EnableMouse(false)
Nenue@59 74
Nenue@59 75 veneer:SetScript('OnShow', VeneerButton_OnShow)
Nenue@59 76 veneer:SetScript('OnHide', VeneerButton_OnHide)
Nenue@59 77
Nenue@59 78 local position = tonumber(name:match("%d"))
Nenue@59 79 if position == 1 then
Nenue@59 80 veneer:Show()
Nenue@59 81 end
Nenue@59 82
Nenue@59 83 veneer.progress = CreateFrame('Frame', name .. 'VeneerProgress', veneer)
Nenue@59 84 veneer.progress:Hide()
Nenue@59 85 veneer.progress:SetPoint('BOTTOMLEFT', veneer, 'BOTTOMLEFT', 3, -6)
Nenue@59 86 veneer.progress:SetPoint('TOPRIGHT', veneer, 'BOTTOMRIGHT', -3, -1)
Nenue@59 87
Nenue@59 88 veneer.progress.bg = veneer.progress:CreateTexture(nil, 'BACKGROUND')
Nenue@61 89 veneer.progress.bg:SetColorTexture(0,0,0,1)
Nenue@59 90 veneer.progress.bg:SetAllPoints(veneer.progress)
Nenue@59 91
Nenue@59 92 veneer.progress.fg = veneer.progress:CreateTexture(nil, 'ARTWORK')
Nenue@59 93 veneer.progress.fg:SetColorTexture(0,1,0,1)
Nenue@59 94 veneer.progress.fg:SetPoint('BOTTOMLEFT', 1,1)
Nenue@59 95 veneer.progress.fg:SetPoint('TOP', 0, -1)
Nenue@59 96
Nenue@59 97 veneer.progress.status = veneer.progress:CreateFontString()
Nenue@59 98 veneer.progress.status:SetFontObject(VeneerNumberFont)
Nenue@59 99 veneer.progress.status:SetPoint('TOP')
Nenue@59 100
Nenue@61 101
Nenue@61 102 veneer.cooldown = CreateFrame('Cooldown', name ..'VeneerCooldown', veneer, 'CooldownFrameTemplate')
Nenue@61 103 veneer.cooldown:SetAllPoints(frame)
Nenue@61 104 veneer.cooldown:SetReverse(true)
Nenue@61 105
Nenue@59 106 end
Nenue@59 107
Nenue@59 108
Nenue@59 109 return _G[name..'Veneer']
Nenue@59 110 end
Nenue@59 111
Nenue@61 112 --- Set widgets to reflect the passed parameters
Nenue@59 113 local UpdateVeneer = function (frame, duration, expires)
Nenue@59 114 local veneer = GetVeneer(frame)
Nenue@59 115
Nenue@61 116 if expires and duration then
Nenue@59 117
Nenue@61 118 if duration ~= 0 then
Nenue@61 119 local startTime = (expires - duration)
Nenue@61 120 local endTime = expires or 0
Nenue@61 121 print('|cFF0088FF'..frame:GetName()..'|r', duration, expires)
Nenue@61 122 veneer.progress:Show()
Nenue@61 123 veneer.elapsed = 0
Nenue@61 124 veneer.progress:SetScript('OnUpdate', function(self, elapsed)
Nenue@61 125 veneer.elapsed = veneer.elapsed + elapsed
Nenue@60 126
Nenue@61 127 local w = floor(veneer.progress:GetWidth()+.5)
Nenue@61 128 local t = GetTime()
Nenue@61 129 local progress = (t - startTime) / duration
Nenue@61 130
Nenue@61 131 local nw = (w-2) - ceil(w * progress)
Nenue@61 132 if veneer.elapsed >= 0.25 then
Nenue@61 133
Nenue@61 134 print(progress, nw, w)
Nenue@61 135 veneer.elapsed = 0.25 - veneer.elapsed
Nenue@61 136 end
Nenue@61 137 if (progress >= 1) or not frame:IsVisible() then
Nenue@61 138 veneer.startTime = nil
Nenue@61 139 self:Hide()
Nenue@61 140 self:SetScript('OnUpdate', nil)
Nenue@61 141 else
Nenue@61 142 self.fg:SetWidth(nw)
Nenue@61 143 end
Nenue@61 144 end)
Nenue@61 145
Nenue@61 146 veneer.cooldown:Show()
Nenue@61 147 veneer.cooldown:SetCooldown(startTime, duration)
Nenue@61 148 else
Nenue@61 149 print('|cFF00FF88'..frame:GetName()..'|r', 'duration zero')
Nenue@61 150 veneer.progress:SetScript('OnUpdate', nil)
Nenue@61 151 veneer.progress:Hide()
Nenue@61 152 veneer.cooldown:Hide()
Nenue@61 153 end
Nenue@61 154 else
Nenue@61 155 veneer.progress:Hide()
Nenue@61 156 veneer.cooldown:SetCooldown(0,0)
Nenue@61 157 veneer.cooldown:Hide()
Nenue@61 158 print('|cFF88FF00'..frame:GetName()..'|r', 'nil duration')
Nenue@59 159 end
Nenue@59 160 veneer:Show()
Nenue@59 161 end
Nenue@59 162
Nenue@59 163
Nenue@59 164 -- Associates skinning elements with said button
Nenue@59 165 local SkinFrame = function(name)
Nenue@59 166 local frame = _G[name ]
Nenue@59 167 if skinnedFrames[frame] then
Nenue@59 168 print('|cFFFF4400Attempting to skin a frame that already went through.|r')
Nenue@59 169 return
Nenue@59 170 end
Nenue@59 171
Nenue@59 172 local icon = _G[name .. 'Icon']
Nenue@59 173 local border = _G[name .. 'Border']
Nenue@59 174 local duration = _G[name .. 'Duration']
Nenue@59 175 local slot = frame:GetID() or 0
Nenue@59 176
Nenue@59 177 tickCounter[frame] = (tickCounter[frame] or 0) + 1
Nenue@59 178
Nenue@59 179
Nenue@59 180 print(tickCounter[frame], frame:GetName(), '|cFFFFFF00'..slot..'|r')
Nenue@59 181 skinnedFrames[frame] = frame
Nenue@59 182 frame:SetSize(48,48)
Nenue@59 183 icon:SetTexCoord(0,1,0,1)
Nenue@59 184 if border then
Nenue@59 185 border:SetSize(50,50)
Nenue@59 186 end
Nenue@59 187 if duration then
Nenue@59 188 duration:ClearAllPoints()
Nenue@61 189 duration:SetPoint('TOP', frame, 'BOTTOM', 0, -8)
Nenue@59 190 duration:SetFontObject(VeneerNumberFont)
Nenue@59 191 duration:SetDrawLayer('OVERLAY')
Nenue@59 192
Nenue@59 193 end
Nenue@59 194
Nenue@59 195 GetVeneer(frame)
Nenue@59 196
Nenue@59 197 anchors[frame] = veneer
Nenue@59 198 print('Initializing', name)
Nenue@59 199 end
Nenue@59 200
Nenue@59 201
Nenue@59 202 --- Provides the number of changed indices for use in deciding between partial and full veneer updates
Nenue@59 203 local CacheCheck = function(frame, ...)
Nenue@59 204 aurasCache[frame] = aurasCache[frame] or {}
Nenue@59 205 local hasChange = 0
Nenue@59 206 local numVals = select('#',...)
Nenue@59 207 for i = 1, numVals do
Nenue@59 208 local arg = select(i, ...)
Nenue@59 209 if aurasCache[frame][i] ~= arg then
Nenue@59 210 hasChange = hasChange + 1
Nenue@59 211 end
Nenue@59 212 aurasCache[frame][i] = arg
Nenue@59 213 end
Nenue@59 214 return hasChange
Nenue@59 215 end
Nenue@59 216
Nenue@59 217 local AuraButton_Update = function(name, index, filter)
Nenue@59 218 local bName = name..index
Nenue@59 219 local frame = _G[bName]
Nenue@59 220 if frame and frame:IsVisible() then
Nenue@59 221 tickCounter[frame] = (tickCounter[frame] or 0) + 1
Nenue@59 222 local cacheDiff = CacheCheck(frame, UnitAura(frame.unit, frame:GetID(), frame.filter))
Nenue@61 223 -- if the name or expirationTime changed
Nenue@61 224 if (cacheDiff >= 1) then
Nenue@59 225 print(frame:GetName(), 'diff:', cacheDiff)
Nenue@61 226 if not skinnedFrames[frame] then
Nenue@61 227 tinsert(pendingFrames, frame)
Nenue@61 228 end
Nenue@59 229 expirationCache[name] = frame.expirationTime
Nenue@59 230 print(unpack(aurasCache[frame]))
Nenue@59 231 UpdateVeneer(frame, aurasCache[frame][6], aurasCache[frame][7])
Nenue@59 232 end
Nenue@59 233
Nenue@59 234 -- is it a new button?
Nenue@59 235 if not skinnedFrames[frame] then
Nenue@59 236 SkinFrame(bName)
Nenue@59 237 end
Nenue@59 238 end
Nenue@59 239 end
Nenue@59 240
Nenue@59 241 local BuffFrame_UpdateAllBuffAnchors = function()
Nenue@59 242 local todo = {}
Nenue@59 243 if #pendingFrames >= 1 then
Nenue@59 244
Nenue@59 245 print('|cFFBBFF00AllBuffAnchors|r', #pendingFrames)
Nenue@59 246 while pendingFrames[1] do
Nenue@59 247 local frame = tremove(pendingFrames)
Nenue@59 248 tinsert(todo, frame:GetName())
Nenue@59 249
Nenue@61 250 -- re-apply custom anchors
Nenue@59 251 end
Nenue@59 252 print(table.concat(todo, ', '))
Nenue@59 253 end
Nenue@59 254 --BuffButton1
Nenue@59 255 --DebuffButton1
Nenue@61 256 --todo: separate frame groups and iterate over them at appropriate times
Nenue@60 257 if BuffButton1 then
Nenue@61 258 --TempEnchant1:SetPoint('TOPRIGHT', BuffButton1, 'TOPRIGHT', BuffButton1:GetWidth()+4, 0)
Nenue@60 259 end
Nenue@60 260
Nenue@59 261 end
Nenue@59 262
Nenue@59 263 local AuraButton_UpdateDuration = function(frame, timeLeft)
Nenue@60 264 local hours = floor(timeLeft/3600)
Nenue@60 265 local minutes = floor(mod(timeLeft, 3600)/60)
Nenue@60 266 local seconds = floor(mod(timeLeft, 60))
Nenue@60 267 local timeString = '%ds'
Nenue@59 268 if timeLeft > 3600 then
Nenue@60 269 timeString = format('%d:%02d', hours, minutes)
Nenue@60 270 elseif timeLeft > 60 then
Nenue@60 271 timeString = format('%d:%02d', minutes, seconds)
Nenue@61 272 else
Nenue@60 273 timeString = format('%d', seconds)
Nenue@59 274 end
Nenue@59 275
Nenue@60 276
Nenue@60 277 frame.duration:SetText(timeString)
Nenue@59 278 frame.duration:SetVertexColor(1,1,1)
Nenue@59 279 end
Nenue@59 280
Nenue@59 281 local visibility = {}
Nenue@59 282 local TempEnchantButton_OnHide = function(self)
Nenue@59 283 local isVisible = self:IsVisible()
Nenue@59 284 if isVisible ~= visibility[self] then
Nenue@59 285 print('|cFFFFFF00HIDE|r', self:GetName())
Nenue@59 286 visibility[self] = isVisible
Nenue@59 287 end
Nenue@59 288 end
Nenue@59 289
Nenue@59 290 -- Obtains the first instance of Tenchant use
Nenue@59 291
Nenue@59 292 local TemporaryEnchantFrame_Update = function(...)
Nenue@59 293 local numVals = select('#', ...)
Nenue@59 294 local numItems = numVals / 4
Nenue@59 295 if numItems >= 1 then
Nenue@59 296 for itemIndex = numItems, 1, -1 do
Nenue@59 297 local frame = _G['TempEnchant'..itemIndex]
Nenue@59 298 local hasEnchant, timeRemaining, enchantCharges = select((4 * (itemIndex -1)) + 1, ...)
Nenue@59 299
Nenue@59 300
Nenue@59 301 if hasEnchant then
Nenue@59 302 local endTime = floor(GetTime()*1000) + timeRemaining
Nenue@59 303
Nenue@59 304
Nenue@59 305 --print(endTime)
Nenue@59 306 if endTime ~= expirationCache[frame] then
Nenue@59 307 if expirationCache[frame] then
Nenue@59 308 print(endTime, expirationCache[frame], endTime - expirationCache[frame])
Nenue@59 309 end
Nenue@59 310 expirationCache[frame] = endTime
Nenue@59 311 print('push tempenchant timer update', timeRemaining / 1000, GetTime()+(timeRemaining/1000))
Nenue@59 312 UpdateVeneer(frame, timeRemaining/1000, GetTime()+(timeRemaining/1000))
Nenue@59 313 end
Nenue@59 314 else
Nenue@59 315 GetVeneer(frame):Hide()
Nenue@59 316 end
Nenue@59 317
Nenue@59 318 end
Nenue@59 319
Nenue@59 320 end
Nenue@59 321
Nenue@59 322 end
Nenue@59 323
Nenue@59 324 local BuffFrame_Update = function(...)
Nenue@61 325
Nenue@59 326 end
Nenue@59 327
Nenue@59 328
Nenue@59 329 hooksecurefunc("BuffFrame_Update", BuffFrame_Update)
Nenue@59 330 hooksecurefunc("AuraButton_UpdateDuration", AuraButton_UpdateDuration)
Nenue@59 331 hooksecurefunc("AuraButton_Update", AuraButton_Update)
Nenue@59 332 hooksecurefunc("BuffFrame_UpdateAllBuffAnchors", BuffFrame_UpdateAllBuffAnchors)
Nenue@59 333 hooksecurefunc("TemporaryEnchantFrame_Update", TemporaryEnchantFrame_Update)
Nenue@59 334
Nenue@59 335 -- The TempEnchant frames are hardcoded in the base FrameXML, so get them now
Nenue@59 336 for i = 1, 3 do
Nenue@59 337
Nenue@59 338 SkinFrame('TempEnchant'..i)
Nenue@59 339 hooksecurefunc(_G['TempEnchant'..i], "Hide", TempEnchantButton_OnHide)
Nenue@59 340
Nenue@59 341
Nenue@59 342 end
Nenue@59 343
Nenue@59 344 plugin.init = function ()
Nenue@61 345
Nenue@61 346
Nenue@61 347
Nenue@59 348 plugin.db = vn.db[PLUGIN_NAME]
Nenue@59 349 end