-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.lua
1308 lines (1173 loc) · 44.8 KB
/
core.lua
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
----------------------------------------------------------------
-- KiwiPlates: core
----------------------------------------------------------------
local addon = KiwiPlates
local Media = LibStub("LibSharedMedia-3.0", true)
local next = next
local pairs = pairs
local ipairs = ipairs
local unpack = unpack
local select = select
local strsub = strsub
local gsub = string.gsub
local format = string.format
local tinsert = table.insert
local tremove = table.remove
local tconcat = table.concat
local C_Timer_After = C_Timer.After
local isClassic = addon.isClassic
local isVanilla = addon.isVanilla
local UNKNOWNOBJECT = UNKNOWNOBJECT
local IsInRaid = IsInRaid
local UnitGUID = UnitGUID
local UnitName = UnitName
local UnitLevel = UnitLevel
local UnitClass = UnitClass
local UnitIsUnit = UnitIsUnit
local UnitHealth = UnitHealth
local UnitIsPlayer = UnitIsPlayer
local UnitIsFriend = UnitIsFriend
local UnitReaction = UnitReaction
local IsInInstance = IsInInstance
local UnitHealthMax = UnitHealthMax
local UnitCanAttack = UnitCanAttack
local UnitIsTapDenied = UnitIsTapDenied
local UnitClassification = UnitClassification
local UnitAffectingCombat = UnitAffectingCombat
local GetNumSubgroupMembers = GetNumSubgroupMembers
local C_GetNamePlateForUnit = C_NamePlate.GetNamePlateForUnit
local C_SetNamePlateSelfSize = C_NamePlate.SetNamePlateSelfSize
local DifficultyColor = addon.DIFFICULTY_LEVEL_COLOR
local UnitGroupRolesAssigned = UnitGroupRolesAssigned or addon.GetCustomDungeonRole
local CastingBarFrame_SetUnit = isVanilla and KiwiPlatesCastingBarFrame_SetUnit or addon.CastingBarFrame_SetUnit
local pixelScale
local targetFrame
local targetExists
local mouseFrame
local GetPlateSkin
local ConditionFields = {}
local activeWidgets = {}
local activeStatuses = {}
local cfgAlpha1
local cfgAlpha2
local cfgAlpha3
local cfgAdjustAlpha
local cfgReactionColor
local cfgHealthColor1
local cfgHealthColor2
local cfgHealthColor3
local cfgHealthThreshold1
local cfgHealthThreshold2
local cfgClassColorReaction = {}
local cfgClassicBorders
local cfgPlatesAdjustW = isClassic and 0 or 24
local NamePlates = {}
local NamePlatesByUnit = {}
local NamePlatesByGUID = {}
local NamePlatesAll = {}
local target = setmetatable({}, {__index = function(t,k) local v=k.."target" t[k]=v return v end})
local Types = { a = 'Player', e = 'Creature', t = 'Pet', m = 'GameObject', h = 'Vehicle', g = 'Vignette' }
local Reactions = { 'hostile', 'hostile', 'hostile', 'neutral', 'friendly', 'friendly', 'friendly', 'friendly' }
local Classifications = { elite = '+', rare = 'r', rareelite = 'r+', boss = 'b' }
local ClassColors = { UNKNOWN = {1,1,1,1} }
for class,color in pairs(RAID_CLASS_COLORS) do
ClassColors[class] = { color.r, color.g, color.b, 1 }
end
local ColorTransparent = { 0,0,0,0 }
local ColorBlack = { 0,0,0,1 }
local ColorWhite = { 1,1,1,1 }
local ColorDefault = ColorWhite
local ColorWidgets = {}
local ColorStatuses = {
color = "Custom Color",
health = "Health Percent",
reaction = "Unit Reaction",
class = "Class Color",
level = "Unit Level",
}
-- Color statuses that cannot be overrided (for example by the threat module)
local ColorsNonOverride = {
blizzard = true, health = true,
}
local FontCache = setmetatable({}, {__index = function(t,k) local v = Media:Fetch('font', k or 'Roboto Condensed Bold'); t[k or 'Roboto Condensed Bold'] = v; return v end})
local TexCache = setmetatable({}, {__index = function(t,k) local v = Media:Fetch('statusbar', k or 'Minimalist'); t[k or 'Minimalist'] = v; return v end})
-- borders
local BorderTextureDefault = 'Flat'
local BorderTexturesData = {}
local BorderTextures = {} -- Used by option tables as values for 'select' type
local function SetBorderTexture(anchorObj, texObj, texName, color)
local f = BorderTexturesData[texName] or BorderTexturesData.Flat
local w, h = anchorObj:GetSize()
texObj:ClearAllPoints()
texObj:SetPoint("TOPLEFT", anchorObj, "TOPLEFT", f[2]*w, f[4]*h)
texObj:SetPoint("BOTTOMRIGHT", anchorObj, "BOTTOMRIGHT",f[3]*w, f[5]*h)
texObj:SetTexture( f[1] )
if color then texObj:SetVertexColor( unpack(color or ColorDefault) ) end
end
local function RegisterBorderTexture(name, file, x1, x2, y1, y2)
local m1 = x1 / (x2-x1)
local m2 = (1-x2) / (x2-x1)
local m3 = y1 / (y2-y1)
local m4 = (1-y2) / (y2-y1)
BorderTextures[name] = name
BorderTexturesData[name] = { file, -m1, m2, m3, -m4 }
BorderTexturesData[file] = name
end
RegisterBorderTexture( 'Flat', 'Interface\\Addons\\KiwiPlates\\media\\borderf', 1/127, 126/127, 1/15, 14/15)
RegisterBorderTexture( 'Bliz Gold', 'Interface\\Addons\\KiwiPlates\\media\\borderg', 3/127, 124/127, 3/15, 12/15)
RegisterBorderTexture( 'Bliz White', 'Interface\\Addons\\KiwiPlates\\media\\borderw', 3/127, 124/127, 3/15, 12/15)
RegisterBorderTexture( 'Bliz StatusBar', 'Interface\\Tooltips\\UI-StatusBar-Border', 3/127, 124/127, 3/15, 12/15)
RegisterBorderTexture( 'Bliz CastingBar', 'Interface\\CastingBar\\UI-CastingBar-Border', 33/255,222/255, 27/63, 37/63)
RegisterBorderTexture( 'Bliz CastingBar Small', 'Interface\\CastingBar\\UI-CastingBar-Border-Small', 32/255,223/255, 25/63, 38/63)
-- classification textures
local CoordEmpty = { 0,0,0,0 }
local ClassTexturesCoord = {
WARRIOR = { 0, .25, 0, .25 },
MAGE = { .25, .50, 0, .25 },
ROGUE = { .50, .75, 0, .25 },
DRUID = { .75, 1, 0, .25 },
HUNTER = { 0, .25, .25, .50 },
SHAMAN = { .25, .50, .25, .50 },
PRIEST = { .50, .75, .25, .50 },
WARLOCK = { .75, 1, .25, .50 },
PALADIN = { 0, .25, .50, .75 },
DEATHKNIGHT = { .25, .50, .50, .75 },
MONK = { .50, .75, .50, .75 },
DEMONHUNTER = { .75, 1, .50, .75 },
elite = { 0, .25, .75, 1 },
rare = { .25, .50, .75, 1 },
rareelite = { .50, .75, .75, 1 },
boss = { .75, 1, .75, 1 },
}
----------------------------------------------------------------
-- Database Defaults
----------------------------------------------------------------
addon.defaults = {
version = 4,
general = {
highlight = true,
classColor = {},
healthColor = { threshold1 = .9, threshold2 = .3, color1 = { .6,1,.8,1 }, color2 = { 1,1,1,1 }, color3 = { 1,.4,.3,1 } },
reactionColor = { hostile = {.7,.2,.1,1}, neutral = {1,.8,0,1}, friendly = {.2,.6,.1,1}, tapped = {.5,.5,.5,1}, playerfriendly = {.2,.6,.1,1}, playerhostile = {.7,.2,.1,1} },
},
skins = { { __skinName = 'Default', kCastBar_enabled = true, kHealthBar_enabled = true, kHealthBorder_enabled = true, kNameText_enabled = true, kLevelText_enabled = true, kHealthText_enabled = true, RaidTargetFrame_enabled = true } },
rules = { { 'and' } },
minimapIcon = {},
roles = isClassic and {} or nil,
}
----------------------------------------------------------------
-- Used to reparent disabled/unused textures
----------------------------------------------------------------
local HiddenFrame = CreateFrame("Frame")
HiddenFrame:Hide()
addon.HiddenFrame = HiddenFrame
----------------------------------------------------------------
-- Highlight texture
----------------------------------------------------------------
local HighlightTex = HiddenFrame:CreateTexture(nil, "OVERLAY")
HighlightTex:SetColorTexture(1,1,1,.2)
HighlightTex:SetVertexColor(1,1,1,1)
HighlightTex:SetBlendMode('ADD')
----------------------------------------------------------------
-- Register media stuff early
----------------------------------------------------------------
Media:Register('font', 'Yanone Kaffesatz Bold', "Interface\\Addons\\KiwiPlates\\media\\yanone.ttf" )
Media:Register('font', 'FrancoisOne', "Interface\\Addons\\KiwiPlates\\media\\francois.ttf" )
Media:Register('font', 'Roboto Condensed Bold', "Interface\\Addons\\KiwiPlates\\media\\roboto.ttf" )
Media:Register("font", "Accidental Presidency", "Interface\\Addons\\KiwiPlates\\media\\accid___.ttf" )
Media:Register("statusbar", "Minimalist", "Interface\\Addons\\KiwiPlates\\media\\Minimalist")
Media:Register("statusbar", "Gradient", "Interface\\Addons\\KiwiPlates\\media\\gradient")
Media:Register("statusbar", "Blizzard Solid White", "Interface\\Buttons\\white8x8")
Media:Register("statusbar", "Blizzard NamePlate", "Interface\\TargetingFrame\\UI-TargetingFrame-BarFill")
----------------------------------------------------------------
-- Statuses color painting management
----------------------------------------------------------------
-- default color sources/statuses used to paint widgets
-- example: { kHealthBorder = 'color', kLevelText = 'reaction' }
local ColorStatusDefaults = {}
-- default static colors used to paint widgets
-- example { kHealthBorder = ColorBlack }
local ColorDefaults = {}
-- Calculate and return the status color for the unit
local ColorMethods = {
blizzard = function(UnitFrame)
return ColorTransparent
end,
reaction = function(UnitFrame)
if UnitFrame.__type == "Player" then
if cfgClassColorReaction[UnitFrame.__reaction] then
return ClassColors[UnitFrame.__class] or ColorWhite
elseif UnitFrame.__reaction == 'friendly' then
return cfgReactionColor.playerfriendly or ColorWhite
else
return cfgReactionColor.playerhostile or ColorWhite
end
else
return (UnitFrame.__tapped and cfgReactionColor.tapped) or cfgReactionColor[UnitFrame.__reaction] or ColorWhite
end
end,
health = function(UnitFrame,_,per)
per = per or UnitHealth(UnitFrame.unit)/UnitHealthMax(UnitFrame.unit)
return (per>=cfgHealthThreshold1 and cfgHealthColor1) or (per>=cfgHealthThreshold2 and cfgHealthColor2) or cfgHealthColor3 or ColorWhite
end,
class = function(UnitFrame)
return ClassColors[UnitFrame.__class] or ClassColors.UNKNOWN
end,
level = function(UnitFrame)
return DifficultyColor[UnitFrame.__level] or DifficultyColor[-1]
end,
color = function(UnitFrame, widgetName)
return UnitFrame.__update[widgetName] or ColorWhite
end,
}
local function UpdatePlateColors(UnitFrame)
local update = UnitFrame.__update
for widgetName,func in pairs(update.methods) do
local widget = UnitFrame[widgetName]
widget:SetWidgetColor( unpack(widget.colorOverride or func(UnitFrame, widgetName)) )
end
end
local function UpdateWidgetColor(UnitFrame, widgetName)
local widget = UnitFrame[widgetName]
widget:SetWidgetColor( unpack( widget.colorOverride or UnitFrame.__update.methods[widgetName](UnitFrame, widgetName) ) )
end
local function UpdateWidgetStatusColor(UnitFrame, statusName)
local widgets = UnitFrame.__update[statusName]
local count = #widgets
if count>0 then
local func = ColorMethods[statusName]
for i=count,1,-1 do
local widgetName = widgets[i]
local widget = UnitFrame[widgetName]
widget:SetWidgetColor( unpack( widget.colorOverride or func(UnitFrame, widgetName) ) )
end
end
end
----------------------------------------------------------------
-- Widgets management
----------------------------------------------------------------
-- Registered widgets:
-- widgetKey => widget table, see widgets folder for widgets tables definitions
local WidgetRegistered = {}
-- Registered Widgets Keys, index part: keys when widget active, hash part: key when widget active => key of created widget
-- example: { kLevelText, kLevelText = 'kkLevelText' }
local WidgetNames = {}
-- cached widget.Update() functions
local WidgetMethods = {}
-- Table that caches settings for each skin to update widgets
-- colors & values, example:
-- WidgetUpdate[skin] = {
-- -- functions to update widgets texts and statusbars
-- [1] = WidgetMethods.kHealthBar, [2] = WidgetMethods.kLevelText, ...
-- -- user defined colors & active widgets
-- ['kHealthBar'] = customColor1, ['kNameText'] = customColor2, ['ClassificationFrame'] = true, ...
-- -- statuses
-- methods = { ['kHealthBar'] = UpdateColorReaction, ['kLevelText'] = UpdateColorCustom, ['kNameText'] = UpdateColorCustom },
-- reaction = { 'kHealthBar' },
-- color = { 'kNameText', 'kLevelText' }, -- color = customColor = statusName
-- }
local WidgetUpdate = {}
function UpdatePlateValues(UnitFrame)
local widgets = UnitFrame.__update
for i=#widgets,1,-1 do
widgets[i](UnitFrame)
end
end
----------------------------------------------------------------
-- Health update for health text widget & health status
----------------------------------------------------------------
local HealthFrame
do
local UpdateColorHealth = ColorMethods.health
local UpdateColorReaction = ColorMethods.reaction
HealthFrame = CreateFrame("Frame")
HealthFrame:SetScript("OnEvent", function(_, _, unit)
local UnitFrame = NamePlatesByUnit[unit]
if UnitFrame then
local update, percent = UnitFrame.__update
if UnitFrame.kHealthText then
percent = WidgetMethods.kHealthText(UnitFrame)
end
local widgets = update.health
if #widgets>0 then
local color = UpdateColorHealth(UnitFrame,nil,percent)
for i=1,#widgets do
local widget = UnitFrame[widgets[i]]
widget:SetWidgetColor( unpack( widget.colorOverride or color ) )
end
end
local widgets = update.reaction
if #widgets>0 then
local tapped = UnitIsTapDenied(unit)
if tapped ~= UnitFrame.__tapped then
UnitFrame.__tapped = tapped
local color = UpdateColorReaction(UnitFrame)
for i=1,#widgets do
local widget = UnitFrame[widgets[i]]
widget:SetWidgetColor( unpack( widget.colorOverride or color ) )
end
end
end
end
end )
end
----------------------------------------------------------------
-- Disable blizzard stuff
----------------------------------------------------------------
local function ForceHide(self)
self:Hide()
end
local function ForceHideHealth(self)
if self.kkDisabled then self:Hide() end
end
local function DisableBlizzardStuff(UnitFrame)
local healthBar = UnitFrame.healthBar
healthBar.barTexture:SetColorTexture(0,0,0,0)
if isClassic then
local level = healthBar:GetFrameLevel()+1
UnitFrame.RaidTargetFrame:SetFrameLevel(level)
UnitFrame.LevelFrame:Hide()
if not cfgClassicBorders then healthBar.border:Hide() end
else
local textures = healthBar.border and healthBar.border.Textures or UnitFrame.HealthBarsContainer.border.Textures
for i=#textures,1,-1 do
textures[i]:SetVertexColor(0,0,0,0)
textures[i]:SetColorTexture(0,0,0,0)
textures[i]:Hide()
end
local level = UnitFrame.castBar:GetFrameLevel()+1
UnitFrame.RaidTargetFrame:SetFrameLevel(level)
UnitFrame.ClassificationFrame:SetScript('OnShow', ForceHide)
UnitFrame.ClassificationFrame:Hide()
if UnitFrame.HealthBarsContainer then
UnitFrame.HealthBarsContainer:SetScript("OnShow", ForceHideHealth)
end
end
end
----------------------------------------------------------------
-- Skin a nameplate
----------------------------------------------------------------
-- cached widget.Layout() functions, indexes by widget key
local SkinMethods = {}
local function SkinPlate(plateFrame, UnitFrame, UnitAdded)
-- calculate skin
local db = addon.db.skins[ GetPlateSkin(UnitFrame, addon.InCombat, addon.InstanceType) ]
-- opacity & frame level
local target = UnitFrame.__target
local mouse = UnitFrame.__mouseover
UnitFrame:SetFrameStrata( (target or mouse) and "HIGH" or "MEDIUM" )
UnitFrame:SetAlpha( (mouse and 1) or (target and cfgAlpha1) or (not targetExists and cfgAlpha3) or cfgAlpha2 )
local Reskin = (db ~= UnitFrame.__skin)
if Reskin or UnitAdded then -- blizzard code resets these settings, so we need to reapply them even if our skin has not changed.
local healthContainer = UnitFrame.HealthBarsContainer
-- UnitFrame
UnitFrame:ClearAllPoints()
UnitFrame:SetPoint( 'TOP', plateFrame, 'TOP', 0, 0 )
UnitFrame:SetPoint( 'BOTTOM', plateFrame, 'BOTTOM', 0, db.plateOffsetY or 6 )
UnitFrame:SetWidth( (db.healthBarWidth or 136) + cfgPlatesAdjustW )
-- healthBar
local healthBar = UnitFrame.healthBar
local anchorFrame = UnitFrame.castBar or UnitFrame
local gap = db.castBarGap or (isClassic and 0) or nil
if gap ~= UnitFrame.castBarGap then
-- in classic we execute this code if gap is not defined to reanchor healthBar the "(isClassic and 0)" above forces to execute this code
-- in retail is not necessary because healthBar is already anchored to castBar with "correct" point values
local bar = healthContainer or healthBar
bar:ClearAllPoints()
bar:SetPoint('BOTTOMLEFT', anchorFrame, isVanilla and 'BOTTOMLEFT' or 'TOPLEFT', 0, gap or 0 )
bar:SetPoint('BOTTOMRIGHT',anchorFrame, isVanilla and 'BOTTOMRIGHT' or 'TOPRIGHT', 0, gap or 0 )
UnitFrame.castBarGap = gap
end
if healthContainer then
healthContainer:SetHeight( db.healthBarHeight or 12 )
if db.kHealthBar_enabled then
healthContainer.kkDisabled = nil
healthContainer:Show()
else
healthContainer.kkDisabled = true
healthContainer:Hide()
end
else
healthBar:SetHeight( db.healthBarHeight or 12 )
if db.kHealthBar_enabled then
healthBar:Show()
if not healthBar:IsShown() then -- Workaround to weird bug, hidden bars refused to be visible
C_Timer_After(0, function() healthBar:Show() end)
end
else
healthBar:Hide()
end
end
-- castBar
local castBar = UnitFrame.kkCastBar
if castBar then
castBar:SetHeight( db.castBarHeight or 10 ) -- SetHeight() is called for disabled castBars, wrong but necessary in retail because healthbar is anchored to the castbar
if not isClassic then
castBar.Text:SetFont( FontCache[db.castBarFontFile or 'Roboto Condensed Bold'], db.castBarFontSize or 8, db.castBarFontFlags or 'OUTLINE' )
end
if db.kCastBar_enabled and not ((UnitFrame.__reaction=='friendly')==db.castBarHiddenFriendly) then
CastingBarFrame_SetUnit(castBar, UnitFrame.unit, false, true)
else
CastingBarFrame_SetUnit(castBar, nil)
end
end
end
if Reskin then
local update = WidgetUpdate[db]
-- save skin & update stuff
UnitFrame.__skin = db
UnitFrame.__update = update
-- skin widgets
local frameAnchor = UnitFrame.healthBar
for i=1,#WidgetNames do
local widgetName = WidgetNames[i]
SkinMethods[widgetName]( UnitFrame, frameAnchor, db, update[widgetName] )
end
-- update widgets values & color
UpdatePlateValues(UnitFrame)
UpdatePlateColors(UnitFrame)
-- notify that a plate has been skinned to other modules
addon:SendMessage('PLATE_SKINNED',UnitFrame, db)
return true
elseif UnitAdded then
-- update widgets values & color
UpdatePlateValues(UnitFrame)
UpdatePlateColors(UnitFrame)
end
end
----------------------------------------------------------------
-- Fix Unknown entities in plate names
----------------------------------------------------------------
function addon:UNIT_NAME_UPDATE(unit)
local UnitFrame = NamePlatesByUnit[unit]
if UnitFrame then
local name = UnitName(unit)
UnitFrame.__name = name
local text = UnitFrame.kNameText
if text then
text:SetText(name)
end
if ConditionFields.names then
SkinPlate( C_GetNamePlateForUnit(unit), UnitFrame )
end
end
end
----------------------------------------------------------------
-- Reskin visible nameplates
----------------------------------------------------------------
local function ReskinPlates()
for plateFrame, UnitFrame in pairs(NamePlates) do
SkinPlate(plateFrame, UnitFrame)
end
end
----------------------------------------------------------------
-- Highlights nameplate
----------------------------------------------------------------
local function HighlightSet(UnitFrame)
if UnitFrame then
if UnitFrame.kHealthBar then
HighlightTex:ClearAllPoints()
HighlightTex:SetParent(UnitFrame.healthBar)
HighlightTex:SetAllPoints()
end
else
HighlightTex:SetParent(HiddenFrame)
end
end
----------------------------------------------------------------
-- Units Combat Status tracking (not player in combat)
----------------------------------------------------------------
local function UpdatePlatesUnitCombatValues()
for plateFrame, UnitFrame in pairs(NamePlates) do
local unit = UnitFrame.unit
UnitFrame.__combat = UnitAffectingCombat(unit) or ( UnitIsPlayer(target[unit]) and UnitIsFriend(target[unit],'player') )
end
end
local UpdateCombatTracking
do
local timer = addon.CreateTimer(.25, function()
for plateFrame, UnitFrame in pairs(NamePlates) do
local unit = UnitFrame.unit
local combat = UnitAffectingCombat(unit) or ( UnitIsPlayer(target[unit]) and UnitIsFriend(target[unit],'player') )
if combat ~= UnitFrame.__combat then
UnitFrame.__combat = combat
SkinPlate(plateFrame, UnitFrame)
end
end
end )
function UpdateCombatTracking(enabled)
timer:SetPlaying(not not enabled)
end
end
----------------------------------------------------------------
-- Mouseover management
----------------------------------------------------------------
do
local timer
timer = addon.CreateTimer(.2, function()
if not (mouseFrame and (not mouseFrame.UnitFrame or UnitIsUnit('mouseover', mouseFrame.UnitFrame.unit)) ) then
timer:Stop()
addon:UPDATE_MOUSEOVER_UNIT()
end
end )
function addon:UPDATE_MOUSEOVER_UNIT()
local plateFrame = C_GetNamePlateForUnit('mouseover')
if plateFrame~=mouseFrame then
if mouseFrame then
local UnitFrame = mouseFrame.UnitFrame
if UnitFrame then
UnitFrame.__mouseover = nil
SkinPlate(mouseFrame, UnitFrame)
mouseFrame = nil
HighlightSet(nil)
timer:Stop()
end
end
if plateFrame and NamePlates[plateFrame] then
local UnitFrame = plateFrame.UnitFrame
if UnitFrame then
UnitFrame.__mouseover = true
mouseFrame = plateFrame
SkinPlate(mouseFrame, UnitFrame)
HighlightSet(UnitFrame)
timer:Play()
end
end
end
end
end
----------------------------------------------------------------
-- Opacity adjust
----------------------------------------------------------------
local function UpdatePlatesOpacity()
local alpha = targetExists and cfgAlpha2 or cfgAlpha3
for plateFrame, UnitFrame in pairs(NamePlates) do
if plateFrame~=targetFrame and plateFrame~=mouseFrame then
UnitFrame:SetAlpha( alpha )
end
end
end
----------------------------------------------------------------
-- Personal resource bar
----------------------------------------------------------------
local function PersonalBarAdded(plateFrame)
-- undo some custom changes to avoid displaying a messed personal bar
local UnitFrame = plateFrame.UnitFrame
UnitFrame.healthBar.barTexture:SetTexture("Interface\\TargetingFrame\\UI-StatusBar")
for i=1,#WidgetNames do
local widget = UnitFrame[WidgetNames[i]]
if widget then widget:Hide() end
end
UnitFrame.__skin = nil
end
local function PersonalBarRemoved(plateFrame)
-- redo our custom changes once the personal resource bar is hidden
local UnitFrame = plateFrame.UnitFrame
if UnitFrame then
UnitFrame.healthBar.barTexture:SetColorTexture(0,0,0,0)
end
end
----------------------------------------------------------------
-- Player target management
----------------------------------------------------------------
function addon:PLAYER_TARGET_CHANGED()
local plateFrame = C_GetNamePlateForUnit('target')
if plateFrame ~= targetFrame then
if targetFrame then
local UnitFrame = targetFrame.UnitFrame
if UnitFrame then
UnitFrame.__target = nil
SkinPlate(targetFrame, UnitFrame)
targetFrame = nil
end
end
if plateFrame and NamePlates[plateFrame] then
local UnitFrame = plateFrame.UnitFrame
if UnitFrame then
UnitFrame.__target = true
SkinPlate(plateFrame, UnitFrame)
targetFrame = plateFrame
self:SendMessage('PLAYER_TARGET_ACQUIRED', plateFrame, 'target' )
end
end
self:SendMessage("NAME_PLATE_TARGET_CHANGED", targetFrame)
end
if targetExists ~= UnitExists('target') then
targetExists = not targetExists
if cfgAdjustAlpha then
UpdatePlatesOpacity()
end
end
self:SendMessage('PLAYER_TARGET_CHANGED', plateFrame, 'target')
end
----------------------------------------------------------------
-- Fix Health Bar height because game changes the user defined value
----------------------------------------------------------------
local fix_health_stop = false
local function FixHealthBarSize(bar, w, h)
if not fix_health_stop and bar.UnitFrame then
local db = bar.UnitFrame.__skin
if db then
local j = db.healthBarHeight or 12
local d = j - h
if d > 0.5 or d < -0.5 then
fix_health_stop = true
bar:SetHeight(j)
fix_health_stop = false
end
end
end
end
---------------------------------------------------------------
-- Nameplate created event
----------------------------------------------------------------
local CreateMethods = {}
local CreateNamePlate
do
function CreateNamePlate(UnitFrame)
for i=1,#activeWidgets do
local widgetName = activeWidgets[i]
if not UnitFrame[WidgetNames[widgetName]] then
CreateMethods[widgetName](UnitFrame)
end
end
if not UnitFrame.HealthBarsContainer then
UnitFrame.healthBar:SetScript('OnSizeChanged', FixHealthBarSize) -- see FixHealthBarSize()
end
UnitFrame.__skin = nil
end
function addon:NAME_PLATE_CREATED(plateFrame)
local UnitFrame = plateFrame.UnitFrame
if not UnitFrame.__kInitialized then
UnitFrame.__kInitialized = true
DisableBlizzardStuff(UnitFrame)
CreateNamePlate(UnitFrame)
NamePlatesAll[#NamePlatesAll+1] = UnitFrame
self:SendMessage("NAME_PLATE_CREATED", UnitFrame, plateFrame)
end
end
end
---------------------------------------------------------------
-- Nameplate added event
----------------------------------------------------------------
function addon:NAME_PLATE_UNIT_ADDED(unit)
local plateFrame = C_GetNamePlateForUnit(unit)
if UnitIsUnit(unit,'player') then return PersonalBarAdded(plateFrame) end
if plateFrame then
self:NAME_PLATE_CREATED(plateFrame)
local guid = UnitGUID(unit)
local UnitFrame = plateFrame.UnitFrame
NamePlates[plateFrame] = UnitFrame
NamePlatesByUnit[unit] = UnitFrame
NamePlatesByGUID[guid] = UnitFrame
UnitFrame:SetParent(WorldFrame)
UnitFrame:SetScale(pixelScale)
UnitFrame.__guid = guid
UnitFrame.__class = select(2,UnitClass(unit))
UnitFrame.__type = Types[ strsub( guid, 3,3 ) ]
UnitFrame.__reaction = Reactions [ UnitReaction( unit, "player") or 1 ]
UnitFrame.__level = UnitLevel( unit )
UnitFrame.__classification = UnitClassification(unit) or 'unknow'
UnitFrame.__combat = UnitAffectingCombat(unit) or ( UnitIsPlayer(target[unit]) and UnitIsFriend(target[unit],'player') )
UnitFrame.__tapped = UnitIsTapDenied(unit)
UnitFrame.__attackable = UnitCanAttack('player',unit)
UnitFrame.__attackers = 0
if UnitFrame.__level==-1 or UnitFrame.__classification=='worldboss' then
UnitFrame.__classification = 'boss'
end
UnitFrame.__name = UnitName( unit )
local newTarget = UnitIsUnit( unit, 'target' ) or nil
UnitFrame.__target = newTarget
if newTarget then
if targetFrame and targetFrame.UnitFrame then -- unmark&reskin old target frame
targetFrame.UnitFrame.__target = nil
SkinPlate(targetFrame, targetFrame.UnitFrame)
end
targetFrame = plateFrame
end
SkinPlate( plateFrame, UnitFrame, true )
UnitFrame.healthBar.UnitFrame = UnitFrame -- see FixHealthBarSize()
self:SendMessage("NAME_PLATE_UNIT_ADDED", UnitFrame, unit)
if newTarget then
self:SendMessage("NAME_PLATE_TARGET_CHANGED", targetFrame)
end
end
end
----------------------------------------------------------------
-- Nameplate removed event
----------------------------------------------------------------
function addon:NAME_PLATE_UNIT_REMOVED(unit)
local plateFrame = C_GetNamePlateForUnit(unit)
if UnitIsUnit(unit,'player') then return PersonalBarRemoved(plateFrame) end
local UnitFrame = plateFrame.UnitFrame or NamePlates[plateFrame]
if UnitFrame then
local targetCleared
UnitFrame.healthBar.UnitFrame = nil -- see FixHealthBarSize()
UnitFrame.__threat = nil
UnitFrame.__target = nil
UnitFrame.__mouseover = nil
if plateFrame == targetFrame then
targetFrame = nil
targetCleared = true
end
if plateFrame == mouseFrame then
mouseFrame = nil
end
if isClassic and UnitFrame.kCastBar then
CastingBarFrame_SetUnit(UnitFrame.kCastBar, nil)
end
UnitFrame:SetParent(plateFrame)
UnitFrame:SetScale(1)
UnitFrame:ClearAllPoints()
UnitFrame:SetAllPoints()
NamePlates[plateFrame] = nil
NamePlatesByUnit[unit] = nil
NamePlatesByGUID[UnitFrame.__guid or 0] = nil
self:SendMessage("NAME_PLATE_UNIT_REMOVED", UnitFrame, unit)
if targetCleared then
self:SendMessage("NAME_PLATE_TARGET_CHANGED")
end
end
end
----------------------------------------------------------------
-- Events triggering reaction/attackable stuff update
----------------------------------------------------------------
function addon:UNIT_FLAGS(unit)
local UnitFrame = NamePlatesByUnit[unit]
if UnitFrame then
local reaction = Reactions [ UnitReaction( unit, "player") or 1 ]
local attackable = UnitCanAttack('player',unit)
if reaction~=UnitFrame.__reaction or attackable~=UnitFrame.__attackable then
local reskinned
UnitFrame.__reaction = reaction
UnitFrame.__attackable = attackable
if ConditionFields['@attackable'] or ConditionFields['@reaction'] then
reskinned = SkinPlate( C_GetNamePlateForUnit(unit), UnitFrame )
end
if not reskinned and activeStatuses.reaction then
UpdateWidgetStatusColor(UnitFrame, 'reaction')
end
end
end
end
addon.UNIT_TARGETABLE_CHANGED = UNIT_FLAGS
addon.UNIT_FACTION = UNIT_FLAGS
----------------------------------------------------------------
-- Events triggering level/classification stuff update
----------------------------------------------------------------
function addon:UNIT_CLASSIFICATION_CHANGED(unit)
local UnitFrame = NamePlatesByUnit[unit]
if UnitFrame then
UnitFrame.__level = UnitLevel( unit )
UnitFrame.__classification = UnitClassification(unit) or 'unknow'
if not ConditionFields['@classification'] or not SkinPlate( C_GetNamePlateForUnit(unit), UnitFrame ) then
addon:SendMessage('UNIT_CLASSIFICATION_CHANGED', UnitFrame, unit)
end
end
end
----------------------------------------------------------------
-- kAttackers
----------------------------------------------------------------
function addon:GROUP_ROSTER_UPDATE()
local group = not IsInRaid() and GetNumSubgroupMembers()>0
if group ~= addon.InGroup then
addon.InGroup = group
addon:SendMessage('GROUP_TYPE_CHANGED')
end
end
----------------------------------------------------------------
-- Combat switch reskin
----------------------------------------------------------------
local function CombatReskinCheck(delay)
if delay then
-- We need to add a delay because some times UnitAffectingCombat() does not return correct values just after combat start.
C_Timer_After(.05, CombatReskinCheck)
return
end
local reskin = ConditionFields['@combat']
if reskin then
UpdatePlatesUnitCombatValues()
else
reskin = ConditionFields['combat']
end
if reskin then
ReskinPlates()
end
end
----------------------------------------------------------------
-- Combat Visibility
----------------------------------------------------------------
local function UpdateVisibility()
if not InCombatLockdown() then
local value = addon.db.general.nameplateShowFriends or 0
if value>=2 then
if value<=3 then
value = addon.InCombat == (value==2)
else
value = not IsInInstance() == (value==5)
end
value = value and "1" or "0"
if GetCVar("nameplateShowFriends")~=value then
SetCVar("nameplateShowFriends", value)
end
end
local value = addon.db.general.nameplateShowEnemies or 0
if value>=2 then
if value<=3 then
value = addon.InCombat == (value==2)
else
value = not IsInInstance() == (value==5)
end
value = value and "1" or "0"
if GetCVar("nameplateShowEnemies")~=value then
SetCVar("nameplateShowEnemies", value)
end
end
end
end
----------------------------------------------------------------
-- Combat Start
----------------------------------------------------------------
function addon:PLAYER_REGEN_DISABLED()
addon.InCombat = true
UpdateVisibility()
self:SendMessage('COMBAT_START')
if ConditionFields['@combat'] then
UpdateCombatTracking(true)
CombatReskinCheck(true)
else
CombatReskinCheck()
end
end
----------------------------------------------------------------
-- Combat End
----------------------------------------------------------------
function addon:PLAYER_REGEN_ENABLED()
addon.InCombat = false
self:SendMessage('COMBAT_END')
if ConditionFields['@combat'] then
UpdateCombatTracking(false)
end
CombatReskinCheck()
UpdateVisibility()
end
----------------------------------------------------------------
-- Zone Changed, reskin plates if necessary
----------------------------------------------------------------
function addon:ZONE_CHANGED_NEW_AREA(event)
local _, type = IsInInstance()
if type ~= addon.InstanceType then
addon.InstanceType = type
UpdateVisibility()
if ConditionFields.instance then
ReskinPlates()
end
end
end
----------------------------------------------------------------
-- Player entering world
----------------------------------------------------------------
if addon.isClassic then
addon.PLAYER_ENTERING_WORLD = addon.ZONE_CHANGED_NEW_AREA
else
addon.ZONE_CHANGED_NEW_AREA = addon.ZONE_CHANGED_NEW_AREA
end
----------------------------------------------------------------
-- Compile a function to calculate nameplate skin
----------------------------------------------------------------
local UpdateSkinCheckFunction
do
local function NamesToList(names)
local lines = {}
local t = { strsplit("\n",names) }
for i=1,#t do -- Remove comments: any text starting with #@\/-[ characters.
local s = strtrim( (strsplit( "#@\\\/\-\[", t[i] )) ) -- Don't remove strsplit extra brackets.
if #s>0 then tinsert( lines, format('["%s"]=true',s) ) end
end
return tconcat( lines , ',')
end
local function MakeSkinCheckWithClosure(names, source)
local lines = { "return function()" }
for i=1,#names do
tinsert( lines, format("local names%d = {%s}",i, NamesToList(names[i]) ) )
end
tinsert( lines, source )
tinsert( lines, 'end' )
return assert(loadstring(tconcat( lines, "\n")))()()
end