Skip to content

Instantly share code, notes, and snippets.

@Earu
Last active April 18, 2020 01:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Earu/b437be27eb75780f108ac6cad58eecde to your computer and use it in GitHub Desktop.
Save Earu/b437be27eb75780f108ac6cad58eecde to your computer and use it in GitHub Desktop.
An EasyChat nametags module using markup.
surface.CreateFont("NameTagFont", {
font = "Tahoma",
extended = true,
size = 100,
weight = 880,
additive = false,
})
surface.CreateFont("NameTagShadowFont", {
font = "Tahoma",
extended = true,
size = 100,
weight = 880,
blursize = 5,
})
local nametag = {
Nick = "",
DefaultColor = color_white,
Markup = nil,
}
function nametag:ParseNick(ply)
local team_col, nick = team.GetColor(ply:Team()), ply:Nick()
if nick ~= self.Nick or team_col ~= self.DefaultColor or not self.Markup then
self.Markup = ec_markup.AdvancedParse(nick, {
nick = true,
default_color = team_col,
default_font = "NameTagFont",
default_shadow_font = "NameTagShadowFont",
shadow_intensity = 1,
})
self.DefaultColor = team_col
self.Nick = nick
end
end
function nametag:Draw(ply)
self:ParseNick(ply)
self.Markup:SetPos(-self.Markup:GetWide() / 2, 0)
--render.PushFilterMag(TEXFILTER.ANISOTROPIC)
--render.PushFilterMin(TEXFILTER.ANISOTROPIC)
self.Markup:Draw()
--render.PopFilterMag()
--render.PopFilterMin()
end
-- actual drawing after this
local player_GetAll, ipairs, LocalPlayer = _G.player.GetAll, _G.ipairs, _G.LocalPlayer
local IsValid, EyeAngles, Vector = _G.IsValid, _G.EyeAngles, _G.Vector
local cam_Start3D2D, cam_End3D2D = _G.cam.Start3D2D, _G.cam.End3D2D
local table_copy = _G.table.Copy
local function should_get_overhead_pos(ply)
local lp = LocalPlayer()
if lp == ply and not ply:ShouldDrawLocalPlayer() then return false end
if ply:Crouching() then return false end
if lp:GetPos():DistToSqr(ply:GetPos()) > 5000 * 5000 then return false end
return true
end
local function get_overhead_pos(ply)
if not should_get_overhead_pos(ply) then return end
local bone = 6
local pos = ply:GetBonePosition(bone) or ply:EyePos()
if not ply:GetBoneName(bone):lower():find("head") and ply:GetBoneCount() >= bone then
for i = 1, ply:GetBoneCount() do
if ply:GetBoneName(i):lower():find("head") then
pos = ply:GetBonePosition(i)
bone = i
end
end
end
if not ply:Alive() then
local rag = ply:GetRagdollEntity()
if IsValid(rag) then
pos = rag:GetBonePosition(bone)
end
end
return pos
end
local function create_new_nametag(ply)
local nt = table_copy(nametag)
ply.Nametag = nt
end
hook.Add("PostDrawTranslucentRenderables", "nametag", function()
for _, ply in ipairs(player_GetAll()) do
local pos = get_overhead_pos(ply)
if pos then
local ang = EyeAngles()
ang:RotateAroundAxis(ang:Right(), 90)
ang:RotateAroundAxis(ang:Up(), -90)
cam_Start3D2D(pos + Vector(0, 0, 18), ang, 0.07)
-- create a nametag object for each player
if not ply.Nametag then
create_new_nametag(ply)
end
ply.Nametag:Draw(ply)
cam_End3D2D()
end
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment