-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathUtil.lua
1606 lines (1340 loc) · 42.8 KB
/
Util.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
function CanAccessObject(obj)
return issecure() or not obj:IsForbidden();
end
function GetTextureInfo(obj)
if obj:GetObjectType() == "Texture" then
local assetName = obj:GetAtlas();
local assetType = "Atlas";
if not assetName then
assetName = obj:GetTextureFilePath();
assetType = "File";
end
if not assetName then
assetName = obj:GetTextureFileID();
assetType = "FileID";
end
if not assetName then
assetName = "UnknownAsset";
assetType = "Unknown";
end
local ulX, ulY, blX, blY, urX, urY, brX, brY = obj:GetTexCoord();
return assetName, assetType, ulX, ulY, blX, blY, urX, urY, brX, brY;
end
end
function CalculateDistanceBetweenRegions(regionA, regionB)
local ax, ay = regionA:GetCenter();
local bx, by = regionB:GetCenter();
if ax and bx then
local dx, dy = bx - ax, by - ay;
return math.sqrt(dx * dx + dy * dy);
else
return 0;
end
end
CLASS_ICON_TCOORDS = {
["WARRIOR"] = {0, 0.25, 0, 0.25},
["MAGE"] = {0.25, 0.49609375, 0, 0.25},
["ROGUE"] = {0.49609375, 0.7421875, 0, 0.25},
["DRUID"] = {0.7421875, 0.98828125, 0, 0.25},
["HUNTER"] = {0, 0.25, 0.25, 0.5},
["SHAMAN"] = {0.25, 0.49609375, 0.25, 0.5},
["PRIEST"] = {0.49609375, 0.7421875, 0.25, 0.5},
["WARLOCK"] = {0.7421875, 0.98828125, 0.25, 0.5},
["PALADIN"] = {0, 0.25, 0.5, 0.75},
["DEATHKNIGHT"] = {0.25, .5, 0.5, .75},
["MONK"] = {0.5, 0.73828125, 0.5, .75},
["DEMONHUNTER"] = {0.7421875, 0.98828125, 0.5, 0.75},
};
function SetClampedTextureRotation(texture, rotationDegrees)
if (rotationDegrees ~= 0 and rotationDegrees ~= 90 and rotationDegrees ~= 180 and rotationDegrees ~= 270) then
error("SetRotation: rotationDegrees must be 0, 90, 180, or 270");
return;
end
if not (texture.rotationDegrees) then
texture.origTexCoords = {texture:GetTexCoord()};
texture.origWidth = texture:GetWidth();
texture.origHeight = texture:GetHeight();
end
if (texture.rotationDegrees == rotationDegrees) then
return;
end
texture.rotationDegrees = rotationDegrees;
if (rotationDegrees == 0 or rotationDegrees == 180) then
texture:SetWidth(texture.origWidth);
texture:SetHeight(texture.origHeight);
else
texture:SetWidth(texture.origHeight);
texture:SetHeight(texture.origWidth);
end
if (rotationDegrees == 0) then
texture:SetTexCoord( texture.origTexCoords[1], texture.origTexCoords[2],
texture.origTexCoords[3], texture.origTexCoords[4],
texture.origTexCoords[5], texture.origTexCoords[6],
texture.origTexCoords[7], texture.origTexCoords[8] );
elseif (rotationDegrees == 90) then
texture:SetTexCoord( texture.origTexCoords[3], texture.origTexCoords[4],
texture.origTexCoords[7], texture.origTexCoords[8],
texture.origTexCoords[1], texture.origTexCoords[2],
texture.origTexCoords[5], texture.origTexCoords[6] );
elseif (rotationDegrees == 180) then
texture:SetTexCoord( texture.origTexCoords[7], texture.origTexCoords[8],
texture.origTexCoords[5], texture.origTexCoords[6],
texture.origTexCoords[3], texture.origTexCoords[4],
texture.origTexCoords[1], texture.origTexCoords[2] );
elseif (rotationDegrees == 270) then
texture:SetTexCoord( texture.origTexCoords[5], texture.origTexCoords[6],
texture.origTexCoords[1], texture.origTexCoords[2],
texture.origTexCoords[7], texture.origTexCoords[8],
texture.origTexCoords[3], texture.origTexCoords[4] );
end
end
function ClearClampedTextureRotation(texture)
if (texture.rotationDegrees) then
SetClampedTextureRotation(0);
texture.origTexCoords = nil;
texture.origWidth = nil;
texture.origHeight = nil;
end
end
function GetTexCoordsByGrid(xOffset, yOffset, textureWidth, textureHeight, gridWidth, gridHeight)
local widthPerGrid = gridWidth/textureWidth;
local heightPerGrid = gridHeight/textureHeight;
return (xOffset-1)*widthPerGrid, (xOffset)*widthPerGrid, (yOffset-1)*heightPerGrid, (yOffset)*heightPerGrid;
end
function GetTexCoordsForRole(role)
local textureHeight, textureWidth = 256, 256;
local roleHeight, roleWidth = 67, 67;
if ( role == "GUIDE" ) then
return GetTexCoordsByGrid(1, 1, textureWidth, textureHeight, roleWidth, roleHeight);
elseif ( role == "TANK" ) then
return GetTexCoordsByGrid(1, 2, textureWidth, textureHeight, roleWidth, roleHeight);
elseif ( role == "HEALER" ) then
return GetTexCoordsByGrid(2, 1, textureWidth, textureHeight, roleWidth, roleHeight);
elseif ( role == "DAMAGER" ) then
return GetTexCoordsByGrid(2, 2, textureWidth, textureHeight, roleWidth, roleHeight);
else
error("Unknown role: "..tostring(role));
end
end
function ConvertPixelsToUI(pixels, frameScale)
local physicalScreenHeight = select(2, GetPhysicalScreenSize());
return (pixels * 768.0)/(physicalScreenHeight * frameScale);
end
function ReloadUI()
C_UI.Reload();
end
function tDeleteItem(tbl, item)
local index = 1;
while tbl[index] do
if ( item == tbl[index] ) then
tremove(tbl, index);
else
index = index + 1;
end
end
end
function tIndexOf(tbl, item)
for i, v in ipairs(tbl) do
if item == v then
return i;
end
end
end
function tContains(tbl, item)
return tIndexOf(tbl, item) ~= nil;
end
-- This is a deep compare on the values of the table (based on depth) but not a deep comparison
-- of the keys, as this would be an expensive check and won't be necessary in most cases.
function tCompare(lhsTable, rhsTable, depth)
depth = depth or 1;
for key, value in pairs(lhsTable) do
if type(value) == "table" then
local rhsValue = rhsTable[key];
if type(rhsValue) ~= "table" then
return false;
end
if depth > 1 then
if not tCompare(value, rhsValue, depth - 1) then
return false;
end
end
elseif value ~= rhsTable[key] then
return false;
end
end
-- Check for any keys that are in rhsTable and not lhsTable.
for key, value in pairs(rhsTable) do
if lhsTable[key] == nil then
return false;
end
end
return true;
end
function tInvert(tbl)
local inverted = {};
for k, v in pairs(tbl) do
inverted[v] = k;
end
return inverted;
end
function tFilter(tbl, pred, isIndexTable)
local out = {};
if (isIndexTable) then
local currentIndex = 1;
for i, v in ipairs(tbl) do
if (pred(v)) then
out[currentIndex] = v;
currentIndex = currentIndex + 1;
end
end
else
for k, v in pairs(tbl) do
if (pred(v)) then
out[k] = v;
end
end
end
return out;
end
function tAppendAll(table, addedArray)
for i, element in ipairs(addedArray) do
tinsert(table, element);
end
end
function CopyTable(settings)
local copy = {};
for k, v in pairs(settings) do
if ( type(v) == "table" ) then
copy[k] = CopyTable(v);
else
copy[k] = v;
end
end
return copy;
end
function FindInTableIf(tbl, pred)
for k, v in pairs(tbl) do
if (pred(v)) then
return k, v;
end
end
return nil;
end
function ExtractHyperlinkString(linkString)
local preString, hyperlinkString, postString = linkString:match("^(.*)|H(.+)|h(.*)$");
return preString ~= nil, preString, hyperlinkString, postString;
end
function ExtractLinkData(link)
return string.match(link, "(.-):(.*)");
end
function ExtractQuestRewardID(linkString)
return linkString:match("^questreward:(%d+)$");
end
function SplitTextIntoLines(text, delimiter)
local lines = {};
local startIndex = 1;
local foundIndex = string.find(text, delimiter);
while foundIndex do
table.insert(lines, text:sub(startIndex, foundIndex - 1));
startIndex = foundIndex + 2;
foundIndex = string.find(text, delimiter, startIndex);
end
if startIndex <= #text then
table.insert(lines, text:sub(startIndex));
end
return lines;
end
function SplitTextIntoHeaderAndNonHeader(text)
local foundIndex = string.find(text, "|n");
if not foundIndex then
-- There was no newline...the whole thing is a header
return text;
elseif #text == 2 then
-- There was a newline, but that was all that was in the string.
return nil;
elseif foundIndex == 1 then
-- There was a newline at the very beginning...the whole rest of the string is a header
return text:sub(3);
elseif foundIndex == #text - 1 then
-- There was a newline at the very end...the whole rest of the string is a header
return text:sub(1, foundIndex - 1);
else
-- There was a newline somewhere in the middle...everything before it is the header and everything after it is the non-header
return text:sub(1, foundIndex - 1), text:sub(foundIndex + 2);
end
end
function GetItemInfoFromHyperlink(link)
local strippedItemLink, itemID = link:match("|Hitem:((%d+).-)|h");
if itemID then
return tonumber(itemID), strippedItemLink;
end
end
function GetAchievementInfoFromHyperlink(link)
return tonumber(link:match("|Hachievement:(%d+)"));
end
function FormatValueWithSign(value)
local formatString = value < 0 and SYMBOLIC_NEGATIVE_NUMBER or SYMBOLIC_POSITIVE_NUMBER;
return formatString:format(math.abs(value));
end
function GetPlayerGuid()
return UnitGUID("player");
end
function IsPlayerGuid(guid)
return guid == GetPlayerGuid();
end
function FormatLargeNumber(amount)
amount = tostring(amount);
local newDisplay = "";
local strlen = amount:len();
--Add each thing behind a comma
for i=4, strlen, 3 do
newDisplay = LARGE_NUMBER_SEPERATOR..amount:sub(-(i - 1), -(i - 3))..newDisplay;
end
--Add everything before the first comma
newDisplay = amount:sub(1, (strlen % 3 == 0) and 3 or (strlen % 3))..newDisplay;
return newDisplay;
end
COPPER_PER_SILVER = 100;
SILVER_PER_GOLD = 100;
COPPER_PER_GOLD = COPPER_PER_SILVER * SILVER_PER_GOLD;
function GetMoneyString(money, separateThousands)
local goldString, silverString, copperString;
local gold = floor(money / (COPPER_PER_SILVER * SILVER_PER_GOLD));
local silver = floor((money - (gold * COPPER_PER_SILVER * SILVER_PER_GOLD)) / COPPER_PER_SILVER);
local copper = mod(money, COPPER_PER_SILVER);
if ( ENABLE_COLORBLIND_MODE == "1" ) then
if (separateThousands) then
goldString = FormatLargeNumber(gold)..GOLD_AMOUNT_SYMBOL;
else
goldString = gold..GOLD_AMOUNT_SYMBOL;
end
silverString = silver..SILVER_AMOUNT_SYMBOL;
copperString = copper..COPPER_AMOUNT_SYMBOL;
else
if (separateThousands) then
goldString = GOLD_AMOUNT_TEXTURE_STRING:format(FormatLargeNumber(gold), 0, 0);
else
goldString = GOLD_AMOUNT_TEXTURE:format(gold, 0, 0);
end
silverString = SILVER_AMOUNT_TEXTURE:format(silver, 0, 0);
copperString = COPPER_AMOUNT_TEXTURE:format(copper, 0, 0);
end
local moneyString = "";
local separator = "";
if ( gold > 0 ) then
moneyString = goldString;
separator = " ";
end
if ( silver > 0 ) then
moneyString = moneyString..separator..silverString;
separator = " ";
end
if ( copper > 0 or moneyString == "" ) then
moneyString = moneyString..separator..copperString;
end
return moneyString;
end
function Lerp(startValue, endValue, amount)
return (1 - amount) * startValue + amount * endValue;
end
function Clamp(value, min, max)
if value > max then
return max;
elseif value < min then
return min;
end
return value;
end
function Saturate(value)
return Clamp(value, 0.0, 1.0);
end
function Wrap(value, max)
return (value - 1) % max + 1;
end
function ClampDegrees(value)
return ClampMod(value, 360);
end
function ClampMod(value, mod)
return ((value % mod) + mod) % mod;
end
function NegateIf(value, condition)
return condition and -value or value;
end
function PercentageBetween(value, startValue, endValue)
if startValue == endValue then
return 0.0;
end
return (value - startValue) / (endValue - startValue);
end
function ClampedPercentageBetween(value, startValue, endValue)
return Saturate(PercentageBetween(value, startValue, endValue));
end
local TARGET_FRAME_PER_SEC = 60.0;
function DeltaLerp(startValue, endValue, amount, timeSec)
return Lerp(startValue, endValue, Saturate(amount * timeSec * TARGET_FRAME_PER_SEC));
end
function FrameDeltaLerp(startValue, endValue, amount)
return DeltaLerp(startValue, endValue, amount, GetTickTime());
end
function RandomFloatInRange(minValue, maxValue)
return Lerp(minValue, maxValue, math.random());
end
function GetNavigationButtonEnabledStates(count, index)
-- Returns indicate whether navigation for "previous" and "next" should be enabled, respectively.
if count > 1 then
return index > 1, index < count;
end
return false, false;
end
----------------------------------
-- TRIAL/VETERAN FUNCTIONS
----------------------------------
function GameLimitedMode_IsActive()
return IsTrialAccount() or IsVeteranTrialAccount();
end
function TriStateCheckbox_SetState(checked, checkButton)
local checkedTexture = _G[checkButton:GetName().."CheckedTexture"];
if ( not checkedTexture ) then
message("Can't find checked texture");
end
if ( not checked or checked == 0 ) then
-- nil or 0 means not checked
checkButton:SetChecked(false);
checkButton.state = 0;
elseif ( checked == 2 ) then
-- 2 is a normal
checkButton:SetChecked(true);
checkedTexture:SetVertexColor(1, 1, 1);
checkedTexture:SetDesaturated(false);
checkButton.state = 2;
else
-- 1 is a gray check
checkButton:SetChecked(true);
checkedTexture:SetDesaturated(true);
checkButton.state = 1;
end
end
RectangleMixin = {};
function CreateRectangle(left, right, top, bottom)
local rectangle = CreateFromMixins(RectangleMixin);
rectangle:OnLoad(left, right, top, bottom);
return rectangle;
end
function RectangleMixin:OnLoad(left, right, top, bottom)
self:SetSides(left or 0.0, right or 0.0, top or 0.0, bottom or 0.0);
end
function RectangleMixin:SetSides(left, right, top, bottom)
self.left = left;
self.right = right;
self.top = top;
self.bottom = bottom;
end
function RectangleMixin:Reset()
self.left = 0.0;
self.right = 0.0;
self.top = 0.0;
self.bottom = 0.0;
end
function RectangleMixin:Stretch(x, y)
self:Adjust(-x, x, -y, y);
end
function RectangleMixin:Move(x, y)
self:Adjust(x, x, y, y);
end
function RectangleMixin:Adjust(left, right, top, bottom)
self.left = self.left + left;
self.right = self.right + right;
self.top = self.top + top;
self.bottom = self.bottom + bottom;
end
function RectangleMixin:IsEmpty()
return self.left == self.right or self.top == self.bottom;
end
function RectangleMixin:IsInsideOut()
return self.left > self.right or self.top > self.bottom;
end
function RectangleMixin:EnclosesPoint(x, y)
return x >= self.left and x <= self.right and y >= self.top and y <= self.bottom;
end
function RectangleMixin:EnclosesRect(otherRect)
return self:EnclosesPoint(otherRect:GetLeft(), otherRect:GetTop()) and self:EnclosesPoint(otherRect:GetRight(), otherRect:GetBottom());
end
function RectangleMixin:IntersectsRect(otherRect)
return not (
self.left > otherRect.right or
self.right < otherRect.left or
self.top > otherRect.bottom or
self.bottom < otherRect.top
);
end
function RectangleMixin:GetTop()
return self.top;
end
function RectangleMixin:GetBottom()
return self.bottom;
end
function RectangleMixin:GetLeft()
return self.left;
end
function RectangleMixin:GetRight()
return self.right;
end
function RectangleMixin:GetWidth()
return self.right - self.left;
end
function RectangleMixin:GetHeight()
return self.bottom - self.top;
end
function RectangleMixin:GetCenter()
return Lerp(self.left, self.right, .5), Lerp(self.top, self.bottom, .5);
end
function RectangleMixin:SetTop(top)
self.top = top;
end
function RectangleMixin:SetBottom(bottom)
self.bottom = bottom;
end
function RectangleMixin:SetLeft(left)
self.left = left;
end
function RectangleMixin:SetRight(right)
self.right = right;
end
function RectangleMixin:SetWidth(width)
self.right = self.left + width;
end
function RectangleMixin:SetHeight(height)
self.bottom = self.top + height;
end
function RectangleMixin:SetSize(width, height)
self:SetWidth(width);
self:SetHeight(height);
end
function RectangleMixin:SetCenter(x, y)
local width = self:GetWidth();
local height = self:GetHeight();
self.left = x - width * .5;
self.right = x + width * .5;
self.top = y - height * .5;
self.bottom = y + height * .5;
end
local g_updatingBars = {};
local function IsCloseEnough(bar, newValue, targetValue)
local min, max = bar:GetMinMaxValues();
local range = max - min;
if range > 0.0 then
return math.abs((newValue - targetValue) / range) < .00001;
end
return true;
end
local function ProcessSmoothStatusBars()
for bar, targetValue in pairs(g_updatingBars) do
local effectiveTargetValue = Clamp(targetValue, bar:GetMinMaxValues());
local newValue = FrameDeltaLerp(bar:GetValue(), effectiveTargetValue, .25);
if IsCloseEnough(bar, newValue, effectiveTargetValue) then
g_updatingBars[bar] = nil;
bar:SetValue(effectiveTargetValue);
else
bar:SetValue(newValue);
end
end
end
C_Timer.NewTicker(0, ProcessSmoothStatusBars);
SmoothStatusBarMixin = {};
function SmoothStatusBarMixin:ResetSmoothedValue(value) --If nil, tries to set to the last target value
local targetValue = g_updatingBars[self];
if targetValue then
g_updatingBars[self] = nil;
self:SetValue(value or targetValue);
elseif value then
self:SetValue(value);
end
end
function SmoothStatusBarMixin:SetSmoothedValue(value)
g_updatingBars[self] = value;
end
function SmoothStatusBarMixin:SetMinMaxSmoothedValue(min, max)
self:SetMinMaxValues(min, max);
local targetValue = g_updatingBars[self];
if targetValue then
local ratio = 1;
if max ~= 0 and self.lastSmoothedMax and self.lastSmoothedMax ~= 0 then
ratio = max / self.lastSmoothedMax;
end
g_updatingBars[self] = targetValue * ratio;
end
self.lastSmoothedMin = min;
self.lastSmoothedMax = max;
end
function WrapTextInColorCode(text, colorHexString)
return ("|c%s%s|r"):format(colorHexString, text);
end
ColorMixin = {};
function CreateColor(r, g, b, a)
local color = CreateFromMixins(ColorMixin);
color:OnLoad(r, g, b, a);
return color;
end
local function ExtractColorValueFromHex(str, index)
return tonumber(str:sub(index, index + 1), 16) / 255;
end
function CreateColorFromHexString(hexColor)
if #hexColor == 8 then
local a, r, g, b = ExtractColorValueFromHex(hexColor, 1), ExtractColorValueFromHex(hexColor, 3), ExtractColorValueFromHex(hexColor, 5), ExtractColorValueFromHex(hexColor, 7);
return CreateColor(r, g, b, a);
else
GMError("CreateColorFromHexString input must be hexadecimal digits in this format: AARRGGBB.");
end
end
function CreateColorFromBytes(r, g, b, a)
return CreateColor(r / 255, g / 255, b / 255, a / 255);
end
function AreColorsEqual(left, right)
if left and right then
return left:IsEqualTo(right);
end
return left == right;
end
function ColorMixin:OnLoad(r, g, b, a)
self:SetRGBA(r, g, b, a);
end
function ColorMixin:IsEqualTo(otherColor)
return self.r == otherColor.r
and self.g == otherColor.g
and self.b == otherColor.b
and self.a == otherColor.a;
end
function ColorMixin:GetRGB()
return self.r, self.g, self.b;
end
function ColorMixin:GetRGBAsBytes()
return self.r * 255, self.g * 255, self.b * 255;
end
function ColorMixin:GetRGBA()
return self.r, self.g, self.b, self.a;
end
function ColorMixin:GetRGBAAsBytes()
return self.r * 255, self.g * 255, self.b * 255, (self.a or 1) * 255;
end
function ColorMixin:SetRGBA(r, g, b, a)
self.r = r;
self.g = g;
self.b = b;
self.a = a;
end
function ColorMixin:SetRGB(r, g, b)
self:SetRGBA(r, g, b, nil);
end
function ColorMixin:GenerateHexColor()
return ("ff%.2x%.2x%.2x"):format(self:GetRGBAsBytes());
end
function ColorMixin:GenerateHexColorMarkup()
return "|c"..self:GenerateHexColor();
end
function ColorMixin:WrapTextInColorCode(text)
return WrapTextInColorCode(text, self:GenerateHexColor());
end
RAID_CLASS_COLORS = {};
do
local classes = {"HUNTER", "WARLOCK", "PRIEST", "PALADIN", "MAGE", "ROGUE", "DRUID", "SHAMAN", "WARRIOR", "DEATHKNIGHT", "MONK", "DEMONHUNTER"};
for i, className in ipairs(classes) do
RAID_CLASS_COLORS[className] = C_ClassColor.GetClassColor(className);
end
end
for k, v in pairs(RAID_CLASS_COLORS) do
v.colorStr = v:GenerateHexColor();
end
function GetClassColor(classFilename)
local color = RAID_CLASS_COLORS[classFilename];
if color then
return color.r, color.g, color.b, color.colorStr;
end
return 1, 1, 1, "ffffffff";
end
function GetClassColorObj(classFilename)
-- TODO: Remove this, convert everything that's using GetClassColor to use the object instead, then begin using that again
return RAID_CLASS_COLORS[classFilename];
end
function GetClassColoredTextForUnit(unit, text)
local classFilename = select(2, UnitClass(unit));
local color = GetClassColorObj(classFilename);
return color:WrapTextInColorCode(text);
end
function GetFactionColor(factionGroupTag)
return PLAYER_FACTION_COLORS[PLAYER_FACTION_GROUP[factionGroupTag]];
end
-- Time --
function SecondsToClock(seconds, displayZeroHours)
seconds = math.max(seconds, 0);
local hours = math.floor(seconds / 3600);
seconds = seconds - (hours * 3600);
local minutes = math.floor(seconds / 60);
seconds = seconds % 60;
if hours > 0 or displayZeroHours then
return format(HOURS_MINUTES_SECONDS, hours, minutes, seconds);
else
return format(MINUTES_SECONDS, minutes, seconds);
end
end
function SecondsToTime(seconds, noSeconds, notAbbreviated, maxCount, roundUp)
local time = "";
local count = 0;
local tempTime;
seconds = roundUp and ceil(seconds) or floor(seconds);
maxCount = maxCount or 2;
if ( seconds >= 86400 ) then
count = count + 1;
if ( count == maxCount and roundUp ) then
tempTime = ceil(seconds / 86400);
else
tempTime = floor(seconds / 86400);
end
if ( notAbbreviated ) then
time = D_DAYS:format(tempTime);
else
time = DAYS_ABBR:format(tempTime);
end
seconds = mod(seconds, 86400);
end
if ( count < maxCount and seconds >= 3600 ) then
count = count + 1;
if ( time ~= "" ) then
time = time..TIME_UNIT_DELIMITER;
end
if ( count == maxCount and roundUp ) then
tempTime = ceil(seconds / 3600);
else
tempTime = floor(seconds / 3600);
end
if ( notAbbreviated ) then
time = time..D_HOURS:format(tempTime);
else
time = time..HOURS_ABBR:format(tempTime);
end
seconds = mod(seconds, 3600);
end
if ( count < maxCount and seconds >= 60 ) then
count = count + 1;
if ( time ~= "" ) then
time = time..TIME_UNIT_DELIMITER;
end
if ( count == maxCount and roundUp ) then
tempTime = ceil(seconds / 60);
else
tempTime = floor(seconds / 60);
end
if ( notAbbreviated ) then
time = time..D_MINUTES:format(tempTime);
else
time = time..MINUTES_ABBR:format(tempTime);
end
seconds = mod(seconds, 60);
end
if ( count < maxCount and seconds > 0 and not noSeconds ) then
if ( time ~= "" ) then
time = time..TIME_UNIT_DELIMITER;
end
if ( notAbbreviated ) then
time = time..D_SECONDS:format(seconds);
else
time = time..SECONDS_ABBR:format(seconds);
end
end
return time;
end
function SecondsToTimeAbbrev(seconds)
local tempTime;
if ( seconds >= 86400 ) then
tempTime = ceil(seconds / 86400);
return DAY_ONELETTER_ABBR, tempTime;
end
if ( seconds >= 3600 ) then
tempTime = ceil(seconds / 3600);
return HOUR_ONELETTER_ABBR, tempTime;
end
if ( seconds >= 60 ) then
tempTime = ceil(seconds / 60);
return MINUTE_ONELETTER_ABBR, tempTime;
end
return SECOND_ONELETTER_ABBR, seconds;
end
function FormatShortDate(day, month, year)
if (year) then
if (LOCALE_enGB) then
return SHORTDATE_EU:format(day, month, year);
else
return SHORTDATE:format(day, month, year);
end
else
if (LOCALE_enGB) then
return SHORTDATENOYEAR_EU:format(day, month);
else
return SHORTDATENOYEAR:format(day, month);
end
end
end
function Round(value)
if value < 0.0 then
return math.ceil(value - .5);
end
return math.floor(value + .5);
end
function FormatPercentage(percentage, roundToNearestInteger)
if roundToNearestInteger then
percentage = Round(percentage * 100);
else
percentage = percentage * 100;
end
return PERCENTAGE_STRING:format(percentage);
end
function FormatFraction(numerator, denominator)
return GENERIC_FRACTION_STRING:format(numerator, denominator);
end
function CreateTextureMarkup(file, fileWidth, fileHeight, width, height, left, right, top, bottom, xOffset, yOffset)
return ("|T%s:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d|t"):format(
file
, height
, width
, xOffset or 0
, yOffset or 0
, fileWidth
, fileHeight
, left * fileWidth
, right * fileWidth
, top * fileHeight
, bottom * fileHeight
);
end
function CreateAtlasMarkup(atlasName, width, height, offsetX, offsetY)
return ("|A:%s:%d:%d:%d:%d|a"):format(
atlasName
, height or 0
, width or 0
, offsetX or 0
, offsetY or 0
);
end
-- NOTE: Many of the TextureKit functions below use the following parameters
-- If setVisibilityOfRegions is true, the frame will be shown or hidden based on whether the textureKit and atlas element were found
-- If useAtlasSize is true, the frame will be resized to be the same size as the atlas element.
-- Use the constants in TextureKitConstants for both
TextureKitConstants = {
SetVisiblity = true;
DoNotSetVisibility = false;
UseAtlasSize = true;
IgnoreAtlasSize = false;
}
-- Pass in a frame and a table containing parentKeys (on frame) as keys and atlas member names as the values
function SetupAtlasesOnRegions(frame, regionsToAtlases, useAtlasSize)
for region, atlas in pairs(regionsToAtlases) do
if frame[region] then
if frame[region]:GetObjectType() == "StatusBar" then
frame[region]:SetStatusBarAtlas(atlas);
elseif frame[region].SetAtlas then
frame[region]:SetAtlas(atlas, useAtlasSize);
end
end
end
end
function GetFinalNameFromTextureKit(fmt, textureKits)
if type(textureKits) == "table" then
return fmt:format(unpack(textureKits));
else
return fmt:format(textureKits);
end
end
-- Pass in a TextureKit ID, a frame and a formatting string.
-- The TextureKit name will be inserted into fmt (at the first %s). The resulting atlas name will be set on frame
-- Use "%s" for fmt if the TextureKit name is the entire atlas element name
function SetupTextureKitOnFrameByID(textureKitID, frame, fmt, setVisibilityOfRegions, useAtlasSize)