-
Notifications
You must be signed in to change notification settings - Fork 24
/
helpers.lua
418 lines (365 loc) · 10.9 KB
/
helpers.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
Helpers = CreateFrame('Frame', nil, UIParent);
local debug = false;
function Helpers.Debug()
return debug;
end
--[[
Removes an element from a table by key.
]]
function Helpers.removekey(table, key)
local element = table[key]
table[key] = nil
return element
end
--[[
Simply returns what percentage the first var is of the second.
]]
function Helpers.to_percentage(first, second)
return (first / second) * 100;
end
--[[
Simply returns the current game build integer.
]]
function Helpers.GetBuild()
return select(4,GetBuildInfo());
end
--[[
Simple check for if we're currently running in a Classic WoW Client.
]]
function Helpers.IsClassic()
return Helpers.GetBuild() <= 19999;
end
function Helpers.IsTBC()
return (not Helpers.IsClassic() and not Helpers.IsRetail());
end
--[[
Simple check for if we're currently running in a Retail WoW Client.
]]
function Helpers.IsRetail()
return Helpers.GetBuild() >= 90000;
end
--[[
Just gets the string for the supported build.
]]
function Helpers.GetSupportedBuild()
if (Helpers.IsRetail()) then
return '9.1.0';
end
if (Helpers.IsTBC()) then
return '2.5.1';
end
if (Helpers.IsClassic()) then
return '1.13.7';
end
end
--[[
Returns a human readable string for the current environment.
]]
function Helpers.GetEnvironment()
if (Helpers.IsRetail()) then
return 'Shadowlands';
end
if (Helpers.IsTBC()) then
return 'The Burning Crusade';
end
if (Helpers.IsClassic()) then
return 'Classic';
end
end
--[[
Just a helper for getting consistent font styles.
]]
function Helpers.get_styled_font(font)
local font = LSM:Fetch('font', font);
local _, _, flags = PlayerFrameHealthBarTextLeft:GetFont();
local r, g, b, a = PlayerFrameHealthBarTextLeft:GetTextColor();
return {
font = font,
flags = flags,
r = r,
g = g,
b = b,
a = a,
};
end
--[[
Just a helper for storing positions in a table.
]]
function Helpers.pack_position(point, relativeTo, relativePoint, xOfs, yOfs)
return {
point = point,
relativeTo = relativeTo,
relativePoint = relativePoint,
x = xOfs,
y = yOfs,
};
end
--[[
To debug exact position of a frame.
]]
function Helpers.debug_position(frame)
local point, relativeTo, relativePoint, xOfs, yOfs = frame:GetPoint();
print(frame:GetName());
print(format('Point: %s', point));
print('Relative To:');
print(Helpers.dump_table(relativeTo));
print(format('Relative Point: %s', relativePoint));
print(format('xOffset: %s', xOfs));
print(format('yOffset: %s', yOfs));
end
--[[
To debug a tables contents.
]]
function Helpers.dump_table(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. Helpers.dump_table(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
--[[
Adds a tooltip to a frame.
]]
function Helpers.add_tooltip(frame, text)
frame:SetScript('OnEnter', function(self)
GameTooltip:SetOwner(frame, 'ANCHOR_TOPLEFT');
GameTooltip:AddLine(text, 1, 1, 0);
GameTooltip:Show();
end);
frame:SetScript('OnLeave', GameTooltip_Hide);
end
--[[
Creates a Draggable Frame.
]]
function Helpers.create_drag_frame(name, width, height, label)
-- Create Drag Frame
local frame = CreateFrame('Frame', name, UIParent);
frame:SetMovable(true);
frame:EnableMouse(true);
-- On Click Set as Moving.
frame:SetScript('OnMouseDown', function(self, button)
if (button == 'LeftButton' and not self.isMoving) then
self:StartMoving();
self.isMoving = true;
end
end);
-- Mouse Released
frame:SetScript('OnMouseUp', function(self, button)
if (button == 'LeftButton' and self.isMoving) then
self:StopMovingOrSizing();
self.isMoving = false;
end
end);
-- Hiding
frame:SetScript('OnHide', function(self)
if ( self.isMoving ) then
self:StopMovingOrSizing();
self.isMoving = false;
end
end);
-- Make the Drag Frame Visible.
frame:SetPoint('CENTER');
frame:SetWidth(width);
frame:SetHeight(height);
local tex = frame:CreateTexture('ARTWORK');
tex:SetAllPoints();
tex:SetTexture(1.0, 0.5, 0);
tex:SetAlpha(0.5);
frame:SetFrameStrata('HIGH');
-- Give it a Label.
Helpers.add_tooltip(frame, label);
-- Hide it
frame:Hide();
return frame;
end
--[[
When passed an RGBA table simply returns it seperated.
]]
function Helpers.colour_unpack(colour)
return colour.r, colour.g, colour.b, colour.a;
end
--[[
Packs seperate RGBA values into a single table.
]]
function Helpers.colour_pack(r, g, b, a)
return {
r = r,
g = g,
b = b,
a = a,
};
end
--[[
Converts a number to a string with comma values
@ param int $number The number we're converting
@
@ return string
]]
function Helpers.format_num(number)
local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
int = int:reverse():gsub("(%d%d%d)", "%1,")
return minus .. int:reverse():gsub("^,", "") .. fraction;
end
--[[
Gets the Unit's class colour in RGB
@ param Unit $unit The unit we're checking
@ return array
]]
function Helpers.GetClassColour(unit)
local _, class = UnitClass(unit);
if (Helpers.IsClassic() and class == 'SHAMAN') then
return Helpers.colour_pack(0.0, 0.44, 0.87, 1);
end
-- If we can find the class then return the class colours.
if (class and RAID_CLASS_COLORS[class]) then
return RAID_CLASS_COLORS[class];
end
-- Under rare circumstances can be nil. Just return white.
return Helpers.colour_pack(1, 1, 1, 1);
end
--[[
Converts an RGB Percentage to Hex - Source: WoWWiki
@ param float $r Red
@ param float $g Green
@ param float $b Blue
@
@ return void
]]
function Helpers.RGBPercToHex(r, g, b)
if (type(r) == "table") then
g = r.g
b = r.b
r = r.r
end
r = r <= 1 and r >= 0 and r or 0
g = g <= 1 and g >= 0 and g or 0
b = b <= 1 and b >= 0 and b or 0
return string.format("%02x%02x%02x", r*255, g*255, b*255)
end
--[[
Applies the unit's class colour to the status bar that is passed in
@ param Frame $statusBar The Status Bar we're tweaking
@ param Unit $unit The unit we're checking
@ return void
]]
function Helpers.ApplyClassColours(statusBar, unit)
if ( UnitIsConnected(unit) and unit == statusBar.unit and UnitClass(unit) ) then
local c = Helpers.GetClassColour(unit);
statusBar:SetStatusBarColor(c.r, c.g, c.b );
end
end
--[[
Helper function for moving a Blizzard frame that has a SetMoveable flag
@ param Frame $frame The Frame we're tweaking
@ param string $anchor The anchoring point of the frame. CENTER, TOP, BOTTOM etc.
@ param Frame $parent Optional Frame to parent to
@ param int $posX Frames new X Position
@ param int $posY Frames new Y Position
@ param float $scale Frames new scale
@ return void
]]
function Helpers.ModifyFrame(frame, anchor, parent, posX, posY, scale)
frame:SetMovable(true);
frame:ClearAllPoints();
if(parent == nil) then frame:SetPoint(anchor, posX, posY) else frame:SetPoint(anchor, parent, posX, posY) end
if(scale ~= nil) then frame:SetScale(scale) end
frame:SetUserPlaced(true);
frame:SetMovable(false);
end
--[[
Helper function for moving a Blizzard frame that does NOT have a SetMoveable flag
@ param Frame $frame The Frame we're tweaking
@ param string $anchor The anchoring point of the frame. CENTER, TOP, BOTTOM etc.
@ param Frame $parent Optional Frame to parent to
@ param int $posX Frames new X Position
@ param int $posY Frames new Y Position
@ param float $scale Frames new scale
@ return void
]]
function Helpers.ModifyBasicFrame(frame, anchor, parent, posX, posY, scale)
frame:ClearAllPoints();
if(parent == nil) then frame:SetPoint(anchor, posX, posY) else frame:SetPoint(anchor, parent, posX, posY) end
if(scale ~= nil) then frame:SetScale(scale) end
end
--[[
Helper function for essentially completely deleting a Frame.
@ param Frame $frame The Frame we're destroying
@ return void
]]
function Helpers.DestroyFrame(frame)
if type(frame) == 'table' and frame.SetScript then
frame:UnregisterAllEvents();
frame:SetScript('OnEvent',nil);
frame:SetScript('OnUpdate',nil);
frame:SetScript('OnHide',nil);
frame:Hide();
frame.SetScript = function() end;
frame.RegisterEvent = function() end;
frame.RegisterAllEvents = function() end;
frame.Show = function() end;
end
end
function round(number, decimals)
return (("%%.%df"):format(decimals)):format(number);
end
--[[
Figures out if provided guid is a Player.
]]
function Helpers.IsPlayerByGUID(guid)
local type, _, _, _, _, _, _ = strsplit("-", guid);
return type == 'Player';
end
--[[
Figures out if provided guid is an NPC.
]]
function Helpers.IsCreatureByGUID(guid)
local type, _, _, _, _, _, _ = strsplit("-", guid);
return type == 'Creature';
end
--[[
Figures out a players faction from their guid.
]]
function Helpers.GetFactionByGUID(guid)
_, _, _, englishRace, _, _, _ = GetPlayerInfoByGUID(guid);
return Helpers.GetFactionByRace(englishRace);
end
--[[
I absolutely hate that that is required. Shoot me.
]]
function Helpers.GetFactionByRace(race)
local horde = 'Horde';
local alliance = 'Alliance';
local unknown = 'Unknown';
-- Edge Cases
if (race == 'Pandaren') then return unknown end
-- Alliance
if (race == 'Human') then return alliance end
if (race == 'Dwarf') then return alliance end
if (race == 'NightElf') then return alliance end
if (race == 'Gnome') then return alliance end
if (race == 'Draenei') then return alliance end
if (race == 'Worgen') then return alliance end
if (race == 'VoidElf') then return alliance end
if (race == 'LightforgedDraenei') then return alliance end
if (race == 'KulTiran') then return alliance end
if (race == 'ThinHuman') then return alliance end
if (race == 'DarkIronDwarf') then return alliance end
if (race == 'Mechagnome') then return alliance end
-- Horde
if (race == 'Orc') then return horde end
if (race == 'Scourge') then return horde end
if (race == 'Tauren') then return horde end
if (race == 'Troll') then return horde end
if (race == 'Goblin') then return horde end
if (race == 'BloodElf') then return horde end
if (race == 'Nightborne') then return horde end
if (race == 'HighmountainTauren') then return horde end
if (race == 'ZandalariTroll') then return horde end
if (race == 'Vulpera') then return horde end
if (race == 'MagharOrc') then return horde end
end