-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompactUnitFrame.lua
More file actions
438 lines (368 loc) · 12 KB
/
Copy pathCompactUnitFrame.lua
File metadata and controls
438 lines (368 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
local _, ns = ...
--- Batch update frame used to defer frame updates until OnUpdate cycle
local batchFrame = ns.CreateFrame("Frame", nil, UIParent)
batchFrame:Hide()
--- Queue of frames pending updates in current batch
local updateQueue = {}
--- Cache mapping frame → custom shield bar StatusBar
local containers = {}
--- Cache mapping frame → custom overlay bar StatusBar
local overlayContainers = {}
--- Tracks how many batch cycles each unready frame has been retried
local retryCount = {}
--- Maximum OnUpdate cycles to retry an unready frame before dropping it
local MAX_RETRIES = 10
local CACHE_CLEANUP_INTERVAL = 5
local FULL_REFRESH_RECOVERY_DELAY = 0.25
local lastCleanupAt = 0
local pendingRecoveryRefreshToken = 0
--- Exports caches for use by AppearanceManager
ns.absorbCache = containers
ns.overlayCache = overlayContainers
local function ScheduleRecoveryRefresh()
if not ns.UpdateAllFrameAppearances then
return
end
if not C_Timer or not C_Timer.After then
ns.UpdateAllFrameAppearances()
return
end
pendingRecoveryRefreshToken = pendingRecoveryRefreshToken + 1
local refreshToken = pendingRecoveryRefreshToken
C_Timer.After(FULL_REFRESH_RECOVERY_DELAY, function()
if refreshToken ~= pendingRecoveryRefreshToken then
return
end
ns.UpdateAllFrameAppearances()
end)
end
--- Creates or retrieves a custom StatusBar for a compact unit frame.
-- @param cache The cache table to read/write
-- @param frame The compact unit frame
-- @param levelOffset Frame level offset from healthBar (0 = absorb, 1 = overlay)
-- @return StatusBar frame, or nil if healthBar unavailable
local function GetOrCreate(cache, frame, levelOffset)
if cache[frame] then
--@alpha@
ns.Debug.Inc("barReuses")
--@end-alpha@
return cache[frame]
end
local healthBar = frame.healthBar
if not healthBar then return nil end
--@alpha@
ns.Debug.Inc("barCreates")
--@end-alpha@
local bar = ns.CreateFrame("StatusBar", nil, healthBar)
bar:SetAllPoints(healthBar)
bar:SetReverseFill(true)
bar:SetFrameLevel(healthBar:GetFrameLevel() + levelOffset)
bar:Hide()
-- Track anchor mode for conditional positioning
bar._anchorMode = "default"
cache[frame] = bar
return bar
end
local function SuppressNativeAbsorbVisuals(frame)
if not frame then
return
end
--@alpha@
ns.Debug.Inc("nativeBarsSuppressed")
--@end-alpha@
local nativeAbsorb = frame.totalAbsorb
if not ns.FrameIsForbidden(nativeAbsorb) then
nativeAbsorb:Hide()
if not ns.FrameIsForbidden(nativeAbsorb.overlay) then
nativeAbsorb.overlay:Hide()
end
end
local nativeOverlay = frame.totalAbsorbOverlay
if not ns.FrameIsForbidden(nativeOverlay) then
nativeOverlay:Hide()
end
end
function ns.IsKnownCompactUnitFrame(frame)
if ns.FrameIsForbidden(frame) then
return false
end
local frameName = frame.GetName and frame:GetName()
if frameName then
if ns.string_find(frameName, "CompactPartyFrameMember", 1, true) == 1
or ns.string_find(frameName, "CompactRaidFramePet", 1, true) == 1
or ns.string_find(frameName, "CompactPartyFramePet", 1, true) == 1
or ns.string_find(frameName, "CompactRaidFrame", 1, true) == 1 then
return true
end
end
local parent = frame.GetParent and frame:GetParent()
if parent == CompactPartyFrame or parent == CompactRaidFrameContainer then
return true
end
return false
end
function ns.EnforceNativeAbsorbVisibility(frame, profile)
if ns.FrameIsForbidden(frame) or not ns.IsKnownCompactUnitFrame(frame) then
return
end
if not OvershieldsReforged:IsFrameContextEnabled(frame) then
return
end
local db = profile or OvershieldsReforged.db and OvershieldsReforged.db.profile
if not db then
return
end
local glowVisible = ns.IsGlowVisible(frame)
if glowVisible then
SuppressNativeAbsorbVisuals(frame)
return
end
local shieldState = ns.ResolveShieldState(frame, glowVisible)
if not ns.ShouldUseNativeVisualOnly(db, shieldState) then
SuppressNativeAbsorbVisuals(frame)
end
end
local function ApplyNativeVisualOnlyShielded(frame, profile)
ns.HideCustomBars(frame, ns.StyleCache)
ns.ApplyAppearanceToNativeBar(frame.totalAbsorb, false, profile)
ns.ApplyAppearanceToNativeOverlay(frame.totalAbsorbOverlay, false, profile)
end
--- Updates the anchor and fill direction for a bar based on overshield state and user setting.
-- Uses condition-specific anchor mode settings for shielded and overshielded states.
-- @param bar The StatusBar to update
-- @param frame The compact unit frame
-- @param healthBar The parent health bar
-- @param targetMode Normalized anchor mode string
-- @param healthTexture The health bar status bar texture (or nil)
local function UpdateBarAnchor(bar, frame, healthBar, targetMode, healthTexture)
if not bar or not frame or not healthBar then return end
if bar._anchorMode == targetMode then
return
end
--@alpha@
ns.Debug.Inc("anchorModeChanges")
--@end-alpha@
bar._anchorMode = targetMode
bar:ClearAllPoints()
ns.ApplyAnchorStrategy(bar, frame, healthBar, targetMode, healthTexture)
end
local function ApplyCustomBars(frame, profile, healthBar, unit, shieldState, glowVisible, absorbValue)
local _, maxHealth = healthBar:GetMinMaxValues()
absorbValue = absorbValue or (ns.UnitGetTotalAbsorbs(unit) or 0)
local frameVisible = frame:IsVisible()
local healthTexture = healthBar:GetStatusBarTexture()
local targetMode = ns.NormalizeAnchorMode(ns.ResolveAnchorMode(profile, shieldState), healthTexture)
-- Update custom shield bar values using state-specific anchor modes.
local absorb = GetOrCreate(containers, frame, 0)
if absorb then
UpdateBarAnchor(absorb, frame, healthBar, targetMode, healthTexture)
absorb:SetShown(frameVisible)
absorb:SetMinMaxValues(0, maxHealth)
absorb:SetValue(absorbValue)
ns.ApplyAppearanceToBar(absorb, glowVisible, profile)
end
-- Update custom overlay bar values
local overlay = GetOrCreate(overlayContainers, frame, 1)
if overlay then
UpdateBarAnchor(overlay, frame, healthBar, targetMode, healthTexture)
overlay:SetShown(frameVisible)
overlay:SetMinMaxValues(0, maxHealth)
overlay:SetValue(absorbValue)
ns.ApplyAppearanceToOverlay(overlay, glowVisible, profile)
end
end
--- Updates a compact unit frame with current absorb bar values and glow state.
-- Synchronizes bar values with unit API and glow visibility.
-- Appearance is managed exclusively by AppearanceManager.
-- Note: re-checks IsFrameContextEnabled even though QueueCompactUnitFrameUpdate already
-- checked it at queue time — a toggle can flip between queueing and batch processing.
-- @param frame The compact unit frame to update
-- @param profile The active db.profile table
-- @return true if the frame was processed (or intentionally skipped), false if unready for retry
local function HandleCompactUnitFrameUpdate(frame, profile)
if ns.FrameIsForbidden(frame) then
return true
end
if not OvershieldsReforged:IsFrameContextEnabled(frame) then
ns.ReleaseFrame(frame, ns.StyleCache)
return true
end
local unit = frame.displayedUnit
if not unit or not ns.UnitExists(unit) then
--@alpha@
ns.Debug.Inc("earlyExits")
--@end-alpha@
return true
end
local glow = frame.overAbsorbGlow
if ns.FrameIsForbidden(glow) then
--@alpha@
ns.Debug.Inc("earlyExits")
--@end-alpha@
return true
end
local glowVisible = glow:IsVisible()
if glowVisible then
ns.ApplyAppearanceToOverAbsorbGlow(glow, profile)
end
local absorbValue = ns.UnitGetTotalAbsorbs(unit) or 0
local healthBar = frame.healthBar
if not healthBar then
--@alpha@
ns.Debug.Inc("earlyExits")
--@end-alpha@
return false
end
--@alpha@
ns.Debug.Inc("frameUpdates")
--@end-alpha@
local shieldState = ns.ResolveShieldState(frame, glowVisible)
local useNativeVisualOnly = ns.ShouldUseNativeVisualOnly(profile, shieldState)
if useNativeVisualOnly then
ApplyNativeVisualOnlyShielded(frame, profile)
return true
end
SuppressNativeAbsorbVisuals(frame)
ApplyCustomBars(frame, profile, healthBar, unit, shieldState, glowVisible, absorbValue)
return true
end
--- Process queued frame updates once per cycle.
-- Frames whose healthBar is not yet available are retried on subsequent cycles
-- up to MAX_RETRIES times before being dropped.
batchFrame:SetScript("OnUpdate", function()
local profile = OvershieldsReforged.db and OvershieldsReforged.db.profile
if not profile then return end
local now = ns.GetTime()
if now - lastCleanupAt >= CACHE_CLEANUP_INTERVAL then
ns.CleanupStaleCacheEntries()
lastCleanupAt = now
end
--@alpha@
local batchSize = 0
--@end-alpha@
local hasRetries = false
for frame in ns.next, updateQueue do
local success = HandleCompactUnitFrameUpdate(frame, profile)
--@alpha@
batchSize = batchSize + 1
--@end-alpha@
if success then
--@alpha@
if retryCount[frame] then ns.Debug.Inc("retrySuccesses") end
--@end-alpha@
updateQueue[frame] = nil
retryCount[frame] = nil
else
local count = (retryCount[frame] or 0) + 1
--@alpha@
ns.Debug.Inc("retryAttempts")
--@end-alpha@
if count >= MAX_RETRIES then
updateQueue[frame] = nil
retryCount[frame] = nil
ScheduleRecoveryRefresh()
--@alpha@
ns.Debug.Inc("retryDrops")
--@end-alpha@
else
retryCount[frame] = count
hasRetries = true
end
end
end
--@alpha@
ns.Debug.Inc("batchCycles")
ns.Debug.Inc("batchFramesTotal", batchSize)
ns.Debug.Max("peakBatchSize", batchSize)
--@end-alpha@
if not hasRetries then
ns.wipe(updateQueue)
batchFrame:Hide()
end
end)
--- Queues a compact unit frame for appearance update.
-- Frames are batched and processed during the next OnUpdate cycle for efficiency.
-- @param frame The compact unit frame to queue for update
function ns.QueueCompactUnitFrameUpdate(frame)
if ns.hibernating then return end
if ns.FrameIsForbidden(frame) or not ns.IsKnownCompactUnitFrame(frame) then
return
end
--@alpha@
ns.Debug.Inc("queueAttempts")
--@end-alpha@
if not OvershieldsReforged:IsFrameContextEnabled(frame) then
updateQueue[frame] = nil
retryCount[frame] = nil
ns.ReleaseFrame(frame, ns.StyleCache)
--@alpha@
ns.Debug.Inc("queueSkipsDisabled")
--@end-alpha@
return
end
if updateQueue[frame] then
--@alpha@
ns.Debug.Inc("queueSkipsDuplicate")
--@end-alpha@
return
end
--@alpha@
ns.Debug.Inc("queueAdds")
--@end-alpha@
updateQueue[frame] = true
batchFrame:Show()
end
--- Releases all custom bars, hiding them and clearing both container caches.
-- Also restores all previously managed frames to vanilla Blizzard behavior.
-- Called on profile change or hibernation to ensure a clean slate.
function ns.ReleaseAllBars()
local frames = {}
for frame in ns.next, containers do
frames[frame] = true
end
for frame in ns.next, overlayContainers do
frames[frame] = true
end
for frame in ns.next, frames do
ns.HideCustomBars(frame, ns.StyleCache)
ns.RestoreNativeAbsorbVisuals(frame, ns.StyleCache)
end
-- Clear all caches
ns.wipe(containers)
ns.wipe(overlayContainers)
ns.wipe(updateQueue)
ns.wipe(retryCount)
pendingRecoveryRefreshToken = pendingRecoveryRefreshToken + 1
batchFrame:Hide()
end
--- Removes cache entries for frames that no longer display a unit or are hidden.
-- Restores frames to vanilla Blizzard behavior before removing from cache.
-- Safe to call periodically to prevent stale entries from accumulating.
function ns.CleanupStaleCacheEntries()
local framesToClean = {}
for frame in ns.next, containers do
if ns.FrameIsForbidden(frame) or not frame.displayedUnit or not frame:IsShown() then
framesToClean[frame] = true
end
end
for frame in ns.next, overlayContainers do
if ns.FrameIsForbidden(frame) or not frame.displayedUnit or not frame:IsShown() then
framesToClean[frame] = true
end
end
for frame in ns.next, framesToClean do
ns.ReleaseFrame(frame, ns.StyleCache)
if containers[frame] then
containers[frame] = nil
end
if overlayContainers[frame] then
overlayContainers[frame] = nil
end
end
end
local cleanupEventFrame = ns.CreateFrame("Frame")
cleanupEventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
cleanupEventFrame:RegisterEvent("GROUP_ROSTER_UPDATE")
cleanupEventFrame:SetScript("OnEvent", function()
ns.CleanupStaleCacheEntries()
end)