annotate Modules/BuffFrame.lua @ 85:1196b8175674

missing "else" control
author Nenue
date Tue, 18 Oct 2016 13:16:57 -0400
parents 16b300d96724
children 48182978d1c6
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@75 14 local BUFFS_PER_ROW = 12
Nenue@64 15 local BUFF_BUTTON_SIZE = 48
Nenue@71 16 local BUFF_BUTTON_SPACING_H = 4
Nenue@71 17 local BUFF_BUTTON_SPACING_V = 14
Nenue@64 18 local BUFF_PROGRESS_SIZE = 4
Nenue@64 19 local BUFF_PROGRESS_INSET = 1
Nenue@68 20 local BUFF_BUTTON_ZOOM = .15
Nenue@79 21 local BORDER_SIZE_L = 1
Nenue@79 22 local BORDER_SIZE_R = 1
Nenue@79 23 local BORDER_SIZE_U = 1
Nenue@79 24 local BORDER_SIZE_D = 1
Nenue@75 25 local BUFF_FRAMES_X = -230
Nenue@75 26 local BUFF_FRAMES_Y = -4
Nenue@64 27
Nenue@84 28 VeneerBuffFrameMixin = {
Nenue@84 29 moduleName = 'Buff Frames',
Nenue@84 30 defaultCluster = 'TOPRIGHT',
Nenue@84 31 Buttons = {},
Nenue@84 32 DetectedFrames = {},
Nenue@84 33 AuraCache = {}
Nenue@84 34 }
Nenue@84 35 local plugin = VeneerBuffFrameMixin
Nenue@62 36
Nenue@84 37 local vn = Veneer
Nenue@84 38 local print = DEVIAN_WORKSPACE and function(...) _G.print('BuffFrame', ...) end or function() end
Nenue@68 39 local tprint = DEVIAN_WORKSPACE and function(...) _G.print('Timer', ...) end or function() end
Nenue@59 40
Nenue@68 41 local _G, UIParent = _G, UIParent
Nenue@68 42 local tinsert, tremove, unpack, select, tconcat = table.insert, table.remove, unpack, select, table.concat
Nenue@68 43 local floor, tonumber, format = math.floor, tonumber, string.format
Nenue@68 44 local UnitAura, GetTime, CreateFrame = UnitAura, GetTime, CreateFrame
Nenue@68 45 local hooksecurefunc = hooksecurefunc
Nenue@59 46
Nenue@75 47 local aurasCache = {}
Nenue@75 48 local skinnedFrames = {}
Nenue@75 49 local pendingFrames = {}
Nenue@75 50 local veneers = {}
Nenue@75 51 local expirationCache = {}
Nenue@75 52 local visibility = {}
Nenue@75 53 local isHooked = {}
Nenue@75 54
Nenue@75 55 plugin.options = {
Nenue@75 56 nameString = 'Buff Frames',
Nenue@59 57 {
Nenue@75 58 name = 'BuffButtonZoom',
Nenue@75 59 type = 'slider',
Nenue@75 60 min = 0,
Nenue@75 61 max = 100,
Nenue@75 62 fullwidth = true,
Nenue@59 63 },
Nenue@59 64 {
Nenue@75 65 name = 'BuffBorderLeft',
Nenue@75 66 type = 'slider',
Nenue@75 67 min = 0,
Nenue@75 68 max = 16,
Nenue@59 69 },
Nenue@59 70 {
Nenue@75 71 name = 'BuffBorderLeft',
Nenue@75 72 type = 'slider',
Nenue@75 73 min = 0,
Nenue@75 74 max = 16,
Nenue@59 75 }
Nenue@59 76 }
Nenue@59 77
Nenue@59 78
Nenue@84 79 function plugin:Acquire(target)
Nenue@75 80
Nenue@84 81 local frame = self.Buttons[target]
Nenue@84 82 if not (self.Buttons[target]) then
Nenue@84 83 local name = target:GetName()
Nenue@84 84 local id = target:GetID()
Nenue@68 85 print('|cFF88FF00Creating', name,'Veneer')
Nenue@59 86
Nenue@84 87 frame = vn:Acquire(target, 'VeneerBuffTemplate')
Nenue@59 88
Nenue@84 89 frame.progress:SetHeight(BUFF_PROGRESS_SIZE + (BUFF_PROGRESS_INSET * 2))
Nenue@59 90
Nenue@84 91 frame.progress.fg:ClearAllPoints()
Nenue@84 92 frame.progress.fg:SetPoint('BOTTOMLEFT', BUFF_PROGRESS_INSET,BUFF_PROGRESS_INSET)
Nenue@84 93 frame.progress.fg:SetPoint('TOP', 0, -BUFF_PROGRESS_INSET)
Nenue@75 94
Nenue@59 95
Nenue@84 96 frame.underlay:SetParent(UIParent)
Nenue@84 97 frame.underlay:SetFrameStrata('BACKGROUND')
Nenue@84 98 frame.border:SetColorTexture(0,0,0,1)
Nenue@84 99 frame.border:SetPoint('TOPLEFT', frame, 'TOPLEFT', -BORDER_SIZE_L, BORDER_SIZE_U)
Nenue@84 100 frame.border:SetPoint('BOTTOMRIGHT', frame, 'BOTTOMRIGHT', BORDER_SIZE_R, -BORDER_SIZE_D)
Nenue@84 101 frame.border:Show()
Nenue@59 102
Nenue@84 103 self.Buttons[target] = frame
Nenue@59 104 end
Nenue@84 105 return frame
Nenue@59 106 end
Nenue@59 107
Nenue@84 108 function plugin:OnLoad()
Nenue@84 109 Veneer:AddHandler(self, self.defaultCluster)
Nenue@84 110 end
Nenue@68 111
Nenue@84 112 function plugin:Setup()
Nenue@84 113
Nenue@84 114
Nenue@84 115 hooksecurefunc("BuffFrame_Update", function(...) self:OnBuffFrameUpdate(...) end)
Nenue@84 116 hooksecurefunc("AuraButton_UpdateDuration", function(...) self:OnUpdateDuration(...) end)
Nenue@84 117 hooksecurefunc("AuraButton_Update", function(...) self:OnAuraButton_Update(...) end)
Nenue@84 118 hooksecurefunc("BuffFrame_UpdateAllBuffAnchors", function(...) self:OnUpdateAllBuffAnchors(...) end)
Nenue@84 119 hooksecurefunc("TemporaryEnchantFrame_Update", function(...) self:OnTemporaryEnchantFrameUpdate(...) end)
Nenue@84 120 for i = 1, 3 do
Nenue@84 121 self:SetupButton('TempEnchant'..i)
Nenue@84 122 _G['TempEnchant'..i..'Border']:SetVertexColor(0.5,0,1,1)
Nenue@84 123 end
Nenue@84 124 end
Nenue@68 125 -- Associates skinning elements with said button
Nenue@84 126 local surrogates = {
Nenue@84 127 Show = false,
Nenue@84 128 Hide = false,
Nenue@84 129 SetText = false,
Nenue@84 130 SetVertexColor = function(surrogate, frame, r, g, b, a)
Nenue@84 131 frame:Hide()
Nenue@84 132 print('|cFF0088FFborder:SetVertexColor|r', r,g,b,a)
Nenue@84 133 surrogate.progress.fg:SetColorTexture(r,g,b,a)
Nenue@84 134 surrogate.border:Show()
Nenue@84 135 end,
Nenue@84 136 }
Nenue@84 137 local DoRegionHooks = function (veneer, region)
Nenue@84 138
Nenue@84 139 if region then
Nenue@84 140 print('hooking', region:GetName())
Nenue@84 141 region:ClearAllPoints()
Nenue@84 142 for method, callback in ipairs(surrogates) do
Nenue@84 143 print(method, callback)
Nenue@84 144 if region[method] then
Nenue@84 145 local func
Nenue@84 146 if callback then
Nenue@84 147 print('hooking', region:GetName(), method)
Nenue@84 148 func = function(self,...)
Nenue@84 149 print(self:GetName(), ':', method)
Nenue@84 150 self:ClearAllPoints()
Nenue@84 151 veneer[method](...)
Nenue@84 152 end
Nenue@84 153 else
Nenue@84 154 func = function(self, ...)
Nenue@84 155 self:ClearAllPoints()
Nenue@84 156 callback(veneer, region, ...)
Nenue@84 157 end
Nenue@84 158 end
Nenue@84 159 hooksecurefunc(region, method, callback)
Nenue@84 160 end
Nenue@84 161 end
Nenue@84 162 end
Nenue@84 163 end
Nenue@84 164
Nenue@84 165
Nenue@84 166 function plugin:SetupButton (name)
Nenue@68 167 local frame = _G[name ]
Nenue@84 168 if self.DetectedFrames[frame] then
Nenue@68 169 print('|cFFFF4400Attempting to skin a frame that already went through.|r')
Nenue@68 170 return
Nenue@68 171 end
Nenue@68 172 print('|cFFFFFF00Adopting', name)
Nenue@68 173
Nenue@68 174 local icon = _G[name .. 'Icon']
Nenue@68 175 local border = _G[name .. 'Border']
Nenue@68 176 local count = _G[name .. 'Count']
Nenue@68 177 local duration = _G[name .. 'Duration']
Nenue@84 178 local veneer = self:Acquire(frame)
Nenue@84 179 local offset = BUFF_BUTTON_ZOOM/2
Nenue@68 180
Nenue@84 181 self.DetectedFrames[frame] = frame
Nenue@68 182 frame:SetSize(BUFF_BUTTON_SIZE,BUFF_BUTTON_SIZE)
Nenue@84 183 icon:SetTexCoord(offset, 1 - offset, offset, 1 - offset)
Nenue@68 184
Nenue@78 185
Nenue@84 186 DoRegionHooks(veneer, border)
Nenue@84 187 DoRegionHooks(veneer.duration, duration)
Nenue@84 188 DoRegionHooks(veneer.count, count)
Nenue@68 189 if border then
Nenue@68 190 local color = DebuffTypeColor["none"]
Nenue@68 191 if aurasCache[frame] and aurasCache[frame][5] then
Nenue@68 192 color = DebuffTypeColor[aurasCache[frame][5]]
Nenue@68 193 end
Nenue@71 194 veneer.progress.fg:SetColorTexture(color.r,color.g,color.b)
Nenue@75 195 veneer.border:SetColorTexture(0,0,0,1)
Nenue@79 196 veneer.border:Show()
Nenue@79 197 else
Nenue@79 198 veneer.border:SetColorTexture(0,0,0,1)
Nenue@79 199 veneer.border:Show()
Nenue@68 200 end
Nenue@68 201
Nenue@68 202
Nenue@68 203 hooksecurefunc(frame, "Hide", function(self)
Nenue@68 204 local isVisible = self:IsVisible()
Nenue@68 205 if isVisible ~= visibility[self] then
Nenue@68 206 visibility[self] = isVisible
Nenue@68 207 end
Nenue@68 208 veneer:Hide()
Nenue@73 209 veneer.underlay:Hide()
Nenue@68 210 end)
Nenue@68 211
Nenue@68 212 hooksecurefunc(frame, 'Show', function(self)
Nenue@68 213 veneer:Show()
Nenue@68 214 local isVisible = self:IsVisible()
Nenue@68 215 if isVisible ~= visibility[self] then
Nenue@68 216 print('|cFFFFFF00SHOW|r', self:GetName())
Nenue@68 217 visibility[self] = isVisible
Nenue@68 218 end
Nenue@74 219 veneer.underlay:Show()
Nenue@68 220 end)
Nenue@68 221
Nenue@68 222 end
Nenue@68 223
Nenue@68 224
Nenue@61 225 --- Set widgets to reflect the passed parameters
Nenue@84 226 function plugin:UpdateButton (frame, duration, expires)
Nenue@84 227 local veneer = self:Acquire(frame)
Nenue@68 228 -- is it a new button?
Nenue@84 229 if not self.DetectedFrames[frame] then
Nenue@84 230 self:SetupButton(frame:GetName())
Nenue@68 231 end
Nenue@68 232
Nenue@59 233
Nenue@61 234 if expires and duration then
Nenue@61 235 if duration ~= 0 then
Nenue@61 236 local startTime = (expires - duration)
Nenue@61 237 local endTime = expires or 0
Nenue@61 238 print('|cFF0088FF'..frame:GetName()..'|r', duration, expires)
Nenue@61 239 veneer.progress:Show()
Nenue@61 240 veneer.elapsed = 0
Nenue@61 241 veneer.progress:SetScript('OnUpdate', function(self, elapsed)
Nenue@61 242 veneer.elapsed = veneer.elapsed + elapsed
Nenue@60 243
Nenue@67 244 local w = floor(veneer.progress:GetWidth()+.5) - (BUFF_PROGRESS_INSET*2)
Nenue@61 245 local t = GetTime()
Nenue@61 246 local progress = (t - startTime) / duration
Nenue@61 247
Nenue@67 248 local nw = (w - (w * progress))
Nenue@61 249 if veneer.elapsed >= 0.25 then
Nenue@61 250
Nenue@68 251 tprint(t, startTime, floor(progress*100), w * progress, nw, w)
Nenue@61 252 veneer.elapsed = 0.25 - veneer.elapsed
Nenue@61 253 end
Nenue@61 254 if (progress >= 1) or not frame:IsVisible() then
Nenue@61 255 veneer.startTime = nil
Nenue@61 256 self:Hide()
Nenue@61 257 self:SetScript('OnUpdate', nil)
Nenue@61 258 else
Nenue@61 259 self.fg:SetWidth(nw)
Nenue@61 260 end
Nenue@61 261 end)
Nenue@61 262
Nenue@61 263 veneer.cooldown:Show()
Nenue@61 264 veneer.cooldown:SetCooldown(startTime, duration)
Nenue@61 265 else
Nenue@61 266 print('|cFF00FF88'..frame:GetName()..'|r', 'duration zero')
Nenue@61 267 veneer.progress:SetScript('OnUpdate', nil)
Nenue@61 268 veneer.progress:Hide()
Nenue@61 269 veneer.cooldown:Hide()
Nenue@61 270 end
Nenue@61 271 else
Nenue@61 272 veneer.progress:Hide()
Nenue@61 273 veneer.cooldown:SetCooldown(0,0)
Nenue@61 274 veneer.cooldown:Hide()
Nenue@61 275 print('|cFF88FF00'..frame:GetName()..'|r', 'nil duration')
Nenue@59 276 end
Nenue@59 277 veneer:Show()
Nenue@59 278 end
Nenue@59 279
Nenue@59 280
Nenue@59 281 --- Provides the number of changed indices for use in deciding between partial and full veneer updates
Nenue@84 282 function plugin:ButtonHasChanged (frame, ...)
Nenue@59 283 aurasCache[frame] = aurasCache[frame] or {}
Nenue@59 284 local hasChange = 0
Nenue@59 285 local numVals = select('#',...)
Nenue@59 286 for i = 1, numVals do
Nenue@59 287 local arg = select(i, ...)
Nenue@59 288 if aurasCache[frame][i] ~= arg then
Nenue@59 289 hasChange = hasChange + 1
Nenue@59 290 end
Nenue@59 291 aurasCache[frame][i] = arg
Nenue@59 292 end
Nenue@59 293 return hasChange
Nenue@59 294 end
Nenue@59 295
Nenue@84 296 function plugin:OnAuraButton_Update (name, index, filter)
Nenue@59 297 local bName = name..index
Nenue@59 298 local frame = _G[bName]
Nenue@59 299 if frame and frame:IsVisible() then
Nenue@84 300 local cacheDiff = self:ButtonHasChanged(frame, UnitAura(frame.unit, frame:GetID(), frame.filter))
Nenue@61 301 -- if the name or expirationTime changed
Nenue@61 302 if (cacheDiff >= 1) then
Nenue@68 303 print('|cFFFF4400', frame:GetName(), 'diff:', cacheDiff)
Nenue@61 304 if not skinnedFrames[frame] then
Nenue@61 305 tinsert(pendingFrames, frame)
Nenue@61 306 end
Nenue@59 307 expirationCache[name] = frame.expirationTime
Nenue@59 308 print(unpack(aurasCache[frame]))
Nenue@68 309
Nenue@84 310 self:UpdateButton(frame, aurasCache[frame][6], aurasCache[frame][7])
Nenue@59 311 end
Nenue@59 312
Nenue@59 313 end
Nenue@59 314 end
Nenue@59 315
Nenue@84 316 function plugin:OnUpdateAllBuffAnchors ()
Nenue@59 317 local todo = {}
Nenue@59 318 if #pendingFrames >= 1 then
Nenue@59 319
Nenue@59 320 print('|cFFBBFF00AllBuffAnchors|r', #pendingFrames)
Nenue@59 321 while pendingFrames[1] do
Nenue@59 322 local frame = tremove(pendingFrames)
Nenue@59 323 tinsert(todo, frame:GetName())
Nenue@59 324
Nenue@61 325 -- re-apply custom anchors
Nenue@59 326 end
Nenue@68 327 print(tconcat(todo, ', '))
Nenue@59 328 end
Nenue@59 329 --BuffButton1
Nenue@59 330 --DebuffButton1
Nenue@61 331 --todo: separate frame groups and iterate over them at appropriate times
Nenue@60 332 if BuffButton1 then
Nenue@78 333
Nenue@68 334 TempEnchant1:SetPoint('TOPRIGHT', BuffButton1, 'TOPRIGHT', BuffButton1:GetWidth()+4, 0)
Nenue@60 335 end
Nenue@60 336
Nenue@70 337 local lastBuff, topBuff
Nenue@74 338 local numBuffs = 0
Nenue@70 339 for i = 1, BUFF_ACTUAL_DISPLAY do
Nenue@70 340 local buff = _G['BuffButton'..i]
Nenue@70 341 if buff then
Nenue@74 342 numBuffs = numBuffs + 1
Nenue@74 343 buff:ClearAllPoints()
Nenue@75 344 if mod(numBuffs,BUFFS_PER_ROW) == 1 then
Nenue@74 345 if numBuffs == 1 then
Nenue@80 346 buff:SetPoint('TOPRIGHT', UIParent, 'TOPRIGHT', BUFF_FRAMES_X, BUFF_FRAMES_Y)
Nenue@74 347 plugin.currentTop = buff:GetTop()
Nenue@71 348 else
Nenue@71 349 buff:SetPoint('TOPRIGHT', topBuff, 'BOTTOMRIGHT', 0, -BUFF_BUTTON_SPACING_V)
Nenue@71 350 end
Nenue@70 351 topBuff = buff
Nenue@70 352 else
Nenue@71 353 buff:SetPoint('TOPRIGHT', lastBuff, 'TOPLEFT', -BUFF_BUTTON_SPACING_H, 0)
Nenue@70 354 end
Nenue@70 355 lastBuff = buff
Nenue@70 356 end
Nenue@70 357 end
Nenue@70 358
Nenue@74 359 numBuffs = 0
Nenue@70 360 for i = 1, DEBUFF_ACTUAL_DISPLAY do
Nenue@70 361 local debuff = _G['DebuffButton'..i]
Nenue@70 362 if debuff then
Nenue@74 363 numBuffs = numBuffs + 1
Nenue@74 364 if numBuffs == 1 then
Nenue@70 365 if topBuff then
Nenue@71 366 debuff:SetPoint('TOPRIGHT', topBuff, 'BOTTOMRIGHT', 0, -BUFF_BUTTON_SPACING_V)
Nenue@70 367 else
Nenue@70 368 debuff:SetPoint('TOPRIGHT', UIParent, 'TOPRIGHT', -120, -6)
Nenue@70 369 end
Nenue@70 370 topBuff = debuff
Nenue@75 371 elseif mod(numBuffs, BUFFS_PER_ROW) == 1 then
Nenue@71 372 debuff:SetPoint('TOPRIGHT', topBuff, 'BOTTOMRIGHT', 0, -BUFF_BUTTON_SPACING_V)
Nenue@70 373 topBuff = debuff
Nenue@70 374 else
Nenue@71 375 debuff:SetPoint('TOPRIGHT', lastBuff, 'TOPLEFT', -BUFF_BUTTON_SPACING_H, 0)
Nenue@70 376 end
Nenue@70 377 lastBuff = debuff
Nenue@70 378 end
Nenue@70 379 end
Nenue@70 380
Nenue@74 381 if lastBuff then
Nenue@74 382 plugin.currentBottom = lastBuff:GetBottom()
Nenue@74 383 end
Nenue@59 384 end
Nenue@59 385
Nenue@84 386 function plugin:OnUpdateDuration (frame, timeLeft)
Nenue@84 387 local veneer = self:Acquire(frame)
Nenue@60 388 local hours = floor(timeLeft/3600)
Nenue@60 389 local minutes = floor(mod(timeLeft, 3600)/60)
Nenue@60 390 local seconds = floor(mod(timeLeft, 60))
Nenue@60 391 local timeString = '%ds'
Nenue@59 392 if timeLeft > 3600 then
Nenue@60 393 timeString = format('%d:%02d', hours, minutes)
Nenue@60 394 elseif timeLeft > 60 then
Nenue@60 395 timeString = format('%d:%02d', minutes, seconds)
Nenue@61 396 else
Nenue@60 397 timeString = format('%d', seconds)
Nenue@59 398 end
Nenue@59 399
Nenue@74 400 if timeLeft < 10 then
Nenue@74 401 if not veneer.duration.getHuge then
Nenue@74 402 veneer.duration.getHuge = true
Nenue@74 403 veneer.duration:SetFontObject(VeneerNumberFontLarge)
Nenue@75 404 veneer.duration:SetTextColor(1,1,0,1)
Nenue@74 405 end
Nenue@74 406 else
Nenue@74 407 if veneer.duration.getHuge then
Nenue@74 408 veneer.duration.getHuge = nil
Nenue@74 409 veneer.duration:SetFontObject(VeneerNumberFont)
Nenue@74 410 veneer.duration:SetTextColor(1,1,1,1)
Nenue@74 411 end
Nenue@74 412
Nenue@74 413 end
Nenue@74 414
Nenue@60 415
Nenue@69 416 veneer.duration:SetText(timeString)
Nenue@59 417 end
Nenue@59 418
Nenue@59 419
Nenue@59 420 -- Obtains the first instance of Tenchant use
Nenue@59 421
Nenue@84 422 function plugin:OnTemporaryEnchantFrameUpdate (...)
Nenue@59 423 local numVals = select('#', ...)
Nenue@59 424 local numItems = numVals / 4
Nenue@59 425 if numItems >= 1 then
Nenue@59 426 for itemIndex = numItems, 1, -1 do
Nenue@59 427 local frame = _G['TempEnchant'..itemIndex]
Nenue@59 428 local hasEnchant, timeRemaining, enchantCharges = select((4 * (itemIndex -1)) + 1, ...)
Nenue@59 429
Nenue@59 430
Nenue@59 431 if hasEnchant then
Nenue@59 432 local endTime = floor(GetTime()*1000) + timeRemaining
Nenue@59 433
Nenue@59 434
Nenue@59 435 --print(endTime)
Nenue@59 436 if endTime ~= expirationCache[frame] then
Nenue@59 437 if expirationCache[frame] then
Nenue@59 438 print(endTime, expirationCache[frame], endTime - expirationCache[frame])
Nenue@59 439 end
Nenue@59 440 expirationCache[frame] = endTime
Nenue@59 441 print('push tempenchant timer update', timeRemaining / 1000, GetTime()+(timeRemaining/1000))
Nenue@59 442 UpdateVeneer(frame, timeRemaining/1000, GetTime()+(timeRemaining/1000))
Nenue@59 443 end
Nenue@59 444 else
Nenue@84 445 self:Acquire(frame):Hide()
Nenue@59 446 end
Nenue@59 447
Nenue@59 448 end
Nenue@59 449
Nenue@59 450 end
Nenue@59 451
Nenue@59 452 end
Nenue@84 453 function plugin:OnBuffFrameUpdate ()
Nenue@59 454 end
Nenue@59 455
Nenue@59 456
Nenue@84 457 -- The TempEnchant frames are hardcoded in the base FrameXML, so get them now
Nenue@59 458