forked from Peterodox/Plumber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.lua
2080 lines (1736 loc) · 64.1 KB
/
API.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 _, addon = ...
local API = addon.API;
local L = addon.L;
local tonumber = tonumber;
local match = string.match;
local format = string.format;
local gsub = string.gsub;
local tinsert = table.insert;
local tremove = table.remove;
local floor = math.floor;
local sqrt = math.sqrt;
local time = time;
local unpack = unpack;
local GetCVarBool = C_CVar.GetCVarBool;
local CreateFrame = CreateFrame;
local securecallfunction = securecallfunction;
local function Nop(...)
end
do -- Table
local function Mixin(object, ...)
for i = 1, select("#", ...) do
local mixin = select(i, ...)
for k, v in pairs(mixin) do
object[k] = v;
end
end
return object
end
API.Mixin = Mixin;
local function CreateFromMixins(...)
return Mixin({}, ...)
end
API.CreateFromMixins = CreateFromMixins;
local function RemoveValueFromList(tbl, v)
for i = 1, #tbl do
if tbl[i] == v then
tremove(tbl, i);
return true
end
end
end
API.RemoveValueFromList = RemoveValueFromList;
local function ReverseList(list)
if not list then return end;
local tbl = {};
local n = 0;
for i = #list, 1, -1 do
n = n + 1;
tbl[n] = list[i];
end
return tbl
end
API.ReverseList = ReverseList;
end
do -- String
local function GetCreatureIDFromGUID(guid)
local id = match(guid, "Creature%-0%-%d*%-%d*%-%d*%-(%d*)");
if id then
return tonumber(id)
end
end
API.GetCreatureIDFromGUID = GetCreatureIDFromGUID;
local function GetVignetteIDFromGUID(guid)
local id = match(guid, "Vignette%-0%-%d*%-%d*%-%d*%-(%d*)");
if id then
return tonumber(id)
end
end
API.GetVignetteIDFromGUID = GetVignetteIDFromGUID;
local function GetWaypointFromText(text)
local uiMapID, x, y = match(text, "|Hworldmap:(%d*):(%d*):(%d*)|h");
if uiMapID and x and y then
return tonumber(uiMapID), tonumber(x), tonumber(y)
end
end
API.GetWaypointFromText = GetWaypointFromText;
local UnitGUID = UnitGUID;
local function GetUnitCreatureID(unit)
local guid = UnitGUID(unit);
if guid then
return GetCreatureIDFromGUID(guid)
end
end
API.GetUnitCreatureID = GetUnitCreatureID;
local function GetGlobalObject(objNameKey)
--Get object via string "FrameName.Key1.Key2"
local obj = _G;
for k in string.gmatch(objNameKey, "%w+") do
obj = obj[k];
if not obj then
return
end
end
return obj
end
API.GetGlobalObject = GetGlobalObject;
end
do -- DEBUG
local function CreateSaveDB(key)
if not PlumberDevOutput then
PlumberDevOutput = {};
end
if not PlumberDevOutput[key] then
PlumberDevOutput[key] = {};
end
end
local function SaveLocalizedText(localizedText, englishText)
local locale = GetLocale();
CreateSaveDB(locale);
PlumberDevOutput[locale][localizedText] = englishText or true;
end
API.SaveLocalizedText = SaveLocalizedText;
local function SaveDataUnderKey(key, ...)
CreateSaveDB(key);
PlumberDevOutput[key] = {...}
end
API.SaveDataUnderKey = SaveDataUnderKey;
end
do --Math
local function Clamp(value, min, max)
if value > max then
return max
elseif value < min then
return min
end
return value
end
API.Clamp = Clamp;
local function Lerp(startValue, endValue, amount)
return (1 - amount) * startValue + amount * endValue;
end
API.Lerp = Lerp;
local function GetPointsDistance2D(x1, y1, x2, y2)
return sqrt( (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2))
end
API.GetPointsDistance2D = GetPointsDistance2D;
local function Round(n)
return floor(n + 0.5);
end
API.Round = Round;
local function RoundCoord(n)
return floor(n * 1000 + 0.5) * 0.001
end
API.RoundCoord = RoundCoord;
local function Saturate(value)
return Clamp(value, 0.0, 1.0);
end
local function DeltaLerp(startValue, endValue, amount, timeSec)
return Lerp(startValue, endValue, Saturate(amount * timeSec * 60.0));
end
API.DeltaLerp = DeltaLerp;
end
do -- Color
local ColorSwatches = {
SelectionBlue = {12, 105, 216},
SmoothGreen = {124, 197, 118},
WarningRed = {212, 100, 28}, --228, 13, 14 248, 81, 73
};
for _, swatch in pairs(ColorSwatches) do
swatch[1] = swatch[1]/255;
swatch[2] = swatch[2]/255;
swatch[3] = swatch[3]/255;
end
local function GetColorByName(colorName)
if ColorSwatches[colorName] then
return unpack(ColorSwatches[colorName])
else
return 1, 1, 1
end
end
API.GetColorByName = GetColorByName;
-- Make Rare and Epic brighter (use the color in Narcissus)
local ITEM_QUALITY_COLORS = ITEM_QUALITY_COLORS;
local QualityColors = {};
QualityColors[1] = CreateColor(0.92, 0.92, 0.92, 1);
QualityColors[3] = CreateColor(105/255, 158/255, 255/255, 1);
QualityColors[4] = CreateColor(185/255, 83/255, 255/255, 1);
local function GetItemQualityColor(quality)
if QualityColors[quality] then
return QualityColors[quality]
else
return ITEM_QUALITY_COLORS[quality].color
end
end
API.GetItemQualityColor = GetItemQualityColor;
local function IsWarningColor(r, g, b)
--Used to determine if the tooltip fontstring is red, which indicates there is a requirement you don't meet
return (r > 0.99 and r <= 1) and (g > 0.1254 and g < 0.1255) and (b > 0.1254 and b < 0.1255)
end
API.IsWarningColor = IsWarningColor;
local function SetTextColorByGlobal(fontString, colorMixin)
local r, g, b;
if colorMixin then
r, g, b = colorMixin:GetRGB();
else
r, g, b = 1, 1, 1;
end
fontString:SetTextColor(r, g, b);
end
API.SetTextColorByGlobal = SetTextColorByGlobal;
end
do -- Time
local D_DAYS = D_DAYS or "%d |4Day:Days;";
local D_HOURS = D_HOURS or "%d |4Hour:Hours;";
local D_MINUTES = D_MINUTES or "%d |4Minute:Minutes;";
local D_SECONDS = D_SECONDS or "%d |4Second:Seconds;";
local DAYS_ABBR = DAYS_ABBR or "%d |4Day:Days;"
local HOURS_ABBR = HOURS_ABBR or "%d |4Hr:Hr;";
local MINUTES_ABBR = MINUTES_ABBR or "%d |4Min:Min;";
local SECONDS_ABBR = SECONDS_ABBR or "%d |4Sec:Sec;";
local SHOW_HOUR_BELOW_DAYS = 3;
local SHOW_MINUTE_BELOW_HOURS = 12;
local SHOW_SECOND_BELOW_MINUTES = 10;
local COLOR_RED_BELOW_SECONDS = 172800;
local function BakePlural(number, singularPlural)
singularPlural = gsub(singularPlural, ";", "");
if number > 1 then
return format(gsub(singularPlural, "|4[^:]*:", ""), number);
else
singularPlural = gsub(singularPlural, ":.*", "");
singularPlural = gsub(singularPlural, "|4", "");
return format(singularPlural, number);
end
end
local function FormatTime(t, pattern, bakePluralEscapeSequence)
if bakePluralEscapeSequence then
return BakePlural(t, pattern);
else
return format(pattern, t);
end
end
local function SecondsToTime(seconds, abbreviated, partialTime, bakePluralEscapeSequence)
--partialTime: Stop processing if the remaining units don't really matter. e.g. to display the remaining time of an event when there are still days left
--bakePluralEscapeSequence: Convert EcsapeSequence like "|4Sec:Sec;" to its result so it can be sent to chat
local intialSeconds = seconds;
local timeString = "";
local isComplete = false;
local days = 0;
local hours = 0;
local minutes = 0;
if seconds >= 86400 then
days = floor(seconds / 86400);
seconds = seconds - days * 86400;
local dayText = FormatTime(days, (abbreviated and DAYS_ABBR) or D_DAYS, bakePluralEscapeSequence);
timeString = dayText;
if partialTime and days >= SHOW_HOUR_BELOW_DAYS then
isComplete = true;
end
end
if not isComplete then
hours = floor(seconds / 3600);
seconds = seconds - hours * 3600;
if hours > 0 then
local hourText = FormatTime(hours, (abbreviated and HOURS_ABBR) or D_HOURS, bakePluralEscapeSequence);
if timeString == "" then
timeString = hourText;
else
timeString = timeString.." "..hourText;
end
if partialTime and hours >= SHOW_MINUTE_BELOW_HOURS then
isComplete = true;
end
else
if timeString ~= "" and partialTime then
isComplete = true;
end
end
end
if partialTime and days > 0 then
isComplete = true;
end
if not isComplete then
minutes = floor(seconds / 60);
seconds = seconds - minutes * 60;
if minutes > 0 then
local minuteText = FormatTime(minutes, (abbreviated and MINUTES_ABBR) or D_MINUTES, bakePluralEscapeSequence);
if timeString == "" then
timeString = minuteText;
else
timeString = timeString.." "..minuteText;
end
if partialTime and minutes >= SHOW_SECOND_BELOW_MINUTES then
isComplete = true;
end
else
if timeString ~= "" and partialTime then
isComplete = true;
end
end
end
if (not isComplete) and seconds > 0 then
seconds = floor(seconds);
local secondText = FormatTime(seconds, (abbreviated and SECONDS_ABBR) or D_SECONDS, bakePluralEscapeSequence);
if timeString == "" then
timeString = secondText;
else
timeString = timeString.." "..secondText;
end
end
if partialTime and intialSeconds < COLOR_RED_BELOW_SECONDS and not bakePluralEscapeSequence then
--WARNING_FONT_COLOR
timeString = "|cffff4800"..timeString.."|r";
end
return timeString
end
API.SecondsToTime = SecondsToTime;
local function SecondsToClock(seconds)
--Clock: 00:00
return format("%s:%02d", math.floor(seconds / 60), math.floor(seconds % 60))
end
API.SecondsToClock = SecondsToClock;
--Unix Epoch is in UTC
local MonthDays = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
};
local function IsLeapYear(year)
return year % 400 == 0 or (year % 4 == 0 and year % 100 ~= 0)
end
local function GetFebruaryDays(year)
if IsLeapYear(year) then
return 29
else
return 28
end
end
local function IsLeftTimeFuture(time1, time2, i)
if not time1[i] then return false end;
if time1[i] > time2[i] then
return true;
elseif time1[i] == time2[i] then
return IsLeftTimeFuture(time1, time2, i + 1);
else
return false
end
end
local function GetNumDaysToDate(year, month, day)
local numDays = day;
for yr = 1, (year -1) do
if IsLeapYear(yr) then
numDays = numDays + 366;
else
numDays = numDays + 365;
end
end
for m = 1, (month - 1) do
if m == 2 then
numDays = numDays + GetFebruaryDays(year);
else
numDays = numDays + MonthDays[m];
end
end
return numDays
end
local function GetNumSecondsToDate(year, month, day, hour, minute, second)
hour = hour or 0;
minute = minute or 0;
second = second or 0;
local numDays = GetNumDaysToDate(year, month, day);
local numSeconds = second;
numSeconds = numSeconds + numDays * 86400;
numSeconds = numSeconds + hour * 3600 + minute * 60;
return numSeconds
end
local function ConvertCalendarTime(calendarTime)
--WoW's CalendarTime See https://warcraft.wiki.gg/wiki/API_C_DateAndTime.GetCurrentCalendarTime
local year = calendarTime.year;
local month = calendarTime.month;
local day = calendarTime.monthDay;
local hour = calendarTime.hour;
local minute = calendarTime.minute;
local second = calendarTime.second or 0; --the original calendarTime does not contain second
return {year, month, day, hour, minute, second}
end
local function GetCalendarTimeDifference(lhsCalendarTime, rhsCalendarTime)
--time = {year, month, day, hour, minute, second}
local time1 = ConvertCalendarTime(lhsCalendarTime);
local time2 = ConvertCalendarTime(rhsCalendarTime);
local second1 = GetNumSecondsToDate(unpack(time1));
local second2 = GetNumSecondsToDate(unpack(time2));
return second2 - second1
end
API.GetCalendarTimeDifference = GetCalendarTimeDifference;
local function WrapNumberWithBrackets(text)
text = gsub(text, "%%d%+", "%%d");
text = gsub(text, "%%d", "%(%%d%+%)");
return text
end
local PATTERN_DAYS = WrapNumberWithBrackets(DAYS_ABBR);
local PATTERN_HOURS = WrapNumberWithBrackets(HOURS_ABBR);
local PATTERN_MINUTES = WrapNumberWithBrackets(MINUTES_ABBR);
local PATTERN_SECONDS = WrapNumberWithBrackets(SECONDS_ABBR);
local function ConvertTextToSeconds(durationText)
if not durationText then return 0 end;
local hours = tonumber(match(durationText, PATTERN_HOURS) or 0);
local minutes = tonumber(match(durationText, PATTERN_MINUTES) or 0);
local seconds = tonumber(match(durationText, PATTERN_SECONDS) or 0);
return 3600 * hours + 60 * minutes + seconds;
end
API.TimeLeftTextToSeconds = ConvertTextToSeconds;
end
do -- Item
local C_Item = C_Item;
local GetItemSpell = GetItemSpell;
local function ColorizeTextByQuality(text, quality, allowColorBlind)
if not (text and quality) then
return text
end
local color = API.GetItemQualityColor(quality);
text = color:WrapTextInColorCode(text);
if allowColorBlind and GetCVarBool("colorblindMode") then
text = text.." |cffffffff[".._G[format("ITEM_QUALITY%s_DESC", quality)].."]|r";
end
return text
end
API.ColorizeTextByQuality = ColorizeTextByQuality;
local function GetColorizedItemName(itemID)
local name = C_Item.GetItemNameByID(itemID);
local quality = C_Item.GetItemQualityByID(itemID);
return ColorizeTextByQuality(name, quality, true);
end
API.GetColorizedItemName = GetColorizedItemName;
local function GetItemSpellID(item)
local spellName, spellID = GetItemSpell(item);
return spellID
end
API.GetItemSpellID = GetItemSpellID;
end
do -- Tooltip Parser
local GetInfoByHyperlink = C_TooltipInfo.GetHyperlink;
local function GetLineText(lines, index)
if lines[index] then
return lines[index].leftText;
end
end
local function GetCreatureName(creatureID)
if not creatureID then return end;
local tooltipData = GetInfoByHyperlink("unit:Creature-0-0-0-0-"..creatureID);
if tooltipData then
return GetLineText(tooltipData.lines, 1);
end
end
API.GetCreatureName = GetCreatureName;
end
do -- Holiday
local CalendarTextureXHolidayKey = {
--Only the important ones :)
[235469] = "lunarfestival",
[235470] = "lunarfestival",
[235471] = "lunarfestival",
[235466] = "loveintheair",
[235467] = "loveintheair",
[235468] = "loveintheair",
[235475] = "noblegarden",
[235476] = "noblegarden",
[235477] = "noblegarden",
[235443] = "childrensweek",
[235444] = "childrensweek",
[235445] = "childrensweek",
[235472] = "midsummer",
[235473] = "midsummer",
[235474] = "midsummer",
[235439] = "brewfest",
[235440] = "brewfest",
[235441] = "brewfest",
[235442] = "brewfest",
[235460] = "hallowsendend",
[235461] = "hallowsendend",
[235462] = "hallowsendend",
[235482] = "winterveil",
[235483] = "winterveil",
[235484] = "winterveil",
[235485] = "winterveil",
};
local HolidayInfoMixin = {};
function HolidayInfoMixin:GetRemainingSeconds()
if self.endTime then
local presentTime = time();
return self.endTime - presentTime
else
return 0
end
end
function HolidayInfoMixin:GetEndTimeString()
--MM/DD 00:00
return self.endTimeString
end
function HolidayInfoMixin:GetRemainingTimeString()
--DD/HH/MM/SS
local seconds = self:GetRemainingSeconds();
return API.SecondsToTime(seconds, false, true);
end
function HolidayInfoMixin:GetName()
return self.name
end
function HolidayInfoMixin:GetKey()
return self.key
end
local function GetActiveMajorHolidayInfo()
local currentCalendarTime = C_DateAndTime.GetCurrentCalendarTime();
local presentDay = currentCalendarTime.monthDay;
local monthOffset = 0;
local holidayInfo;
local holidayKey, holidayName;
local endTimeString;
local eventEndTimeMixin; --{}
local endTime; --number time()
local activeHolidayData; --{ {holiday1}, {holiday2} }
for i = 1, C_Calendar.GetNumDayEvents(monthOffset, presentDay) do --Need to request data first with C_Calendar.OpenCalendar()
holidayInfo = C_Calendar.GetHolidayInfo(monthOffset, presentDay, i);
--print(i, holidayInfo.name)
if holidayInfo and holidayInfo.texture and CalendarTextureXHolidayKey[holidayInfo.texture] then
holidayKey = CalendarTextureXHolidayKey[holidayInfo.texture];
holidayName = holidayInfo.name;
if holidayInfo.startTime and holidayInfo.endTime then
endTimeString = FormatShortDate(holidayInfo.endTime.monthDay, holidayInfo.endTime.month) .." ".. GameTime_GetFormattedTime(holidayInfo.endTime.hour, holidayInfo.endTime.minute, true);
eventEndTimeMixin = holidayInfo.endTime;
end
local isEventActive = true;
if eventEndTimeMixin then
local presentTime = time();
local remainingSeconds = API.GetCalendarTimeDifference(currentCalendarTime, eventEndTimeMixin);
endTime = presentTime + remainingSeconds;
if remainingSeconds <= 0 then
isEventActive = false;
end
end
if isEventActive and holidayName then
local mixin = API.CreateFromMixins(HolidayInfoMixin);
mixin.name = holidayName;
mixin.key = holidayKey;
mixin.endTimeString = endTimeString;
mixin.endTime = endTime;
if not activeHolidayData then
activeHolidayData = {};
end
tinsert(activeHolidayData, mixin);
end
end
end
return activeHolidayData
end
API.GetActiveMajorHolidayInfo = GetActiveMajorHolidayInfo;
end
do --Fade Frame
local abs = math.abs;
local tinsert = table.insert;
local wipe = wipe;
local fadeInfo = {};
local fadingFrames = {};
local f = CreateFrame("Frame");
local function OnUpdate(self, elpased)
local i = 1;
local frame, info, timer, alpha;
local isComplete = true;
while fadingFrames[i] do
frame = fadingFrames[i];
info = fadeInfo[frame];
if info then
timer = info.timer + elpased;
if timer >= info.duration then
alpha = info.toAlpha;
fadeInfo[frame] = nil;
if info.alterShownState and alpha <= 0 then
frame:Hide();
end
else
alpha = info.fromAlpha + (info.toAlpha - info.fromAlpha) * timer/info.duration;
info.timer = timer;
end
frame:SetAlpha(alpha);
isComplete = false;
end
i = i + 1;
end
if isComplete then
f:Clear();
end
end
function f:Clear()
self:SetScript("OnUpdate", nil);
wipe(fadingFrames);
wipe(fadeInfo);
end
function f:Add(frame, fullDuration, fromAlpha, toAlpha, alterShownState, useConstantDuration)
local alpha = frame:GetAlpha();
if alterShownState then
if toAlpha > 0 then
frame:Show();
end
if toAlpha == 0 then
if not frame:IsShown() then
frame:SetAlpha(0);
alpha = 0;
end
if alpha == 0 then
frame:Hide();
end
end
end
if fromAlpha == toAlpha or alpha == toAlpha then
if fadeInfo[frame] then
fadeInfo[frame] = nil;
end
return;
end
local duration;
if useConstantDuration then
duration = fullDuration;
else
if fromAlpha then
duration = fullDuration * (alpha - toAlpha)/(fromAlpha - toAlpha);
else
duration = fullDuration * abs(alpha - toAlpha);
end
end
if duration <= 0 then
frame:SetAlpha(toAlpha);
if toAlpha == 0 then
frame:Hide();
end
return;
end
fadeInfo[frame] = {
fromAlpha = alpha,
toAlpha = toAlpha,
duration = duration,
timer = 0,
alterShownState = alterShownState,
};
for i = 1, #fadingFrames do
if fadingFrames[i] == frame then
return;
end
end
tinsert(fadingFrames, frame);
self:SetScript("OnUpdate", OnUpdate);
end
function f:SimpleFade(frame, toAlpha, alterShownState, speedMultiplier)
--Use a constant fading speed: 1.0 in 0.25s
--alterShownState: if true, run Frame:Hide() when alpha reaches zero / run Frame:Show() at the beginning
speedMultiplier = speedMultiplier or 1;
local alpha = frame:GetAlpha();
local duration = abs(alpha - toAlpha) * 0.25 * speedMultiplier;
if duration <= 0 then
return;
end
self:Add(frame, duration, alpha, toAlpha, alterShownState, true);
end
function f:Snap()
local i = 1;
local frame, info;
while fadingFrames[i] do
frame = fadingFrames[i];
info = fadeInfo[frame];
if info then
frame:SetAlpha(info.toAlpha);
end
i = i + 1;
end
self:Clear();
end
local function UIFrameFade(frame, duration, toAlpha, initialAlpha)
if initialAlpha then
frame:SetAlpha(initialAlpha);
f:Add(frame, duration, initialAlpha, toAlpha, true, false);
else
f:Add(frame, duration, nil, toAlpha, true, false);
end
end
local function UIFrameFadeIn(frame, duration)
frame:SetAlpha(0);
f:Add(frame, duration, 0, 1, true, false);
end
API.UIFrameFade = UIFrameFade; --from current alpha
API.UIFrameFadeIn = UIFrameFadeIn; --from 0 to 1
end
do -- Map
---- Create Module that will be activated in specific zones:
---- 1. Soridormi Auto-report
---- 2. Dreamseed
local C_Map = C_Map;
local GetMapInfo = C_Map.GetMapInfo;
local GetBestMapForUnit = C_Map.GetBestMapForUnit;
local CreateVector2D = CreateVector2D;
local controller;
local modules;
local lastMapID, total;
local ZoneTriggeredModuleMixin = {};
ZoneTriggeredModuleMixin.validMaps = {};
ZoneTriggeredModuleMixin.inZone = false;
ZoneTriggeredModuleMixin.enabled = true;
local function DoNothing(arg)
end
ZoneTriggeredModuleMixin.enterZoneCallback = DoNothing;
ZoneTriggeredModuleMixin.leaveZoneCallback = DoNothing;
function ZoneTriggeredModuleMixin:IsZoneValid(uiMapID)
return self.validMaps[uiMapID]
end
function ZoneTriggeredModuleMixin:SetValidZones(...)
self.validMaps = {};
local map;
for i = 1, select("#", ...) do
map = select(i, ...);
if type(map) == "table" then
for _, uiMapID in ipairs(map) do
self.validMaps[uiMapID] = true;
end
else
self.validMaps[map] = true;
end
end
end
function ZoneTriggeredModuleMixin:PlayerEnterZone(mapID)
if not self.inZone then
self.inZone = true;
self.enterZoneCallback(mapID);
end
if mapID ~= self.currentMapID then
self.currentMapID = mapID;
self:OnCurrentMapChanged(mapID);
end
end
function ZoneTriggeredModuleMixin:PlayerLeaveZone()
if self.inZone then
self.inZone = false;
self.currentMapID = nil;
self.leaveZoneCallback();
end
end
function ZoneTriggeredModuleMixin:SetEnterZoneCallback(callback)
self.enterZoneCallback = callback;
end
function ZoneTriggeredModuleMixin:SetLeaveZoneCallback(callback)
self.leaveZoneCallback = callback;
end
function ZoneTriggeredModuleMixin:OnCurrentMapChanged(newMapID)
end
function ZoneTriggeredModuleMixin:SetEnabled(state)
self.enabled = state or false;
if not self.enabled then
self:PlayerLeaveZone();
end
end
function ZoneTriggeredModuleMixin:Update()
if not self.enabled then return end;
local mapID = GetBestMapForUnit("player");
if self:IsZoneValid(mapID) then
self:PlayerEnterZone(mapID);
else
self:PlayerLeaveZone();
end
end
local function AddZoneModules(module)
if not controller then
controller = CreateFrame("Frame");
modules = {};
total = 0;
controller:SetScript("OnEvent", function(f, event, ...)
local mapID = GetBestMapForUnit("player");
if mapID and mapID ~= lastMapID then
lastMapID = mapID;
else
return
end
for i = 1, total do
if modules[i].enabled then
if modules[i]:IsZoneValid(mapID) then
modules[i]:PlayerEnterZone(mapID);
else
modules[i]:PlayerLeaveZone();
end
end
end
end);
controller:RegisterEvent("ZONE_CHANGED_NEW_AREA");
controller:RegisterEvent("PLAYER_ENTERING_WORLD");
end
table.insert(modules, module);
total = total + 1;
end
local function CreateZoneTriggeredModule(tag)
local module = {
tag = tag,
validMaps = {},
};
for k, v in pairs(ZoneTriggeredModuleMixin) do
module[k] = v;
end
AddZoneModules(module);
return module
end
API.CreateZoneTriggeredModule = CreateZoneTriggeredModule;
--Get Player Coord Less RAM cost
local UnitPosition = UnitPosition;
local GetPlayerMapPosition = C_Map.GetPlayerMapPosition;
local _posY, _posX, _data;
local lastUiMapID;
local MapData = {};
local function CacheMapData(uiMapID)
if MapData[uiMapID] then return end;
local instance, topLeft = C_Map.GetWorldPosFromMapPos(uiMapID, {x=0, y=0});
local width, height = C_Map.GetMapWorldSize(uiMapID);
if topLeft then
local top, left = topLeft:GetXY()
MapData[uiMapID] = {width, height, left, top};
end
end
local function GetPlayerMapCoord_Fallback(uiMapID)
local position = GetPlayerMapPosition(uiMapID, "player");
if position then
return position.x, position.Y
end
end
local function GetPlayerMapCoord(uiMapID)
_posY, _posX = UnitPosition("player");
if not (_posX and _posY) then return GetPlayerMapCoord_Fallback(uiMapID) end;
if uiMapID ~= lastUiMapID then
lastUiMapID = uiMapID;
CacheMapData(uiMapID);
end
_data = MapData[uiMapID]
if not _data or _data[1] == 0 or _data[2] == 0 then return GetPlayerMapCoord_Fallback(uiMapID) end;
return (_data[3] - _posX) / _data[1], (_data[4] - _posY) / _data[2]
end
API.GetPlayerMapCoord = GetPlayerMapCoord;
local function ConvertMapPositionToContinentPosition(uiMapID, x, y, poiID)
local info = GetMapInfo(uiMapID);
if not info then return end;
local continentMapID; --uiMapID
while info do
if info.mapType == Enum.UIMapType.Continent then
continentMapID = info.mapID;
break