annotate Modules/BuffFrame.lua @ 64:ba9c13261bb2

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