forked from enderneko/Cell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.lua
1866 lines (1639 loc) · 52 KB
/
Utils.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 _, Cell = ...
local L = Cell.L
local F = Cell.funcs
Cell.vars.playerFaction = UnitFactionGroup("player")
-------------------------------------------------
-- game version
-------------------------------------------------
Cell.isAsian = LOCALE_zhCN or LOCALE_zhTW or LOCALE_koKR
Cell.isRetail = WOW_PROJECT_ID == WOW_PROJECT_MAINLINE
Cell.isClassic = WOW_PROJECT_ID == WOW_PROJECT_CLASSIC
-- Cell.isBCC = WOW_PROJECT_ID == WOW_PROJECT_BURNING_CRUSADE_CLASSIC and LE_EXPANSION_LEVEL_CURRENT == LE_EXPANSION_BURNING_CRUSADE
-- Cell.isWrath = WOW_PROJECT_ID == WOW_PROJECT_WRATH_CLASSIC and LE_EXPANSION_LEVEL_CURRENT == LE_EXPANSION_WRATH_OF_THE_LICH_KING
Cell.isWrath = WOW_PROJECT_ID == WOW_PROJECT_WRATH_CLASSIC
-------------------------------------------------
-- class
-------------------------------------------------
local localizedClass = {}
FillLocalizedClassList(localizedClass)
local sortedClasses = {}
local classFileToID = {}
local classIDToFile = {}
do
-- WARRIOR = 1,
-- PALADIN = 2,
-- HUNTER = 3,
-- ROGUE = 4,
-- PRIEST = 5,
-- DEATHKNIGHT = 6,
-- SHAMAN = 7,
-- MAGE = 8,
-- WARLOCK = 9,
-- MONK = 10,
-- DRUID = 11,
-- DEMONHUNTER = 12,
-- EVOKER = 13,
for i = 1, GetNumClasses() do --! returns the highest class ID
local classFile = select(2, GetClassInfo(i))
if classFile then --! returns nil for classes that don't exist in Classic.
tinsert(sortedClasses, classFile)
classFileToID[classFile] = i
classIDToFile[i] = classFile
end
end
sort(sortedClasses)
end
function F:GetClassID(classFile)
return classFileToID[classFile]
end
function F:GetLocalizedClassName(classFileOrID)
if type(classFileOrID) == "string" then
return localizedClass[classFileOrID] or classFileOrID
elseif type(classFileOrID) == "number" and classIDToFile[classFileOrID] then
return localizedClass[classIDToFile[classFileOrID]] or classFileOrID
end
return ""
end
function F:IterateClasses()
local i = 0
return function()
i = i + 1
if i <= GetNumClasses() then
return sortedClasses[i], classFileToID[sortedClasses[i]], i
end
end
end
-------------------------------------------------
-- WotLK
-------------------------------------------------
function F:GetActiveTalentInfo()
local which = GetActiveTalentGroup() == 1 and L["Primary Talents"] or L["Secondary Talents"]
local maxPoints = 0
local specName, specIcon, specFileName
for i = 1, GetNumTalentTabs() do
local name, texture, pointsSpent, fileName = GetTalentTabInfo(i)
if pointsSpent > maxPoints then
maxPoints = pointsSpent
specIcon = texture
specFileName = fileName
elseif pointsSpent == maxPoints then
specIcon = 132148
end
end
return which, specIcon, specFileName
end
-- local specRoles = {
-- ["DeathKnightBlood"] = "DAMAGER",
-- ["DeathKnightFrost"] = "TANK",
-- ["DeathKnightUnholy"] = "DAMAGER",
-- ["DruidRestoration"] = "HEALER",
-- ["DruidBalance"] = "DAMAGER",
-- -- ["DruidFeralCombat"] = nil,
-- ["HunterBeastMastery"] = "DAMAGER",
-- ["HunterSurvival"] = "DAMAGER",
-- ["HunterMarksmanship"] = "DAMAGER",
-- ["MageFrost"] = "DAMAGER",
-- ["MageArcane"] = "DAMAGER",
-- ["MageFire"] = "DAMAGER",
-- ["PaladinHoly"] = "HEALER",
-- ["PaladinCombat"] = "DAMAGER",
-- ["PaladinProtection"] = "TANK",
-- ["PriestShadow"] = "DAMAGER",
-- ["PriestHoly"] = "HEALER",
-- ["PriestDiscipline"] = "HEALER",
-- ["RogueCombat"] = "DAMAGER",
-- ["RogueSubtlety"] = "DAMAGER",
-- ["RogueAssassination"] = "DAMAGER",
-- ["ShamanElementalCombat"] = "DAMAGER",
-- ["ShamanEnhancement"] = "DAMAGER",
-- ["ShamanRestoration"] = "HEALER",
-- ["WarlockSummoning"] = "DAMAGER",
-- ["WarlockDestruction"] = "DAMAGER",
-- ["WarlockCurses"] = "DAMAGER",
-- ["WarriorArms"] = "DAMAGER",
-- ["WarriorFury"] = "DAMAGER",
-- ["WarriorProtection"] = "TANK",
-- }
-- function F:GetPlayerRole()
-- end
-------------------------------------------------
-- color
-------------------------------------------------
function F:ConvertRGB(r, g, b, desaturation)
if not desaturation then desaturation = 1 end
r = r / 255 * desaturation
g = g / 255 * desaturation
b = b / 255 * desaturation
return r, g, b
end
function F:ConvertRGB_256(r, g, b)
return floor(r * 255), floor(g * 255), floor(b * 255)
end
function F:ConvertRGBToHEX(r, g, b)
local result = ""
for key, value in pairs({r, g, b}) do
local hex = ""
while(value > 0)do
local index = math.fmod(value, 16) + 1
value = math.floor(value / 16)
hex = string.sub("0123456789ABCDEF", index, index) .. hex
end
if(string.len(hex) == 0)then
hex = "00"
elseif(string.len(hex) == 1)then
hex = "0" .. hex
end
result = result .. hex
end
return result
end
function F:ConvertHEXToRGB(hex)
hex = hex:gsub("#","")
return tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6))
end
-- https://wowpedia.fandom.com/wiki/ColorGradient
function F:ColorGradient(perc, r1,g1,b1, r2,g2,b2, r3,g3,b3)
perc = perc or 1
if perc >= 1 then
return r3, g3, b3
elseif perc <= 0 then
return r1, g1, b1
end
local segment, relperc = math.modf(perc * 2)
local rr1, rg1, rb1, rr2, rg2, rb2 = select((segment * 3) + 1, r1,g1,b1, r2,g2,b2, r3,g3,b3)
return rr1 + (rr2 - rr1) * relperc, rg1 + (rg2 - rg1) * relperc, rb1 + (rb2 - rb1) * relperc
end
--! From ColorPickerAdvanced by Feyawen-Llane
--[[ Convert RGB to HSV ---------------------------------------------------
Inputs:
r = Red [0, 1]
g = Green [0, 1]
b = Blue [0, 1]
Outputs:
H = Hue [0, 360]
S = Saturation [0, 1]
B = Brightness [0, 1]
]]--
function F:ConvertRGBToHSB(r, g, b)
local colorMax = max(max(r, g), b)
local colorMin = min(min(r, g), b)
local delta = colorMax - colorMin
local H, S, B
-- WoW's LUA doesn't handle floating point numbers very well (Somehow 1.000000 != 1.000000 WTF?)
-- So we do this weird conversion of, Number to String back to Number, to make the IF..THEN work correctly!
colorMax = tonumber(format("%f", colorMax))
r = tonumber(format("%f", r))
g = tonumber(format("%f", g))
b = tonumber(format("%f", b))
if (delta > 0) then
if (colorMax == r) then
H = 60 * (((g - b) / delta) % 6)
elseif (colorMax == g) then
H = 60 * (((b - r) / delta) + 2)
elseif (colorMax == b) then
H = 60 * (((r - g) / delta) + 4)
end
if (colorMax > 0) then
S = delta / colorMax
else
S = 0
end
B = colorMax
else
H = 0
S = 0
B = colorMax
end
if (H < 0) then
H = H + 360
end
return H, S, B
end
--[[ Convert HSB to RGB ---------------------------------------------------
Inputs:
h = Hue [0, 360]
s = Saturation [0, 1]
b = Brightness [0, 1]
Outputs:
R = Red [0,1]
G = Green [0,1]
B = Blue [0,1]
]]--
function F:ConvertHSBToRGB(h, s, b)
local chroma = b * s
local prime = (h / 60) % 6
local X = chroma * (1 - abs((prime % 2) - 1))
local M = b - chroma
local R, G, B
if (0 <= prime) and (prime < 1) then
R = chroma
G = X
B = 0
elseif (1 <= prime) and (prime < 2) then
R = X
G = chroma
B = 0
elseif (2 <= prime) and (prime < 3) then
R = 0
G = chroma
B = X
elseif (3 <= prime) and (prime < 4) then
R = 0
G = X
B = chroma
elseif (4 <= prime) and (prime < 5) then
R = X
G = 0
B = chroma
elseif (5 <= prime) and (prime < 6) then
R = chroma
G = 0
B = X
else
R = 0
G = 0
B = 0
end
R = R + M
G = G + M
B = B + M
return R, G, B
end
-------------------------------------------------
-- number
-------------------------------------------------
local symbol_1K, symbol_10K, symbol_1B
if LOCALE_zhCN then
symbol_1K, symbol_10K, symbol_1B = "千", "万", "亿"
elseif LOCALE_zhTW then
symbol_1K, symbol_10K, symbol_1B = "千", "萬", "億"
elseif LOCALE_koKR then
symbol_1K, symbol_10K, symbol_1B = "천", "만", "억"
end
if Cell.isAsian then
function F:FormatNumber(n)
if abs(n) >= 100000000 then
return string.format("%.3f"..symbol_1B, n/100000000)
elseif abs(n) >= 10000 then
return string.format("%.2f"..symbol_10K, n/10000)
elseif abs(n) >= 1000 then
return string.format("%.1f"..symbol_1K, n/1000)
else
return n
end
end
else
function F:FormatNumber(n)
if abs(n) >= 1000000000 then
return string.format("%.3fB", n/1000000000)
elseif abs(n) >= 1000000 then
return string.format("%.2fM", n/1000000)
elseif abs(n) >= 1000 then
return string.format("%.1fK", n/1000)
else
return n
end
end
end
function F:KeepDecimals(num, n)
if num < 0 then
return -(abs(num) - abs(num) % 0.1 ^ n)
else
return num - num % 0.1 ^ n
end
end
-------------------------------------------------
-- string
-------------------------------------------------
function F:UpperFirst(str, lowerOthers)
if lowerOthers then
str = strlower(str)
end
return (str:gsub("^%l", string.upper))
end
function F:SplitToNumber(sep, str)
if not str then return end
local ret = {strsplit(sep, str)}
for i, v in ipairs(ret) do
ret[i] = tonumber(v) or ret[i] -- keep non number
end
return unpack(ret)
end
local function Chsize(char)
if not char then
return 0
elseif char > 240 then
return 4
elseif char > 225 then
return 3
elseif char > 192 then
return 2
else
return 1
end
end
function F:Utf8sub(str, startChar, numChars)
if not str then return "" end
local startIndex = 1
while startChar > 1 do
local char = string.byte(str, startIndex)
startIndex = startIndex + Chsize(char)
startChar = startChar - 1
end
local currentIndex = startIndex
while numChars > 0 and currentIndex <= #str do
local char = string.byte(str, currentIndex)
currentIndex = currentIndex + Chsize(char)
numChars = numChars -1
end
return str:sub(startIndex, currentIndex - 1)
end
function F:FitWidth(fs, text, alignment)
fs:SetText(text)
if fs:IsTruncated() then
for i = 1, string.utf8len(text) do
if strlower(alignment) == "right" then
fs:SetText("..."..string.utf8sub(text, i))
else
fs:SetText(string.utf8sub(text, i).."...")
end
if not fs:IsTruncated() then
break
end
end
end
end
-------------------------------------------------
-- table
-------------------------------------------------
function F:Getn(t)
local count = 0
for k, v in pairs(t) do
count = count + 1
end
return count
end
function F:GetIndex(t, e)
for i, v in pairs(t) do
if e == v then
return i
end
end
return nil
end
function F:GetKeys(t)
local keys = {}
for k in pairs(t) do
tinsert(keys, k)
end
return keys
end
function F:Copy(t)
local newTbl = {}
for k, v in pairs(t) do
if type(v) == "table" then
newTbl[k] = F:Copy(v)
else
newTbl[k] = v
end
end
return newTbl
end
function F:TContains(t, v)
for _, value in pairs(t) do
if value == v then return true end
end
return false
end
function F:TInsert(t, v)
local i, done = 1
repeat
if not t[i] then
t[i] = v
done = true
end
i = i + 1
until done
end
function F:TRemove(t, v)
for i = #t, 1, -1 do
if t[i] == v then
table.remove(t, i)
end
end
end
function F:TMergeOverwrite(...)
local tbls = {...}
if #tbls == 0 then return {} end
local temp = F:Copy(tbls[1])
for i = 2, #tbls do
for k, v in pairs(tbls[i]) do
temp[k] = v
end
end
return temp
end
function F:RemoveElementsExceptKeys(tbl, ...)
local keys = {}
for _, v in ipairs({...}) do
keys[v] = true
end
for k in pairs(tbl) do
if not keys[k] then
tbl[k] = nil
end
end
end
function F:RemoveElementsByKeys(tbl, ...)
local keys = {}
for _, v in ipairs({...}) do
keys[v] = true
end
for k in pairs(tbl) do
if keys[k] then
tbl[k] = nil
end
end
end
function F:Sort(t, k1, order1, k2, order2, k3, order3)
table.sort(t, function(a, b)
if a[k1] ~= b[k1] then
if order1 == "ascending" then
return a[k1] < b[k1]
else -- "descending"
return a[k1] > b[k1]
end
elseif k2 and order2 and a[k2] ~= b[k2] then
if order2 == "ascending" then
return a[k2] < b[k2]
else -- "descending"
return a[k2] > b[k2]
end
elseif k3 and order3 and a[k3] ~= b[k3] then
if order3 == "ascending" then
return a[k3] < b[k3]
else -- "descending"
return a[k3] > b[k3]
end
end
end)
end
function F:StringToTable(s, sep, convertToNum)
local t = {}
for i, v in pairs({string.split(sep, s)}) do
v = strtrim(v)
if v ~= "" then
if convertToNum then
v = tonumber(v)
if v then tinsert(t, v) end
else
tinsert(t, v)
end
end
end
return t
end
function F:TableToString(t, sep)
return table.concat(t, sep)
end
function F:ConvertTable(t)
local temp = {}
for k, v in ipairs(t) do
temp[v] = k
end
return temp
end
local GetSpellInfo = GetSpellInfo
function F:ConvertSpellTable(t, convertIdToName)
if not convertIdToName then
return F:ConvertTable(t)
end
local temp = {}
for k, v in ipairs(t) do
local name = GetSpellInfo(v)
if name then
temp[name] = k
end
end
return temp
end
function F:ConvertSpellDurationTable(t, convertIdToName)
local temp = {}
for _, v in ipairs(t) do
local id, duration = strsplit(":", v)
local name = GetSpellInfo(id)
if name then
if convertIdToName then
temp[name] = tonumber(duration)
else
temp[tonumber(id)] = tonumber(duration)
end
end
end
return temp
end
function F:CheckTableRemoved(previous, after)
local aa = {}
local ret = {}
for k,v in pairs(previous) do aa[v] = true end
for k,v in pairs(after) do aa[v] = nil end
for k,v in pairs(previous) do
if aa[v] then
tinsert(ret, v)
end
end
return ret
end
function F:FilterInvalidSpells(t)
if not t then return end
for i = #t, 1, -1 do
local spellId
if type(t[i]) == "number" then
spellId = t[i]
else -- consumables
spellId = t[i][1]
end
if not GetSpellInfo(spellId) then
tremove(t, i)
end
end
end
-------------------------------------------------
-- general
-------------------------------------------------
-- function F:GetRealmName()
-- return string.gsub(GetRealmName(), " ", "")
-- end
function F:UnitFullName(unit)
if not unit or not UnitIsPlayer(unit) then return end
local name = GetUnitName(unit, true)
--? name might be nil in some cases?
if name and not string.find(name, "-") then
local server = GetNormalizedRealmName()
--? server might be nil in some cases?
if server then
name = name.."-"..server
end
end
return name
end
function F:ToShortName(fullName)
if not fullName then return "" end
local shortName = strsplit("-", fullName)
return shortName
end
function F:FormatTime(s)
if s >= 3600 then
return "%dh", ceil(s / 3600)
elseif s >= 60 then
return "%dm", ceil(s / 60)
end
return "%ds", floor(s)
end
-- function F:SecondsToTime(seconds)
-- local m = seconds / 60
-- local s = seconds % 60
-- return format("%d:%02d", m, s)
-- end
local SEC = _G.SPELL_DURATION_SEC
local MIN = _G.SPELL_DURATION_MIN
local PATTERN_SEC
local PATTERN_MIN
if strfind(SEC, "1f") then
PATTERN_SEC = "%.0"
elseif strfind(SEC, "2f") then
PATTERN_SEC = "%.00"
end
if strfind(MIN, "1f") then
PATTERN_MIN = "%.0"
elseif strfind(MIN, "2f") then
PATTERN_MIN = "%.00"
end
function F:SecondsToTime(seconds)
if seconds > 60 then
return gsub(format(MIN, seconds / 60), PATTERN_MIN, "")
else
return gsub(format(SEC, seconds), PATTERN_SEC, "")
end
end
-------------------------------------------------
-- unit buttons
-------------------------------------------------
function F:IterateAllUnitButtons(func, updateCurrentGroupOnly)
-- solo
if not updateCurrentGroupOnly or (updateCurrentGroupOnly and Cell.vars.groupType == "solo") then
for _, b in pairs(Cell.unitButtons.solo) do
func(b)
end
end
-- party
if not updateCurrentGroupOnly or (updateCurrentGroupOnly and Cell.vars.groupType == "party") then
for index, b in pairs(Cell.unitButtons.party) do
if index ~= "units" then
func(b)
end
end
end
-- raid
if not updateCurrentGroupOnly or (updateCurrentGroupOnly and Cell.vars.groupType == "raid") then
for index, header in pairs(Cell.unitButtons.raid) do
if index ~= "units" then
for _, b in ipairs(header) do
func(b)
end
end
end
-- arena pet
for _, b in pairs(Cell.unitButtons.arena) do
func(b)
end
-- raid pet
for index, b in pairs(Cell.unitButtons.raidpet) do
if index ~= "units" then
func(b)
end
end
end
-- npc
for _, b in ipairs(Cell.unitButtons.npc) do
func(b)
end
-- spotlight
for _, b in pairs(Cell.unitButtons.spotlight) do
func(b)
end
end
function F:IterateSharedUnitButtons(func)
-- npc
for _, b in ipairs(Cell.unitButtons.npc) do
func(b)
end
-- spotlight
for _, b in pairs(Cell.unitButtons.spotlight) do
func(b)
end
end
local spotlights = {}
function F:GetUnitButtonByUnit(unit, getSpotlights)
if not unit then return end
local normal
if Cell.vars.groupType == "raid" then
if Cell.vars.inBattleground == 5 then
normal = Cell.unitButtons.raid.units[unit] or Cell.unitButtons.npc.units[unit] or Cell.unitButtons.arena[unit]
else
normal = Cell.unitButtons.raid.units[unit] or Cell.unitButtons.npc.units[unit] or Cell.unitButtons.raidpet.units[unit]
end
elseif Cell.vars.groupType == "party" then
normal = Cell.unitButtons.party.units[unit] or Cell.unitButtons.npc.units[unit]
else -- solo
normal = Cell.unitButtons.solo[unit] or Cell.unitButtons.npc.units[unit]
end
if getSpotlights then
wipe(spotlights)
for _, b in pairs(Cell.unitButtons.spotlight) do
if b.state.unit and UnitIsUnit(b.state.unit, unit) then
tinsert(spotlights, b)
end
end
return normal, spotlights
end
return normal
end
function F:GetUnitButtonByGUID(guid, getSpotlights)
return F:GetUnitButtonByUnit(Cell.vars.guids[guid], getSpotlights)
end
function F:GetUnitButtonByName(name, getSpotlights)
return F:GetUnitButtonByUnit(Cell.vars.names[name], getSpotlights)
end
function F:HandleUnitButton(type, unit, func, ...)
if not unit then return end
if type == "guid" then
unit = Cell.vars.guids[unit]
elseif type == "name" then
unit = Cell.vars.names[unit]
end
if not unit then return end
local handled, normal
if Cell.vars.groupType == "raid" then
if Cell.vars.inBattleground == 5 then
normal = Cell.unitButtons.raid.units[unit] or Cell.unitButtons.npc.units[unit] or Cell.unitButtons.arena[unit]
else
normal = Cell.unitButtons.raid.units[unit] or Cell.unitButtons.npc.units[unit] or Cell.unitButtons.raidpet.units[unit]
end
elseif Cell.vars.groupType == "party" then
normal = Cell.unitButtons.party.units[unit] or Cell.unitButtons.npc.units[unit]
else -- solo
normal = Cell.unitButtons.solo[unit] or Cell.unitButtons.npc.units[unit]
end
if normal then
func(normal, ...)
handled = true
end
for _, b in pairs(Cell.unitButtons.spotlight) do
if b.state.unit and UnitIsUnit(b.state.unit, unit) then
func(b, ...)
handled = true
end
end
return handled
end
function F:UpdateTextWidth(fs, text, width, relativeTo)
if not text or not width then return end
if width == "unlimited" then
fs:SetText(text)
elseif width[1] == "percentage" then
local percent = width[2] or 0.75
local width = relativeTo:GetWidth() - 2
for i = string.utf8len(text), 0, -1 do
fs:SetText(string.utf8sub(text, 1, i))
if fs:GetWidth() / width <= percent then
break
end
end
elseif width[1] == "length" then
if Cell.isAsian then
if string.len(text) == string.utf8len(text) then -- en
fs:SetText(string.utf8sub(text, 1, width[3] or width[2]))
else
fs:SetText(string.utf8sub(text, 1, width[2]))
end
else
fs:SetText(string.utf8sub(text, 1, width[2]))
end
end
end
function F:GetMarkEscapeSequence(index)
index = index - 1
local left, right, top, bottom
local coordIncrement = 64 / 256
left = mod(index , 4) * coordIncrement
right = left + coordIncrement
top = floor(index / 4) * coordIncrement
bottom = top + coordIncrement
return string.format("|TInterface\\TargetingFrame\\UI-RaidTargetingIcons:0:0:0:0:64:64:%d:%d:%d:%d|t", left*64, right*64, top*64, bottom*64)
end
-- local scriptObjects = {}
-- local frame = CreateFrame("Frame")
-- frame:RegisterEvent("PLAYER_REGEN_DISABLED")
-- frame:RegisterEvent("PLAYER_REGEN_ENABLED")
-- frame:SetScript("OnEvent", function(self, event)
-- if event == "PLAYER_REGEN_ENABLED" then
-- for _, obj in pairs(scriptObjects) do
-- obj:Show()
-- end
-- else
-- for _, obj in pairs(scriptObjects) do
-- obj:Hide()
-- end
-- end
-- end)
-- function F:SetHideInCombat(obj)
-- tinsert(scriptObjects, obj)
-- end
-------------------------------------------------
-- frame colors
-------------------------------------------------
local RAID_CLASS_COLORS = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS
function F:GetClassColor(class)
if class and class ~= "" and RAID_CLASS_COLORS[class] then
if CUSTOM_CLASS_COLORS then
return CUSTOM_CLASS_COLORS[class].r, CUSTOM_CLASS_COLORS[class].g, CUSTOM_CLASS_COLORS[class].b
else
return RAID_CLASS_COLORS[class]:GetRGB()
end
else
return 1, 1, 1
end
end
function F:GetClassColorStr(class)
if class and class ~= "" and RAID_CLASS_COLORS[class] then
return "|c"..RAID_CLASS_COLORS[class].colorStr
else
return "|cffffffff"
end
end
local function GetPowerColor(unit)
local r, g, b, t
-- https://wow.gamepedia.com/API_UnitPowerType
local powerType, powerToken, altR, altG, altB = UnitPowerType(unit)
t = powerType
local info = PowerBarColor[powerToken]
if powerType == 0 then -- MANA
info = {r=0, g=.5, b=1} -- default mana color is too dark!
elseif powerType == 13 then -- INSANITY
info = {r=.6, g=.2, b=1}
end
if info then
--The PowerBarColor takes priority
r, g, b = info.r, info.g, info.b
else
if not altR then
-- Couldn't find a power token entry. Default to indexing by power type or just mana if we don't have that either.
info = PowerBarColor[powerType] or PowerBarColor["MANA"]
r, g, b = info.r, info.g, info.b
else
r, g, b = altR, altG, altB
end
end
return r, g, b, t
end
function F:GetPowerColor(unit, class)
local r, g, b, lossR, lossG, lossB, t
r, g, b, t = GetPowerColor(unit)
if not Cell.loaded then
return r, g, b, r*0.2, g*0.2, b*0.2, t
end
if CellDB["appearance"]["powerColor"][1] == "power_color_dark" then
lossR, lossG, lossB = r, g, b
r, g, b = r*0.2, g*0.2, b*0.2
elseif CellDB["appearance"]["powerColor"][1] == "class_color" then
r, g, b = F:GetClassColor(class)
lossR, lossG, lossB = r*0.2, g*0.2, b*0.2
elseif CellDB["appearance"]["powerColor"][1] == "custom" then
r, g, b = unpack(CellDB["appearance"]["powerColor"][2])
lossR, lossG, lossB = r*0.2, g*0.2, b*0.2
else
lossR, lossG, lossB = r*0.2, g*0.2, b*0.2
end
return r, g, b, lossR, lossG, lossB, t
end
function F:GetHealthColor(percent, isDeadOrGhost, r, g, b)
if not Cell.loaded then
return r, g, b, r*0.2, g*0.2, b*0.2
end
local barR, barG, barB, lossR, lossG, lossB