comparison Modules/BuffFrame.lua @ 59:07ef62fe201f

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