-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.lua
1461 lines (1306 loc) · 40.7 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
--====================================================================
-- KiwiHUD @ 2018 MiChaeL
--====================================================================
local addonName = ...
local addon = CreateFrame("Frame")
addon.addonName = addonName
_G[addonName] = addon
--====================================================================
local GetAddOnMetadata = C_AddOns and C_AddOns.GetAddOnMetadata or GetAddOnMetadata
local versionToc = GetAddOnMetadata(addonName,'Version')
addon.versionToc = versionToc=='\@project-version\@' and 'Dev' or 'v'..versionToc
--====================================================================
local versionCli = select(4,GetBuildInfo())
addon.isClassic = versionCli<40000 -- vanilla or tbc or wrath
addon.isVanilla = versionCli<20000
addon.isTBC = versionCli>=20000 and versionCli<30000
addon.isRetail = versionCli>=90000
--====================================================================
local Media = LibStub("LibSharedMedia-3.0", true)
--====================================================================
local UnitExists = UnitExists
local UnitIsUnit = UnitIsUnit
local UnitCastingInfo = UnitCastingInfo or CastingInfo
local UnitChannelInfo = UnitChannelInfo or ChannelInfo
local next, unpack, tremove, tinsert, floor, max = next, unpack, table.remove, table.insert, floor, max
--====================================================================
local InCombat
local AlphaCombat
local isClassic = addon.isClassic
local isRetail = addon.isRetail
local PlayerClass = select(2,UnitClass('player'))
local PlayerGUID = UnitGUID('player')
local isMaxLevel = UnitLevel('player') == (isTBC and 70 or 60)
--====================================================================
addon.defaults = {
offsetY = 0,
height = 128,
width = 128,
gap = 250,
sep = 20,
texfg = "Interface\\Addons\\KiwiHUD\\media\\dhudfg",
texbg = "Interface\\Addons\\KiwiHUD\\media\\dhudbg",
colorbg = {1,1,1,1},
alphaCombat = 1,
alphaOOC = .5,
reactionColors = {},
classColors = {},
powerColors = {},
schoolColors = {},
bars = {
{
type = "health",
unit = "player",
index = -4,
colorType = 'reaction',
textEnabled = false,
},
{
type = "power",
unit = "player",
index = -3,
textEnabled = false,
},
{
type = "cast",
unit = "player",
index = -2,
color = { 1,1,0,1 },
},
{
type = "gcd",
index = -1,
color = { 1,0,0,1 },
invert = false,
},
{
type = "power",
unit = "target",
index = 1,
textEnabled = false,
},
{
type = "health",
unit = "target",
colorType = 'reaction',
index = 2,
textEnabled = false,
},
},
minimapIcon = { hide = false },
}
--====================================================================
addon.setupFunc = {}
--====================================================================
local REACTION_COLORS = {
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}
}
local SCHOOL_COLORS = {
{ 0.94, 0.88, 0.55, 1 }, -- all
{ 0.98, 0.78, 0.63, 1 }, -- physical
{ 0.02, 0.49, 0.97, 1 }, -- magic
{ 1.00, 0.98, 0.02, 1 }, -- holy
{ 1.00, 0.49, 0.00, 1 }, -- fire
{ 0.51, 0.98, 0.00, 1 }, -- nature
{ 0.00, 1.00, 0.97, 1 }, -- frost
{ 0.73, 0.45, 1.00, 1 }, -- shadow
{ 0.99, 0.62, 1.00, 1 }, -- arcane
}
local Reactions = { 'hostile', 'hostile', 'hostile', 'neutral', 'friendly', 'friendly', 'friendly', 'friendly' }
local SchoolColors = {}
local ReactionColors = {}
local ClassColors = {}
local PowerColors = {}
local ColorDefault = { 1,1,1,1 }
local ColorGreen = { 0,1,0,1 }
local FontBarAnchors = {
[ 1] = { TOP = 'TOPLEFT', CENTER = 'LEFT', BOTTOM = 'BOTTOMLEFT' },
[-1] = { TOP = 'TOPRIGHT', CENTER = 'RIGHT', BOTTOM = 'BOTTOMRIGHT' },
}
--====================================================================
-- Misc functions
--====================================================================
local function OnEvent(self,event,...)
self[event](self,event,...)
end
local function Embed(dst, src)
if src then
for k,v in pairs(src) do
dst[k] = v
end
end
end
local function FillColorsTable(dst, src, override)
wipe(dst)
for k,c in pairs(src) do
if override and override[k] then
dst[k] = { unpack(override[k]) }
elseif c.r then
dst[k] = { c.r, c.g, c.b, 1 }
else
dst[k] = { unpack(c) }
end
end
end
local function CreateTimer(frame, duration, func)
local timer = frame:CreateAnimationGroup()
timer.anim = timer:CreateAnimation()
timer.anim:SetDuration(duration)
timer:SetLooping("REPEAT")
timer:SetScript("OnLoop", func)
return timer
end
--====================================================================
-- First Run
--====================================================================
addon:RegisterEvent("ADDON_LOADED")
addon:RegisterEvent("PLAYER_LOGIN")
addon:SetScript("OnEvent", function(self, event, name)
if event == "ADDON_LOADED" and name == addonName then
addon.__loaded = true
end
if addon.__loaded and IsLoggedIn() then
self:UnregisterAllEvents()
self:SetScript("OnEvent", OnEvent)
addon:InitializeDB()
addon:InitializeOptions()
addon:Initialize()
end
end )
--====================================================================
-- For future use
--====================================================================
--function addon:PLAYER_ENTERING_WORLD(event,isLogin,isReload)
--end
--====================================================================
-- Track combat status
--====================================================================
function addon:PLAYER_REGEN_DISABLED(event)
InCombat = (event == 'PLAYER_REGEN_DISABLED')
AlphaCombat = InCombat and (self.db.alphaCombat or 1) or (self.db.alphaOOC or .5)
for _,bar in ipairs(self.bars) do
local func = bar.UpdateOpacity
if func then func(bar) end
end
end
addon.PLAYER_REGEN_ENABLED = addon.PLAYER_REGEN_DISABLED
--====================================================================
-- Enable test mode
--====================================================================
function addon:ToggleTestMode()
self.testModeEnabled = not self.testModeEnabled or nil
for _,bar in ipairs(self.bars) do
if bar.TestMode then
if self.testModeEnabled then
bar:TestMode()
else
bar:Update()
end
end
end
end
--====================================================================
-- UI elements factories
--====================================================================
local Frame_Create, Texture_Create, Font_Create, Frame_Release, Texture_Release, Font_Release
do
local fonts = {}
local textures = {}
local frames = setmetatable( {} , { __index = function(t,k) local v={}; t[k]=v; return v; end } )
function Frame_Create(type)
local frame = tremove(frames[type or 'default']) or CreateFrame('Frame', nil, UIParent)
frame.__factory_type = type
frame:SetParent(UIParent)
frame:ClearAllPoints()
frame:Hide()
return frame
end
function Frame_Release(frame)
if frame then
frame:SetScript("OnEvent", nil)
frame:SetScript("OnUpdate",nil)
frame:UnregisterAllEvents()
frame:SetAlpha(1)
frame:SetScale(1)
frame:Hide()
tinsert( frames[frame.__factory_type or 'default'], frame )
end
end
function Texture_Create(self, layer, sublayer)
local tex = tremove(textures) or self:CreateTexture()
tex:SetParent(self)
tex:SetDrawLayer(layer or "ARTWORK", sublayer)
tex:SetVertexColor(1,1,1,1)
tex:SetTexCoord(0,1,0,1)
tex:ClearAllPoints()
tex:Hide()
return tex
end
function Texture_Release(tex)
if tex then
tex:SetParent(addon)
tex:Hide()
textures[#textures+1] = tex
end
end
function Font_Create(self, layer)
local text = tremove(fonts) or self:CreateFontString()
text:SetParent(self)
text:SetDrawLayer(layer or 'ARTWORK')
text:SetTextColor(1,1,1,1)
text:ClearAllPoints()
text:Hide()
return text
end
function Font_Release(text)
if text then
text:SetParent(addon)
text:Hide()
fonts[#fonts+1] = text
end
end
end
--====================================================================
-- Generic Bar class
--====================================================================
local Bar_Create
do
local class = {}
function class:Destroy()
for _,tex in pairs(self.textures) do
Texture_Release(tex)
end
wipe(self.textures)
self.Text = Font_Release(self.Text)
Frame_Release(self)
end
function class:Layout()
local db = addon.db
local bar = self.db
self.unit = bar.unit
self.hideValue = bar.hideValue
self.height = db.height
if bar.index>0 then
self.side, self.coord1, self.coord2 = 1, 0, 1
else
self.side, self.coord1, self.coord2 = -1, 1, 0
end
-- frame
self:ClearAllPoints()
self:SetScale( db.scale or 1 )
self:SetPoint("CENTER", self.side * db.gap + bar.index * db.sep - self.side * db.sep, db.offsetY or 0 )
self:SetSize( db.width, db.height )
-- textures
for _,tex in pairs(self.textures) do
tex:SetTexCoord( self.coord1, self.coord2, 0, 1 )
end
if db.colorbg then
self.textures[0]:SetVertexColor( unpack(db.colorbg) )
end
if bar.color then
self:SetColor( bar.color or ColorDefault )
end
-- text
local Text = self.Text
if Text then
Text:ClearAllPoints()
Text:SetPoint( bar.textAlign or 'CENTER', self, FontBarAnchors[self.side][bar.textAnchor or 'BOTTOM'], bar.textOffsetX, bar.textOffsetY )
Text:SetFont( Media:Fetch('font', bar.textFont) or STANDARD_TEXT_FONT, bar.textFontSize or 14, 'OUTLINE' )
Text:SetTextColor( unpack(bar.textColor or ColorDefault) )
end
end
function class:SetColor(color)
local tex = self.textures[1]
tex:SetVertexColor( unpack(color) )
end
function class:SetValue(value, valueMax)
local pvalue = (valueMax and valueMax>0 and value/valueMax) or value
local tex = self.textures[1]
tex:SetHeight( self.height * pvalue )
tex:SetTexCoord( self.coord1, self.coord2, 1-pvalue, 1 )
tex:SetShown(value>0)
local hideValue = self.hideValue
if self.Text then
if valueMax then -- value & valueMax
self.Text:SetFormattedText(value)
else -- percent, no valueMax
self.Text:SetFormattedText("%.0f%%",value*100)
end
end
if hideValue then
self:SetShown( InCombat or pvalue ~= hideValue )
end
self.value = value
self.valuePer = pvalue
self.valueMax = valueMax
end
function class:UpdateValue()
if self.value then
self:SetValue(self.value, self.valueMax)
end
end
function class:UpdateVisibility(value) -- value == 0 to 1
value = value or self.valuePer
local hideValue = self.hideValue
if value and hideValue then
self:SetShown( InCombat or value ~= hideValue )
end
end
function class:UpdateOpacity()
-- we check unit=='player' because UnitExists('player') can return false when exiting from an instance if the player was in combat (because PLAYER_REGEN_ENABLED is triggered before PLAYER_ENTERING_WORLD)
local unit = self.unit
self:SetAlpha( (not unit or unit=='player' or UnitExists(unit)) and AlphaCombat or 0 )
if self.hideValue then
self:SetShown( InCombat or self.valuePer ~= self.hideValue )
end
end
function class:Update()
self:UpdateValue()
self:UpdateColor()
self:UpdateOpacity()
end
function class:UpdateColor()
end
function class:UpdateDB()
end
function class:TestMode()
if self.db.textEnabled and (self.Text:GetText() or '')=='' then
self.Text:SetText('999')
end
self:SetAlpha(1)
self:Show()
end
function Bar_Create(db, embed)
local self = Frame_Create()
self.prototype = class
self.db = db
self.textures = self.textures or {}
Embed(self, class)
Embed(self, embed)
local tex = Texture_Create(self, 'BACKGROUND')
tex:SetTexture(addon.db.texbg)
tex:SetAllPoints()
tex:SetVertexColor(.5,.5,.5,1)
tex:Show()
self.textures[0] = tex
local tex = Texture_Create(self, 'ARTWORK')
tex:SetTexture(addon.db.texfg)
tex:SetPoint('BOTTOMLEFT')
tex:SetPoint('BOTTOMRIGHT')
tex:SetHeight(0)
tex:SetVertexColor(1,1,1,1)
tex:Hide()
self.textures[index or 1] = tex
self.margin = addon.db.texmargin or 1
if db.textEnabled then
local text = Font_Create(self,'BORDER')
text:SetShadowOffset(1,-1)
text:SetShadowColor(0,0,0, 1)
text:Show()
self.Text = text
end
self:UpdateDB()
self:Layout()
return self
end
end
--====================================================================
-- Energy ticker
--====================================================================
local EnergyTicker_Register
do
local RESET_SPELLS = { ['Maul'] = true, ['Raptor Strike'] = true, ['Cleave'] = true, ['Slam'] = true, ['Heroic Strike'] = true }
local POWER_ENERGY = Enum.PowerType.Energy
local POWER_RAGE = Enum.PowerType.Rage
local POWER_MANA = Enum.PowerType.Mana
local GetTime = GetTime
local UnitPower = UnitPower
local UnitAttackSpeed = UnitAttackSpeed
local CombatLogGetCurrentEventInfo = CombatLogGetCurrentEventInfo
local frame
local bars = {}
local lastP = 1
local lastEnergy = 0
local lastTick = GetTime()
local lastSwing, weaponSpeed
local lastCast
local function ThemeSparks(r,g,b,a)
for bar in next,bars do
bar.spark:SetVertexColor(r,g,b,a)
end
end
local function HideSparks()
for bar in next,bars do
bar.spark:Hide()
end
end
local function UpdateEnergy(self)
local now = GetTime()
local energy = UnitPower("player")
if energy>lastEnergy or now>=lastTick+2 then
lastEnergy, lastTick = energy, now
end
local p = (now - lastTick) / 2
if p<1 then
local offset = p * addon.db.height
for bar in next,bars do
if bar:IsVisible() then
local spark = bar.spark
spark:ClearAllPoints()
spark:SetPoint( 'BOTTOM', 0, offset )
spark:SetTexCoord( bar.coord1, bar.coord2, 0.975-p, 1-p)
spark:Show()
end
end
elseif lastP<1 then -- or True ???
HideSparks()
end
lastP = p
end
local function UpdateSwing(self)
if lastSwing then
local p = (GetTime() - lastSwing) / weaponSpeed
if p<1 then
local offset = p * addon.db.height
for bar in next,bars do
if bar:IsVisible() then
local spark = bar.spark
spark:ClearAllPoints()
spark:SetPoint( 'BOTTOM', 0, offset )
spark:SetTexCoord( bar.coord1, bar.coord2, 0.975-p, 1-p)
spark:Show()
end
end
else
lastSwing = nil
HideSparks()
end
end
end
local function ResetSwing()
lastSwing = GetTime()
weaponSpeed = UnitAttackSpeed('player')
end
local function CombatLogEvent(self)
local _, event, _, sguid, _, _, _, dguid, _, _, _, _, isOffHandOrSpell, _ , _, _, _, _, _, _, isOffHand = CombatLogGetCurrentEventInfo()
if PlayerGUID==sguid then
if (event == "SWING_DAMAGE" and not isOffHand) or
(event == "SWING_MISSED" and not isOffHandOrSpell ) or
((event == "SPELL_DAMAGE" or event == "SPELL_MISSED") and RESET_SPELLS[isOffHandOrSpell]) then
ResetSwing()
end
end
end
local function UpdateMana(self)
if lastCast then -- tracks mana 5 seconds rule
local p = (GetTime() - lastCast) / 5
if p<1 then
local offset = p * addon.db.height
for bar in next,bars do
if bar:IsVisible() then
local spark = bar.spark
spark:ClearAllPoints()
spark:SetPoint( 'BOTTOM', 0, offset )
spark:SetTexCoord( bar.coord1, bar.coord2, 0.975-p, 1-p)
spark:Show()
end
end
else
HideSparks()
lastCast = nil
lastTick = 0
lastEnergy = UnitPower("player")
frame:Show()
end
else -- tracks mana ticks every 2 seconds
UpdateEnergy(self)
end
end
local function UpdateVisibility(self)
local power = UnitPowerType('player')
local tSwing = PlayerClass=='HUNTER' or (PlayerClass=='DRUID' and power == POWER_RAGE)
local tEnergy = power==POWER_ENERGY
local tMP5 = (not isRetail) and PlayerClass~='HUNTER' and power==POWER_MANA
local visible = false
if tSwing then
frame:SetScript('OnUpdate', UpdateSwing)
frame:RegisterEvent('COMBAT_LOG_EVENT_UNFILTERED')
ThemeSparks(1,1,1,1)
visible = true
elseif tEnergy and versionCli<30000 then
frame:SetScript( 'OnUpdate', UpdateEnergy)
frame:UnregisterEvent('COMBAT_LOG_EVENT_UNFILTERED')
ThemeSparks(1,0,0,1)
visible = true
elseif tMP5 and versionCli<30000 then
lastEnergy = UnitPower("player")
frame:SetScript( 'OnUpdate', UpdateMana)
frame:UnregisterEvent('COMBAT_LOG_EVENT_UNFILTERED')
frame:RegisterEvent('UNIT_SPELLCAST_SUCCEEDED')
ThemeSparks(1,0,0,1)
visible = true
end
HideSparks()
frame:SetShown(visible)
end
local function DisableMP5(self)
if UnitPower('player')<lastEnergy then -- only spells using mana
lastCast = GetTime()
frame:Show()
end
end
local function Destroy(self)
self.prototype.Destroy(self)
self.spark = Texture_Release(self.spark)
bars[self] = nil
if frame and not next(bars) then frame:Hide() end
end
function EnergyTicker_Register(bar)
if frame then
if not next(bars) then frame:Show() end
else
frame = CreateFrame('Frame')
if PlayerClass == 'ROGUE' or PlayerClass == 'MONK' then
frame:SetScript('OnUpdate',UpdateEnergy)
else
frame:SetScript('OnEvent', OnEvent)
frame:RegisterEvent('UNIT_DISPLAYPOWER')
frame.UNIT_DISPLAYPOWER = UpdateVisibility
frame.COMBAT_LOG_EVENT_UNFILTERED = CombatLogEvent
frame.UNIT_SPELLCAST_SUCCEEDED = DisableMP5
UpdateVisibility()
end
end
local tex = Texture_Create(bar, 'OVERLAY')
tex:SetTexture( addon.db.texfg )
tex:SetHeight(addon.db.height*0.025)
tex:SetWidth(addon.db.width)
tex:SetVertexColor(1,0,0,1)
bar.spark = tex
bar.Destroy = Destroy
bars[bar] = true
end
end
--====================================================================
-- Power Bar
--====================================================================
do
local GetTime = GetTime
local UnitPower = UnitPower
local UnitPowerMax = UnitPowerMax
local UnitPowerType = UnitPowerType
local POWER_MANA = Enum.PowerType.Mana
local function UpdateColor(self)
local type = UnitPowerType(self.unit)
self:SetColor( PowerColors[type] or PowerColors[POWER_MANA] )
end
local function UpdateValue(self, _, _, powerType)
local u = self.unit
local _, typ = UnitPowerType(u)
if powerType == nil or typ == powerType then
local m = UnitPowerMax(u)
if m>0 then
local p = UnitPower(u)
if m>150 then
self:SetValue( p / m )
else
self:SetValue( p, m )
end
end
end
end
local embed = { UpdateColor = UpdateColor, UpdateValue = UpdateValue }
addon.setupFunc['power'] = function(db)
local self = Bar_Create(db, embed)
self:Show()
self:SetScript("OnEvent", OnEvent)
if versionCli>=30000 then self:RegisterUnitEvent("UNIT_POWER_FREQUENT", db.unit) end
self:RegisterUnitEvent("UNIT_POWER_UPDATE", db.unit)
self:RegisterUnitEvent("UNIT_MAXPOWER", db.unit)
self:RegisterUnitEvent("UNIT_DISPLAYPOWER", db.unit)
if PlayerClass=='DRUID' then
self:RegisterEvent("UPDATE_SHAPESHIFT_FORM")
self.UPDATE_SHAPESHIFT_FORM = self.UpdateValue
end
if db.unit == 'target' then
self.PLAYER_TARGET_CHANGED = self.Update
self:RegisterEvent( "PLAYER_TARGET_CHANGED" )
elseif db.unit == 'focus' then
self.PLAYER_FOCUS_CHANGED = self.Update
self:RegisterEvent( "PLAYER_FOCUS_CHANGED" )
elseif db.unit == 'pet' then
self.UNIT_PET = self.Update
self:RegisterUnitEvent( "UNIT_PET", 'player' )
elseif db.unit == 'player' then
if db.tickerEnabled then
EnergyTicker_Register(self)
end
end
self.UNIT_DISPLAYPOWER = self.UpdateColor
self.UNIT_POWER_UPDATE = self.UpdateValue
self.UNIT_POWER_FREQUENT = self.UpdateValue
self.UNIT_MAXPOWER = self.UpdateValue
self:Update()
return self
end
end
--====================================================================
-- Player Mana Alt
--====================================================================
do
local ENABLED = (PlayerClass == 'PRIEST' or PlayerClass == 'SHAMAN' or PlayerClass == 'DRUID' or PlayerClass == 'MONK')
local POWER_MANA = Enum.PowerType.Mana
local UnitPower = UnitPower
local UnitPowerMax = UnitPowerMax
local UnitPowerType = UnitPowerType
local GetSpecialization = isRetail and GetSpecialization or function() end
local function UpdateValue(self)
local show = UnitPowerType('player') ~= POWER_MANA and (PlayerClass~='MONK' or GetSpecialization() == SPEC_MONK_MISTWEAVER)
self:SetShown(show)
if show then
self:SetValue( UnitPower('player', POWER_MANA), UnitPowerMax('player', POWER_MANA) )
else
self.value, self.valuePer = nil, nil
end
end
local function UpdateColor(self)
self:SetColor( PowerColors[POWER_MANA] )
UpdateValue(self)
end
local embed = { UpdateColor = UpdateColor, UpdateValue = UpdateValue }
addon.setupFunc['manaalt'] = function(db)
local self = Bar_Create(db, embed)
if ENABLED then
self:Show()
self:SetScript("OnEvent", OnEvent)
self:RegisterUnitEvent("UNIT_POWER_UPDATE", 'player')
self:RegisterUnitEvent("UNIT_MAXPOWER", 'player')
self:RegisterUnitEvent("UNIT_DISPLAYPOWER", 'player')
self.UNIT_POWER_UPDATE = self.UpdateValue
self.UNIT_MAXPOWER = self.UpdateValue
self.UNIT_DISPLAYPOWER = self.UpdateColor
if PlayerClass=='DRUID' then
self:RegisterEvent("UPDATE_SHAPESHIFT_FORM")
self.UPDATE_SHAPESHIFT_FORM = self.UpdateColor
end
end
self:Update()
return self
end
end
--====================================================================
-- Health Bar
--====================================================================
do
local min = math.min
local UnitHealth = UnitHealth
local UnitHealthMax = UnitHealthMax
local UnitGetTotalAbsorbs = UnitGetTotalAbsorbs
local function UpdateShieldValue(self, unit, valueFrom , valueMax)
local value = UnitGetTotalAbsorbs(unit) or 0
if value~=0 or self.shieldValue then
local tex = self.textures[2]
tex:SetShown( value>0 )
self.shieldValue = value~=0 and value or nil
local valueTo = min(1, valueFrom + value/valueMax)
tex:SetTexCoord( self.coord1, self.coord2, 1-valueTo, 1-valueFrom)
tex:SetHeight( self.height * (valueTo-valueFrom) )
end
end
local function UpdateShieldColor(self, color)
local tex = self.textures[2]
tex:SetVertexColor( unpack(color) )
tex:SetAlpha( color[4]/4 )
end
local function UpdateColor(self)
local color = self.db.color
self:SetColor( self.db.color )
if self.shieldDisplay then
UpdateShieldColor(self, color)
end
end
local function UpdateReactionColor(self)
local color = ReactionColors[ Reactions[ UnitReaction(self.unit,"player") ] ] or ColorDefault
self:SetColor( color )
if self.shieldDisplay then
UpdateShieldColor(self, color)
end
end
local function UpdateClassColor(self)
local _,class = UnitClass(self.unit)
local color = ClassColors[class] or ColorDefault
self:SetColor( color )
if self.shieldDisplay then
UpdateShieldColor(self, color)
end
end
local function UpdateValue(self)
local u = self.unit
local m = UnitHealthMax(u)
if m==0 then m = 1 end
local v = UnitHealth(u)/m
self:SetValue(v)
if self.shieldDisplay then
UpdateShieldValue(self, u, v, m)
end
end
local function UpdateDB(self)
self.shieldDisplay = self.db.shieldDisplay
end
local embed = { UpdateDB = UpdateDB, UpdateValue = UpdateValue }
addon.setupFunc['health'] = function(db)
local self = Bar_Create(db, embed)
self:Show()
if self.db.shieldDisplay then
local tex = Texture_Create(self, 'ARTWORK')
tex:SetTexture(addon.db.texfg)
tex:SetPoint('BOTTOMLEFT', self.textures[1], 'TOPLEFT')
tex:SetPoint('BOTTOMRIGHT', self.textures[1], 'TOPRIGHT')
tex:SetHeight(0)
tex:SetVertexColor(1,1,1,.5)
tex:Hide()
self.textures[2] = tex
self:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", db.unit)
self.UNIT_ABSORB_AMOUNT_CHANGED = UpdateValue
end
if db.colorType == 'class' then
self.UpdateColor = UpdateClassColor
elseif db.colorType == 'reaction' then
self.UpdateColor = UpdateReactionColor
else
self.UpdateColor = UpdateColor
end
self:SetScript("OnEvent", OnEvent)
self:RegisterUnitEvent("UNIT_HEALTH", db.unit)
if isClassic then self:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", db.unit) end
self:RegisterUnitEvent("UNIT_MAXHEALTH", db.unit)
if db.unit == 'target' then
self.PLAYER_TARGET_CHANGED = self.Update
self:RegisterEvent( "PLAYER_TARGET_CHANGED" )
elseif db.unit == 'focus' then
self.PLAYER_FOCUS_CHANGED = self.Update
self:RegisterEvent( "PLAYER_FOCUS_CHANGED" )
elseif db.unit == 'pet' then
self.UNIT_PET = self.Update
self:RegisterUnitEvent( "UNIT_PET", 'player' )
end
self.UNIT_HEALTH = self.UpdateValue
if isClassic then self.UNIT_HEALTH_FREQUENT = self.UpdateValue end
self.UNIT_MAXHEALTH = self.UpdateValue
self:Update()
return self
end
end
--====================================================================
-- My Shields Bar
--====================================================================
if isRetail then
local UnitHealthMax = UnitHealthMax
local UnitGetTotalAbsorbs = UnitGetTotalAbsorbs
local function UpdateValue(self)
local m = UnitHealthMax('player') or 0
local a = UnitGetTotalAbsorbs('player') or 0
if a>m then a = m end
self:SetValue(m>0 and a/m or 0)
end
local embed = { UpdateValue = UpdateValue }
addon.setupFunc['myshields'] = function(db)
local self = Bar_Create(db, embed)
self:Show()
self:SetScript("OnEvent", UpdateValue)
self:RegisterUnitEvent("UNIT_MAXHEALTH", 'player')
self:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", 'player')
self:Update()
return self
end
end
--====================================================================
-- Cast Bar
--====================================================================
do
local max = max
local GetTime = GetTime
local UnitCastingInfo = UnitCastingInfo
local UnitChannelInfo = UnitChannelInfo
local function OnUpdate(self,elapsed)
local dur = self.dur + elapsed
if dur>=self.max then self:Hide(); return end
self.dur = dur
local value = dur / self.maxa
local tex = self.textures[1]
tex:SetHeight( self.height * value )
tex:SetTexCoord( self.coord1, self.coord2, 1-value, 1 )
end
local function CastStart(self, event, unit)
local name, _, _, start, finish, _, castID = UnitCastingInfo(unit)
if name then
self.castID = castID
self.dur = max( GetTime() - start/1000 , 0 )
self.max = (finish - start) / 1000
self.maxa = self.max * self.margin
self:Show()
else
self:Hide()
end
end
local function ChannelStart(self, event, unit)
local name, _, _, start, finish = UnitChannelInfo(unit)
if name then
self.castID = nil
self.dur = max( GetTime() - start/1000 , 0 )
self.max = (finish - start) / 1000
self.maxa = self.max * self.margin
self:Show()
else
self:Hide()
end
end
local function CastStop(self, event, unit, castID)
if castID == self.castID then
self:Hide()
end
end
addon.setupFunc['cast'] = function(db)
local self = Bar_Create(db)
self.TestMode = nil
self.textures[1]:Show()
self:SetScript("OnUpdate", OnUpdate)
self:SetScript("OnEvent", OnEvent)
self:RegisterUnitEvent("UNIT_SPELLCAST_START", db.unit)
self:RegisterUnitEvent("UNIT_SPELLCAST_STOP", db.unit)
self:RegisterUnitEvent("UNIT_SPELLCAST_FAILED", db.unit)
self:RegisterUnitEvent("UNIT_SPELLCAST_DELAYED", db.unit)
self:RegisterUnitEvent("UNIT_SPELLCAST_CHANNEL_STOP", db.unit)
self:RegisterUnitEvent("UNIT_SPELLCAST_CHANNEL_START", db.unit)
self:RegisterUnitEvent("UNIT_SPELLCAST_CHANNEL_UPDATE", db.unit)
self.UNIT_SPELLCAST_START = CastStart
self.UNIT_SPELLCAST_DELAYED = CastStart
self.UNIT_SPELLCAST_FAILED = CastStop
self.UNIT_SPELLCAST_STOP = CastStop
self.UNIT_SPELLCAST_INTERRUPTED = CastStop
self.UNIT_SPELLCAST_CHANNEL_STOP = CastStop
self.UNIT_SPELLCAST_CHANNEL_INTERRUPTED = CastStop
self.UNIT_SPELLCAST_CHANNEL_START = ChannelStart
self.UNIT_SPELLCAST_CHANNEL_UPDATE = ChannelStart
return self
end