-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGUTIL.lua
More file actions
529 lines (456 loc) · 13.3 KB
/
GUTIL.lua
File metadata and controls
529 lines (456 loc) · 13.3 KB
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
---@class GGUI_GUTIL
GGUI_GUTIL = {}
local GUTIL = GGUI_GUTIL
--- CLASSICS insert
local Object = {}
Object.__index = Object
GUTIL.Object = Object
function Object:new()
end
function Object:extend()
local cls = {}
for k, v in pairs(self) do
if k:find("__") == 1 then
cls[k] = v
end
end
cls.__index = cls
cls.super = self
setmetatable(cls, self)
return cls
end
function Object:implement(...)
for _, cls in pairs({ ... }) do
for k, v in pairs(cls) do
if self[k] == nil and type(v) == "function" then
self[k] = v
end
end
end
end
function Object:is(T)
local mt = getmetatable(self)
while mt do
if mt == T then
return true
end
mt = getmetatable(mt)
end
return false
end
function Object:__tostring()
return "Object"
end
function Object:__call(...)
local obj = setmetatable({}, self)
obj:new(...)
return obj
end
--- CLASSICS END
if not GUTIL then return end
---Returns an item string from an item link if found
---@param itemLink string
---@return string? itemString
function GUTIL:GetItemStringFromLink(itemLink)
return select(3, strfind(itemLink, "|H(.+)|h%["))
end
---Returns the quality of the item based on an item link if the item has a quality
---@param itemLink string
---@return number? qualityID
---@return boolean? simplified
function GUTIL:GetQualityIDFromLink(itemLink)
local simplified = string.find(itemLink, "Quality%-12%-Tier")
local qualityID
if simplified then
qualityID = string.match(itemLink, "Quality%-12%-Tier(%d+)")
else
qualityID = string.match(itemLink, "Quality%-Tier(%d+)")
end
return tonumber(qualityID), simplified and true or false
end
---Returns the tooltip of an item as one string
---@param itemLink string
---@return string text
function GUTIL:GetItemTooltipText(itemLink)
local tooltipData = C_TooltipInfo.GetHyperlink(itemLink)
if not tooltipData then
return ""
end
local tooltipText = ""
for _, line in pairs(tooltipData.lines) do
local lineText = ""
for _, arg in pairs(line.args) do
if arg.stringVal then
lineText = lineText .. arg.stringVal
end
end
tooltipText = tooltipText .. lineText .. "\n"
end
return tooltipText
end
---Finds the first element in the table where findFunc(element) returns true
---@param t any
---@param findFunc any
---@return any? element
---@return any? key
function GUTIL:Find(t, findFunc)
for k, v in pairs(t) do
if findFunc(v) then
return v, k
end
end
return nil
end
-- to concat lists together (behaviour unpredictable with tables that have strings or not ordered numbers as indices)
function GUTIL:Concat(tableList)
local finalList = {}
for _, currentTable in pairs(tableList) do
for _, item in pairs(currentTable) do
table.insert(finalList, item)
end
end
return finalList
end
function GUTIL:ToSet(t)
local set = {}
for k, v in pairs(t) do
if not tContains(set, v) then
table.insert(set, v)
end
end
return set
end
-- options: subTable, isTableList
-- subTable: a subproperty that is a table that is to be mapped instead of the table itself
-- isTableList: if the table only consists of other tables, map each subTable instead
function GUTIL:Map(t, mapFunc, options)
options = options or {}
local mapped = {}
if not options.subTable then
for k, v in pairs(t) do
if options.isTableList then
if type(v) ~= "table" then
error("GUTIL.Map: t contains a nontable element")
end
for subK, subV in pairs(v) do
local mappedValue = mapFunc(subV, subK)
if not mappedValue then
error("GUTIL.Map: Did you forget to return in mapFunc?")
end
table.insert(mapped, mappedValue)
end
else
local mappedValue = mapFunc(v, k)
if mappedValue then
table.insert(mapped, mappedValue)
end
end
end
return mapped
else
for k, v in pairs(t) do
if not v[options.subTable] or type(v[options.subTable]) ~= "table" then
print("GUTIL.Map: given options.subTable is not existing or no table: " .. tostring(v[options.subTable]))
else
for subK, subV in pairs(v[options.subTable]) do
local mappedValue = mapFunc(subV, subK)
if not mappedValue then
error("GUTIL.Map: Did you forget to return in mapFunc?")
end
table.insert(mapped, mappedValue)
end
end
end
return mapped
end
end
function GUTIL:Filter(t, filterFunc)
local filtered = {}
for k, v in pairs(t) do
if filterFunc(v) then
table.insert(filtered, v)
end
end
return filtered
end
function GUTIL:CreateRegistreeForEvents(events)
local registree = CreateFrame("Frame", nil)
registree:SetScript("OnEvent", function(self, event, ...) self[event](self, ...) end)
for _, event in pairs(events) do
registree:RegisterEvent(event)
end
return registree
end
---Validate if a string is of format 100g50s10c
---@param moneyString string
---@return boolean valid
function GUTIL:ValidateMoneyString(moneyString)
-- check if the string matches the pattern
if not string.match(moneyString, "^%d*g?%d*s?%d*c?$") then
return false
end
-- check if the string contains at least one of g, s, or c
if not string.match(moneyString, "[gsc]") then
return false
end
-- check if the string contains multiple g, s, or c
if string.match(moneyString, "g.*g") then
return false
end
if string.match(moneyString, "s.*s") then
return false
end
if string.match(moneyString, "c.*c") then
return false
end
-- check if it ends incorrectly
if string.match(moneyString, "%d$") then
return false
end
-- check if the string contains invalid characters
if string.match(moneyString, "[^%dgsc]") then
return false
end
-- all checks passed, the string is valid
return true
end
---Returns the given copper value as gold, silver and copper seperated, as string formated or as numbers
---@param copperValue number
---@param formatString? boolean
---@return string | number
---@return number?
---@return number?
function GUTIL:GetMoneyValuesFromCopper(copperValue, formatString)
local gold = math.floor(copperValue / 1e4)
local silver = math.floor(copperValue / 100 % 100)
local copper = math.floor(copperValue % 100)
if not formatString then
return gold, silver, copper
else
return gold .. "g " .. silver .. "s " .. copper .. "c"
end
end
---Colorizes a Text based on a color in GUTIL.COLORS (hex with alpha prefix)
---@param text string
---@param color string
---@return string colorizedText
function GUTIL:ColorizeText(text, color)
local startLine = "\124c"
local endLine = "\124r"
return startLine .. color .. text .. endLine
end
---@enum GUTIL.COLORS
GUTIL.COLORS = {
GREEN = "ff00FF00",
RED = "ffFF0000",
DARK_BLUE = "ff2596be",
BRIGHT_BLUE = "ff00ccff",
LEGENDARY = "ffff8000",
EPIC = "ffa335ee",
RARE = "ff0070dd",
UNCOMMON = "ff1eff00",
GREY = "ff9d9d9d",
ARTIFACT = "ffe6cc80",
GOLD = "fffffc01",
SILVER = "ffdadada",
COPPER = "ffc9803c",
PATREON = "ffff424D",
WHISPER = "ffff80ff",
WHITE = "ffffffff",
}
---@enum GUTIL.CLASS_COLORS
GUTIL.CLASS_COLORS = {
WARRIOR = "ffc79c6e", -- #C79C6E
ARMS = "ffc79c6e",
FURY = "ffc79c6e",
PROTECTION = "ffc79c6e",
PALADIN = "fff58cba", -- #F58CBA
HOLY = "fff58cba",
RETRIBUTION = "fff58cba",
PROTECTION_PALADIN = "fff58cba",
HUNTER = "ffabd473", -- #ABD473
BEAST_MASTERY = "ffabd473",
MARKSMANSHIP = "ffabd473",
SURVIVAL = "ffabd473",
ROGUE = "fffff569", -- #FFF569
ASSASSINATION = "fffff569",
OUTLAW = "fffff569",
SUBTLETY = "fffff569",
PRIEST = "ffffffff", -- #FFFFFF
DISCIPLINE = "ffffffff",
HOLY_PRIEST = "ffffffff",
SHADOW = "ffffffff",
DEATHKNIGHT = "ffc41f3b", -- #C41F3B
BLOOD = "ffc41f3b",
FROST = "ffc41f3b",
UNHOLY = "ffc41f3b",
SHAMAN = "ff0070de", -- #0070DE
ELEMENTAL = "ff0070de",
ENHANCEMENT = "ff0070de",
RESTORATION = "ff0070de",
MAGE = "ff69ccf0", -- #69CCF0
ARCANE = "ff69ccf0",
FIRE = "ff69ccf0",
FROST_MAGE = "ff69ccf0",
WARLOCK = "ff9482c9", -- #9482C9
AFFLICTION = "ff9482c9",
DEMONOLOGY = "ff9482c9",
DESTRUCTION = "ff9482c9",
MONK = "ff00ff96", -- #00FF96
BREWMASTER = "ff00ff96",
MISTWEAVER = "ff00ff96",
WINDWALKER = "ff00ff96",
DRUID = "ffff7d0a", -- #FF7D0A
BALANCE = "ffff7d0a",
FERAL = "ffff7d0a",
GUARDIAN = "ffff7d0a",
RESTORATION_DRUID = "ffff7d0a",
DEMONHUNTER = "ffa330c9", -- #A330C9
HAVOC = "ffa330c9",
VENGEANCE = "ffa330c9",
EVOKER = "ff33937f", -- #33937F
AUGMENTATION = "ff33937f",
DEVASTATION = "ff33937f",
PRESERVATION = "ff33937f",
}
-- Thanks to arkinventory
function GUTIL:StripColor(text)
local text = text or ""
text = string.gsub(text, "|c%x%x%x%x%x%x%x%x", "")
text = string.gsub(text, "|c%x%x %x%x%x%x%x", "") -- the trading parts colour has a space instead of a zero for some weird reason
text = string.gsub(text, "|r", "")
return text
end
function GUTIL:FormatMoney(copperValue, useColor, percentRelativeTo)
local absValue = abs(copperValue)
local minusText = ""
local color = GUTIL.COLORS.GREEN
local percentageText = ""
if percentRelativeTo then
local oneP = percentRelativeTo / 100
local percent = GUTIL:Round(copperValue / oneP, 0)
if oneP == 0 then
percent = 0
end
percentageText = " (" .. percent .. "%)"
end
if copperValue < 0 then
minusText = "-"
color = GUTIL.COLORS.RED
end
if useColor then
return GUTIL:ColorizeText(minusText .. C_CurrencyInfo.GetCoinTextureString(absValue, 10) .. percentageText, color)
else
return minusText .. C_CurrencyInfo.GetCoinTextureString(absValue, 10) .. percentageText
end
end
function GUTIL:Round(number, decimals)
return (("%%.%df"):format(decimals)):format(number)
end
function GUTIL:GetItemIDByLink(hyperlink)
local _, _, foundID = string.find(hyperlink, "item:(%d+)")
return tonumber(foundID)
end
---@param itemList ItemMixin[]
---@param callback function
function GUTIL:ContinueOnAllItemsLoaded(itemList, callback)
local itemsToLoad = #itemList
if itemsToLoad == 0 then
callback()
end
local itemLoaded = function()
itemsToLoad = itemsToLoad - 1
if itemsToLoad <= 0 then
callback()
end
end
if itemsToLoad >= 1 then
for _, itemToLoad in pairs(itemList) do
itemToLoad:ContinueOnItemLoad(itemLoaded)
end
end
end
function GUTIL:EquipItemByLink(link)
for bag = BANK_CONTAINER, NUM_BAG_SLOTS + NUM_BANKBAGSLOTS do
for slot = 1, C_Container.GetContainerNumSlots(bag) do
local item = C_Container.GetContainerItemLink(bag, slot)
if item and item == link then
if CursorHasItem() or CursorHasMoney() or CursorHasSpell() then ClearCursor() end
C_Container.PickupContainerItem(bag, slot)
AutoEquipCursorItem()
return true
end
end
end
end
function GUTIL:isItemSoulbound(itemID)
return select(14, C_Item.GetItemInfo(itemID)) == 1
end
--> GGUI or keep here?
function GUTIL:GetQualityIconString(qualityID, sizeX, sizeY, offsetX, offsetY)
return CreateAtlasMarkup("Professions-Icon-Quality-Tier" .. qualityID, sizeX, sizeY, offsetX, offsetY)
end
function GUTIL:Count(t, func)
local count = 0
for _, v in pairs(t) do
if func and func(v) then
count = count + 1
elseif not func then
count = count + 1
end
end
return count
end
function GUTIL:Sort(t, compFunc)
local sorted = {}
for _, item in pairs(t) do
if sorted[1] == nil then
table.insert(sorted, item)
else
local inserted = false
for sortedIndex, sortedItem in pairs(sorted) do
if compFunc(item, sortedItem) then
table.insert(sorted, sortedIndex, item)
inserted = true
break
end
end
if not inserted then
table.insert(sorted, item)
end
end
end
return sorted
end
---@generic K
---@generic V
---@param t table<K, V>
---@param initialValue any
---@param foldFunction fun(foldValue: any, nextElement: V, key: K): any
function GUTIL:Fold(t, initialValue, foldFunction)
local accumulator = initialValue
for key, value in pairs(t) do
accumulator = foldFunction(accumulator, value, key)
end
return accumulator
end
function GUTIL:IconToText(iconPath, height, width)
if not width then
return "\124T" .. iconPath .. ":" .. height .. "\124t"
else
return "\124T" .. iconPath .. ":" .. height .. ":" .. width .. "\124t"
end
end
function GUTIL:ValidateNumberString(str, min, max, allowDecimals)
local num = tonumber(str)
if num == nil then
return false -- Not a valid number
end
if not allowDecimals and num ~= math.floor(num) then
return false -- Decimals not allowed
end
if (min and num < min) or (max and num > max) then
return false -- Outside specified range
end
return true -- Valid number within range
end