-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLagBar.lua
More file actions
488 lines (412 loc) · 12.7 KB
/
LagBar.lua
File metadata and controls
488 lines (412 loc) · 12.7 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
--LagBar by Xruptor
local ADDON_NAME, addon = ...
if not _G[ADDON_NAME] then
_G[ADDON_NAME] = CreateFrame("Frame", ADDON_NAME, UIParent, BackdropTemplateMixin and "BackdropTemplate")
end
addon = _G[ADDON_NAME]
local L = addon.L
if not L then
L = {}
setmetatable(L, { __index = function(_, k) return k end })
addon.L = L
end
local DEFAULT_CHAT_FRAME = DEFAULT_CHAT_FRAME
local LagBar_SlashCommand
local UPDATE_INTERVAL = 1
local lagBarTooltip = CreateFrame("GameTooltip", "LagBarTooltip", UIParent, "GameTooltipTemplate")
local floor = math.floor
local format = string.format
local max = math.max
local GetFramerate = GetFramerate
local GetNetStats = GetNetStats
local wipe = wipe or function(t) for k in pairs(t) do t[k] = nil end end
local tconcat = table.concat
local unpack = unpack or table.unpack
local MIN_FRAME_WIDTH = 30
local MIN_FRAME_HEIGHT = 25
local TEXT_PADDING_X = 20
local TEXT_PADDING_Y = 10
local PING_THRESHOLDS = { 1000, 500, 250, 100, 0 }
local DEFAULTS = {
bgShown = true,
ttShown = true,
worldping = true,
impdisplay = true,
scale = 1,
fps = true,
homeping = true,
metric = true,
clampToScreen = true,
addonLoginMsg = true,
}
----------------------
-- Color Functions --
----------------------
local function LagBar_GetThresholdPercentage(quality, ...)
local n = select('#', ...)
if n <= 1 then
return LagBar_GetThresholdPercentage(quality, 0, ... or 1)
end
local worst = ...
local best = select(n, ...)
if worst == best and quality == worst then
return 0.5
end
if worst <= best then
if quality <= worst then
return 0
elseif quality >= best then
return 1
end
local last = worst
for i = 2, n-1 do
local value = select(i, ...)
if quality <= value then
return ((i-2) + (quality - last) / (value - last)) / (n-1)
end
last = value
end
local value = select(n, ...)
return ((n-2) + (quality - last) / (value - last)) / (n-1)
else
if quality >= worst then
return 0
elseif quality <= best then
return 1
end
local last = worst
for i = 2, n-1 do
local value = select(i, ...)
if quality >= value then
return ((i-2) + (quality - last) / (value - last)) / (n-1)
end
last = value
end
local value = select(n, ...)
return ((n-2) + (quality - last) / (value - last)) / (n-1)
end
end
--check for infinite
local function isInf(value)
return value == math.huge or value == -math.huge
end
local function LagBar_GetThresholdColor(quality, ...)
if quality ~= quality or isInf(quality) then
return 1, 1, 1
end
local percent = LagBar_GetThresholdPercentage(quality, ...)
if percent <= 0 then
return 1, 0, 0
elseif percent <= 0.5 then
return 1, percent*2, 0
elseif percent >= 1 then
return 0, 1, 0
else
return 2 - percent*2, 1, 0
end
end
local function LagBar_GetThresholdHexColor(quality, ...)
local r, g, b = LagBar_GetThresholdColor(quality, ...)
return format("%02x%02x%02x", r*255, g*255, b*255)
end
----------------------
-- Enable --
----------------------
local function EnsureDefaults()
if not LagBar_DB then LagBar_DB = {} end
for key, value in pairs(DEFAULTS) do
if LagBar_DB[key] == nil then
LagBar_DB[key] = value
end
end
end
function addon:EnableAddon()
EnsureDefaults()
self:DrawGUI()
self:RestoreLayout(ADDON_NAME)
self:BackgroundToggle()
SLASH_LAGBAR1 = "/lagbar"
SlashCmdList["LAGBAR"] = LagBar_SlashCommand
if addon.configFrame then addon.configFrame:EnableConfig() end
if LagBar_DB.addonLoginMsg then
local GetAddonMetadata = (C_AddOns and C_AddOns.GetAddOnMetadata) or GetAddOnMetadata
local ver = (GetAddonMetadata and GetAddonMetadata(ADDON_NAME, "Version")) or "1.0"
DEFAULT_CHAT_FRAME:AddMessage(format("|cFF99CC33%s|r [v|cFF20ff20%s|r] loaded: /lagbar", ADDON_NAME, ver or "1.0"))
end
end
local function normalizeSlashToken(token)
if not token then return nil end
return token:lower()
end
local function matchesSlash(token, key, baseL)
if not token or not key then return false end
local v = L[key]
if type(v) == "string" and token == v:lower() then return true end
if baseL then
local bv = baseL[key]
if type(bv) == "string" and token == bv:lower() then return true end
end
return false
end
LagBar_SlashCommand = function(cmd)
local token, rest = cmd:match("^(%S+)%s*(.-)%s*$")
token = normalizeSlashToken(token)
local baseL = addon.L_enUS
if token then
if matchesSlash(token, "SlashBG", baseL) then
addon.aboutPanel.btnBG.func()
return true
elseif matchesSlash(token, "SlashTT", baseL) then
addon.aboutPanel.btnTT.func()
return true
elseif matchesSlash(token, "SlashReset", baseL) then
addon.aboutPanel.btnReset.func()
return true
elseif matchesSlash(token, "SlashScale", baseL) then
local scalenum = tonumber(rest)
if scalenum and scalenum >= 0.5 and scalenum <= 5 then
addon:SetAddonScale(scalenum)
else
DEFAULT_CHAT_FRAME:AddMessage(L.SlashScaleSetInvalid)
end
return true
elseif matchesSlash(token, "SlashWorldPing", baseL) then
addon.aboutPanel.btnWorldPing.func()
return true
elseif matchesSlash(token, "SlashFPS", baseL) then
addon.aboutPanel.btnFPS.func()
return true
elseif matchesSlash(token, "SlashHomePing", baseL) then
addon.aboutPanel.btnHomePing.func()
return true
elseif matchesSlash(token, "SlashImpDisplay", baseL) then
addon.aboutPanel.btnImpDisplay.func()
return true
elseif matchesSlash(token, "SlashMetricLabels", baseL) then
addon.aboutPanel.btnMetricLabels.func()
return true
end
end
DEFAULT_CHAT_FRAME:AddMessage(ADDON_NAME, 64/255, 224/255, 208/255)
DEFAULT_CHAT_FRAME:AddMessage("/lagbar "..L.SlashReset.." - "..L.SlashResetInfo)
DEFAULT_CHAT_FRAME:AddMessage("/lagbar "..L.SlashBG.." - "..L.SlashBGInfo)
DEFAULT_CHAT_FRAME:AddMessage("/lagbar "..L.SlashTT.." - "..L.SlashTTInfo)
DEFAULT_CHAT_FRAME:AddMessage("/lagbar "..L.SlashFPS.." - "..L.SlashFPSInfo)
DEFAULT_CHAT_FRAME:AddMessage("/lagbar "..L.SlashHomePing.." - "..L.SlashHomePingInfo)
DEFAULT_CHAT_FRAME:AddMessage("/lagbar "..L.SlashWorldPing.." - "..L.SlashWorldPingInfo)
DEFAULT_CHAT_FRAME:AddMessage("/lagbar "..L.SlashImpDisplay.." - "..L.SlashImpDisplayInfo)
DEFAULT_CHAT_FRAME:AddMessage("/lagbar "..L.SlashMetricLabels.." - "..L.SlashMetricLabelsInfo)
DEFAULT_CHAT_FRAME:AddMessage("/lagbar "..L.SlashScale.." # - "..L.SlashScaleInfo)
end
addon:RegisterEvent("ADDON_LOADED")
addon:SetScript("OnEvent", function(self, event, ...)
if event == "ADDON_LOADED" or event == "PLAYER_LOGIN" then
if event == "ADDON_LOADED" then
local arg1 = ...
if arg1 and arg1 == ADDON_NAME then
self:UnregisterEvent("ADDON_LOADED")
self:RegisterEvent("PLAYER_LOGIN")
end
return
end
if IsLoggedIn() then
self:EnableAddon(event, ...)
self:UnregisterEvent("PLAYER_LOGIN")
end
return
end
if self[event] then
return self[event](self, event, ...)
end
end)
local BACKDROP_STYLE = {
bgFile = "Interface\\TutorialFrame\\TutorialFrameBackground",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true, tileSize = 32, edgeSize = 16,
insets = { left = 5, right = 5, top = 5, bottom = 5 },
}
local function ApplyBackdrop(frame, enabled)
if enabled then
frame:SetBackdrop(BACKDROP_STYLE)
frame:SetBackdropBorderColor(0.5, 0.5, 0.5)
frame:SetBackdropColor(0.5, 0.5, 0.5, 0.6)
else
frame:SetBackdrop(nil)
end
end
function addon:DrawGUI()
addon:SetWidth(MIN_FRAME_WIDTH)
addon:SetHeight(MIN_FRAME_HEIGHT)
addon:SetMovable(true)
addon:SetClampedToScreen(LagBar_DB.clampToScreen)
addon:SetAddonScale(LagBar_DB.scale, true)
ApplyBackdrop(addon, LagBar_DB.bgShown)
addon:EnableMouse(true)
local g = addon:CreateFontString("$parentText", "ARTWORK", "GameFontNormalSmall")
g:SetJustifyH("LEFT")
g:SetPoint("CENTER", 0, 0)
g:SetText("")
addon.text = g
addon:SetScript("OnMouseDown", function(self)
if IsShiftKeyDown() then
self.isMoving = true
self:StartMoving()
end
end)
addon:SetScript("OnMouseUp", function(self)
if self.isMoving then
self.isMoving = nil
self:StopMovingOrSizing()
addon:SaveLayout(ADDON_NAME)
end
end)
addon:StartUpdateTicker()
addon:SetScript("OnLeave", function()
lagBarTooltip:Hide()
end)
addon:SetScript("OnEnter", function()
lagBarTooltip:SetOwner(self, "ANCHOR_TOP")
lagBarTooltip:SetPoint(self:GetTipAnchor(addon))
lagBarTooltip:ClearLines()
if LagBar_DB.ttShown then
lagBarTooltip:AddLine(ADDON_NAME)
lagBarTooltip:AddLine(L.TooltipDragInfo, 64/255, 224/255, 208/255)
end
lagBarTooltip:Show()
end)
addon:Show()
end
local parts = {}
local function formatValue(value, hexColor, suffix)
return format("|cff%s%d|r%s", hexColor, value, suffix or "")
end
function addon:UpdateDisplay()
if not LagBar_DB then return end
if not self.text then return end
if not self:IsShown() then return end
local db = LagBar_DB
local metricEnabled = db.metric
local metricFps = metricEnabled and (" " .. (L.FPS or "fps")) or ""
local metricMs = metricEnabled and (" " .. (L.Milliseconds or "ms")) or ""
wipe(parts)
if db.fps then
local framerate = floor(GetFramerate() + 0.5)
parts[#parts + 1] = formatValue(framerate, LagBar_GetThresholdHexColor(framerate / 60), metricFps)
end
if db.homeping or db.worldping then
local _, _, latencyHome, latencyWorld = GetNetStats()
if db.homeping then
local latencyText = formatValue(latencyHome, LagBar_GetThresholdHexColor(latencyHome, unpack(PING_THRESHOLDS)), metricMs)
if db.impdisplay then
latencyText = format("|cFF99CC33%s: |r%s", L.Home or "H", latencyText)
end
parts[#parts + 1] = latencyText
end
if db.worldping then
local latencyText = formatValue(latencyWorld, LagBar_GetThresholdHexColor(latencyWorld, unpack(PING_THRESHOLDS)), metricMs)
if db.impdisplay then
latencyText = format("|cFF99CC33%s: |r%s", L.World or "W", latencyText)
end
parts[#parts + 1] = latencyText
end
end
local finalText = tconcat(parts, " | ")
if finalText ~= self._lastText then
self.text:SetText(finalText)
self._lastText = finalText
end
local newWidth = max(MIN_FRAME_WIDTH, self.text:GetStringWidth() + TEXT_PADDING_X)
local newHeight = max(MIN_FRAME_HEIGHT, self.text:GetStringHeight() + TEXT_PADDING_Y)
if newWidth ~= self._lastWidth then
self:SetWidth(newWidth)
self._lastWidth = newWidth
end
if newHeight ~= self._lastHeight then
self:SetHeight(newHeight)
self._lastHeight = newHeight
end
end
function addon:StopUpdateTicker()
if self._ticker then
self._ticker:Cancel()
self._ticker = nil
end
self:SetScript("OnUpdate", nil)
end
function addon:StartUpdateTicker()
self:StopUpdateTicker()
self:UpdateDisplay()
if C_Timer and C_Timer.NewTicker then
self._ticker = C_Timer.NewTicker(UPDATE_INTERVAL, function()
if addon and addon.UpdateDisplay then
addon:UpdateDisplay()
end
end)
else
local timeSinceLastUpdate = 0
self:SetScript("OnUpdate", function(self, elapsed)
timeSinceLastUpdate = timeSinceLastUpdate + elapsed
if timeSinceLastUpdate >= UPDATE_INTERVAL then
timeSinceLastUpdate = 0
self:UpdateDisplay()
end
end)
end
end
local function ClampScale(value)
if value < 0.5 then return 0.5 end
if value > 5 then return 5 end
return value
end
function addon:SetAddonScale(value, bypass)
value = ClampScale(value)
LagBar_DB.scale = value
if not bypass then
DEFAULT_CHAT_FRAME:AddMessage(format(L.SlashScaleSet, value))
end
addon:SetScale(LagBar_DB.scale)
end
local function EnsureLayoutEntry(frame)
if not LagBar_DB then LagBar_DB = {} end
local opt = LagBar_DB[frame]
if not opt or not opt.point or not opt.xOfs then
opt = {
point = "CENTER",
relativePoint = "CENTER",
xOfs = 0,
yOfs = 0,
}
LagBar_DB[frame] = opt
end
return opt
end
function addon:SaveLayout(frame)
if type(frame) ~= "string" then return end
if not _G[frame] then return end
local opt = EnsureLayoutEntry(frame)
local point, _, relativePoint, xOfs, yOfs = _G[frame]:GetPoint()
opt.point = point
opt.relativePoint = relativePoint
opt.xOfs = xOfs
opt.yOfs = yOfs
end
function addon:RestoreLayout(frame)
if type(frame) ~= "string" then return end
if not _G[frame] then return end
local opt = EnsureLayoutEntry(frame)
_G[frame]:ClearAllPoints()
_G[frame]:SetPoint(opt.point, UIParent, opt.relativePoint, opt.xOfs, opt.yOfs)
end
function addon:BackgroundToggle()
ApplyBackdrop(addon, LagBar_DB.bgShown)
end
------------------------
-- Tooltip! --
------------------------
function addon:GetTipAnchor(frame)
local x,y = frame:GetCenter()
if not x or not y then return "TOPLEFT", "BOTTOMLEFT" end
local hhalf = (x > UIParent:GetWidth()*2/3) and "RIGHT" or (x < UIParent:GetWidth()/3) and "LEFT" or ""
local vhalf = (y > UIParent:GetHeight()/2) and "TOP" or "BOTTOM"
return vhalf..hhalf, frame, (vhalf == "TOP" and "BOTTOM" or "TOP")..hhalf
end