zombiesurvival-evolved/gamemodes/zombiesurvival/gamemode/vgui/dpingmeter.lua

82 lines
1.7 KiB
Lua
Raw Normal View History

2014-10-02 08:49:54 +08:00
local PANEL = {}
PANEL.IdealPing = 50
PANEL.MaxPing = 400
PANEL.RefreshTime = 1
PANEL.PingBars = 5
2014-10-02 08:49:54 +08:00
PANEL.m_Player = NULL
PANEL.m_Ping = 0
PANEL.NextRefresh = 0
function PANEL:Init()
end
local colPing = Color(255, 255, 60, 255)
function PANEL:Paint()
local ping = self:GetPing()
local pingmul = 1 - math.Clamp((ping - self.IdealPing) / self.MaxPing, 0, 1)
local wid, hei = self:GetWide(), self:GetTall()
local pingbars = math.max(1, self.PingBars)
local barwidth = math.floor(wid / pingbars)
local baseheight = math.floor(hei / pingbars)
2014-10-02 08:49:54 +08:00
colPing.r = (1 - pingmul) * 255
colPing.g = pingmul * 255
for i=1, pingbars do
local barheight = math.floor(baseheight * i)
2014-10-02 08:49:54 +08:00
local x, y = (i - 1) * barwidth, hei - barheight
surface.SetDrawColor(20, 20, 20, 255)
surface.DrawRect(x, y, barwidth, barheight)
if i == 1 or pingmul >= i / pingbars then
surface.SetDrawColor(colPing)
surface.DrawRect(x, y, barwidth, barheight)
end
surface.SetDrawColor(80, 80, 80, 255)
surface.DrawOutlinedRect(x, y, barwidth, barheight)
end
draw.SimpleText(ping, "ZSScoreBoardPing", 0, 0, colPing)
2014-10-02 08:49:54 +08:00
return true
end
function PANEL:RefreshContents()
2014-10-02 08:49:54 +08:00
local pl = self:GetPlayer()
if pl:IsValid() then
self:SetPing(pl:Ping())
else
self:SetPing(0)
end
end
function PANEL:Think()
if RealTime() >= self.NextRefresh then
self.NextRefresh = RealTime() + self.RefreshTime
self:RefreshContents()
2014-10-02 08:49:54 +08:00
end
end
function PANEL:SetPlayer(pl)
self.m_Player = pl or NULL
self:RefreshContents()
2014-10-02 08:49:54 +08:00
end
function PANEL:GetPlayer()
return self.m_Player
end
function PANEL:SetPing(ping)
self.m_Ping = ping
end
function PANEL:GetPing()
return self.m_Ping
end
vgui.Register("DPingMeter", PANEL, "Panel")