-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathTargetedSpells.lua
More file actions
3476 lines (2984 loc) · 130 KB
/
Copy pathTargetedSpells.lua
File metadata and controls
3476 lines (2984 loc) · 130 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
local addonName, DF = ...
-- ============================================================
-- TARGETED SPELLS SYSTEM
-- Shows incoming spell casts targeting party/raid members
--
-- When an enemy casts a spell targeting a party member, this
-- displays an icon with cast bar on that member's frame to
-- warn healers of incoming damage.
--
-- Supports multiple simultaneous incoming spells with stacking.
-- Features:
-- - Highlight important spells (C_Spell.IsSpellImportant)
-- - Sort by cast time (newest/oldest first)
-- - Max icons limit
-- - Interrupted visual feedback
-- - Off-screen nameplate support
-- ============================================================
local pairs, ipairs, wipe = pairs, ipairs, wipe
local GetTime = GetTime
local UnitExists = UnitExists
local UnitIsUnit = UnitIsUnit
local UnitGUID = UnitGUID
local UnitCastingInfo = UnitCastingInfo
local UnitChannelInfo = UnitChannelInfo
local UnitCastingDuration = UnitCastingDuration
local UnitChannelDuration = UnitChannelDuration
local UnitCanAttack = UnitCanAttack
local C_Spell = C_Spell
local C_CVar = C_CVar
-- Track all enemy casters we're monitoring
-- Structure: activeCasters[casterUnit] = { startTime = time, spellID = id, isChannel = bool }
-- Using unit token (e.g. "nameplate7") as key instead of GUID because GUIDs are secret values
local activeCasters = {}
-- Personal display variables (declared early for HandleTargetChange access)
local personalContainer = nil
local personalIcons = {}
local personalActiveSpells = {}
-- Cast history for learning/review (test feature)
-- Stores recent enemy casts with targeting info
local castHistory = {}
local MAX_HISTORY = 50
-- Event frame for tracking casts
local eventFrame = CreateFrame("Frame")
eventFrame:Hide()
-- ============================================================
-- HIGHLIGHT STYLE ANIMATIONS
-- ============================================================
-- Animation settings for marching ants
local ANIM_SPEED = 40
local DASH_LENGTH = 4
local GAP_LENGTH = 4
local PATTERN_LENGTH = DASH_LENGTH + GAP_LENGTH
-- Global animator for marching ants and pulse on targeted spell icons
local TargetedSpellAnimator = CreateFrame("Frame")
TargetedSpellAnimator.elapsed = 0
TargetedSpellAnimator.frames = {}
TargetedSpellAnimator.pulseFrames = {}
TargetedSpellAnimator.hasWork = false -- Track whether any frames are registered
local function TargetedSpellAnimator_OnUpdate(self, elapsed)
-- PERF TEST: Skip animations if disabled
if DF.PerfTest and not DF.PerfTest.enableAnimations then return end
-- Marching ants animation
self.elapsed = self.elapsed + elapsed
local offset = (self.elapsed * ANIM_SPEED) % PATTERN_LENGTH
for highlightFrame in pairs(self.frames) do
if highlightFrame:IsShown() and highlightFrame.animBorder then
DF:UpdateTargetedSpellAnimatedBorder(highlightFrame, offset)
end
end
-- Pulse animation (animates border texture alpha, not frame alpha)
for highlightFrame in pairs(self.pulseFrames) do
if highlightFrame:IsShown() and highlightFrame.pulseState and highlightFrame.glowBorder then
local state = highlightFrame.pulseState
state.elapsed = state.elapsed + elapsed
-- Calculate current alpha based on time
local progress = state.elapsed / state.duration
if progress >= 1 then
-- Reverse direction
state.direction = -state.direction
state.elapsed = 0
progress = 0
end
-- Smooth interpolation (smoothstep)
local smoothProgress = progress * progress * (3 - 2 * progress)
local alpha
if state.direction == 1 then
alpha = state.minAlpha + (state.maxAlpha - state.minAlpha) * smoothProgress
else
alpha = state.maxAlpha - (state.maxAlpha - state.minAlpha) * smoothProgress
end
-- Apply alpha to border textures
local border = highlightFrame.glowBorder
local r = highlightFrame.pulseR or 1
local g = highlightFrame.pulseG or 0.8
local b = highlightFrame.pulseB or 0
if border.top then border.top:SetColorTexture(r, g, b, alpha * 0.8) end
if border.bottom then border.bottom:SetColorTexture(r, g, b, alpha * 0.8) end
if border.left then border.left:SetColorTexture(r, g, b, alpha * 0.8) end
if border.right then border.right:SetColorTexture(r, g, b, alpha * 0.8) end
end
end
end
-- Check if animator has any work to do and enable/disable accordingly
local function TargetedSpellAnimator_UpdateState()
local hasWork = next(TargetedSpellAnimator.frames) or next(TargetedSpellAnimator.pulseFrames)
if hasWork and not TargetedSpellAnimator.hasWork then
TargetedSpellAnimator.hasWork = true
TargetedSpellAnimator:SetScript("OnUpdate", TargetedSpellAnimator_OnUpdate)
elseif not hasWork and TargetedSpellAnimator.hasWork then
TargetedSpellAnimator.hasWork = false
TargetedSpellAnimator:SetScript("OnUpdate", nil)
end
end
-- Export for test mode access
DF.TargetedSpellAnimator = TargetedSpellAnimator
-- Create dashes for one edge of the animated border
local function CreateEdgeDashes(parent, count)
local dashes = {}
for i = 1, count do
local dash = parent:CreateTexture(nil, "OVERLAY")
dash:SetColorTexture(1, 1, 1, 1)
dash:Hide()
dashes[i] = dash
end
return dashes
end
-- Initialize animated border on a highlight frame
local function InitAnimatedBorder(highlightFrame)
if highlightFrame.animBorder then return highlightFrame.animBorder end
highlightFrame.animBorder = {
topDashes = CreateEdgeDashes(highlightFrame, 15),
bottomDashes = CreateEdgeDashes(highlightFrame, 15),
leftDashes = CreateEdgeDashes(highlightFrame, 15),
rightDashes = CreateEdgeDashes(highlightFrame, 15),
}
return highlightFrame.animBorder
end
DF.InitAnimatedBorder = InitAnimatedBorder
-- Update animated border with current offset
function DF:UpdateTargetedSpellAnimatedBorder(highlightFrame, offset)
local border = highlightFrame.animBorder
if not border then return end
local thick = highlightFrame.animThickness or 2
local r, g, b, a = highlightFrame.animR or 1, highlightFrame.animG or 0.8, highlightFrame.animB or 0, highlightFrame.animA or 1
local frameWidth, frameHeight = highlightFrame:GetWidth(), highlightFrame:GetHeight()
if frameWidth <= 0 or frameHeight <= 0 then return end
local function DrawHorizontalEdge(dashes, isTop, edgeOffset)
local numDashes = math.ceil(frameWidth / PATTERN_LENGTH) + 2
for i, dash in ipairs(dashes) do dash:Hide() end
local startPos = -(edgeOffset % PATTERN_LENGTH)
for i = 1, numDashes do
local dashStart = startPos + (i - 1) * PATTERN_LENGTH
local dashEnd = dashStart + DASH_LENGTH
local visStart, visEnd = math.max(0, dashStart), math.min(frameWidth, dashEnd)
if visEnd > visStart and dashes[i] then
local dash = dashes[i]
dash:ClearAllPoints()
dash:SetSize(visEnd - visStart, thick)
if isTop then
dash:SetPoint("TOPLEFT", highlightFrame, "TOPLEFT", visStart, 0)
else
dash:SetPoint("BOTTOMLEFT", highlightFrame, "BOTTOMLEFT", visStart, 0)
end
dash:SetColorTexture(r, g, b, a)
dash:Show()
end
end
end
local function DrawVerticalEdge(dashes, isRight, edgeOffset)
local numDashes = math.ceil(frameHeight / PATTERN_LENGTH) + 2
for i, dash in ipairs(dashes) do dash:Hide() end
local startPos = -(edgeOffset % PATTERN_LENGTH)
for i = 1, numDashes do
local dashStart = startPos + (i - 1) * PATTERN_LENGTH
local dashEnd = dashStart + DASH_LENGTH
local visStart, visEnd = math.max(0, dashStart), math.min(frameHeight, dashEnd)
if visEnd > visStart and dashes[i] then
local dash = dashes[i]
dash:ClearAllPoints()
dash:SetSize(thick, visEnd - visStart)
if isRight then
dash:SetPoint("TOPRIGHT", highlightFrame, "TOPRIGHT", 0, -visStart)
else
dash:SetPoint("TOPLEFT", highlightFrame, "TOPLEFT", 0, -visStart)
end
dash:SetColorTexture(r, g, b, a)
dash:Show()
end
end
end
-- Counter-clockwise marching ants
DrawHorizontalEdge(border.bottomDashes, false, offset)
DrawVerticalEdge(border.leftDashes, false, frameWidth + offset)
DrawHorizontalEdge(border.topDashes, true, frameWidth + frameHeight - offset)
DrawVerticalEdge(border.rightDashes, true, (2 * frameWidth) + frameHeight - offset)
end
-- Hide animated border
local function HideAnimatedBorder(highlightFrame)
if not highlightFrame.animBorder then return end
for _, dashes in pairs(highlightFrame.animBorder) do
for _, dash in ipairs(dashes) do dash:Hide() end
end
end
DF.HideAnimatedBorder = HideAnimatedBorder
-- Create solid border (4 edge textures)
local function InitSolidBorder(highlightFrame)
if highlightFrame.solidBorder then return highlightFrame.solidBorder end
highlightFrame.solidBorder = {
top = highlightFrame:CreateTexture(nil, "BORDER"),
bottom = highlightFrame:CreateTexture(nil, "BORDER"),
left = highlightFrame:CreateTexture(nil, "BORDER"),
right = highlightFrame:CreateTexture(nil, "BORDER"),
}
return highlightFrame.solidBorder
end
DF.InitSolidBorder = InitSolidBorder
-- Update solid border
local function UpdateSolidBorder(highlightFrame, thickness, r, g, b, a)
local border = highlightFrame.solidBorder
if not border then return end
border.top:ClearAllPoints()
border.top:SetPoint("TOPLEFT", highlightFrame, "TOPLEFT", 0, 0)
border.top:SetPoint("TOPRIGHT", highlightFrame, "TOPRIGHT", 0, 0)
border.top:SetHeight(thickness)
border.top:SetColorTexture(r, g, b, a)
border.top:SetBlendMode("BLEND")
border.top:Show()
border.bottom:ClearAllPoints()
border.bottom:SetPoint("BOTTOMLEFT", highlightFrame, "BOTTOMLEFT", 0, 0)
border.bottom:SetPoint("BOTTOMRIGHT", highlightFrame, "BOTTOMRIGHT", 0, 0)
border.bottom:SetHeight(thickness)
border.bottom:SetColorTexture(r, g, b, a)
border.bottom:SetBlendMode("BLEND")
border.bottom:Show()
border.left:ClearAllPoints()
border.left:SetPoint("TOPLEFT", highlightFrame, "TOPLEFT", 0, -thickness)
border.left:SetPoint("BOTTOMLEFT", highlightFrame, "BOTTOMLEFT", 0, thickness)
border.left:SetWidth(thickness)
border.left:SetColorTexture(r, g, b, a)
border.left:SetBlendMode("BLEND")
border.left:Show()
border.right:ClearAllPoints()
border.right:SetPoint("TOPRIGHT", highlightFrame, "TOPRIGHT", 0, -thickness)
border.right:SetPoint("BOTTOMRIGHT", highlightFrame, "BOTTOMRIGHT", 0, thickness)
border.right:SetWidth(thickness)
border.right:SetColorTexture(r, g, b, a)
border.right:SetBlendMode("BLEND")
border.right:Show()
end
DF.UpdateSolidBorder = UpdateSolidBorder
-- Hide solid border
local function HideSolidBorder(highlightFrame)
if not highlightFrame or not highlightFrame.solidBorder then return end
highlightFrame.solidBorder.top:Hide()
highlightFrame.solidBorder.bottom:Hide()
highlightFrame.solidBorder.left:Hide()
highlightFrame.solidBorder.right:Hide()
end
DF.HideSolidBorder = HideSolidBorder
-- Create glow border (4 edge textures with ADD blend mode for glow effect)
local function InitGlowBorder(highlightFrame)
if highlightFrame.glowBorder then return highlightFrame.glowBorder end
highlightFrame.glowBorder = {
top = highlightFrame:CreateTexture(nil, "OVERLAY"),
bottom = highlightFrame:CreateTexture(nil, "OVERLAY"),
left = highlightFrame:CreateTexture(nil, "OVERLAY"),
right = highlightFrame:CreateTexture(nil, "OVERLAY"),
}
-- Set ADD blend mode for glow effect
for _, tex in pairs(highlightFrame.glowBorder) do
tex:SetBlendMode("ADD")
end
return highlightFrame.glowBorder
end
DF.InitGlowBorder = InitGlowBorder
-- Update glow border
local function UpdateGlowBorder(highlightFrame, thickness, r, g, b, a)
local border = highlightFrame.glowBorder
if not border then return end
border.top:ClearAllPoints()
border.top:SetPoint("TOPLEFT", highlightFrame, "TOPLEFT", 0, 0)
border.top:SetPoint("TOPRIGHT", highlightFrame, "TOPRIGHT", 0, 0)
border.top:SetHeight(thickness)
border.top:SetColorTexture(r, g, b, a)
border.top:SetBlendMode("ADD")
border.top:Show()
border.bottom:ClearAllPoints()
border.bottom:SetPoint("BOTTOMLEFT", highlightFrame, "BOTTOMLEFT", 0, 0)
border.bottom:SetPoint("BOTTOMRIGHT", highlightFrame, "BOTTOMRIGHT", 0, 0)
border.bottom:SetHeight(thickness)
border.bottom:SetColorTexture(r, g, b, a)
border.bottom:SetBlendMode("ADD")
border.bottom:Show()
border.left:ClearAllPoints()
border.left:SetPoint("TOPLEFT", highlightFrame, "TOPLEFT", 0, -thickness)
border.left:SetPoint("BOTTOMLEFT", highlightFrame, "BOTTOMLEFT", 0, thickness)
border.left:SetWidth(thickness)
border.left:SetColorTexture(r, g, b, a)
border.left:SetBlendMode("ADD")
border.left:Show()
border.right:ClearAllPoints()
border.right:SetPoint("TOPRIGHT", highlightFrame, "TOPRIGHT", 0, -thickness)
border.right:SetPoint("BOTTOMRIGHT", highlightFrame, "BOTTOMRIGHT", 0, thickness)
border.right:SetWidth(thickness)
border.right:SetColorTexture(r, g, b, a)
border.right:SetBlendMode("ADD")
border.right:Show()
end
DF.UpdateGlowBorder = UpdateGlowBorder
-- Hide glow border
local function HideGlowBorder(highlightFrame)
if not highlightFrame or not highlightFrame.glowBorder then return end
highlightFrame.glowBorder.top:Hide()
highlightFrame.glowBorder.bottom:Hide()
highlightFrame.glowBorder.left:Hide()
highlightFrame.glowBorder.right:Hide()
end
DF.HideGlowBorder = HideGlowBorder
-- Create pulse animation group - animates border texture alpha, not frame alpha
-- This prevents the animation from overriding SetAlphaFromBoolean on the frame
local function InitPulseAnimation(highlightFrame)
if highlightFrame.pulseAnim then return highlightFrame.pulseAnim end
-- Store pulse state on the frame
highlightFrame.pulseState = {
elapsed = 0,
minAlpha = 0.3,
maxAlpha = 1.0,
duration = 0.5,
direction = 1, -- 1 = fading in, -1 = fading out
}
-- Create a dummy animation group that we use to track if pulsing is active
local ag = {}
ag.isPlaying = false
ag.Play = function(self)
self.isPlaying = true
highlightFrame.pulseState.elapsed = 0
highlightFrame.pulseState.direction = 1
-- Register with animator
TargetedSpellAnimator.pulseFrames[highlightFrame] = true
TargetedSpellAnimator_UpdateState()
end
ag.Stop = function(self)
self.isPlaying = false
TargetedSpellAnimator.pulseFrames[highlightFrame] = nil
TargetedSpellAnimator_UpdateState()
end
ag.IsPlaying = function(self)
return self.isPlaying
end
highlightFrame.pulseAnim = ag
return ag
end
DF.InitPulseAnimation = InitPulseAnimation
-- ============================================================
-- HELPER FUNCTIONS
-- ============================================================
-- Get all party/raid units to check
local function GetGroupUnits()
local units = {}
-- Always include player
table.insert(units, "player")
if IsInRaid() then
for i = 1, 40 do
local unit = "raid" .. i
-- Note: "raidN" tokens never equal "player" string, so simple ~= check is safe
-- (avoids potential secret value issues with UnitIsUnit)
if UnitExists(unit) and unit ~= "player" then
table.insert(units, unit)
end
end
else
for i = 1, 4 do
local unit = "party" .. i
if UnitExists(unit) then
table.insert(units, unit)
end
end
end
return units
end
-- Get current content type
-- Returns: "openworld", "dungeon", "raid", "arena", "battleground"
local function GetContentType()
local inInstance, instanceType = IsInInstance()
if not inInstance then
return "openworld"
end
if instanceType == "party" then
return "dungeon"
elseif instanceType == "raid" then
return "raid"
elseif instanceType == "arena" then
return "arena"
elseif instanceType == "pvp" then
return "battleground"
elseif instanceType == "scenario" then
return "dungeon" -- Treat scenarios as dungeons
end
return "openworld"
end
-- Check if targeted spells should be shown for party/player frames based on content type
local function ShouldShowTargetedSpells(db)
if not db.targetedSpellEnabled then return false end
local contentType = GetContentType()
if contentType == "openworld" then
return db.targetedSpellInOpenWorld ~= false
elseif contentType == "dungeon" then
return db.targetedSpellInDungeons ~= false
elseif contentType == "arena" then
return db.targetedSpellInArena ~= false
end
return true -- Default to showing
end
-- Check if targeted spells should be shown for raid frames based on content type
local function ShouldShowRaidTargetedSpells(db)
if not db.targetedSpellEnabled then return false end
local contentType = GetContentType()
if contentType == "openworld" then
return db.targetedSpellInOpenWorld ~= false
elseif contentType == "raid" then
return db.targetedSpellInRaids ~= false
elseif contentType == "battleground" then
return db.targetedSpellInBattlegrounds ~= false
end
return true -- Default to showing
end
-- Check if personal targeted spells should be shown based on content type
local function ShouldShowPersonalTargetedSpells(db)
if not db.personalTargetedSpellEnabled then return false end
local contentType = GetContentType()
if contentType == "openworld" then
return db.personalTargetedSpellInOpenWorld ~= false
elseif contentType == "dungeon" then
return db.personalTargetedSpellInDungeons ~= false
elseif contentType == "raid" then
return db.personalTargetedSpellInRaids ~= false
elseif contentType == "arena" then
return db.personalTargetedSpellInArena ~= false
elseif contentType == "battleground" then
return db.personalTargetedSpellInBattlegrounds ~= false
end
return true -- Default to showing
end
-- Check if a unit is valid for targeted spell tracking
-- We ONLY track nameplate units - boss/arena/target/focus all have nameplates too
-- so tracking them separately would cause duplicates
local function IsValidCasterUnit(unit)
if not unit then return false end
-- Only nameplate units
if string.find(unit, "nameplate") then
return true
end
return false
end
-- Get enemy units that might be casting at us
-- Note: We only track nameplates - boss/arena units have nameplates too
local function GetEnemyUnits()
local units = {}
-- Nameplates only (boss/arena/target/focus all have nameplates)
for i = 1, 40 do
local unit = "nameplate" .. i
if UnitExists(unit) then
table.insert(units, unit)
end
end
return units
end
-- Get the frame for a unit
local function GetFrameForUnit(unit)
-- Fast path: use unitFrameMap
if DF.unitFrameMap and DF.unitFrameMap[unit] then
return DF.unitFrameMap[unit]
end
local foundFrame = nil
DF:IterateAllFrames(function(frame)
if frame and frame.unit and frame.unit == unit then
foundFrame = frame
return true -- Stop iteration
end
end)
return foundFrame
end
-- ============================================================
-- ICON CREATION AND POOLING
-- ============================================================
-- Create a single targeted spell icon
local function CreateSingleIcon(parent, index)
local container = CreateFrame("Frame", nil, parent)
container:SetFrameLevel(parent:GetFrameLevel() + 30 + index)
container:Hide()
container.index = index
-- Disable mouse completely - these should be click-through
container:EnableMouse(false)
-- Make hitbox zero so clicks pass through
container:SetHitRectInsets(10000, 10000, 10000, 10000)
-- Importance filter frame - nested inside container
-- This allows us to filter by importance using SetAlphaFromBoolean
-- when importantOnly is enabled, without affecting the targeting logic
local importanceFilterFrame = CreateFrame("Frame", nil, container)
importanceFilterFrame:SetAllPoints()
importanceFilterFrame:EnableMouse(false)
importanceFilterFrame:SetHitRectInsets(10000, 10000, 10000, 10000)
container.importanceFilterFrame = importanceFilterFrame
-- Icon container (with border) - now parented to importanceFilterFrame
local iconFrame = CreateFrame("Frame", nil, importanceFilterFrame)
iconFrame:SetSize(28, 28)
iconFrame:EnableMouse(false)
iconFrame:SetHitRectInsets(10000, 10000, 10000, 10000)
container.iconFrame = iconFrame
-- Icon border - 4 edge textures (consistent with defensive/missing buff icons)
local defBorderSize = 2
local borderLeft = iconFrame:CreateTexture(nil, "BACKGROUND")
borderLeft:SetPoint("TOPLEFT", 0, 0)
borderLeft:SetPoint("BOTTOMLEFT", 0, 0)
borderLeft:SetWidth(defBorderSize)
borderLeft:SetColorTexture(1, 0.3, 0, 1)
container.borderLeft = borderLeft
iconFrame.borderLeft = borderLeft
local borderRight = iconFrame:CreateTexture(nil, "BACKGROUND")
borderRight:SetPoint("TOPRIGHT", 0, 0)
borderRight:SetPoint("BOTTOMRIGHT", 0, 0)
borderRight:SetWidth(defBorderSize)
borderRight:SetColorTexture(1, 0.3, 0, 1)
container.borderRight = borderRight
iconFrame.borderRight = borderRight
local borderTop = iconFrame:CreateTexture(nil, "BACKGROUND")
borderTop:SetPoint("TOPLEFT", defBorderSize, 0)
borderTop:SetPoint("TOPRIGHT", -defBorderSize, 0)
borderTop:SetHeight(defBorderSize)
borderTop:SetColorTexture(1, 0.3, 0, 1)
container.borderTop = borderTop
iconFrame.borderTop = borderTop
local borderBottom = iconFrame:CreateTexture(nil, "BACKGROUND")
borderBottom:SetPoint("BOTTOMLEFT", defBorderSize, 0)
borderBottom:SetPoint("BOTTOMRIGHT", -defBorderSize, 0)
borderBottom:SetHeight(defBorderSize)
borderBottom:SetColorTexture(1, 0.3, 0, 1)
container.borderBottom = borderBottom
iconFrame.borderBottom = borderBottom
-- Important spell highlight frame - use a frame so we can SetAlphaFromBoolean
-- Set frame level ABOVE iconFrame so it renders on top when inset
local highlightFrame = CreateFrame("Frame", nil, iconFrame)
highlightFrame:SetPoint("TOPLEFT", -4, 4)
highlightFrame:SetPoint("BOTTOMRIGHT", 4, -4)
highlightFrame:SetFrameLevel(iconFrame:GetFrameLevel() + 5)
highlightFrame:Hide()
highlightFrame:EnableMouse(false)
highlightFrame:SetHitRectInsets(10000, 10000, 10000, 10000)
container.highlightFrame = highlightFrame
iconFrame.highlightFrame = highlightFrame
-- Icon texture - positioned with inset for border, with TexCoord cropping
local icon = iconFrame:CreateTexture(nil, "ARTWORK")
icon:SetPoint("TOPLEFT", defBorderSize, -defBorderSize)
icon:SetPoint("BOTTOMRIGHT", -defBorderSize, defBorderSize)
icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)
container.icon = icon
iconFrame.icon = icon
-- Cooldown frame for swipe animation on icon
local cooldown = CreateFrame("Cooldown", nil, iconFrame, "CooldownFrameTemplate")
cooldown:SetAllPoints(icon)
cooldown:SetDrawEdge(false)
cooldown:SetDrawBling(false)
cooldown:SetDrawSwipe(true)
cooldown:SetReverse(true)
cooldown:SetHideCountdownNumbers(true) -- We use our own duration text
cooldown:EnableMouse(false)
cooldown:SetHitRectInsets(10000, 10000, 10000, 10000)
container.cooldown = cooldown
iconFrame.cooldown = cooldown
-- Overlay frame for duration text (sits above cooldown swipe)
local textOverlay = CreateFrame("Frame", nil, iconFrame)
textOverlay:SetAllPoints()
textOverlay:SetFrameLevel(cooldown:GetFrameLevel() + 5)
textOverlay:EnableMouse(false)
textOverlay:SetHitRectInsets(10000, 10000, 10000, 10000)
container.textOverlay = textOverlay
-- Custom duration text (on overlay so it's above the swipe)
local durationText = textOverlay:CreateFontString(nil, "OVERLAY")
durationText:SetFont("Fonts\\FRIZQT__.TTF", 10, "OUTLINE")
durationText:SetPoint("CENTER", iconFrame, "CENTER", 0, 0)
durationText:SetTextColor(1, 1, 1, 1)
container.durationText = durationText
iconFrame.durationText = durationText
-- Interrupted overlay (X mark)
local interruptOverlay = CreateFrame("Frame", nil, iconFrame)
interruptOverlay:SetAllPoints()
interruptOverlay:SetFrameLevel(cooldown:GetFrameLevel() + 10)
interruptOverlay:Hide()
interruptOverlay:EnableMouse(false)
interruptOverlay:SetHitRectInsets(10000, 10000, 10000, 10000)
container.interruptOverlay = interruptOverlay
-- Red tint for interrupted
local interruptTint = interruptOverlay:CreateTexture(nil, "OVERLAY")
interruptTint:SetAllPoints()
interruptTint:SetColorTexture(1, 0, 0, 0.5)
container.interruptTint = interruptTint
-- X mark for interrupted
local interruptX = interruptOverlay:CreateFontString(nil, "OVERLAY")
interruptX:SetFont("Fonts\\FRIZQT__.TTF", 16, "OUTLINE")
interruptX:SetPoint("CENTER", iconFrame, "CENTER", 0, 0)
interruptX:SetText("X")
interruptX:SetTextColor(1, 0, 0, 1)
container.interruptX = interruptX
-- OnUpdate for cleanup checking and duration text
local durationThrottle = 0
container:SetScript("OnUpdate", function(self, elapsed)
-- Skip if not active (alpha is controlled by SetAlphaFromBoolean, can't read it)
if not self.isActive then return end
-- Handle interrupted animation (needs to run every frame for smooth animation)
if self.isInterrupted then
self.interruptTimer = (self.interruptTimer or 0) + elapsed
local db = self.unitFrame and DF:GetFrameDB(self.unitFrame) or DF:GetDB()
local duration = db.targetedSpellInterruptedDuration or 0.5
if self.interruptTimer >= duration then
-- Animation complete, hide icon
if self.unitFrame and self.casterKey then
DF:HideTargetedSpellIcon(self.unitFrame, self.casterKey, true)
end
end
return
end
-- Throttle duration text updates to ~10 FPS for performance
durationThrottle = durationThrottle + elapsed
if durationThrottle < 0.1 then return end
durationThrottle = 0
-- Update duration text from duration object
-- Note: GetRemainingDuration returns a secret value so we can't compare it
-- Just display it and use a fixed color from settings
-- TODO: Can use durationObject:EvaluateRemainingPercent(colorCurve) for dynamic color-by-time
-- similar to how aura icons do it in Frames/Create.lua
if self.durationObject and self.durationText then
local ok, remaining = pcall(self.durationObject.GetRemainingDuration, self.durationObject)
if ok and remaining then
-- Use SetFormattedText which handles secret values
self.durationText:SetFormattedText("%.1f", remaining)
-- Apply the configured color (can't do color-by-time with secret values)
if self.durationColor then
self.durationText:SetTextColor(self.durationColor.r, self.durationColor.g, self.durationColor.b, 1)
end
end
end
-- Note: We DON'T check if cast is still active here anymore
-- Events (UNIT_SPELLCAST_STOP, INTERRUPTED, etc.) handle all cleanup
-- This prevents race conditions with interrupt visuals
end)
return container
end
-- Ensure icon pool exists for a frame
local function EnsureIconPool(frame, count)
-- Create OOR container if it doesn't exist
-- This container receives out-of-range alpha, so individual icons
-- can use SetAlphaFromBoolean for targeting without conflict
if not frame.targetedSpellContainer then
local container = CreateFrame("Frame", nil, frame)
container:SetAllPoints()
container:SetFrameLevel(frame:GetFrameLevel() + 29)
container:EnableMouse(false)
container:SetHitRectInsets(10000, 10000, 10000, 10000)
frame.targetedSpellContainer = container
end
if not frame.targetedSpellIcons then
frame.targetedSpellIcons = {}
end
if not frame.dfActiveTargetedSpells then
frame.dfActiveTargetedSpells = {}
end
count = count or 5 -- Default pool size
local existing = #frame.targetedSpellIcons
if existing >= count then return end
-- Raid frames: create only 1 icon now, stagger the rest to avoid
-- "script ran too long" when 40 frames each create 5 icons simultaneously
if DF:IsRaidFrame(frame) and existing == 0 then
frame.targetedSpellIcons[1] = CreateSingleIcon(frame.targetedSpellContainer, 1)
frame.targetedSpellIcons[1].unitFrame = frame
-- Schedule remaining icons one-per-timer-tick
if not frame.dfIconPoolStaggered then
frame.dfIconPoolStaggered = true
for i = 2, count do
C_Timer.After(0.05 * (i - 1), function()
if not frame.targetedSpellIcons then return end
if #frame.targetedSpellIcons >= i then return end
frame.targetedSpellIcons[i] = CreateSingleIcon(frame.targetedSpellContainer, i)
frame.targetedSpellIcons[i].unitFrame = frame
end)
end
end
return
end
for i = existing + 1, count do
-- Parent icons to the OOR container, not directly to frame
frame.targetedSpellIcons[i] = CreateSingleIcon(frame.targetedSpellContainer, i)
frame.targetedSpellIcons[i].unitFrame = frame
end
end
-- Expose EnsureIconPool for test mode
function DF:EnsureTargetedSpellIconPool(frame, count)
EnsureIconPool(frame, count)
end
-- Get an available icon from the pool
local function GetAvailableIcon(frame)
EnsureIconPool(frame, 5)
for i, icon in ipairs(frame.targetedSpellIcons) do
if not icon:IsShown() or not icon.isActive then
return icon, i
end
end
-- All icons in use, create a new one - parent to container
local newIndex = #frame.targetedSpellIcons + 1
frame.targetedSpellIcons[newIndex] = CreateSingleIcon(frame.targetedSpellContainer, newIndex)
frame.targetedSpellIcons[newIndex].unitFrame = frame
return frame.targetedSpellIcons[newIndex], newIndex
end
-- ============================================================
-- LAYOUT AND POSITIONING
-- ============================================================
-- Position all icons based on growth direction
-- Sorts by cast start time for consistent ordering
local function PositionIcons(frame)
if not frame or not frame.targetedSpellIcons or not frame.dfActiveTargetedSpells then return end
local db = DF:GetFrameDB(frame)
local iconSize = db.targetedSpellSize or 28
local scale = db.targetedSpellScale or 1.0
local anchor = db.targetedSpellAnchor or "LEFT"
local x = db.targetedSpellX or -30
local y = db.targetedSpellY or 0
local growthDirection = db.targetedSpellGrowth or "DOWN"
local spacing = db.targetedSpellSpacing or 2
local frameLevel = db.targetedSpellFrameLevel or 0
local maxIcons = db.targetedSpellMaxIcons or 5
-- local sortByTime = db.targetedSpellSortByTime ~= false -- Keep for future use
-- local newestFirst = db.targetedSpellSortNewestFirst ~= false -- Keep for future use
-- Apply pixel perfect to icon size
if db.pixelPerfect then
iconSize = DF:PixelPerfect(iconSize)
spacing = DF:PixelPerfect(spacing)
end
-- Apply scale to size for positioning calculations
local scaledSize = iconSize * scale
local scaledSpacing = spacing * scale
-- Collect active casters with their data
local casterData = {}
for casterKey, iconIndex in pairs(frame.dfActiveTargetedSpells) do
local icon = frame.targetedSpellIcons[iconIndex]
if icon and icon.isActive then
table.insert(casterData, {
casterKey = casterKey,
iconIndex = iconIndex,
startTime = icon.startTime or 0
})
end
end
-- Sort by caster key (unit token) for deterministic order
-- This ensures icons don't jump around as casts end
table.sort(casterData, function(a, b)
return a.casterKey < b.casterKey
end)
--[[ ALTERNATIVE: Sort by time (uncomment to use)
if sortByTime then
table.sort(casterData, function(a, b)
if newestFirst then
return a.startTime > b.startTime
else
return a.startTime < b.startTime
end
end)
end
--]]
-- Limit to max icons
local numIcons = math.min(#casterData, maxIcons)
-- Position each icon based on its sorted position
for i = 1, #casterData do
local data = casterData[i]
local icon = frame.targetedSpellIcons[data.iconIndex]
if icon then
if i <= maxIcons then
local offsetX, offsetY = 0, 0
local index = i - 1 -- 0-based for calculation
if growthDirection == "UP" then
offsetY = index * (scaledSize + scaledSpacing)
elseif growthDirection == "DOWN" then
offsetY = -index * (scaledSize + scaledSpacing)
elseif growthDirection == "LEFT" then
offsetX = -index * (scaledSize + scaledSpacing)
elseif growthDirection == "RIGHT" then
offsetX = index * (scaledSize + scaledSpacing)
elseif growthDirection == "CENTER_H" then
-- Grow horizontally from center
local centerOffset = (numIcons - 1) * (scaledSize + scaledSpacing) / 2
offsetX = index * (scaledSize + scaledSpacing) - centerOffset
elseif growthDirection == "CENTER_V" then
-- Grow vertically from center
local centerOffset = (numIcons - 1) * (scaledSize + scaledSpacing) / 2
offsetY = index * (scaledSize + scaledSpacing) - centerOffset
end
icon:ClearAllPoints()
icon:SetPoint(anchor, frame, anchor, x + offsetX, y + offsetY)
icon:SetSize(scaledSize, scaledSize)
-- Set frame level
icon:SetFrameLevel(frame:GetFrameLevel() + 30 + frameLevel + data.iconIndex)
-- Position icon frame within container
icon.iconFrame:SetSize(scaledSize, scaledSize)
icon.iconFrame:ClearAllPoints()
icon.iconFrame:SetPoint("CENTER", icon, "CENTER", 0, 0)
icon:Show()
else
-- Hide icons beyond max limit
icon:Hide()
end
end
end
end
-- Apply settings to a single icon
local function ApplyIconSettings(icon, db, spellID)
local borderColor = db.targetedSpellBorderColor or {r = 1, g = 0.3, b = 0}
local borderSize = db.targetedSpellBorderSize or 2
local showBorder = db.targetedSpellShowBorder ~= false
local showSwipe = not db.targetedSpellHideSwipe
local showDuration = db.targetedSpellShowDuration ~= false
local durationFont = db.targetedSpellDurationFont or "Fonts\\FRIZQT__.TTF"
local durationScale = db.targetedSpellDurationScale or 1.0
local durationOutline = db.targetedSpellDurationOutline or "OUTLINE"
local durationX = db.targetedSpellDurationX or 0
local durationY = db.targetedSpellDurationY or 0
local durationColor = db.targetedSpellDurationColor or {r = 1, g = 1, b = 1}
local alpha = db.targetedSpellAlpha or 1.0
local highlightImportant = db.targetedSpellHighlightImportant ~= false
local highlightStyle = db.targetedSpellHighlightStyle or "glow"
local highlightColor = db.targetedSpellHighlightColor or {r = 1, g = 0.8, b = 0}
local highlightSize = db.targetedSpellHighlightSize or 3
local highlightInset = db.targetedSpellHighlightInset or 0
local importantOnly = db.targetedSpellImportantOnly
if durationOutline == "NONE" then durationOutline = "" end
-- Apply pixel perfect to border size
if db.pixelPerfect then
borderSize = DF:PixelPerfect(borderSize)
end
-- Store settings on icon for OnUpdate to use
icon.durationColor = durationColor
icon.baseAlpha = alpha
-- Important spell filter (nested frame approach)
-- When importantOnly is enabled, use SetAlphaFromBoolean to hide non-important spells
if icon.importanceFilterFrame then
if importantOnly and spellID then
local isImportant = C_Spell.IsSpellImportant(spellID)
icon.importanceFilterFrame:SetAlphaFromBoolean(isImportant)
else
-- Not filtering, show everything
icon.importanceFilterFrame:SetAlpha(1)
end
end
-- Important spell highlight
if icon.highlightFrame then
-- Calculate position with inset (negative inset = larger, positive = smaller/inward)
local offset = borderSize + highlightSize - highlightInset
-- Position the highlight frame
icon.highlightFrame:ClearAllPoints()
icon.highlightFrame:SetPoint("TOPLEFT", icon.iconFrame, "TOPLEFT", -offset, offset)
icon.highlightFrame:SetPoint("BOTTOMRIGHT", icon.iconFrame, "BOTTOMRIGHT", offset, -offset)
-- Hide all highlight styles first
HideAnimatedBorder(icon.highlightFrame)
HideSolidBorder(icon.highlightFrame)
HideGlowBorder(icon.highlightFrame)