-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathunitframe.lua
2568 lines (2136 loc) · 80.6 KB
/
unitframe.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
local detailsFramework = _G["DetailsFramework"]
if (not detailsFramework or not DetailsFrameworkCanLoad) then
return
end
local _
--lua locals
local rawset = rawset --lua local
local rawget = rawget --lua local
local setmetatable = setmetatable --lua local
local unpack = table.unpack or unpack --lua local
local type = type --lua local
local floor = math.floor --lua local
local loadstring = loadstring --lua local
local G_CreateFrame = _G.CreateFrame
local CreateFrame = function (frameType , name, parent, template, id)
local frame = G_CreateFrame(frameType , name, parent, template, id)
detailsFramework:Mixin(frame, detailsFramework.FrameFunctions)
frame:SetClipsChildren(false)
return frame
end
local UnitHealth = UnitHealth
local UnitHealthMax = UnitHealthMax
local UnitGetIncomingHeals = UnitGetIncomingHeals
local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs
local UnitGetTotalAbsorbs = UnitGetTotalAbsorbs
local UnitPowerMax = UnitPowerMax
local UnitPower = UnitPower
local UnitPowerBarID = UnitPowerBarID
local GetUnitPowerBarInfoByID = GetUnitPowerBarInfoByID
local IsInGroup = IsInGroup
local UnitPowerType = UnitPowerType
local UnitIsConnected = UnitIsConnected
local UnitPlayerControlled = UnitPlayerControlled
local UnitIsTapDenied = UnitIsTapDenied
local max = math.max
local min = math.min
local abs = math.abs
local GetSpellInfo = GetSpellInfo or function(spellID) if not spellID then return nil end local si = C_Spell.GetSpellInfo(spellID) if si then return si.name, nil, si.iconID, si.castTime, si.minRange, si.maxRange, si.spellID, si.originalIconID end end
local IS_WOW_PROJECT_MAINLINE = WOW_PROJECT_ID == WOW_PROJECT_MAINLINE
local IS_WOW_PROJECT_NOT_MAINLINE = WOW_PROJECT_ID ~= WOW_PROJECT_MAINLINE
local IS_WOW_PROJECT_CLASSIC_ERA = WOW_PROJECT_ID == WOW_PROJECT_CLASSIC
local CastInfo = detailsFramework.CastInfo
local PixelUtil = PixelUtil or DFPixelUtil
local UnitGroupRolesAssigned = detailsFramework.UnitGroupRolesAssigned
local cleanfunction = function() end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--health bar frame
--[=[
DF:CreateHealthBar (parent, name, settingsOverride)
creates a health bar to show an unit health
@parent = frame to pass for the CreateFrame function
@name = absolute name of the frame, if omitted it uses the parent's name .. "HealthBar"
@settingsOverride = table with keys and values to replace the defaults from the framework
methods:
healthbar:SetUnit(unit)
healthBar:GetTexture()
healthBar:SetTexture(texture)
--]=]
---@class df_healthbarsettings : table
---@field CanTick boolean
---@field ShowHealingPrediction boolean
---@field ShowShields boolean
---@field BackgroundColor table
---@field Texture texturepath|textureid|atlasname
---@field ShieldIndicatorTexture texturepath|textureid|atlasname
---@field ShieldGlowTexture texturepath|textureid|atlasname
---@field ShieldGlowWidth number
---@field DontSetStatusBarTexture boolean
---@field Width number
---@field Height number
---@class df_healthbar : statusbar, df_scripthookmixin, df_statusbarmixin
---@field unit unit
---@field displayedUnit unit
---@field oldHealth number
---@field currentHealth number
---@field currentHealthMax number
---@field nextShieldHook number
---@field WidgetType string
---@field Settings df_healthbarsettings
---@field background texture
---@field incomingHealIndicator texture
---@field shieldAbsorbIndicator texture
---@field healAbsorbIndicator texture
---@field shieldAbsorbGlow texture
---@field barTexture texture
---@field SetUnit fun(self:df_healthbar, unit:unit?, displayedUnit:unit)
---@field GetTexture fun(self:df_healthbar) : texture
---@field SetTexture fun(self:df_healthbar, texture:texturepath|textureid|atlasname)
---@field SetColor fun(self:df_healthbar, red:number, green:number, blue:number, alpha:number)
---@field UpdateHealPrediction fun(self:df_healthbar)
---@field UpdateHealth fun(self:df_healthbar)
---@field UpdateMaxHealth fun(self:df_healthbar)
--healthBar meta prototype
local healthBarMetaPrototype = {
WidgetType = "healthBar",
dversion = detailsFramework.dversion,
}
--check if there's a metaPrototype already existing
if (_G[detailsFramework.GlobalWidgetControlNames["healthBar"]]) then
--get the already existing metaPrototype
local oldMetaPrototype = _G[detailsFramework.GlobalWidgetControlNames["healthBar"]]
--check if is older
if ( (not oldMetaPrototype.dversion) or (oldMetaPrototype.dversion < detailsFramework.dversion) ) then
--the version is older them the currently loading one
--copy the new values into the old metatable
for funcName, _ in pairs(healthBarMetaPrototype) do
oldMetaPrototype[funcName] = healthBarMetaPrototype[funcName]
end
end
else
--first time loading the framework
_G[detailsFramework.GlobalWidgetControlNames["healthBar"]] = healthBarMetaPrototype
end
local healthBarMetaFunctions = _G[detailsFramework.GlobalWidgetControlNames["healthBar"]]
detailsFramework:Mixin(healthBarMetaFunctions, detailsFramework.ScriptHookMixin)
--hook list
local defaultHooksForHealthBar = {
OnHide = {},
OnShow = {},
OnHealthChange = {},
OnHealthMaxChange = {},
OnAbsorbOverflow = {},
}
--use the hook already existing
healthBarMetaFunctions.HookList = healthBarMetaFunctions.HookList or defaultHooksForHealthBar
--copy the non existing values from a new version to the already existing hook table
detailsFramework.table.deploy(healthBarMetaFunctions.HookList, defaultHooksForHealthBar)
--Health Bar Meta Functions
--health bar settings
healthBarMetaFunctions.Settings = {
CanTick = false, --if true calls the method 'OnTick' every tick, the function needs to be overloaded, it receives self and deltaTime as parameters
ShowHealingPrediction = true, --when casting a healing pass, show the amount of health that spell will heal
ShowShields = true, --indicator of the amount of damage absortion the unit has
DontSetStatusBarTexture = false,
--appearance
BackgroundColor = detailsFramework:CreateColorTable (.2, .2, .2, .8),
Texture = [[Interface\RaidFrame\Raid-Bar-Hp-Fill]],
ShieldIndicatorTexture = [[Interface\RaidFrame\Shield-Fill]],
ShieldGlowTexture = [[Interface\RaidFrame\Shield-Overshield]],
ShieldGlowWidth = 16,
--default size
Width = 100,
Height = 20,
}
healthBarMetaFunctions.HealthBarEvents = {
{"PLAYER_ENTERING_WORLD"},
{"UNIT_HEALTH", true},
{"UNIT_MAXHEALTH", true},
{(IS_WOW_PROJECT_NOT_MAINLINE) and "UNIT_HEALTH_FREQUENT", true}, -- this one is classic-only...
{"UNIT_HEAL_PREDICTION", true},
{(IS_WOW_PROJECT_MAINLINE) and "UNIT_ABSORB_AMOUNT_CHANGED", true},
{(IS_WOW_PROJECT_MAINLINE) and "UNIT_HEAL_ABSORB_AMOUNT_CHANGED", true},
}
--setup the castbar to be used by another unit
healthBarMetaFunctions.SetUnit = function(self, unit, displayedUnit)
if (self.unit ~= unit or self.displayedUnit ~= displayedUnit or unit == nil) then
self.unit = unit
self.displayedUnit = displayedUnit or unit
--register events
if (unit) then
self.currentHealth = UnitHealth(unit) or 0
self.currentHealthMax = UnitHealthMax(unit) or 0
for _, eventTable in ipairs(self.HealthBarEvents) do
local event = eventTable[1]
local isUnitEvent = eventTable[2]
if event then
if (isUnitEvent) then
self:RegisterUnitEvent(event, self.displayedUnit, self.unit)
else
self:RegisterEvent(event)
end
end
end
--check for settings and update some events
if (not self.Settings.ShowHealingPrediction) then
self:UnregisterEvent("UNIT_HEAL_PREDICTION")
if IS_WOW_PROJECT_MAINLINE then
self:UnregisterEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED")
end
self.incomingHealIndicator:Hide()
self.healAbsorbIndicator:Hide()
end
if (not self.Settings.ShowShields) then
if IS_WOW_PROJECT_MAINLINE then
self:UnregisterEvent("UNIT_ABSORB_AMOUNT_CHANGED")
end
self.shieldAbsorbIndicator:Hide()
self.shieldAbsorbGlow:Hide()
end
--set scripts
self:SetScript("OnEvent", self.OnEvent)
if (self.Settings.CanTick) then
self:SetScript("OnUpdate", self.OnTick)
end
self:PLAYER_ENTERING_WORLD(self.unit, self.displayedUnit)
else
--remove all registered events
for _, eventTable in ipairs(self.HealthBarEvents) do
local event = eventTable[1]
if event then
self:UnregisterEvent(event)
end
end
--remove scripts
self:SetScript("OnEvent", nil)
self:SetScript("OnUpdate", nil)
self:Hide()
end
end
end
healthBarMetaFunctions.Initialize = function(self)
PixelUtil.SetWidth(self, self.Settings.Width, 1)
PixelUtil.SetHeight(self, self.Settings.Height, 1)
self:SetTexture(self.Settings.Texture)
self.background:SetAllPoints()
self.background:SetColorTexture(self.Settings.BackgroundColor:GetColor())
--setpoint of these widgets are set inside the function that updates the incoming heal
self.incomingHealIndicator:SetTexture(self:GetTexture())
self.healAbsorbIndicator:SetTexture(self:GetTexture())
self.healAbsorbIndicator:SetVertexColor(.1, .8, .8)
self.shieldAbsorbIndicator:SetTexture(self.Settings.ShieldIndicatorTexture, true, true)
self.shieldAbsorbGlow:SetWidth(self.Settings.ShieldGlowWidth)
self.shieldAbsorbGlow:SetTexture(self.Settings.ShieldGlowTexture)
self.shieldAbsorbGlow:SetBlendMode("ADD")
self.shieldAbsorbGlow:SetPoint("topright", self, "topright", 8, 0)
self.shieldAbsorbGlow:SetPoint("bottomright", self, "bottomright", 8, 0)
self.shieldAbsorbGlow:Hide()
self:SetUnit(nil)
self.currentHealth = 1
self.currentHealthMax = 2
end
--call every tick
healthBarMetaFunctions.OnTick = function(self, deltaTime) end --if overrided, set 'CanTick' to true on the settings table
--when an event happen for this unit, send it to the apropriate function
healthBarMetaFunctions.OnEvent = function(self, event, ...)
local eventFunc = self[event]
if (eventFunc) then
--the function doesn't receive which event was, only 'self' and the parameters
eventFunc(self, ...)
end
end
--when the unit max health is changed
healthBarMetaFunctions.UpdateMaxHealth = function(self)
local maxHealth = UnitHealthMax(self.displayedUnit)
self:SetMinMaxValues(0, maxHealth)
self.currentHealthMax = maxHealth
if (self.OnHealthMaxChange) then --direct call
self.OnHealthMaxChange(self, self.displayedUnit)
else
self:RunHooksForWidget("OnHealthMaxChange", self, self.displayedUnit)
end
end
healthBarMetaFunctions.UpdateHealth = function(self)
-- update max health regardless to avoid weird wrong values on UpdateMaxHealth sometimes
-- local maxHealth = UnitHealthMax(self.displayedUnit)
-- self:SetMinMaxValues(0, maxHealth)
-- self.currentHealthMax = maxHealth
self.oldHealth = self.currentHealth
local health = UnitHealth(self.displayedUnit)
self.currentHealth = health
PixelUtil.SetStatusBarValue(self, health)
if (self.OnHealthChange) then --direct call
self.OnHealthChange(self, self.displayedUnit)
else
self:RunHooksForWidget("OnHealthChange", self, self.displayedUnit)
end
end
--health and absorbs prediction
healthBarMetaFunctions.UpdateHealPrediction = function(self)
local currentHealth = self.currentHealth
local currentHealthMax = self.currentHealthMax
if (not currentHealthMax or currentHealthMax <= 0) then
return
end
local healthPercent = currentHealth / currentHealthMax
--order is: the health of the unit > damage absorb > heal absorb > incoming heal
local width = self:GetWidth()
if (self.Settings.ShowHealingPrediction) then
--incoming heal on the unit from all sources
local unitHealIncoming = self.displayedUnit and UnitGetIncomingHeals(self.displayedUnit) or 0
--heal absorbs
local unitHealAbsorb = IS_WOW_PROJECT_MAINLINE and self.displayedUnit and UnitGetTotalHealAbsorbs(self.displayedUnit) or 0
if (unitHealIncoming > 0) then
--calculate what is the percent of health incoming based on the max health the player has
local incomingPercent = unitHealIncoming / currentHealthMax
self.incomingHealIndicator:Show()
self.incomingHealIndicator:SetWidth(max(1, min (width * incomingPercent, abs(healthPercent - 1) * width)))
self.incomingHealIndicator:SetPoint("topleft", self, "topleft", width * healthPercent, 0)
self.incomingHealIndicator:SetPoint("bottomleft", self, "bottomleft", width * healthPercent, 0)
else
self.incomingHealIndicator:Hide()
end
if (unitHealAbsorb > 0) then
local healAbsorbPercent = unitHealAbsorb / currentHealthMax
self.healAbsorbIndicator:Show()
self.healAbsorbIndicator:SetWidth(max(1, min (width * healAbsorbPercent, abs(healthPercent - 1) * width)))
self.healAbsorbIndicator:SetPoint("topleft", self, "topleft", width * healthPercent, 0)
self.healAbsorbIndicator:SetPoint("bottomleft", self, "bottomleft", width * healthPercent, 0)
else
self.healAbsorbIndicator:Hide()
end
end
if (self.Settings.ShowShields and IS_WOW_PROJECT_MAINLINE) then
--damage absorbs
local unitDamageAbsorb = self.displayedUnit and UnitGetTotalAbsorbs (self.displayedUnit) or 0
if (unitDamageAbsorb > 0) then
local damageAbsorbPercent = unitDamageAbsorb / currentHealthMax
self.shieldAbsorbIndicator:Show()
--set the width where the max width size is what is lower: the absorb size or the missing amount of health in the health bar
--/dump NamePlate1PlaterUnitFrameHealthBar.shieldAbsorbIndicator:GetSize()
self.shieldAbsorbIndicator:SetWidth(max(1, min (width * damageAbsorbPercent, abs(healthPercent - 1) * width)))
self.shieldAbsorbIndicator:SetPoint("topleft", self, "topleft", width * healthPercent, 0)
self.shieldAbsorbIndicator:SetPoint("bottomleft", self, "bottomleft", width * healthPercent, 0)
--if the absorb percent pass 100%, show the glow
if ((healthPercent + damageAbsorbPercent) > 1) then
self.nextShieldHook = self.nextShieldHook or 0
if (GetTime() >= self.nextShieldHook) then
self:RunHooksForWidget("OnAbsorbOverflow", self, self.displayedUnit, healthPercent + damageAbsorbPercent - 1)
self.nextShieldHook = GetTime() + 0.2
end
self.shieldAbsorbGlow:Show()
else
self.shieldAbsorbGlow:Hide()
if (self.nextShieldHook) then
self:RunHooksForWidget("OnAbsorbOverflow", self, self.displayedUnit, 0)
self.nextShieldHook = nil
end
end
else
self.shieldAbsorbIndicator:Hide()
self.shieldAbsorbGlow:Hide()
if (self.nextShieldHook) then
self:RunHooksForWidget("OnAbsorbOverflow", self, self.displayedUnit, 0)
self.nextShieldHook = nil
end
end
else
self.shieldAbsorbIndicator:Hide()
self.shieldAbsorbGlow:Hide()
if (self.nextShieldHook) then
self:RunHooksForWidget("OnAbsorbOverflow", self, self.displayedUnit, 0)
self.nextShieldHook = nil
end
end
end
--Health Events
healthBarMetaFunctions.PLAYER_ENTERING_WORLD = function(self, ...)
self:UpdateMaxHealth()
self:UpdateHealth()
self:UpdateHealPrediction()
end
healthBarMetaFunctions.UNIT_HEALTH = function(self, unitId)
self:UpdateHealth()
self:UpdateHealPrediction()
end
healthBarMetaFunctions.UNIT_MAXHEALTH = function(self, unitId)
self:UpdateMaxHealth()
self:UpdateHealth()
self:UpdateHealPrediction()
end
healthBarMetaFunctions.UNIT_HEALTH_FREQUENT = function(self, ...)
self:UpdateHealth()
self:UpdateHealPrediction()
end
healthBarMetaFunctions.UNIT_HEAL_PREDICTION = function(self, ...)
self:UpdateMaxHealth()
self:UpdateHealth()
self:UpdateHealPrediction()
end
healthBarMetaFunctions.UNIT_ABSORB_AMOUNT_CHANGED = function(self, ...)
self:UpdateMaxHealth()
self:UpdateHealth()
self:UpdateHealPrediction()
end
healthBarMetaFunctions.UNIT_HEAL_ABSORB_AMOUNT_CHANGED = function(self, ...)
self:UpdateMaxHealth()
self:UpdateHealth()
self:UpdateHealPrediction()
end
-- ~healthbar
---comment
---@param parent frame
---@param name string?
---@param settingsOverride table? a table with key/value pairs to override the default settings
---@return df_healthbar
function detailsFramework:CreateHealthBar(parent, name, settingsOverride)
assert(name or parent:GetName(), "DetailsFramework:CreateHealthBar parameter 'name' omitted and parent has no name.")
local healthBar = CreateFrame("StatusBar", name or (parent:GetName() .. "HealthBar"), parent, "BackdropTemplate")
do --layers
--background
healthBar.background = healthBar:CreateTexture(nil, "background")
healthBar.background:SetDrawLayer("background", -6)
--artwork
--healing incoming
healthBar.incomingHealIndicator = healthBar:CreateTexture(nil, "artwork", nil, 5)
healthBar.incomingHealIndicator:SetDrawLayer("artwork", 4)
--current shields on the unit
healthBar.shieldAbsorbIndicator = healthBar:CreateTexture(nil, "artwork", nil, 3)
healthBar.shieldAbsorbIndicator:SetDrawLayer("artwork", 5)
--debuff absorbing heal
healthBar.healAbsorbIndicator = healthBar:CreateTexture(nil, "artwork", nil, 4)
healthBar.healAbsorbIndicator:SetDrawLayer("artwork", 6)
--the shield fills all the bar, show that cool glow
healthBar.shieldAbsorbGlow = healthBar:CreateTexture(nil, "artwork", nil, 6)
healthBar.shieldAbsorbGlow:SetDrawLayer("artwork", 7)
--statusbar texture
healthBar.barTexture = healthBar:CreateTexture(nil, "artwork", nil, 1)
end
--mixins
detailsFramework:Mixin(healthBar, healthBarMetaFunctions)
detailsFramework:Mixin(healthBar, detailsFramework.StatusBarFunctions)
healthBar:CreateTextureMask()
healthBar:SetTexture([[Interface\WorldStateFrame\WORLDSTATEFINALSCORE-HIGHLIGHT]])
--settings and hooks
local settings = detailsFramework.table.copy({}, healthBarMetaFunctions.Settings)
if (settingsOverride) then
detailsFramework.table.copy(settings, settingsOverride)
end
healthBar.Settings = settings
if (healthBar.Settings.DontSetStatusBarTexture) then
healthBar.barTexture:SetAllPoints()
else
healthBar:SetStatusBarTexture(healthBar.barTexture)
end
--hook list
healthBar.HookList = detailsFramework.table.copy({}, healthBarMetaFunctions.HookList)
--initialize the cast bar
healthBar:Initialize()
return healthBar
end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--power bar frame
--[=[
DF:CreatePowerBar (parent, name, settingsOverride)
creates statusbar frame to show the unit power bar
@parent = frame to pass for the CreateFrame function
@name = absolute name of the frame, if omitted it uses the parent's name .. "PPowerBar"
@settingsOverride = table with keys and values to replace the defaults from the framework
--]=]
---@class df_powerbarsettings : table
---@field ShowAlternatePower boolean
---@field ShowPercentText boolean
---@field HideIfNoPower boolean
---@field CanTick boolean
---@field BackgroundColor table
---@field Texture texturepath|textureid|atlasname
---@field Width number
---@field Height number
---@class df_powerbar : statusbar, df_scripthookmixin, df_statusbarmixin
---@field unit string
---@field displayedUnit string
---@field WidgetType string
---@field currentPower number
---@field currentPowerMax number
---@field powerType number
---@field minPower number
---@field Settings df_powerbarsettings
---@field background texture
---@field percentText fontstring
---@field SetUnit fun(self:df_healthbar, unit:unit?, displayedUnit:unit?)
detailsFramework.PowerFrameFunctions = {
WidgetType = "powerBar",
HookList = {
OnHide = {},
OnShow = {},
},
Settings = {
--misc
ShowAlternatePower = true, --if true it'll show alternate power over the regular power the unit uses
ShowPercentText = true, --if true show a text with the current energy percent
HideIfNoPower = true, --if true and the UnitMaxPower returns zero, it'll hide the power bar with self:Hide()
CanTick = false, --if it calls the OnTick function every tick
--appearance
BackgroundColor = detailsFramework:CreateColorTable (.2, .2, .2, .8),
Texture = [[Interface\RaidFrame\Raid-Bar-Resource-Fill]],
--default size
Width = 100,
Height = 20,
},
PowerBarEvents = {
{"PLAYER_ENTERING_WORLD"},
{"UNIT_DISPLAYPOWER", true},
{"UNIT_POWER_BAR_SHOW", true},
{"UNIT_POWER_BAR_HIDE", true},
{"UNIT_MAXPOWER", true},
{"UNIT_POWER_UPDATE", true},
{"UNIT_POWER_FREQUENT", true},
},
--setup the castbar to be used by another unit
SetUnit = function(self, unit, displayedUnit)
if (self.unit ~= unit or self.displayedUnit ~= displayedUnit or unit == nil) then
self.unit = unit
self.displayedUnit = displayedUnit or unit
--register events
if (unit) then
for _, eventTable in ipairs(self.PowerBarEvents) do
local event = eventTable[1]
local isUnitEvent = eventTable[2]
if (isUnitEvent) then
self:RegisterUnitEvent(event, self.displayedUnit)
else
self:RegisterEvent(event)
end
end
--set scripts
self:SetScript("OnEvent", self.OnEvent)
if (self.Settings.CanTick) then
self:SetScript("OnUpdate", self.OnTick)
end
self:Show()
self:UpdatePowerBar()
else
--remove all registered events
for _, eventTable in ipairs(self.PowerBarEvents) do
local event = eventTable[1]
self:UnregisterEvent(event)
end
--remove scripts
self:SetScript("OnEvent", nil)
self:SetScript("OnUpdate", nil)
self:Hide()
end
end
end,
Initialize = function(self)
PixelUtil.SetWidth (self, self.Settings.Width)
PixelUtil.SetHeight(self, self.Settings.Height)
self:SetTexture(self.Settings.Texture)
self.background:SetAllPoints()
self.background:SetColorTexture(self.Settings.BackgroundColor:GetColor())
if (self.Settings.ShowPercentText) then
self.percentText:Show()
PixelUtil.SetPoint(self.percentText, "center", self, "center", 0, 0)
detailsFramework:SetFontSize(self.percentText, 9)
detailsFramework:SetFontColor(self.percentText, "white")
detailsFramework:SetFontOutline(self.percentText, "OUTLINE")
else
self.percentText:Hide()
end
self:SetUnit(nil)
end,
--call every tick
OnTick = function(self, deltaTime) end, --if overrided, set 'CanTick' to true on the settings table
--when an event happen for this unit, send it to the apropriate function
OnEvent = function(self, event, ...)
local eventFunc = self[event]
if (eventFunc) then
--the function doesn't receive which event was, only 'self' and the parameters
eventFunc(self, ...)
end
end,
UpdatePowerBar = function(self)
self:UpdatePowerInfo()
self:UpdateMaxPower()
self:UpdatePower()
self:UpdatePowerColor()
end,
--power update
UpdateMaxPower = function(self)
self.currentPowerMax = UnitPowerMax(self.displayedUnit, self.powerType)
self:SetMinMaxValues(self.minPower, self.currentPowerMax)
if (self.currentPowerMax == 0 and self.Settings.HideIfNoPower) then
self:Hide()
end
end,
UpdatePower = function(self)
self.currentPower = UnitPower(self.displayedUnit, self.powerType)
PixelUtil.SetStatusBarValue(self, self.currentPower)
if (self.Settings.ShowPercentText) then
self.percentText:SetText(floor(self.currentPower / self.currentPowerMax * 100) .. "%")
end
end,
--when a event different from unit_power_update is triggered, update which type of power the unit should show
UpdatePowerInfo = function(self)
if (IS_WOW_PROJECT_MAINLINE and self.Settings.ShowAlternatePower) then -- not available in classic
local barID = UnitPowerBarID(self.displayedUnit)
local barInfo = GetUnitPowerBarInfoByID(barID)
--local name, tooltip, cost = GetUnitPowerBarStringsByID(barID);
--barInfo.barType,barInfo.minPower, barInfo.startInset, barInfo.endInset, barInfo.smooth, barInfo.hideFromOthers, barInfo.showOnRaid, barInfo.opaqueSpark, barInfo.opaqueFlash, barInfo.anchorTop, name, tooltip, cost, barInfo.ID, barInfo.forcePercentage, barInfo.sparkUnderFrame;
if (barInfo and barInfo.showOnRaid and IsInGroup()) then
self.powerType = ALTERNATE_POWER_INDEX
self.minPower = barInfo.minPower
return
end
end
self.powerType = UnitPowerType (self.displayedUnit)
self.minPower = 0
end,
--tint the bar with the color of the power, e.g. blue for a mana bar
UpdatePowerColor = function(self)
if (not UnitIsConnected (self.unit)) then
self:SetStatusBarColor(.5, .5, .5)
return
end
if (self.powerType == ALTERNATE_POWER_INDEX) then
--don't change this, keep the same color as the game tints on CompactUnitFrame.lua
self:SetStatusBarColor(0.7, 0.7, 0.6)
return
end
local powerColor = PowerBarColor[self.powerType] --don't appear to be, but PowerBarColor is a global table with all power colors /run Details:Dump (PowerBarColor)
if (powerColor) then
self:SetStatusBarColor(powerColor.r, powerColor.g, powerColor.b)
return
end
local _, _, r, g, b = UnitPowerType(self.displayedUnit)
if (r) then
self:SetStatusBarColor(r, g, b)
return
end
--if everything else fails, tint as rogue energy
powerColor = PowerBarColor["ENERGY"]
self:SetStatusBarColor(powerColor.r, powerColor.g, powerColor.b)
end,
--events
PLAYER_ENTERING_WORLD = function(self, ...)
self:UpdatePowerBar()
end,
UNIT_DISPLAYPOWER = function(self, ...)
self:UpdatePowerBar()
end,
UNIT_POWER_BAR_SHOW = function(self, ...)
self:UpdatePowerBar()
end,
UNIT_POWER_BAR_HIDE = function(self, ...)
self:UpdatePowerBar()
end,
UNIT_MAXPOWER = function(self, ...)
self:UpdateMaxPower()
self:UpdatePower()
end,
UNIT_POWER_UPDATE = function(self, ...)
self:UpdatePower()
end,
UNIT_POWER_FREQUENT = function(self, ...)
self:UpdatePower()
end,
}
detailsFramework:Mixin(detailsFramework.PowerFrameFunctions, detailsFramework.ScriptHookMixin)
-- ~powerbar
---create a power bar
---@param parent frame
---@param name string?
---@param settingsOverride table? a table with key/value pairs to override the default settings
---@return df_powerbar
function detailsFramework:CreatePowerBar(parent, name, settingsOverride)
assert(name or parent:GetName(), "DetailsFramework:CreatePowerBar parameter 'name' omitted and parent has no name.")
local powerBar = CreateFrame("StatusBar", name or (parent:GetName() .. "PowerBar"), parent, "BackdropTemplate")
do --layers
--background
powerBar.background = powerBar:CreateTexture(nil, "background")
powerBar.background:SetDrawLayer("background", -6)
--artwork
powerBar.barTexture = powerBar:CreateTexture(nil, "artwork")
powerBar:SetStatusBarTexture(powerBar.barTexture)
--overlay
powerBar.percentText = powerBar:CreateFontString(nil, "overlay", "GameFontNormal")
end
--mixins
detailsFramework:Mixin(powerBar, detailsFramework.PowerFrameFunctions)
detailsFramework:Mixin(powerBar, detailsFramework.StatusBarFunctions)
powerBar:CreateTextureMask()
powerBar:SetTexture([[Interface\WorldStateFrame\WORLDSTATEFINALSCORE-HIGHLIGHT]])
--settings and hooks
local settings = detailsFramework.table.copy({}, detailsFramework.PowerFrameFunctions.Settings)
if (settingsOverride) then
detailsFramework.table.copy(settings, settingsOverride)
end
powerBar.Settings = settings
local hookList = detailsFramework.table.copy({}, detailsFramework.PowerFrameFunctions.HookList)
powerBar.HookList = hookList
--initialize the cast bar
powerBar:Initialize()
return powerBar
end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--cast bar frame
--[=[
DF:CreateCastBar (parent, name, settingsOverride)
creates a cast bar to show an unit cast
@parent = frame to pass for the CreateFrame function
@name = absolute name of the frame, if omitted it uses the parent's name .. "CastBar"
@settingsOverride = table with keys and values to replace the defaults from the framework
--]=]
---@class df_castbarsettings : table
---@field NoFadeEffects boolean if true it won't play fade effects when a cast if finished
---@field ShowTradeSkills boolean if true, it shows cast for trade skills, e.g. creating an icon with blacksmith
---@field ShowShield boolean if true, shows the shield above the spell icon for non interruptible casts
---@field CanTick boolean if true it will run its OnTick function every tick.
---@field ShowCastTime boolean if true, show the remaining time to finish the cast, lazy tick must be enabled
---@field FadeInTime number amount of time in seconds to go from zero to 100% alpha when starting to cast
---@field FadeOutTime number amount of time in seconds to go from 100% to zero alpha when the cast finishes
---@field CanLazyTick boolean if true, it'll execute the lazy tick function, it ticks in a much slower pace comparece with the regular tick
---@field LazyUpdateCooldown number amount of time to wait for the next lazy update, this updates non critical things like the cast timer
---@field ShowEmpoweredDuration boolean full hold time for empowered spells
---@field FillOnInterrupt boolean
---@field HideSparkOnInterrupt boolean
---@field Width number
---@field Height number
---@field Colors df_castcolors
---@field BackgroundColor table
---@field Texture texturepath|textureid
---@field BorderShieldWidth number
---@field BorderShieldHeight number
---@field BorderShieldCoords table
---@field BorderShieldTexture number
---@field SpellIconWidth number
---@field SpellIconHeight number
---@field ShieldIndicatorTexture texturepath|textureid
---@field ShieldGlowTexture texturepath|textureid
---@field SparkTexture texturepath|textureid
---@field SparkWidth number
---@field SparkHeight number
---@field SparkOffset number
---@alias caststage_color
---| "Casting"
---| "Channeling"
---| "Interrupted"
---| "Failed"
---| "NotInterruptable"
---| "Finished"
---@class df_castcolors : table
---@field Casting table
---@field Channeling table
---@field Interrupted table
---@field Failed table
---@field NotInterruptable table
---@field Finished table
---@class df_castbar : statusbar, df_scripthookmixin, df_statusbarmixin
---@field unit string
---@field displayedUnit string
---@field WidgetType string
---@field value number
---@field maxValue number
---@field spellStartTime number
---@field spellEndTime number
---@field empowered boolean
---@field curStage number
---@field numStages number
---@field empStages {start:number, finish:number}[]
---@field stagePips texture[]
---@field holdAtMaxTime number
---@field casting boolean
---@field channeling boolean
---@field interrupted boolean
---@field failed boolean
---@field finished boolean
---@field canInterrupt boolean
---@field spellID spellid
---@field castID number
---@field spellName spellname
---@field spellTexture textureid
---@field Colors df_castcolors
---@field Settings df_castbarsettings
---@field background texture
---@field extraBackground texture
---@field Text fontstring
---@field BorderShield texture
---@field Icon texture
---@field Spark texture
---@field percentText fontstring
---@field barTexture texture
---@field flashTexture texture
---@field fadeOutAnimation animationgroup
---@field fadeInAnimation animationgroup
---@field flashAnimation animationgroup
---@field SetUnit fun(self:df_castbar, unit:string?)
---@field SetDefaultColor fun(self:df_castbar, colorType: caststage_color, red:any, green:number?, blue:number?, alpha:number?)
---@field UpdateCastColor fun(self:df_castbar) after setting a new color, call this function to update the bar color (while casting or channeling)
---@field GetCastColor fun(self:df_castbar) return a table with the color values for the current state of the casting process
detailsFramework.CastFrameFunctions = {
WidgetType = "castBar",
HookList = {
OnHide = {},
OnShow = {},
--can be regular cast or channel
OnCastStart = {},
},
CastBarEvents = {
{"UNIT_SPELLCAST_INTERRUPTED"},
{"UNIT_SPELLCAST_DELAYED"},
{"UNIT_SPELLCAST_CHANNEL_START"},
{"UNIT_SPELLCAST_CHANNEL_UPDATE"},
{"UNIT_SPELLCAST_CHANNEL_STOP"},
{(IS_WOW_PROJECT_MAINLINE) and "UNIT_SPELLCAST_EMPOWER_START"},
{(IS_WOW_PROJECT_MAINLINE) and "UNIT_SPELLCAST_EMPOWER_UPDATE"},
{(IS_WOW_PROJECT_MAINLINE) and "UNIT_SPELLCAST_EMPOWER_STOP"},
{(IS_WOW_PROJECT_MAINLINE) and "UNIT_SPELLCAST_INTERRUPTIBLE"},
{(IS_WOW_PROJECT_MAINLINE) and "UNIT_SPELLCAST_NOT_INTERRUPTIBLE"},
{"PLAYER_ENTERING_WORLD"},
{"UNIT_SPELLCAST_START", true},
{"UNIT_SPELLCAST_STOP", true},
{"UNIT_SPELLCAST_FAILED", true},
},
Settings = {
NoFadeEffects = false, --if true it won't play fade effects when a cast if finished
ShowTradeSkills = false, --if true, it shows cast for trade skills, e.g. creating an icon with blacksmith
ShowShield = true, --if true, shows the shield above the spell icon for non interruptible casts
CanTick = true, --if true it will run its OnTick function every tick.
ShowCastTime = true, --if true, show the remaining time to finish the cast, lazy tick must be enabled
FadeInTime = 0.1, --amount of time in seconds to go from zero to 100% alpha when starting to cast
FadeOutTime = 0.5, --amount of time in seconds to go from 100% to zero alpha when the cast finishes
CanLazyTick = true, --if true, it'll execute the lazy tick function, it ticks in a much slower pace comparece with the regular tick
LazyUpdateCooldown = 0.2, --amount of time to wait for the next lazy update, this updates non critical things like the cast timer
ShowEmpoweredDuration = true, --full hold time for empowered spells
FillOnInterrupt = true,
HideSparkOnInterrupt = true,
--default size
Width = 100,
Height = 20,
--colour the castbar statusbar by the type of the cast
Colors = {
Casting = detailsFramework:CreateColorTable (1, 0.73, .1, 1),
Channeling = detailsFramework:CreateColorTable (1, 0.73, .1, 1),
Finished = detailsFramework:CreateColorTable (0, 1, 0, 1),
NonInterruptible = detailsFramework:CreateColorTable (.7, .7, .7, 1),
Failed = detailsFramework:CreateColorTable (.4, .4, .4, 1),
Interrupted = detailsFramework:CreateColorTable (.965, .754, .154, 1),
},
--appearance
BackgroundColor = detailsFramework:CreateColorTable (.2, .2, .2, .8),
Texture = [[Interface\TargetingFrame\UI-StatusBar]],
BorderShieldWidth = 10,
BorderShieldHeight = 12,
BorderShieldCoords = {0.26171875, 0.31640625, 0.53125, 0.65625},
BorderShieldTexture = 1300837,
SpellIconWidth = 10,
SpellIconHeight = 10,
ShieldIndicatorTexture = [[Interface\RaidFrame\Shield-Fill]],
ShieldGlowTexture = [[Interface\RaidFrame\Shield-Overshield]],
SparkTexture = [[Interface\CastingBar\UI-CastingBar-Spark]],
SparkWidth = 16,
SparkHeight = 16,
SparkOffset = 0,
},
Initialize = function(self)
self.unit = "unutilized unit"
self.lazyUpdateCooldown = self.Settings.LazyUpdateCooldown
self.Colors = self.Settings.Colors
self:SetUnit(nil)
PixelUtil.SetWidth (self, self.Settings.Width)
PixelUtil.SetHeight(self, self.Settings.Height)
self.background:SetColorTexture(self.Settings.BackgroundColor:GetColor())
self.background:SetAllPoints()
self.extraBackground:SetColorTexture(0, 0, 0, 1)
self.extraBackground:SetVertexColor(self.Settings.BackgroundColor:GetColor())
self.extraBackground:SetAllPoints()
self:SetTexture(self.Settings.Texture)
self.BorderShield:SetPoint("center", self, "left", 0, 0)
self.BorderShield:SetTexture(self.Settings.BorderShieldTexture)
self.BorderShield:SetTexCoord(unpack(self.Settings.BorderShieldCoords))
self.BorderShield:SetSize(self.Settings.BorderShieldWidth, self.Settings.BorderShieldHeight)
self.Icon:SetPoint("center", self, "left", 2, 0)