annotate Modules/BuffFrame.lua @ 68:be5cea6e1e8f

- button size metrics - anchor adjustment for temp enchant - clean up call order - use secure hooks to directly catch Show/Hide for individual buttons
author Nenue
date Sun, 21 Aug 2016 07:09:10 -0400
parents f80ee484ac8a
children ebc18a7412a1
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@62 5 --[[
Nenue@62 6 Adds progress bars and cooldown swirls to buffbutton frames
Nenue@60 7
Nenue@62 8 Known Limitations:
Nenue@62 9 - Individual BuffButton frames are created upon use, making it difficult to do any sort of securestate priming
Nenue@62 10 - TempEnchant info returns relative values only, and they don't synchronize with aura events
Nenue@62 11 - BuffButtons can only be hidden/shown by blizzcode, so functions doing that have to be accounted for
Nenue@62 12 --]]
Nenue@62 13
Nenue@64 14 local BUFF_BUTTON_SIZE = 48
Nenue@64 15 local BUFF_PROGRESS_SIZE = 4
Nenue@64 16 local BUFF_PROGRESS_INSET = 1
Nenue@68 17 local BUFF_BUTTON_ZOOM = .15
Nenue@68 18 local BORDER_SIZE_L = 0
Nenue@68 19 local BORDER_SIZE_R = 0
Nenue@68 20 local BORDER_SIZE_U = 1
Nenue@68 21 local BORDER_SIZE_D = 7
Nenue@64 22
Nenue@62 23
Nenue@62 24 local plugin = CreateFrame('Frame', 'VeneerBuffFrame', UIParent)
Nenue@59 25 local vn, print = LibStub("LibKraken").register(VeneerController, plugin)
Nenue@68 26 local tprint = DEVIAN_WORKSPACE and function(...) _G.print('Timer', ...) end or function() end
Nenue@59 27
Nenue@68 28 local _G, UIParent = _G, UIParent
Nenue@68 29 local tinsert, tremove, unpack, select, tconcat = table.insert, table.remove, unpack, select, table.concat
Nenue@68 30 local floor, tonumber, format = math.floor, tonumber, string.format
Nenue@68 31 local UnitAura, GetTime, CreateFrame = UnitAura, GetTime, CreateFrame
Nenue@68 32 local hooksecurefunc = hooksecurefunc
Nenue@59 33
Nenue@59 34 local buttons = {}
Nenue@59 35 local buffTypes = {
Nenue@59 36 {
Nenue@59 37 name = 'buff',
Nenue@59 38 pattern = 'BuffButton(%d)',
Nenue@59 39 filters = 'HELPFUL',
Nenue@59 40 },
Nenue@59 41 {
Nenue@59 42 name = 'debuff',
Nenue@59 43 pattern = 'DebuffButton(%d)',
Nenue@59 44 filters = 'HARMFUL',
Nenue@59 45 },
Nenue@59 46 {
Nenue@59 47 name = 'tempenchant',
Nenue@59 48 pattern = 'TempEnchant(%d)',
Nenue@59 49 filters = 'TEMPENCHANT'
Nenue@59 50 }
Nenue@59 51 }
Nenue@59 52
Nenue@59 53 local textureMapping = {
Nenue@59 54 [1] = 16, --Main hand
Nenue@59 55 [2] = 17, --Off-hand
Nenue@59 56 [3] = 18, --Ranged
Nenue@59 57 }
Nenue@59 58
Nenue@59 59 local tickCounter = {}
Nenue@59 60 local aurasCache = {}
Nenue@59 61 local skinnedFrames = {}
Nenue@59 62 local pendingFrames = {}
Nenue@59 63 local anchors = {}
Nenue@59 64 local expirationCache = {}
Nenue@68 65 local visibility = {}
Nenue@59 66
Nenue@59 67 local VeneerButton_OnHide = function(self)
Nenue@59 68 self:SetScript('OnDragStart', self.StartMoving)
Nenue@59 69 self:SetScript('OnDragStop', self.StopMovingOrSizing)
Nenue@60 70 self:SetMovable(false)
Nenue@60 71 self:EnableMouse(false)
Nenue@59 72 self:RegisterForDrag('LeftButton')
Nenue@59 73 end
Nenue@59 74 local VeneerButton_OnShow = function(self)
Nenue@59 75 self:SetScript('OnDragStart', self.StartMoving)
Nenue@59 76 self:SetScript('OnDragStop', self.StopMovingOrSizing)
Nenue@60 77 self:SetMovable(false)
Nenue@60 78 self:EnableMouse(false)
Nenue@59 79 self:RegisterForDrag('LeftButton')
Nenue@59 80 end
Nenue@59 81
Nenue@59 82
Nenue@59 83 local GetVeneer = function(frame)
Nenue@59 84 local name = frame:GetName()
Nenue@68 85 if not (_G[name..'Veneer'] and _G[name..'Underlay']) then
Nenue@68 86 print('|cFF88FF00Creating', name,'Veneer')
Nenue@59 87 local veneer = CreateFrame('Frame', name..'Veneer', UIParent)
Nenue@60 88 local id = frame:GetID()
Nenue@59 89 veneer:SetAllPoints(frame)
Nenue@60 90 veneer:SetParent(frame)
Nenue@59 91 veneer.bg = veneer:CreateTexture()
Nenue@61 92 veneer.bg:SetColorTexture(1,1,1,0)
Nenue@59 93 veneer.bg:SetAllPoints(veneer)
Nenue@60 94 veneer.bg:Show()
Nenue@59 95 veneer:Hide()
Nenue@60 96 veneer:EnableMouse(false)
Nenue@59 97
Nenue@59 98 veneer:SetScript('OnShow', VeneerButton_OnShow)
Nenue@59 99 veneer:SetScript('OnHide', VeneerButton_OnHide)
Nenue@59 100
Nenue@59 101 local position = tonumber(name:match("%d"))
Nenue@59 102 if position == 1 then
Nenue@59 103 veneer:Show()
Nenue@59 104 end
Nenue@59 105
Nenue@59 106 veneer.progress = CreateFrame('Frame', name .. 'VeneerProgress', veneer)
Nenue@59 107 veneer.progress:Hide()
Nenue@59 108 veneer.progress:SetPoint('BOTTOMLEFT', veneer, 'BOTTOMLEFT', 3, -6)
Nenue@59 109 veneer.progress:SetPoint('TOPRIGHT', veneer, 'BOTTOMRIGHT', -3, -1)
Nenue@68 110 veneer.progress:SetHeight(BUFF_PROGRESS_SIZE + (BUFF_PROGRESS_INSET * 2))
Nenue@59 111
Nenue@59 112 veneer.progress.bg = veneer.progress:CreateTexture(nil, 'BACKGROUND')
Nenue@61 113 veneer.progress.bg:SetColorTexture(0,0,0,1)
Nenue@59 114 veneer.progress.bg:SetAllPoints(veneer.progress)
Nenue@59 115
Nenue@59 116 veneer.progress.fg = veneer.progress:CreateTexture(nil, 'ARTWORK')
Nenue@59 117 veneer.progress.fg:SetColorTexture(0,1,0,1)
Nenue@68 118 veneer.progress.fg:SetPoint('BOTTOMLEFT', BUFF_PROGRESS_INSET,BUFF_PROGRESS_INSET)
Nenue@68 119 veneer.progress.fg:SetPoint('TOP', 0, -BUFF_PROGRESS_INSET)
Nenue@59 120
Nenue@59 121 veneer.progress.status = veneer.progress:CreateFontString()
Nenue@59 122 veneer.progress.status:SetFontObject(VeneerNumberFont)
Nenue@59 123 veneer.progress.status:SetPoint('TOP')
Nenue@59 124
Nenue@61 125
Nenue@61 126 veneer.cooldown = CreateFrame('Cooldown', name ..'VeneerCooldown', veneer, 'CooldownFrameTemplate')
Nenue@61 127 veneer.cooldown:SetAllPoints(frame)
Nenue@61 128 veneer.cooldown:SetReverse(true)
Nenue@61 129
Nenue@68 130
Nenue@68 131
Nenue@68 132
Nenue@68 133 local underlay = CreateFrame('Frame', name..'Underlay', UIParent)
Nenue@68 134 underlay:Show()
Nenue@68 135 underlay:SetFrameStrata('BACKGROUND')
Nenue@68 136 local n = frame:GetNumPoints()
Nenue@68 137 for i = 1, n do
Nenue@68 138 underlay:SetPoint(frame:GetPoint(n))
Nenue@68 139 end
Nenue@68 140
Nenue@68 141
Nenue@68 142
Nenue@68 143
Nenue@68 144 veneer.border = underlay:CreateTexture(name..'VeneerBorder', 'BACKGROUND')
Nenue@68 145 veneer.border:SetPoint('TOPLEFT', veneer, 'TOPLEFT', -BORDER_SIZE_L, BORDER_SIZE_U)
Nenue@68 146 veneer.border:SetPoint('BOTTOMRIGHT', veneer, 'BOTTOMRIGHT', BORDER_SIZE_R, -BORDER_SIZE_D)
Nenue@68 147 --veneer.border:SetColorTexture(0,1,0,1)
Nenue@68 148 veneer.border:Show()
Nenue@68 149
Nenue@59 150 end
Nenue@59 151
Nenue@59 152
Nenue@68 153 return _G[name..'Veneer'], _G[name..'Underlay']
Nenue@59 154 end
Nenue@59 155
Nenue@68 156
Nenue@68 157 -- Associates skinning elements with said button
Nenue@68 158 local SkinFrame = function(name)
Nenue@68 159 local frame = _G[name ]
Nenue@68 160 if skinnedFrames[frame] then
Nenue@68 161 print('|cFFFF4400Attempting to skin a frame that already went through.|r')
Nenue@68 162 return
Nenue@68 163 end
Nenue@68 164 print('|cFFFFFF00Adopting', name)
Nenue@68 165
Nenue@68 166 local icon = _G[name .. 'Icon']
Nenue@68 167 local border = _G[name .. 'Border']
Nenue@68 168 local count = _G[name .. 'Count']
Nenue@68 169 local duration = _G[name .. 'Duration']
Nenue@68 170 local slot = frame:GetID() or 0
Nenue@68 171 local veneer, underlay = GetVeneer(frame)
Nenue@68 172
Nenue@68 173 skinnedFrames[frame] = frame
Nenue@68 174 frame:SetSize(BUFF_BUTTON_SIZE,BUFF_BUTTON_SIZE)
Nenue@68 175
Nenue@68 176 local offset = BUFF_BUTTON_ZOOM/2
Nenue@68 177 icon:SetTexCoord(offset, 1 - offset, offset, 1 - offset)
Nenue@68 178 if border then
Nenue@68 179 border:Hide()
Nenue@68 180 hooksecurefunc(border, 'SetVertexColor', function(frame, r, g, b, a)
Nenue@68 181 frame:Hide()
Nenue@68 182 print('|cFF0088FFborder:SetVertexColor|r', r,g,b,a)
Nenue@68 183 veneer.border:SetColorTexture(r,g,b,a)
Nenue@68 184 end)
Nenue@68 185
Nenue@68 186 local color = DebuffTypeColor["none"]
Nenue@68 187 if aurasCache[frame] and aurasCache[frame][5] then
Nenue@68 188 color = DebuffTypeColor[aurasCache[frame][5]]
Nenue@68 189 end
Nenue@68 190
Nenue@68 191 veneer.border:SetColorTexture(color.r,color.g,color.b)
Nenue@68 192 end
Nenue@68 193 if duration then
Nenue@68 194 duration:ClearAllPoints()
Nenue@68 195 duration:SetPoint('TOP', frame, 'BOTTOM', 0, -8)
Nenue@68 196 duration:SetFontObject(VeneerNumberFont)
Nenue@68 197 duration:SetDrawLayer('OVERLAY')
Nenue@68 198
Nenue@68 199 end
Nenue@68 200
Nenue@68 201
Nenue@68 202 hooksecurefunc(frame, "Hide", function(self)
Nenue@68 203 local isVisible = self:IsVisible()
Nenue@68 204 if isVisible ~= visibility[self] then
Nenue@68 205 visibility[self] = isVisible
Nenue@68 206 end
Nenue@68 207 veneer:Hide()
Nenue@68 208 underlay:Hide()
Nenue@68 209 end)
Nenue@68 210
Nenue@68 211 hooksecurefunc(frame, 'Show', function(self)
Nenue@68 212 veneer:Show()
Nenue@68 213 veneer.border:Show()
Nenue@68 214 underlay:Show()
Nenue@68 215 local isVisible = self:IsVisible()
Nenue@68 216 if isVisible ~= visibility[self] then
Nenue@68 217 print('|cFFFFFF00SHOW|r', self:GetName())
Nenue@68 218 visibility[self] = isVisible
Nenue@68 219 end
Nenue@68 220 end)
Nenue@68 221
Nenue@68 222 anchors[frame] = veneer
Nenue@68 223 end
Nenue@68 224
Nenue@68 225 local Aura_SetBorderColor = function(self, r,g,b,a) end
Nenue@68 226 local Aura_OnShow = function(self) end
Nenue@68 227 local Aura_OnHide = function(self) end
Nenue@68 228
Nenue@61 229 --- Set widgets to reflect the passed parameters
Nenue@59 230 local UpdateVeneer = function (frame, duration, expires)
Nenue@59 231 local veneer = GetVeneer(frame)
Nenue@68 232 -- is it a new button?
Nenue@68 233 if not skinnedFrames[frame] then
Nenue@68 234 SkinFrame(frame:GetName())
Nenue@68 235 end
Nenue@68 236
Nenue@68 237 if frame.filter == 'HARMFUL' then
Nenue@68 238
Nenue@68 239 veneer.border:Show()
Nenue@68 240 end
Nenue@68 241
Nenue@59 242
Nenue@61 243 if expires and duration then
Nenue@61 244 if duration ~= 0 then
Nenue@61 245 local startTime = (expires - duration)
Nenue@61 246 local endTime = expires or 0
Nenue@61 247 print('|cFF0088FF'..frame:GetName()..'|r', duration, expires)
Nenue@61 248 veneer.progress:Show()
Nenue@61 249 veneer.elapsed = 0
Nenue@61 250 veneer.progress:SetScript('OnUpdate', function(self, elapsed)
Nenue@61 251 veneer.elapsed = veneer.elapsed + elapsed
Nenue@60 252
Nenue@67 253 local w = floor(veneer.progress:GetWidth()+.5) - (BUFF_PROGRESS_INSET*2)
Nenue@61 254 local t = GetTime()
Nenue@61 255 local progress = (t - startTime) / duration
Nenue@61 256
Nenue@67 257 local nw = (w - (w * progress))
Nenue@61 258 if veneer.elapsed >= 0.25 then
Nenue@61 259
Nenue@68 260 tprint(t, startTime, floor(progress*100), w * progress, nw, w)
Nenue@61 261 veneer.elapsed = 0.25 - veneer.elapsed
Nenue@61 262 end
Nenue@61 263 if (progress >= 1) or not frame:IsVisible() then
Nenue@61 264 veneer.startTime = nil
Nenue@61 265 self:Hide()
Nenue@61 266 self:SetScript('OnUpdate', nil)
Nenue@61 267 else
Nenue@61 268 self.fg:SetWidth(nw)
Nenue@61 269 end
Nenue@61 270 end)
Nenue@61 271
Nenue@61 272 veneer.cooldown:Show()
Nenue@61 273 veneer.cooldown:SetCooldown(startTime, duration)
Nenue@61 274 else
Nenue@61 275 print('|cFF00FF88'..frame:GetName()..'|r', 'duration zero')
Nenue@61 276 veneer.progress:SetScript('OnUpdate', nil)
Nenue@61 277 veneer.progress:Hide()
Nenue@61 278 veneer.cooldown:Hide()
Nenue@61 279 end
Nenue@61 280 else
Nenue@61 281 veneer.progress:Hide()
Nenue@61 282 veneer.cooldown:SetCooldown(0,0)
Nenue@61 283 veneer.cooldown:Hide()
Nenue@61 284 print('|cFF88FF00'..frame:GetName()..'|r', 'nil duration')
Nenue@59 285 end
Nenue@59 286 veneer:Show()
Nenue@59 287 end
Nenue@59 288
Nenue@59 289
Nenue@59 290 --- Provides the number of changed indices for use in deciding between partial and full veneer updates
Nenue@59 291 local CacheCheck = function(frame, ...)
Nenue@59 292 aurasCache[frame] = aurasCache[frame] or {}
Nenue@59 293 local hasChange = 0
Nenue@59 294 local numVals = select('#',...)
Nenue@59 295 for i = 1, numVals do
Nenue@59 296 local arg = select(i, ...)
Nenue@59 297 if aurasCache[frame][i] ~= arg then
Nenue@59 298 hasChange = hasChange + 1
Nenue@59 299 end
Nenue@59 300 aurasCache[frame][i] = arg
Nenue@59 301 end
Nenue@59 302 return hasChange
Nenue@59 303 end
Nenue@59 304
Nenue@59 305 local AuraButton_Update = function(name, index, filter)
Nenue@59 306 local bName = name..index
Nenue@59 307 local frame = _G[bName]
Nenue@59 308 if frame and frame:IsVisible() then
Nenue@59 309 tickCounter[frame] = (tickCounter[frame] or 0) + 1
Nenue@68 310
Nenue@68 311
Nenue@68 312
Nenue@59 313 local cacheDiff = CacheCheck(frame, UnitAura(frame.unit, frame:GetID(), frame.filter))
Nenue@68 314
Nenue@61 315 -- if the name or expirationTime changed
Nenue@61 316 if (cacheDiff >= 1) then
Nenue@68 317 print('|cFFFF4400', frame:GetName(), 'diff:', cacheDiff)
Nenue@61 318 if not skinnedFrames[frame] then
Nenue@61 319 tinsert(pendingFrames, frame)
Nenue@61 320 end
Nenue@59 321 expirationCache[name] = frame.expirationTime
Nenue@59 322 print(unpack(aurasCache[frame]))
Nenue@68 323
Nenue@68 324
Nenue@68 325
Nenue@59 326 UpdateVeneer(frame, aurasCache[frame][6], aurasCache[frame][7])
Nenue@59 327 end
Nenue@59 328
Nenue@59 329 end
Nenue@59 330 end
Nenue@59 331
Nenue@59 332 local BuffFrame_UpdateAllBuffAnchors = function()
Nenue@59 333 local todo = {}
Nenue@59 334 if #pendingFrames >= 1 then
Nenue@59 335
Nenue@59 336 print('|cFFBBFF00AllBuffAnchors|r', #pendingFrames)
Nenue@59 337 while pendingFrames[1] do
Nenue@59 338 local frame = tremove(pendingFrames)
Nenue@59 339 tinsert(todo, frame:GetName())
Nenue@59 340
Nenue@61 341 -- re-apply custom anchors
Nenue@59 342 end
Nenue@68 343 print(tconcat(todo, ', '))
Nenue@59 344 end
Nenue@59 345 --BuffButton1
Nenue@59 346 --DebuffButton1
Nenue@61 347 --todo: separate frame groups and iterate over them at appropriate times
Nenue@60 348 if BuffButton1 then
Nenue@68 349 TempEnchant1:SetPoint('TOPRIGHT', BuffButton1, 'TOPRIGHT', BuffButton1:GetWidth()+4, 0)
Nenue@60 350 end
Nenue@60 351
Nenue@59 352 end
Nenue@59 353
Nenue@59 354 local AuraButton_UpdateDuration = function(frame, timeLeft)
Nenue@60 355 local hours = floor(timeLeft/3600)
Nenue@60 356 local minutes = floor(mod(timeLeft, 3600)/60)
Nenue@60 357 local seconds = floor(mod(timeLeft, 60))
Nenue@60 358 local timeString = '%ds'
Nenue@59 359 if timeLeft > 3600 then
Nenue@60 360 timeString = format('%d:%02d', hours, minutes)
Nenue@60 361 elseif timeLeft > 60 then
Nenue@60 362 timeString = format('%d:%02d', minutes, seconds)
Nenue@61 363 else
Nenue@60 364 timeString = format('%d', seconds)
Nenue@59 365 end
Nenue@59 366
Nenue@60 367
Nenue@60 368 frame.duration:SetText(timeString)
Nenue@59 369 frame.duration:SetVertexColor(1,1,1)
Nenue@59 370 end
Nenue@59 371
Nenue@59 372
Nenue@59 373 -- Obtains the first instance of Tenchant use
Nenue@59 374
Nenue@59 375 local TemporaryEnchantFrame_Update = function(...)
Nenue@59 376 local numVals = select('#', ...)
Nenue@59 377 local numItems = numVals / 4
Nenue@59 378 if numItems >= 1 then
Nenue@59 379 for itemIndex = numItems, 1, -1 do
Nenue@59 380 local frame = _G['TempEnchant'..itemIndex]
Nenue@59 381 local hasEnchant, timeRemaining, enchantCharges = select((4 * (itemIndex -1)) + 1, ...)
Nenue@59 382
Nenue@59 383
Nenue@59 384 if hasEnchant then
Nenue@59 385 local endTime = floor(GetTime()*1000) + timeRemaining
Nenue@59 386
Nenue@59 387
Nenue@59 388 --print(endTime)
Nenue@59 389 if endTime ~= expirationCache[frame] then
Nenue@59 390 if expirationCache[frame] then
Nenue@59 391 print(endTime, expirationCache[frame], endTime - expirationCache[frame])
Nenue@59 392 end
Nenue@59 393 expirationCache[frame] = endTime
Nenue@59 394 print('push tempenchant timer update', timeRemaining / 1000, GetTime()+(timeRemaining/1000))
Nenue@59 395 UpdateVeneer(frame, timeRemaining/1000, GetTime()+(timeRemaining/1000))
Nenue@59 396 end
Nenue@59 397 else
Nenue@59 398 GetVeneer(frame):Hide()
Nenue@59 399 end
Nenue@59 400
Nenue@59 401 end
Nenue@59 402
Nenue@59 403 end
Nenue@59 404
Nenue@59 405 end
Nenue@59 406
Nenue@59 407 local BuffFrame_Update = function(...)
Nenue@61 408
Nenue@59 409 end
Nenue@59 410
Nenue@59 411
Nenue@59 412 hooksecurefunc("BuffFrame_Update", BuffFrame_Update)
Nenue@59 413 hooksecurefunc("AuraButton_UpdateDuration", AuraButton_UpdateDuration)
Nenue@59 414 hooksecurefunc("AuraButton_Update", AuraButton_Update)
Nenue@59 415 hooksecurefunc("BuffFrame_UpdateAllBuffAnchors", BuffFrame_UpdateAllBuffAnchors)
Nenue@59 416 hooksecurefunc("TemporaryEnchantFrame_Update", TemporaryEnchantFrame_Update)
Nenue@59 417
Nenue@59 418 -- The TempEnchant frames are hardcoded in the base FrameXML, so get them now
Nenue@59 419 for i = 1, 3 do
Nenue@59 420
Nenue@59 421 SkinFrame('TempEnchant'..i)
Nenue@68 422 _G['TempEnchant'..i..'Border']:SetVertexColor(0.5,0,1,1)
Nenue@59 423
Nenue@59 424 end
Nenue@59 425
Nenue@59 426 plugin.init = function ()
Nenue@61 427
Nenue@61 428
Nenue@61 429
Nenue@59 430 plugin.db = vn.db[PLUGIN_NAME]
Nenue@59 431 end