-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffs.lua
More file actions
1418 lines (1263 loc) · 49.1 KB
/
Copy pathBuffs.lua
File metadata and controls
1418 lines (1263 loc) · 49.1 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
setfenv(1, _G.SlackHacks)
--[[
Reminds the player to keep up raid/dungeon consumables (food, flask, oil, augment rune) by showing
clickable icons, similar in spirit to the "ClickableRaidBuffs" addon -- but event-driven instead of
polling aura events constantly. Auras/bags are only rescanned on: PLAYER_ENTERING_WORLD (login/zoning
into an instance), CHALLENGE_MODE_START (key start), entering/leaving combat, joining/leaving a
group, and bag changes (using an item).
]]--
local module = Self:NewModule("Buffs", "AceEvent-3.0")
Self.Buffs = module
local AURA_BUTTON_WIDTH = 30
local MYTHIC_DUNGEON_DIFFICULTY_IDS = { [8] = true, [23] = true } -- Mythic Keystone, Mythic (non-keystone)
local RAID_DIFFICULTY_IDS = { [14] = true, [15] = true, [16] = true } -- Normal, Heroic, Mythic raid (excludes LFR = 17)
local MYTHIC_DUNGEON_THRESHOLD_SECONDS = 40 * 60
local RAID_THRESHOLD_SECONDS = 15 * 60
local runeBuffIDSet = {}
local wellFedBuffIDSet = { [1284617] = true }
-- Buff categories, in display order. `itemNames` key into StaticData's ITEM_NAMES table.
local BUFF_CATEGORIES = {
{
dbKey = "wellFed",
label = "Food",
icon = 133943,
itemNames = FOOD_ITEM_NAMES,
itemIDs = FOOD_ITEM_IDS,
matchAura = function(auraName, spellID)
if wellFedBuffIDSet[spellID] then return true end
return type(auraName) == "string" and auraName:find("Well Fed", 1, true) ~= nil
end,
},
{
dbKey = "flask",
label = "Flask",
icon = 132380,
itemNames = FLASK_ITEM_NAMES,
itemIDs = FLASK_ITEM_IDS,
matchAura = function(auraName, _, itemNameSet) return itemNameSet[auraName] == true end,
},
{
dbKey = "oil",
label = "Oil",
icon = 7548987,
itemNames = OIL_ITEM_NAMES,
itemIDs = OIL_ITEM_IDS,
matchAura = function(_, spellID) return spellID == 1237006 end,
},
{
dbKey = "rune",
label = "Augment Rune",
icon = 4549099,
itemNames = RUNE_ITEM_NAMES,
itemIDs = RUNE_ITEM_IDS,
matchAura = function(_, spellID) return runeBuffIDSet[spellID] == true end,
},
}
for _, category in ipairs(BUFF_CATEGORIES) do
local itemNameSet = {}
for _, itemName in ipairs(category.itemNames) do
itemNameSet[itemName] = true
end
category.itemNameSet = itemNameSet
end
for _, spellID in ipairs(RUNE_BUFF_IDS) do
runeBuffIDSet[spellID] = true
end
local container
local iconButtons = {}
local selection
local editModeDialog
local isEditing = false
local menuFrame
local menuRows = {}
local menuCloseTimer
local auraEventRegistered = false
local trackedAuras = {}
local cachedAuraExpirations = {}
local auraCacheInitialized = false
local durationUpdateTimer
local function closeContextMenu()
if InCombatLockdown() then return end
if menuCloseTimer then
menuCloseTimer:Cancel()
menuCloseTimer = nil
end
if menuFrame then
menuFrame:Hide()
menuFrame.anchorButton = nil
menuFrame.category = nil
end
end
--- Closes the menu shortly after the mouse leaves both the anchor icon and the menu panel.
local function scheduleMenuClose()
if menuCloseTimer then
menuCloseTimer:Cancel()
end
menuCloseTimer = C_Timer.NewTimer(0.25, function()
menuCloseTimer = nil
if not menuFrame then return end
if menuFrame:IsMouseOver() then return end
local anchor = menuFrame.anchorButton
if anchor and anchor:IsMouseOver() then return end
closeContextMenu()
end)
end
local function cancelMenuClose()
if menuCloseTimer then
menuCloseTimer:Cancel()
menuCloseTimer = nil
end
end
local QUALITY_ATLAS_BY_ITEM_ID = {
[241320] = "Professions-Icon-Quality-12-Tier2-Inv",
[241321] = "Professions-Icon-Quality-12-Tier1-Inv",
[241322] = "Professions-Icon-Quality-12-Tier2-Inv",
[241323] = "Professions-Icon-Quality-12-Tier1-Inv",
[241324] = "Professions-Icon-Quality-12-Tier2-Inv",
[241325] = "Professions-Icon-Quality-12-Tier1-Inv",
[241326] = "Professions-Icon-Quality-12-Tier2-Inv",
[241327] = "Professions-Icon-Quality-12-Tier1-Inv",
[243733] = "Professions-Icon-Quality-12-Tier1-Inv",
[243734] = "Professions-Icon-Quality-12-Tier2-Inv",
}
local function setNativeOverlayGlow(button, enabled)
if ActionButtonSpellAlertManager then
if enabled then
ActionButtonSpellAlertManager:ShowAlert(button)
else
ActionButtonSpellAlertManager:HideAlert(button)
end
elseif enabled and ActionButton_ShowOverlayGlow then
ActionButton_ShowOverlayGlow(button)
elseif not enabled and ActionButton_HideOverlayGlow then
ActionButton_HideOverlayGlow(button)
end
end
local function categoryItemIDs(category)
if category.resolvedItemIDs then return category.resolvedItemIDs end
local ids, seen = {}, {}
for _, itemName in ipairs(category.itemNames) do
local itemID = ITEM_NAMES and ITEM_NAMES[itemName]
if itemID and not seen[itemID] then
seen[itemID] = true
table.insert(ids, itemID)
end
end
for _, itemID in ipairs(category.itemIDs or {}) do
if not seen[itemID] then
seen[itemID] = true
table.insert(ids, itemID)
end
end
category.resolvedItemIDs = ids
return category.resolvedItemIDs
end
local function categoryIcon(category)
return category.icon or SLACKHACKS_ICON
end
local function lastUsedItems()
local buffs = db.profile.buffs
buffs.lastUsedItems = buffs.lastUsedItems or {}
return buffs.lastUsedItems
end
local function lastUsedItemID(category)
return lastUsedItems()[category.dbKey]
end
--- Returns false if a secret aura (e.g. right as a Mythic+ key or encounter starts) aborted the scan
--- partway through, so the caller knows not to trust it as a complete picture of the player's auras.
local function forEachPlayerBuff(callback)
for i = 1, 40 do
-- A secret aura throws instead of returning nil while tainted; stop rather than keep hitting it.
local ok, aura = pcall(C_UnitAuras.GetAuraDataByIndex, "player", i, "HELPFUL")
if not ok then return false end
if not aura then break end
callback(aura)
end
return true
end
local function recalculateAuraExpirations()
wipe(cachedAuraExpirations)
for _, aura in pairs(trackedAuras) do
local current = cachedAuraExpirations[aura.categoryKey]
if aura.expiration == 0 then
cachedAuraExpirations[aura.categoryKey] = 0
elseif current ~= 0 and (not current or aura.expiration > current) then
cachedAuraExpirations[aura.categoryKey] = aura.expiration
end
end
end
local function trackAura(aura)
local auraInstanceID = aura and aura.auraInstanceID
if not auraInstanceID then return end
local previous = trackedAuras[auraInstanceID]
trackedAuras[auraInstanceID] = nil
for _, category in ipairs(BUFF_CATEGORIES) do
if category.dbKey ~= "oil" and category.matchAura(aura.name, aura.spellId, category.itemNameSet) then
trackedAuras[auraInstanceID] = {
categoryKey = category.dbKey,
expiration = aura.expirationTime or 0,
}
local current = trackedAuras[auraInstanceID]
return not previous
or previous.categoryKey ~= current.categoryKey
or previous.expiration ~= current.expiration
end
end
return previous ~= nil
end
local function auraCachesDiffer(previous, current)
for auraInstanceID, aura in pairs(current) do
local previousAura = previous[auraInstanceID]
if not previousAura
or previousAura.categoryKey ~= aura.categoryKey
or previousAura.expiration ~= aura.expiration then
return true
end
end
for auraInstanceID in pairs(previous) do
if not current[auraInstanceID] then return true end
end
return false
end
local function rebuildAuraCache()
local previousTrackedAuras = trackedAuras
trackedAuras = {}
if forEachPlayerBuff(trackAura) then
recalculateAuraExpirations()
auraCacheInitialized = true
return auraCachesDiffer(previousTrackedAuras, trackedAuras)
else
-- Auras are secret for now; keep the last known-good state instead of showing every buff as missing.
trackedAuras = previousTrackedAuras
return false
end
end
local function updateAuraCache(updateInfo)
if not auraCacheInitialized or not updateInfo then
return rebuildAuraCache()
end
-- In restricted contexts (combat/instances), aura update fields can be "secret" values/tables that
-- our tainted code isn't allowed to boolean-test or use as table keys; fall back to a full (safe)
-- rebuild whenever that's the case, since a full rebuild never depends on reading those fields.
local isSecretUpdate = (issecretvalue and issecretvalue(updateInfo.isFullUpdate))
or (issecrettable and (issecrettable(updateInfo.removedAuraInstanceIDs)
or issecrettable(updateInfo.updatedAuraInstanceIDs)
or issecrettable(updateInfo.addedAuras)))
if isSecretUpdate or updateInfo.isFullUpdate then
return rebuildAuraCache()
end
local changed = false
for _, auraInstanceID in ipairs(updateInfo.removedAuraInstanceIDs or {}) do
if trackedAuras[auraInstanceID] then
trackedAuras[auraInstanceID] = nil
changed = true
end
end
for _, aura in ipairs(updateInfo.addedAuras or {}) do
changed = trackAura(aura) or changed
end
for _, auraInstanceID in ipairs(updateInfo.updatedAuraInstanceIDs or {}) do
local ok, aura = pcall(C_UnitAuras.GetAuraDataByAuraInstanceID, "player", auraInstanceID)
if ok then
changed = trackAura(aura) or changed
end
end
if changed then recalculateAuraExpirations() end
return changed
end
local function oilBuffExpiration()
if not GetWeaponEnchantInfo then return nil end
local hasMainHandEnchant, mainHandExpiration, _, _, hasOffHandEnchant, offHandExpiration = GetWeaponEnchantInfo()
local expiration
if hasMainHandEnchant and mainHandExpiration then
expiration = GetTime() + (mainHandExpiration / 1000)
end
if hasOffHandEnchant and offHandExpiration then
local offHandEnd = GetTime() + (offHandExpiration / 1000)
expiration = not expiration and offHandEnd or math.max(expiration, offHandEnd)
end
if expiration then return expiration end
return nil
end
--- Returns the latest expiration time (seconds, epoch-relative via GetTime()) for a matching buff on the
--- player, `0` if a matching permanent buff is found, or `nil` if the buff isn't active at all.
local function categoryBuffExpiration(category)
if category.dbKey == "oil" then
return oilBuffExpiration()
end
return cachedAuraExpirations[category.dbKey]
end
--- Which items in the player's bags can currently fulfill this category, with their bag/slot/count.
local function categoryBagItems(category)
local items = {}
for _, itemID in ipairs(categoryItemIDs(category)) do
local count = C_Item.GetItemCount(itemID)
if count > 0 then
local bag, slot = findBagItem(itemID)
local itemName = C_Item.GetItemNameByID(itemID) or ITEM_NAMES_BY_ID and ITEM_NAMES_BY_ID[itemID] or tostring(itemID)
local qualityInfo = C_TradeSkillUI and C_TradeSkillUI.GetItemReagentQualityInfo and C_TradeSkillUI.GetItemReagentQualityInfo(itemID)
local qualityAtlas = qualityInfo and qualityInfo.iconChat or QUALITY_ATLAS_BY_ITEM_ID[itemID]
table.insert(items, {
itemID = itemID,
itemName = itemName,
count = count,
bag = bag,
slot = slot,
icon = C_Item.GetItemIconByID(itemID),
qualityAtlas = qualityAtlas,
qualityMarkup = qualityAtlas and CreateAtlasMarkup(qualityAtlas, 24, 24, 0, 0) or "",
})
end
end
return items
end
local function currentContentContext()
local inInstance, instanceType = IsInInstance()
if not inInstance then return nil end
local _, _, difficultyID = GetInstanceInfo()
if instanceType == "party" and MYTHIC_DUNGEON_DIFFICULTY_IDS[difficultyID] then
return "mythicDungeon"
elseif instanceType == "raid" and RAID_DIFFICULTY_IDS[difficultyID] then
return "raid"
end
return nil
end
local function shouldTrackAuras()
if not db.profile.buffs.enabled then return false end
local inInstance = IsInInstance()
if not inInstance then return false end
local debugging = isDebugging()
if debugging then return true end
local context = currentContentContext()
if not context or not (IsInGroup() or IsInRaid()) then return false end
if context == "mythicDungeon" then return db.profile.buffs.contentTypes.mythicDungeons end
if context == "raid" then return db.profile.buffs.contentTypes.nonLfrRaids end
return false
end
local function contextIsEnabled(context)
if context == "mythicDungeon" then return db.profile.buffs.contentTypes.mythicDungeons end
if context == "raid" then return db.profile.buffs.contentTypes.nonLfrRaids end
return false
end
local function thresholdSecondsForContext(context)
if context == "mythicDungeon" then
if C_ChallengeMode and C_ChallengeMode.GetActiveChallengeMapID then
local mapChallengeModeID = C_ChallengeMode.GetActiveChallengeMapID()
if mapChallengeModeID then
local _, _, timeLimit = C_ChallengeMode.GetMapUIInfo(mapChallengeModeID)
if timeLimit then return timeLimit end
end
end
return MYTHIC_DUNGEON_THRESHOLD_SECONDS
end
if context == "raid" then return RAID_THRESHOLD_SECONDS end
end
--- Whether a category should currently be shown: always if the buff is entirely missing, otherwise
--- only once its remaining duration drops to/under the content's threshold.
local function categoryShouldShow(category, context)
if not db.profile.buffs.categories[category.dbKey] then return false end
local expiration = categoryBuffExpiration(category)
if not expiration then return true end -- buff missing entirely
if expiration == 0 then return false end -- permanent buff present, nothing to remind about
if not db.profile.buffs.showIfExpiring then return false end
local threshold = thresholdSecondsForContext(context)
if not threshold then return false end
return (expiration - GetTime()) <= threshold
end
local function activeCategories()
if InCombatLockdown() then return {} end
if not db.profile.buffs.enabled then return {} end
local debugging = isDebugging()
local context = currentContentContext()
if not debugging and (not context or not contextIsEnabled(context)) then return {} end
if not debugging and not (IsInGroup() or IsInRaid()) then return {} end
local active = {}
for _, category in ipairs(BUFF_CATEGORIES) do
if categoryShouldShow(category, context) then table.insert(active, category) end
end
return active
end
local function updateAuraEventRegistration()
local shouldRegister = not InCombatLockdown() and shouldTrackAuras()
if shouldRegister and not auraEventRegistered then
rebuildAuraCache()
module:RegisterEvent("UNIT_AURA")
auraEventRegistered = true
log("Buffs: registered UNIT_AURA")
elseif not shouldRegister and auraEventRegistered then
module:UnregisterEvent("UNIT_AURA")
auraEventRegistered = false
auraCacheInitialized = false
log("Buffs: deregistered UNIT_AURA")
end
end
function module:UNIT_AURA(_, unit, updateInfo)
if unit ~= "player" or InCombatLockdown() then return end
if updateAuraCache(updateInfo) then self:Refresh() end
end
local function hideBuffs()
if not container then return end
closeContextMenu()
if InCombatLockdown() then return end -- the visibility state driver already hides it in combat
container:Hide()
for _, button in pairs(iconButtons) do
button:Hide()
end
end
local function updateDuration(button)
if GetCVarBool and not GetCVarBool("buffDurations") then
button.duration:Hide()
return
end
local expiration = button.expirationTime
if not expiration or expiration <= 0 then
button.duration:Hide()
return
end
local timeLeft = math.max(expiration - GetTime(), 0)
if timeLeft <= 0 then
button.duration:Hide()
return
end
button.duration:SetFormattedText(SecondsToTimeAbbrev(timeLeft))
if timeLeft < (BUFF_DURATION_WARNING_TIME or 90) then
button.duration:SetTextColor(HIGHLIGHT_FONT_COLOR:GetRGB())
else
button.duration:SetTextColor(NORMAL_FONT_COLOR:GetRGB())
end
button.duration:Show()
end
local function cancelDurationUpdates()
if durationUpdateTimer then
durationUpdateTimer:Cancel()
durationUpdateTimer = nil
end
end
local function scheduleDurationUpdates()
cancelDurationUpdates()
if InCombatLockdown() or not container or not container:IsShown() or isEditing then return end
if GetCVarBool and not GetCVarBool("buffDurations") then return end
local hasDuration = false
for _, button in pairs(iconButtons) do
if button:IsShown() and button.expirationTime and button.expirationTime > GetTime() then
hasDuration = true
break
end
end
if not hasDuration then return end
local delay = math.max(0.05, math.ceil(GetTime()) - GetTime())
durationUpdateTimer = C_Timer.NewTimer(delay, function()
durationUpdateTimer = nil
for _, button in pairs(iconButtons) do
if button:IsShown() then updateDuration(button) end
end
scheduleDurationUpdates()
end)
end
local function updatePosition()
if not container then return end
container:ClearAllPoints()
local buffsDb = db.profile.buffs
local point = buffsDb.point or "TOP"
local relativePoint = buffsDb.relativePoint or point
local x = buffsDb.x or 0
local y = buffsDb.y or -130
container:SetPoint(point, UIParent, relativePoint, x, y)
end
local function createContainer()
if container then return end
container = CreateFrame("Frame", "SlackHacksBuffReminders", UIParent, "SecureHandlerStateTemplate")
container:SetMovable(true)
container:SetClampedToScreen(true)
container:SetDontSavePosition(true)
local scale = (db.profile.buffs.iconSize or 100) / 100
container:SetScale(scale)
container:SetSize(AURA_BUTTON_WIDTH, AURA_BUTTON_WIDTH)
updatePosition()
container:SetAttribute("_onstate-combat", [[
local food = self:GetFrameRef("wellFed")
local flask = self:GetFrameRef("flask")
local oil = self:GetFrameRef("oil")
local rune = self:GetFrameRef("rune")
if newstate == "combat" then
if food then food:Hide() end
if flask then flask:Hide() end
if oil then
if oil:GetAttribute("showInCombat") then oil:Show() else oil:Hide() end
end
if rune then
if rune:GetAttribute("showInCombat") then rune:Show() else rune:Hide() end
end
end
]])
RegisterStateDriver(container, "combat", "[combat] combat; nocombat")
container:Show()
end
local function setButtonAction(button, category, item, onlyLeftClick)
if not button or InCombatLockdown() then return end
button:SetAttribute("type", nil)
button:SetAttribute("item", nil)
button:SetAttribute("macrotext", nil)
button:SetAttribute("unit", nil)
button:SetAttribute("type1", nil)
button:SetAttribute("item1", nil)
button:SetAttribute("macrotext1", nil)
button:SetAttribute("unit1", nil)
if not item or not category then return end
local typeAttr = onlyLeftClick and "type1" or "type"
local macroAttr = onlyLeftClick and "macrotext1" or "macrotext"
if category.dbKey == "oil" then
button:SetAttribute(typeAttr, "macro")
button:SetAttribute(macroAttr, "/use item:" .. item.itemID .. "\n/use 16")
else
button:SetAttribute(typeAttr, "macro")
button:SetAttribute(macroAttr, "/use item:" .. item.itemID)
end
end
local function configureDefaultItem(button, category, itemID)
button.defaultItemID = itemID
setButtonAction(button, category, itemID and { itemID = itemID } or nil, true)
button:SetAttribute("showInCombat", (category.dbKey == "oil" or category.dbKey == "rune") and itemID ~= nil)
end
local function rememberDefaultItem(button, category, item)
if not button or not category or not item then return end
lastUsedItems()[category.dbKey] = item.itemID
configureDefaultItem(button, category, item.itemID)
button.icon:SetTexture(item.icon or categoryIcon(category))
button.hasDefaultItem = true
setNativeOverlayGlow(button, db.profile.buffs.showGlow and button.expirationTime == nil)
end
local function createMenuRow(index)
local row = CreateFrame("Button", "SlackHacksBuffMenuRow" .. index, menuFrame, "BackdropTemplate, SecureActionButtonTemplate")
row:SetHeight(24)
row:RegisterForClicks("AnyUp", "AnyDown") -- secure item/macro click only fires reliably with both registered
local highlight = row:CreateTexture(nil, "HIGHLIGHT")
highlight:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight")
highlight:SetPoint("TOPLEFT", row, "TOPLEFT", 2, -1)
highlight:SetPoint("BOTTOMRIGHT", row, "BOTTOMRIGHT", -2, 1)
highlight:SetVertexColor(1, 1, 1, 0.35)
highlight:SetBlendMode("ADD")
row.highlight = highlight
local icon = row:CreateTexture(nil, "ARTWORK")
icon:SetSize(20, 20)
icon:SetPoint("LEFT", row, "LEFT", 4, 0)
icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)
row.icon = icon
local count = row:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
count:SetPoint("RIGHT", row, "RIGHT", -6, 0)
count:SetJustifyH("RIGHT")
row.count = count
local name = row:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
name:SetPoint("LEFT", icon, "RIGHT", 6, 0)
name:SetPoint("RIGHT", count, "LEFT", -26, 0) -- leaves room for the quality icon after the name
name:SetJustifyH("LEFT")
name:SetWordWrap(false)
row.name = name
local quality = row:CreateTexture(nil, "OVERLAY")
quality:SetSize(16, 16)
row.quality = quality
row:SetScript("OnEnter", function(self)
cancelMenuClose()
if self.itemID then
GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
GameTooltip:SetItemByID(self.itemID)
GameTooltip:Show()
end
end)
row:SetScript("OnLeave", function(self)
GameTooltip_Hide()
scheduleMenuClose()
end)
row:SetScript("PostClick", function(self)
local anchor = menuFrame and menuFrame.anchorButton
local category = menuFrame and menuFrame.category
if anchor and category and self.item then
rememberDefaultItem(anchor, category, self.item)
end
closeContextMenu()
C_Timer.After(0.2, function() module:Refresh() end)
end)
return row
end
local function getMenuRow(index)
menuRows[index] = menuRows[index] or createMenuRow(index)
return menuRows[index]
end
local function createContextMenu()
if menuFrame then return end
menuFrame = CreateFrame("Frame", "SlackHacksBuffContextMenu", UIParent, "BackdropTemplate")
menuFrame:SetFrameStrata("DIALOG")
menuFrame:SetClampedToScreen(true)
menuFrame:EnableMouse(true)
local background = menuFrame:CreateTexture(nil, "BACKGROUND")
background:SetAtlas("common-dropdown-c-bg")
background:SetPoint("TOPLEFT", -17, 12)
background:SetPoint("BOTTOMRIGHT", 17, -22)
menuFrame.background = background
menuFrame:SetScript("OnEnter", cancelMenuClose)
menuFrame:SetScript("OnLeave", scheduleMenuClose)
menuFrame:Hide()
end
local function openContextMenu(anchorButton, category, items)
if InCombatLockdown() then return end
createContextMenu()
if menuFrame:IsShown() and menuFrame.anchorButton == anchorButton then
closeContextMenu()
return
end
if not items or #items == 0 then
closeContextMenu()
return
end
menuFrame.anchorButton = anchorButton
menuFrame.category = category
local rowHeight = 24
local rowGap = 2
local padding = 6
local menuWidth = 240
for index, item in ipairs(items) do
local row = getMenuRow(index)
row.itemID = item.itemID
row.item = item
row:SetWidth(menuWidth - (padding * 2))
row:SetHeight(rowHeight)
row.icon:SetTexture(item.icon or SLACKHACKS_ICON)
row.name:SetText(item.itemName or "")
row.count:SetText("(" .. (item.count or 0) .. ")")
row.quality:ClearAllPoints()
if item.qualityAtlas then
row.quality:SetAtlas(item.qualityAtlas)
row.quality:SetPoint("LEFT", row.name, "LEFT", (row.name:GetStringWidth() or 0) + 4, 0)
row.quality:Show()
else
row.quality:Hide()
end
setButtonAction(row, category, item, false)
row:ClearAllPoints()
local offsetY = -padding - ((index - 1) * (rowHeight + rowGap))
row:SetPoint("TOPLEFT", menuFrame, "TOPLEFT", padding, offsetY)
row:Show()
end
for index = #items + 1, #menuRows do
local row = menuRows[index]
row:Hide()
row.item = nil
setButtonAction(row, nil, nil, false)
end
local totalHeight = (padding * 2) + (#items * rowHeight) + ((#items - 1) * rowGap)
menuFrame:SetSize(menuWidth, totalHeight)
menuFrame:ClearAllPoints()
local top = anchorButton:GetTop() or 0
if top > (GetScreenHeight() / 2) then
menuFrame:SetPoint("TOPLEFT", anchorButton, "BOTTOMLEFT", 0, -4)
else
menuFrame:SetPoint("BOTTOMLEFT", anchorButton, "TOPLEFT", 0, 4)
end
GameTooltip_Hide()
menuFrame:Show()
end
--- Anchors a button's duration label above or below its icon per the "durationPosition" setting.
local function setDurationPosition(duration)
duration:ClearAllPoints()
if db.profile.buffs.durationPosition == "above" then
duration:SetPoint("BOTTOM", duration:GetParent(), "TOP", 0, 2)
else
duration:SetPoint("TOP", duration:GetParent(), "BOTTOM", 0, -2)
end
end
--- Builds one reminder icon button for a category, once. Uses AuraButtonTemplate (for the standard
--- aura look) combined with SecureActionButtonTemplate (so left-click can use an item even in combat).
local function createIconButton(category)
local button = CreateFrame("Button", "SlackHacksBuffReminder" .. category.dbKey, container, "AuraButtonTemplate, SecureActionButtonTemplate")
button:SetSize(AURA_BUTTON_WIDTH, AURA_BUTTON_WIDTH)
button:RegisterForClicks("AnyUp", "AnyDown")
local icon = button.Icon
button.icon = icon
button.TempEnchantBorder:Hide()
local duration = button:CreateFontString(nil, "OVERLAY", DEFAULT_AURA_DURATION_FONT or "GameFontNormalSmall")
setDurationPosition(duration)
duration:SetJustifyH("CENTER")
duration:Hide()
button.duration = duration
button.category = category
button:SetAttribute("showInCombat", false)
container:SetFrameRef(category.dbKey, button)
container:WrapScript(button, "PostClick", [[
if button == "LeftButton" then return nil, "hide" end
]], [[
if message == "hide" then self:Hide() end
]])
button:SetScript("OnHide", function(self)
setNativeOverlayGlow(self, false)
self.duration:Hide()
end)
button:SetScript("OnEnter", function(self)
cancelMenuClose()
if menuFrame and menuFrame:IsShown() and menuFrame.anchorButton == self then
return
end
GameTooltip:SetOwner(self, "ANCHOR_TOP")
GameTooltip:SetText(self.category.label)
if self.defaultItemID then
local itemName = C_Item.GetItemNameByID(self.defaultItemID) or ITEM_NAMES_BY_ID and ITEM_NAMES_BY_ID[self.defaultItemID] or "selected item"
GameTooltip:AddLine("Left-click uses " .. itemName .. ".", 1, 1, 1)
else
GameTooltip:AddLine("No item selected.", 1, 1, 1)
end
if not InCombatLockdown() then
GameTooltip:AddLine("Right-click to select an item.", 1, 1, 1)
end
GameTooltip:Show()
end)
button:SetScript("OnLeave", function(self)
GameTooltip_Hide()
if menuFrame and menuFrame:IsShown() and menuFrame.anchorButton == self then
scheduleMenuClose()
end
end)
button:SetScript("PreClick", function(self, mouseButton)
if mouseButton ~= "RightButton" or InCombatLockdown() then return end
local items = categoryBagItems(self.category)
if #items == 0 then
print("SlackHacks: No " .. self.category.label .. " items in inventory.")
return
end
openContextMenu(self, self.category, items)
end)
return button
end
--- Gets (creating on first use) the reminder icon button for a category.
local function iconButton(category)
iconButtons[category.dbKey] = iconButtons[category.dbKey] or createIconButton(category)
return iconButtons[category.dbKey]
end
--- Rebuilds the container's size/position and every icon button's texture/attributes/anchor from a list
--- of active categories (from `activeCategories()`, or all of BUFF_CATEGORIES while edit-mode
--- previewing). Never runs in combat unless `allowCombatDisplay` (edit mode can't start in combat
--- anyway) -- see file header for why layout changes are unsafe mid-combat.
local function layoutIcons(active, allowCombatDisplay)
if InCombatLockdown() and not allowCombatDisplay then return end
local count = #active
if count == 0 and not isEditing then
closeContextMenu()
cancelDurationUpdates()
for _, button in pairs(iconButtons) do
button:Hide()
end
return
end
local scale = (db.profile.buffs.iconSize or 100) / 100
container:SetScale(scale)
local totalWidth = (count * AURA_BUTTON_WIDTH) + ((count - 1) * db.profile.buffs.iconGap)
container:SetSize(math.max(totalWidth, AURA_BUTTON_WIDTH), AURA_BUTTON_WIDTH)
updatePosition()
local visibleButtons = {}
for index, category in ipairs(active) do
local button = iconButton(category)
visibleButtons[category.dbKey] = true
button.expirationTime = categoryBuffExpiration(category)
setDurationPosition(button.duration)
if not isEditing then
updateDuration(button)
else
button.duration:Hide()
end
local defaultItemID = lastUsedItemID(category)
configureDefaultItem(button, category, defaultItemID)
button.icon:SetTexture(defaultItemID and C_Item.GetItemIconByID(defaultItemID) or categoryIcon(category))
button:Show()
button.hasDefaultItem = defaultItemID ~= nil
setNativeOverlayGlow(button, (not isEditing) and db.profile.buffs.showGlow and button.expirationTime == nil)
button:ClearAllPoints()
local offsetX = (index - 1) * (AURA_BUTTON_WIDTH + db.profile.buffs.iconGap)
button:SetPoint("LEFT", container, "LEFT", offsetX, 0)
end
for categoryKey, button in pairs(iconButtons) do
if not visibleButtons[categoryKey] then
button:Hide()
end
end
container:Show()
scheduleDurationUpdates()
end
--- Begins dragging the (unlocked, edit-mode) container. Blocked in combat since moving a secure frame
--- is itself restricted while InCombatLockdown().
local function onDragStart()
if InCombatLockdown() then return end
if container then
container:StartMoving()
end
end
--- Ends the drag and persists the container's new anchor point/offset to the profile.
local function onDragStop()
if InCombatLockdown() then return end
if not container then return end
container:StopMovingOrSizing()
local point, relativeTo, relativePoint, x, y = container:GetPoint(1)
if point then
db.profile.buffs.point = point
db.profile.buffs.relativePoint = relativePoint or point
db.profile.buffs.x = math.floor(x + 0.5)
db.profile.buffs.y = math.floor(y + 0.5)
end
updatePosition()
end
--- Builds (once) the Edit Mode settings dialog: a plain frame with checkboxes/sliders/dropdowns bound
--- directly to `db.profile.buffs`. Each control's callback re-runs `layoutIcons`/`module:Refresh()`
--- immediately so changes preview live while still in edit mode.
local function createEditModeDialog()
if editModeDialog then return editModeDialog end
local dialog = CreateFrame("Frame", "SlackHacksBuffsEditModeDialog", UIParent, "BackdropTemplate")
dialog:SetSize(340, 470)
dialog:SetPoint("CENTER", UIParent, "CENTER", 200, 0)
dialog:SetMovable(true)
dialog:SetClampedToScreen(true)
dialog:SetDontSavePosition(true)
dialog:SetFrameStrata("DIALOG")
dialog:SetFrameLevel(200)
dialog:EnableMouse(true)
dialog:RegisterForDrag("LeftButton")
dialog:SetScript("OnDragStart", dialog.StartMoving)
dialog:SetScript("OnDragStop", dialog.StopMovingOrSizing)
dialog:Hide()
local border = CreateFrame("Frame", nil, dialog, "DialogBorderTranslucentTemplate")
local title = dialog:CreateFontString(nil, "ARTWORK", "GameFontHighlightLarge")
title:SetPoint("TOP", dialog, "TOP", 0, -16)
title:SetText("Buff Reminders")
dialog.Title = title
local closeButton = CreateFrame("Button", nil, dialog, "UIPanelCloseButton")
closeButton:SetPoint("TOPRIGHT", dialog, "TOPRIGHT", -2, -2)
closeButton:SetScript("OnClick", function()
dialog:Hide()
if selection and selection.ShowHighlighted then
selection:ShowHighlighted()
end
end)
local controls = {}
local function addDivider(yOffset)
local divider = dialog:CreateTexture(nil, "ARTWORK")
divider:SetSize(300, 8)
divider:SetTexture("Interface\\FriendsFrame\\UI-FriendsFrame-OnlineDivider")
divider:SetPoint("TOP", dialog, "TOP", 0, yOffset)
return yOffset - 12
end
local function addHeader(text, yOffset)
local header = dialog:CreateFontString(nil, "ARTWORK", "GameFontNormal")
header:SetPoint("TOPLEFT", dialog, "TOPLEFT", 20, yOffset)
header:SetText(text)
return yOffset - 22
end
local function addCheckbox(label, getFunc, setFunc, yOffset, tooltip, dependents)
local cb = CreateFrame("CheckButton", nil, dialog, "UICheckButtonTemplate")
cb:SetPoint("TOPLEFT", dialog, "TOPLEFT", 18, yOffset)
cb.text:SetText(label)
cb.text:SetFontObject("GameFontHighlight")
local function updateDependents(checked)
if dependents then
for _, dep in ipairs(dependents) do
dep:SetEnabled(checked)
end
end
end
cb:SetScript("OnClick", function(self)
local isChecked = self:GetChecked()
setFunc(isChecked)
updateDependents(isChecked)
if isEditing then
layoutIcons(BUFF_CATEGORIES, true)
else
module:Refresh()
end
end)
if tooltip then
cb:SetScript("OnEnter", function(self)
GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
GameTooltip:SetText(label, 1, 1, 1)
GameTooltip:AddLine(tooltip, nil, nil, nil, true)
GameTooltip:Show()
end)
cb:SetScript("OnLeave", function()
GameTooltip_Hide()
end)
end
table.insert(controls, function()
local checked = getFunc()
cb:SetChecked(checked)
updateDependents(checked)