-
Notifications
You must be signed in to change notification settings - Fork 7
/
core.lua
1508 lines (1271 loc) · 41.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
------------------------------------------------------------
-- Experiencer by Sonaza (https://sonaza.com)
-- Licensed under MIT License
-- See attached license text in file LICENSE
------------------------------------------------------------
local ADDON_NAME = ...;
local Addon = LibStub("AceAddon-3.0"):NewAddon(select(2, ...), ADDON_NAME, "AceEvent-3.0", "AceHook-3.0");
_G[ADDON_NAME] = Addon;
Addon:SetDefaultModuleLibraries("AceEvent-3.0");
local AceDB = LibStub("AceDB-3.0");
local LibSharedMedia = LibStub("LibSharedMedia-3.0");
-- Adding default media to LibSharedMedia in case they're not already added
LibSharedMedia:Register("font", "DorisPP", [[Interface\AddOns\Experiencer\media\DORISPP.TTF]]);
EXPERIENCER_SPLITS_TIP = "You can split Experiencer bar in up to three different sections allowing you to display more information at once.|n|nRight-click the bar to see options.";
local TEXT_VISIBILITY_HIDE = 1;
local TEXT_VISIBILITY_HOVER = 2;
local TEXT_VISIBILITY_ALWAYS = 3;
local FrameLevels = {
"rested",
"visualSecondary",
"visualPrimary",
"change",
"main",
"color",
"highlight",
"textFrame",
};
Addon.activeModules = {}
Addon.orderedModules = {};
ExperiencerModuleBarsMixin = {
module = nil,
moduleId = "",
previousData = nil,
hasModuleChanged = false,
changeTarget = 0,
hasBuffer = false,
bufferTimeout = 0,
};
function Addon:GetPlayerClassColor()
local _, class = UnitClass("player");
return (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)[class or 'PRIEST'];
end
function Addon:GetBarColor()
if (self.db.global.Color.UseClassColor) then
return Addon:GetPlayerClassColor();
else
return {
r = self.db.global.Color.r,
g = self.db.global.Color.g,
b = self.db.global.Color.b
};
end
end
function Addon:OnInitialize()
local defaults = {
char = {
Visible = true,
TextVisibility = TEXT_VISIBILITY_ALWAYS,
NumSplits = 1,
ActiveModules = { },
DataBrokerSource = 1,
},
global = {
AnchorPoint = "BOTTOM",
BigBars = false,
FlashLevelUp = true,
FontFace = "DorisPP",
FontScale = 1,
Color = {
UseClassColor = true,
r = 1,
g = 1,
b = 1,
},
SplitsTipShown = false,
}
};
self.db = AceDB:New("ExperiencerDB", defaults);
Addon:InitializeDataBroker();
Addon:InitializeModules();
if (self.db.char.ActiveModules[1] == nil) then
self.db.char.ActiveModules[1] = "experience";
end
for index = 1, self.db.char.NumSplits do
if (self.db.char.ActiveModules[index]) then
Addon:SetModule(index, self.db.char.ActiveModules[index], true);
else
local newModule = Addon:FindValidModuleForBar(index);
Addon:SetModule(index, newModule.id, true);
end
end
end
function ExperiencerSplitsAlertCloseButton_OnClick(self)
Addon.db.global.SplitsTipShown = true;
end
function Addon:OnEnable()
WorldMapFrame:HookScript("OnShow", function() Addon:UpdateVisiblity() end);
WorldMapFrame:HookScript("OnHide", function() Addon:UpdateVisiblity() end);
hooksecurefunc(WorldMapFrame, "Minimize", function() Addon:UpdateVisiblity() end);
hooksecurefunc(WorldMapFrame, "Maximize", function() Addon:UpdateVisiblity() end);
Addon:RegisterEvent("PLAYER_REGEN_DISABLED");
Addon:RegisterEvent("PET_BATTLE_OPENING_START");
Addon:RegisterEvent("PET_BATTLE_CLOSE");
ExperiencerFrame:EnableMouse(true);
ExperiencerFrame:EnableMouseWheel(true);
ExperiencerFrame:SetScript("OnEnter", Experiencer_OnEnter);
ExperiencerFrame:SetScript("OnLeave", Experiencer_OnLeave);
ExperiencerFrame:SetScript("OnMouseDown", Experiencer_OnMouseDown);
ExperiencerFrame:SetScript("OnMouseWheel", Experiencer_OnMouseWheel);
ExperiencerFrame:SetScript("OnUpdate", Experiencer_OnUpdate);
Addon:UpdateFrames();
Addon:RefreshBars(true);
Addon:UpdateVisiblity();
end
function Addon:IsBarVisible()
return self.db.char.Visible;
end
function Addon:InitializeModules()
for moduleId, module in Addon:IterateModules() do
Addon.orderedModules[module.order] = module;
if (module.savedvars) then
module.db = AceDB:New("ExperiencerDB_module_" .. moduleId, module.savedvars);
end
module:Initialize();
module.initialized = true;
end
Addon.modulesInitialized = true;
end
function Addon:RegisterModule(moduleId, prototype)
if (Addon:GetModule(moduleId, true) ~= nil) then
error(("Addon:RegisterModule(moduleId[, prototype]): Module '%s' is already registered."):format(tostring(moduleId)), 2);
return;
end
local module = Addon:NewModule(moduleId, prototype or {});
module.id = moduleId;
module.Refresh = function(self, instant)
Addon:RefreshModule(self, instant);
end
module.RefreshText = function(self)
Addon:RefreshText(self);
end
return module;
end
function Addon:ToggleVisibility(visiblity)
self.db.char.Visible = visiblity or not self.db.char.Visible;
Addon:UpdateVisiblity();
end
function Addon:UpdateVisiblity()
if (WorldMapFrame) then
ExperiencerFrame:SetShown(not WorldMapFrame.isMaximized or not WorldMapFrame:IsVisible());
end
if (self.db.char.Visible) then
Addon:ShowBar();
else
Addon:HideBar();
end
end
function Addon:ShowBar()
ExperiencerFrameBars:Show();
if (Addon.db.char.TextVisibility == TEXT_VISIBILITY_ALWAYS) then
for _, moduleFrame in Addon:GetModuleFrameIterator() do
moduleFrame.textFrame:Show();
end
end
end
function Addon:HideBar()
ExperiencerFrameBars:Hide();
end
function Addon:GetProgressColor(progress)
local r = math.min(1.0, math.max(0.0, 2.0 - progress * 1.8));
local g = math.min(1.0, math.max(0.0, progress * 2.0));
local b = 0;
return string.format("|cff%02x%02x%02x", r * 255, g * 255, b * 255);
end
local function UnitAuraByNameOrId(unit, aura_name_or_id, filter)
for index = 1, 40 do
local name, _, _, _, _, _, _, _, _, spell_id = UnitAura(unit, index, filter);
if (name == aura_name_or_id or spell_id == aura_name_or_id) then
return UnitAura(unit, index, filter);
end
end
return nil;
end
function Addon:PlayerHasBuff(spellID)
return UnitAuraByNameOrId("player", spellID, "HELPFUL") ~= nil;
end
local function roundnum(num, idp)
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
function Addon:FormatNumber(num)
num = tonumber(num);
if (num >= 1000000) then
num = roundnum((num / 1000000), 2) .. "m";
elseif (num >= 1000) then
num = roundnum((num / 1000), 2) .. "k";
end
return num;
end
function Addon:FormatNumberFancy(num, billions)
billions = billions or true;
num = tonumber(num);
if (not num) then
return num;
end
local divisor = 1;
local suffix = "";
if (num >= 1e9 and billions) then
suffix = "b";
divisor = 1e9;
elseif (num >= 1e6) then
suffix = "m";
divisor = 1e6;
elseif (num >= 1e3) then
suffix = "k";
divisor = 1e3;
end
return BreakUpLargeNumbers(num / divisor) .. suffix;
end
function Addon:GetCurrentSplit()
return Addon.db.char.NumSplits;
end
function Addon:RefreshModule(module, instant)
for _, moduleFrame in Addon:GetModuleFrameIterator() do
if (moduleFrame.module == module) then
moduleFrame:Refresh(instant);
return;
end
end
end
function Addon:RefreshText(module)
for _, moduleFrame in Addon:GetModuleFrameIterator() do
if (not module or moduleFrame.module == module) then
moduleFrame:RefreshText();
end
if (moduleFrame.module == module) then return end
end
end
function Addon:GetModuleFrame(index)
assert(tonumber(index) ~= nil, "Index must be a number");
local frameName = "ExperiencerFrameBarsModule" .. tonumber(index);
return _G[frameName];
end
function Addon:GetModuleFrameIterator()
return ipairs({
Addon:GetModuleFrame(1),
Addon:GetModuleFrame(2),
Addon:GetModuleFrame(3),
});
end
function Addon:UpdateFrames()
local anchor = Addon.db.global.AnchorPoint or "BOTTOM";
local offset = 0;
if (anchor == "TOP") then offset = 1 end
if (anchor == "BOTTOM") then offset = -1 end
ExperiencerFrame:ClearAllPoints();
ExperiencerFrame:SetPoint(anchor .. "LEFT", UIParent, anchor .. "LEFT", 0, offset);
ExperiencerFrame:SetPoint(anchor .. "RIGHT", UIParent, anchor .. "RIGHT", 0, offset);
if (not Addon.db.global.SplitsTipShown) then
local alertFrame = ExperiencerFrame.SplitsAlert;
alertFrame:ClearAllPoints();
alertFrame.Arrow:ClearAllPoints();
if (anchor == "BOTTOM") then
alertFrame:SetPoint("BOTTOM", ExperiencerFrame, "TOP", 0, 30);
alertFrame.Arrow:SetPoint("TOP", alertFrame, "BOTTOM", 0, 2);
SetClampedTextureRotation(alertFrame.Arrow.Arrow, 0);
SetClampedTextureRotation(alertFrame.Arrow.Glow, 0);
elseif (anchor == "TOP") then
alertFrame:SetPoint("TOP", ExperiencerFrame, "BOTTOM", 0, -30);
alertFrame.Arrow:SetPoint("BOTTOM", alertFrame, "TOP", 0, -2);
SetClampedTextureRotation(alertFrame.Arrow.Arrow, 180);
SetClampedTextureRotation(alertFrame.Arrow.Glow, 180);
end
alertFrame.Arrow.Glow:Hide();
alertFrame:Show();
end
if (not Addon.db.global.BigBars) then
ExperiencerFrame:SetHeight(10);
else
ExperiencerFrame:SetHeight(17);
end
local numSplits = Addon:GetCurrentSplit();
local width, height = ExperiencerFrameBars:GetSize();
local sectionWidth = width / numSplits;
local parentFrame = ExperiencerFrameBars;
local moduleBar1 = Addon:GetModuleFrame(1);
local moduleBar2 = Addon:GetModuleFrame(2);
local moduleBar3 = Addon:GetModuleFrame(3);
moduleBar1:ClearAllPoints();
moduleBar2:ClearAllPoints();
moduleBar3:ClearAllPoints();
if (numSplits == 1) then
moduleBar1:Show();
moduleBar1:SetAllPoints(parentFrame);
moduleBar2:Hide();
moduleBar3:Hide();
elseif (numSplits == 2) then
moduleBar1:Show();
moduleBar1:SetPoint("TOPLEFT", parentFrame, "TOPLEFT", 0, 0);
moduleBar1:SetPoint("BOTTOMLEFT", parentFrame, "BOTTOMLEFT", 0, 0);
moduleBar1:SetPoint("BOTTOMRIGHT", parentFrame, "BOTTOM", 0, 0);
moduleBar2:Show();
moduleBar2:SetPoint("TOPRIGHT", parentFrame, "TOPRIGHT", 0, 0);
moduleBar2:SetPoint("BOTTOMLEFT", parentFrame, "BOTTOM", 0, 0);
moduleBar2:SetPoint("BOTTOMRIGHT", parentFrame, "BOTTOMRIGHT", 0, 0);
moduleBar3:Hide();
elseif (numSplits == 3) then
moduleBar1:Show();
moduleBar1:SetPoint("TOPLEFT", parentFrame, "TOPLEFT", 0, 0);
moduleBar1:SetPoint("BOTTOMLEFT", parentFrame, "BOTTOMLEFT", 0, 0);
moduleBar1:SetPoint("BOTTOMRIGHT", parentFrame, "BOTTOMLEFT", sectionWidth, 0);
moduleBar2:Show();
moduleBar2:SetPoint("TOP", parentFrame, "TOP", 0, 0);
moduleBar2:SetPoint("BOTTOMLEFT", parentFrame, "BOTTOM", -sectionWidth / 2, 0);
moduleBar2:SetPoint("BOTTOMRIGHT", parentFrame, "BOTTOM", sectionWidth / 2, 0);
moduleBar3:Show();
moduleBar3:SetPoint("TOPRIGHT", parentFrame, "TOPRIGHT", 0, 0);
moduleBar3:SetPoint("BOTTOMLEFT", parentFrame, "BOTTOMRIGHT", -sectionWidth, 0);
moduleBar3:SetPoint("BOTTOMRIGHT", parentFrame, "BOTTOMRIGHT", 0, 0);
end
local c = Addon:GetBarColor();
local ib = 1 - (0.2126 * c.r + 0.7152 * c.g + 0.0722 * c.b); -- calculate inverse brightness
for _, moduleFrame in Addon:GetModuleFrameIterator() do
moduleFrame.main:SetStatusBarColor(c.r, c.g, c.b);
moduleFrame.main:SetAnimatedTextureColors(c.r, c.g, c.b);
moduleFrame.color:SetStatusBarColor(c.r, c.g, c.b, 0.23 + ib * 0.26); -- adjust color strength by brightness
moduleFrame.rested:SetStatusBarColor(c.r, c.g, c.b, 0.3);
moduleFrame.visualPrimary:SetStatusBarColor(c.r, c.g, c.b, 0.375);
moduleFrame.visualSecondary:SetStatusBarColor(c.r, c.g, c.b, 0.375);
moduleFrame.textFrame:ClearAllPoints();
moduleFrame.textFrame:SetPoint(anchor, moduleFrame, anchor);
moduleFrame.textFrame:SetWidth(sectionWidth);
local fontPath = LibSharedMedia:Fetch("font", self.db.global.FontFace);
ExperiencerFont:SetFont(fontPath, math.floor(10 * self.db.global.FontScale), "OUTLINE");
ExperiencerBigFont:SetFont(fontPath, math.floor(13 * self.db.global.FontScale), "OUTLINE");
local frameHeightMultiplier = (self.db.global.FontScale - 1.0) * 0.55 + 1.0;
if (not Addon.db.global.BigBars) then
moduleFrame.textFrame:SetHeight(math.max(18, 20 * frameHeightMultiplier));
moduleFrame.textFrame.text:SetFontObject("ExperiencerFont");
else
moduleFrame.textFrame:SetHeight(math.max(24, 28 * frameHeightMultiplier));
moduleFrame.textFrame.text:SetFontObject("ExperiencerBigFont");
end
for index, frameName in ipairs(FrameLevels) do
local frame = moduleFrame[frameName];
if (index == 1) then
baseFrameLevel = frame:GetFrameLevel();
else
frame:SetFrameLevel(baseFrameLevel + index - 1);
end
end
end
end
function ExperiencerModuleBarsMixin:SetActiveModule(moduleId)
assert(moduleId ~= nil);
self.module = Addon:GetModule(moduleId, true);
if (self.module) then
self.moduleId = moduleId;
self:Refresh(true);
else
self:RemoveActiveModule();
end
end
function ExperiencerModuleBarsMixin:RemoveActiveModule()
self.moduleId = "";
self.module = nil;
self:Refresh(true);
end
function ExperiencerModuleBarsMixin:SetAnimationSpeed(speed)
assert(speed and speed > 0);
speed = (1 / speed) * 0.5;
self.main.tileTemplateDelay = 0.3 * speed;
local durationPerDistance = 0.008 * speed;
for index, anim in ipairs({ self.main.Anim:GetAnimations() }) do
if (anim.durationPerDistance) then
anim.durationPerDistance = durationPerDistance;
end
if (anim.delayPerDistance) then
anim.delayPerDistance = durationPerDistance;
end
end
end
function ExperiencerModuleBarsMixin:StopAnimation()
for index, anim in ipairs({ self.main.Anim:GetAnimations() }) do
anim:Stop();
end
end
function Addon:RefreshBars(instant)
for _, moduleFrame in Addon:GetModuleFrameIterator() do
moduleFrame:Refresh(instant);
end
end
function ExperiencerModuleBarsMixin:TriggerBufferedUpdate(instant)
if (not self.module) then return end
local data = self.module:GetBarData();
local valueHasChanged = true;
local isLoss = false;
local changeCurrent = data.current;
if (self.previousData and not self.hasModuleChanged) then
if (data.level == self.previousData.level and data.current < self.previousData.current) then
isLoss = true;
changeCurrent = self.previousData.current;
end
if (data.level < self.previousData.level) then
isLoss = true;
changeCurrent = data.max;
end
if (data.current == self.previousData.current) then
valueHasChanged = false;
end
end
if (not isLoss) then
self.main.accumulationTimeoutInterval = 0.01;
else
self.main.accumulationTimeoutInterval = 0.35;
end
self.main.matchBarValueToAnimation = true;
self:SetAnimationSpeed(1.0);
if (valueHasChanged and not self.hasModuleChanged and not instant and not isLoss) then
if (self.previousData) then
local current = data.current;
local previous = self.previousData.current;
if (not self.hasModuleChanged and self.previousData.level < data.level) then
current = current + self.previousData.max;
end
local diff = (current - previous) / data.max;
local speed = math.max(1, math.min(10, diff * 1.2 + 1.0));
speed = speed * speed;
self:SetAnimationSpeed(speed);
end
self.main:SetAnimatedValues(data.current, data.min, data.max, data.level);
else
self.main:SetAnimatedValues(data.current, data.min, data.max, data.level);
self.main:ProcessChangesInstantly();
end
if (not instant and valueHasChanged and not self.hasModuleChanged) then
if (not isLoss) then
self.change.fadegain_in:Stop();
self.change.fadegain_out:Stop();
self.change.fadegain_out:Play();
end
self.main.spark.fade:Stop();
self.main.spark.fade:Play();
end
self.previousData = data;
end
function Addon:ShouldShowSecondaryText(moduleIndex)
return self.db.char.NumSplits < 3 or (Addon.Hovering and Addon:GetModuleIndexFromMousePosition() == moduleIndex);
end
function ExperiencerModuleBarsMixin:RefreshText()
local text = "";
local brokerText = "";
if (self.module and self.module.initialized) then
local primaryText, secondaryText = self.module:GetText();
if (secondaryText and string.len(secondaryText) == 0) then secondaryText = nil end
if (secondaryText and Addon:ShouldShowSecondaryText(self:GetID())) then
text = string.trim(primaryText .. " " .. secondaryText);
elseif (secondaryText) then
text = string.trim(primaryText .. " |cffffff00+|r");
else
text = string.trim(primaryText);
end
brokerText = string.trim(primaryText .. " " .. (secondaryText or ""));
end
self.textFrame.text:SetText(text);
if (Addon.db.char.DataBrokerSource == self:GetID()) then
Addon:UpdateDataBrokerText(brokerText);
end
local numSplits = Addon:GetCurrentSplit();
local width, height = ExperiencerFrameBars:GetSize();
local sectionWidth = width / numSplits;
local stringWidth = self.textFrame.text:GetStringWidth();
self.textFrame:SetWidth(math.max(sectionWidth, stringWidth + 26));
self.textFrame:SetClampedToScreen(true);
if (Addon.Hovering and Addon:GetModuleIndexFromMousePosition() == self:GetID()) then
Addon.ExpandedTextField = self:GetID();
elseif (Addon.ExpandedTextField == self:GetID()) then
Addon.ExpandedTextField = nil;
end
end
function ExperiencerModuleBarsMixin:Refresh(instant)
self:RefreshText();
self.hasModuleChanged = (self.module ~= self.previousModule);
self.previousModule = self.module;
local data;
if (self.module and self.module.initialized) then
data = self.module:GetBarData();
else
data = {};
data.id = nil;
data.level = 0;
data.min = 0;
data.max = 1;
data.current = 0;
data.rested = nil;
data.visual = nil;
end
local valueHasChanged = true;
local isLoss = false;
local changeCurrent = data.current;
self.hasDataIdChanged = self.previousData and self.previousData.id ~= data.id;
if (self.previousData and not self.hasModuleChanged and not self.hasDataIdChanged) then
if (data.level == self.previousData.level and data.current < self.previousData.current) then
isLoss = true;
changeCurrent = self.previousData.current;
end
if (data.level < self.previousData.level) then
isLoss = true;
changeCurrent = data.max;
end
if (data.current == self.previousData.current) then
valueHasChanged = false;
end
end
if (instant or isLoss) then
self.hasBuffer = false;
self:TriggerBufferedUpdate(true);
else
self.hasBuffer = true;
self.bufferTimeout = 0.5;
end
self.color:SetMinMaxValues(data.min, data.max);
self.color:SetValue(self.main:GetContinuousAnimatedValue());
self.change:SetMinMaxValues(data.min, data.max);
if (not isLoss) then
self.changeTarget = changeCurrent;
if (not self.change.fadegain_in:IsPlaying()) then
self.change:SetValue(self.main:GetContinuousAnimatedValue());
end
else
self.changeTarget = self.main:GetContinuousAnimatedValue();
self.change:SetValue(changeCurrent);
end
if (instant or self.hasModuleChanged or self.hasDataIdChanged) then
self.changeTarget = changeCurrent;
self.change:SetValue(self.changeTarget);
end
if (data.rested and data.rested > 0) then
self.rested:Show();
self.rested:SetMinMaxValues(data.min, data.max);
self.rested:SetValue(self.main:GetContinuousAnimatedValue() + data.rested);
else
self.rested:Hide();
end
if (data.visual) then
local primary, secondary;
if (type(data.visual) == "number") then
primary = data.visual;
elseif (type(data.visual) == "table") then
primary, secondary = unpack(data.visual);
end
if (primary and primary > 0) then
self.visualPrimary:Show();
self.visualPrimary:SetMinMaxValues(data.min, data.max);
self.visualPrimary:SetValue(self.main:GetContinuousAnimatedValue() + primary);
else
self.visualPrimary:Hide();
end
if (secondary and secondary > 0) then
self.visualSecondary:Show();
self.visualSecondary:SetMinMaxValues(data.min, data.max);
self.visualSecondary:SetValue(self.main:GetContinuousAnimatedValue() + secondary);
else
self.visualSecondary:Hide();
end
else
self.visualPrimary:Hide();
self.visualSecondary:Hide();
end
if (not instant and valueHasChanged) then
if (not isLoss) then
if (not self.change.fadegain_in:IsPlaying()) then
self.change.fadegain_in:Play();
end
else
self.change.fadeloss:Stop();
self.change.fadeloss:Play();
end
end
end
function Addon:GetHoveredModule()
local hoveredIndex = Addon:GetModuleIndexFromMousePosition();
local moduleFrame = Addon:GetModuleFrame(hoveredIndex)
if (moduleFrame) then
return moduleFrame.module;
end
return nil;
end
function Addon:SendModuleChatMessage()
local module = Addon:GetHoveredModule();
if (not module) then return end
local hasMessage, reason = module:HasChatMessage();
if (not hasMessage) then
DEFAULT_CHAT_FRAME:AddMessage(("|cfffaad07Experiencer|r %s"):format(reason));
return;
end
local msg = module:GetChatMessage();
if (IsControlKeyDown()) then
ChatFrame_OpenChat(msg);
else
DEFAULT_CHAT_FRAME.editBox:SetText(msg)
end
end
function Addon:ToggleTextVisilibity(visibility, noAnimation)
if (visibility) then
self.db.char.TextVisibility = visibility;
elseif (self.db.char.TextVisibility == TEXT_VISIBILITY_HOVER) then
self.db.char.TextVisibility = TEXT_VISIBILITY_ALWAYS;
elseif (self.db.char.TextVisibility == TEXT_VISIBILITY_ALWAYS) then
self.db.char.TextVisibility = TEXT_VISIBILITY_HOVER;
end
if (not noAnimation) then
for _, moduleFrame in Addon:GetModuleFrameIterator() do
if (self.db.char.TextVisibility == TEXT_VISIBILITY_ALWAYS) then
moduleFrame.textFrame.fadeout:Stop();
moduleFrame.textFrame.fadein:Play();
else
moduleFrame.textFrame.fadein:Stop();
moduleFrame.textFrame.fadeout:Play();
end
end
end
end
function Addon:GetNumOfEnabledModules()
local numTotalEnabled = 0;
local numActiveEnabled = 0;
for _, module in Addon:IterateModules() do
if (not module:IsDisabled()) then
numTotalEnabled = numTotalEnabled + 1;
end
end
for _, moduleId in ipairs(self.db.char.ActiveModules) do
local module = Addon:GetModule(moduleId, true);
if (module and not module:IsDisabled()) then
numActiveEnabled = numActiveEnabled + 1;
end
end
return numTotalEnabled, numActiveEnabled;
end
function Addon:CollapseActiveModules()
local collapsedList = {};
for _, moduleId in ipairs(self.db.char.ActiveModules) do
local module = Addon:GetModule(moduleId, true);
if (module and not module:IsDisabled()) then
tinsert(collapsedList, moduleId);
end
end
self.db.char.ActiveModules = {};
if (#collapsedList > 0) then
for index, moduleId in ipairs(collapsedList) do
Addon:SetModule(index, moduleId, true);
end
else
local newModule = Addon:FindValidModuleForBar(1);
Addon:SetModule(index, newModule.id, true);
end
Addon:UpdateFrames();
Addon:RefreshBars(true);
end
function Addon:CheckDisabledStatus()
local numTotalEnabled, numActiveEnabled = Addon:GetNumOfEnabledModules();
if (numActiveEnabled < self.db.char.NumSplits) then
if (numTotalEnabled == numActiveEnabled) then
self.db.char.NumSplits = numActiveEnabled;
Addon:CollapseActiveModules();
else
for index = 1, self.db.char.NumSplits do
local moduleId = self.db.char.ActiveModules[index];
local module = Addon:GetModule(moduleId, true);
if (module and module:IsDisabled()) then
local newModule = Addon:FindValidModuleForBar(index);
Addon:SetModule(index, newModule.id, true);
end
end
end
end
end
function Addon:SetModule(splitIndex, moduleId, novalidation)
if (not novalidation) then
local alreadySet = nil;
for i = 1, 3 do
if (i ~= splitIndex and self.db.char.ActiveModules[i] == moduleId) then
alreadySet = i;
break;
end
end
if (alreadySet) then
self.db.char.ActiveModules[alreadySet] = self.db.char.ActiveModules[splitIndex];
local moduleFrame = Addon:GetModuleFrame(alreadySet);
if (moduleFrame) then
moduleFrame:SetActiveModule(self.db.char.ActiveModules[alreadySet]);
end
end
end
self.db.char.ActiveModules[splitIndex] = moduleId;
local moduleFrame = Addon:GetModuleFrame(splitIndex);
if (moduleId) then
moduleFrame:SetActiveModule(moduleId);
else
moduleFrame:RemoveActiveModule();
end
Addon:UpdateFrames();
Addon:RefreshBars(true);
end
function Addon:IsModuleInUse(moduleId, ignoreIndex)
for index, activeModuleId in ipairs(self.db.char.ActiveModules) do
if (index > self.db.char.NumSplits) then break end
if (activeModuleId == moduleId and ignoreIndex ~= index) then
return true;
end
end
return false;
end
function Addon:FindValidModuleForBar(index, direction, findNext)
findNext = findNext or false;
direction = direction or 1;
local newIndex = 1;
local moduleFrame = Addon:GetModuleFrame(index);
if (moduleFrame.module) then
newIndex = moduleFrame.module.order;
end
local numModules = #Addon.orderedModules;
local loops = 0;
local orderedModule = Addon.orderedModules[newIndex];
if (findNext or orderedModule:IsDisabled() or Addon:IsModuleInUse(orderedModule.id, index)) then
repeat
newIndex = newIndex + direction;
if (newIndex > numModules) then newIndex = 1 end
if (newIndex < 1) then newIndex = numModules end
orderedModule = Addon.orderedModules[newIndex];
local isValid = true;
if (orderedModule:IsDisabled()) then
isValid = false;
elseif (Addon:IsModuleInUse(orderedModule.id, index)) then
isValid = false;
end
loops = loops + 1;
until(isValid or loops > numModules);
-- Nothing found, not enough modules
if (loops > numModules) then
return nil;
end
end
return orderedModule;
end
function Addon:GetAnchors(frame)
local B, T = "BOTTOM", "TOP";
local x, y = frame:GetCenter();
if (y < _G.GetScreenHeight() / 2) then
return B, T, 1;
else
return T, B, -1;
end
end
function Addon:SetSplits(newSplits)
local oldSplits = self.db.char.NumSplits;
self.db.char.NumSplits = newSplits;
if (oldSplits < newSplits) then
for index=2, newSplits do
local moduleId = self.db.char.ActiveModules[index];
if (not moduleId) then
local newModule = Addon:FindValidModuleForBar(index, 1, index);
moduleId = newModule.id;
end
if (moduleId) then
Addon:SetModule(index, moduleId, true);
else
self.db.char.NumSplits = index - 1;
break;
end
end
end
Addon:UpdateFrames();
Addon:RefreshText();
end
function Addon:GenerateFontsMenu(fontsMenu, fontsList, startIndex)
local fontsAdded = 0;
local numFonts = #fontsList;
for index = startIndex, numFonts do
local font = fontsList[index];
tinsert(fontsMenu, {
text = fontsList[index],
func = function()
self.db.global.FontFace = fontsList[index];
Addon:UpdateFrames();
CloseMenus();
end,
checked = function() return self.db.global.FontFace == fontsList[index]; end,
});
fontsAdded = fontsAdded + 1;
if (fontsAdded > 30 and index < numFonts) then
local subMenu = {};
Addon:GenerateFontsMenu(subMenu, fontsList, index+1);
tinsert(fontsMenu, {
text = "|cffffd200More|r",
hasArrow = true,
menuList = subMenu,
notCheckable = true,
});
break;
end
end
end
function Addon:GetSharedFonts()
local sharedFonts = LibSharedMedia:List("font");
local numFonts = #sharedFonts;
local fontsMenu = {};
Addon:GenerateFontsMenu(fontsMenu, sharedFonts, 1);
return fontsMenu;
end
function Addon:GetFontScaleMenu()
local windowScales = { 0.8, 0.85, 0.9, 0.95, 1.0, 1.05, 1.1, 1.2, 1.3, 1.4, 1.5, };
local menu = {};
for index, scale in ipairs(windowScales) do
tinsert(menu, {
text = string.format("%d%%", scale * 100),
func = function()
self.db.global.FontScale = scale;
Addon:UpdateFrames();
CloseMenus();