Nfont = "default-bold"
local NfontScale = 1
nametag = {}
local enabled = true
local nametags = {}
local g_screenX,g_screenY = guiGetScreenSize()
local bHideNametags = false
local NAMETAG_SCALE = 0.3 --Overall adjustment of the nametag, use this to resize but constrain proportions
local NAMETAG_ALPHA_DISTANCE = 50 --Distance to start fading out
local NAMETAG_DISTANCE = 120 --Distance until we're gone
local NAMETAG_ALPHA = 120 --The overall alpha level of the nametag
--The following arent actual pixel measurements, they're just proportional constraints
local NAMETAG_TEXT_BAR_SPACE = 2
local NAMETAG_WIDTH = 50
local NAMETAG_HEIGHT = 5
local NAMETAG_TEXTSIZE_N = 0.7
local NAMETAG_OUTLINE_THICKNESS = 1.2
--
local chaticon = true
local NAMETAG_ALPHA_DIFF = NAMETAG_DISTANCE - NAMETAG_ALPHA_DISTANCE
NAMETAG_SCALE = 1/NAMETAG_SCALE * 1100 / g_screenY -- default was 800
-- Ensure the name tag doesn't get too big
local maxScaleCurve = { {0, 0}, {3, 3}, {13, 5} }
-- Ensure the text doesn't get too small/unreadable
local textScaleCurve = { {0, 0.8}, {0.8, 1.2}, {99, 99} }
-- Make the text a bit brighter and fade more gradually
local textAlphaCurve = { {0, 0}, {25, 100}, {120, 190}, {255, 190} }
function nametag.create ( player )
nametags[player] = true
end
function nametag.destroy ( player )
nametags[player] = nil
end
dxTextCache = {}
dxTextShadowCache = {}
function clearDxCache( )
dxTextCache = {}
dxTextShadowCache = {}
end
addCommandHandler("cleardx", clearDxCache)
setTimer(clearDxCache,1000,0)
function dxDrawColoredText(str, ax, ay, bx, by, color, tcolor, scale, font)
local rax = ax
if not dxTextShadowCache[str] then
dxTextShadowCache[str] = string.gsub( str, '#%x%x%x%x%x%x', '' )
end
dxDrawText(dxTextShadowCache[str], ax+1,ay+1,ax+1,by,tocolor(0,0,0, 0.8 * tcolor[4]),scale,font, "left", "bottom", false,false,false)
if dxTextCache[str] then
for id, text in ipairs(dxTextCache[str]) do
local w = text[2] * ( scale / text[4] )
dxDrawText(text[1], ax + w, ay, ax + w, by, tocolor(text[3][1],text[3][2],text[3][3],tcolor[4]), scale, font, "left", "bottom", false,false,false)
end
else
dxTextCache[str] = {}
local pat = "(.-)#(%x%x%x%x%x%x)"
local s, e, cap, col = str:find(pat, 1)
local last = 1
local r = tcolor[1]
local g = tcolor[2]
local b = tcolor[3]
local textalpha = tcolor[4]
while s do
if cap == "" and col then
r = tonumber("0x"..col:sub(1, 2))
g = tonumber("0x"..col:sub(3, 4))
b = tonumber("0x"..col:sub(5, 6))
color = tocolor(r, g, b, textalpha)
end
if s ~= 1 or cap ~= "" then
local w = dxGetTextWidth(cap, scale, font)
dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")
table.insert(dxTextCache[str], { cap, ax-rax, {r,g,b}, scale } )
ax = ax + w
r = tonumber("0x"..col:sub(1, 2))
g = tonumber("0x"..col:sub(3, 4))
b = tonumber("0x"..col:sub(5, 6))
color = tocolor( r, g, b, textalpha)
end
last = e + 1
s, e, cap, col = str:find(pat, last)
end
if last <= #str then
cap = str:sub(last)
local w = dxGetTextWidth(cap, scale, font)
dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "bottom")
table.insert(dxTextCache[str], { cap, ax-rax, {r,g,b}, scale } )
end
end
end
addEventHandler ( "onClientRender", g_Root,
function()
-- Hideous quick fix --
for i,player in ipairs(g_Players) do
if player ~= g_Me then
setPlayerNametagShowing ( player, false )
if not nametags[player] then
nametag.create ( player )
end
end
end
if bHideNametags then
return
end
local x,y,z = getCameraMatrix()
if not enabled then return end
for player in pairs(nametags) do
while true do
if not isPedInVehicle(player) or isPlayerDead(player) then break end
local vehicle = getPedOccupiedVehicle(player)
local px,py,pz = getElementPosition ( vehicle )
local pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )
if pdistance <= NAMETAG_DISTANCE then
--Get screenposition
local sx,sy = getScreenFromWorldPosition ( px, py, pz+0.95, 0.06 )
if not sx or not sy then break end
--Calculate our components
local scale = 1/(NAMETAG_SCALE * (pdistance / NAMETAG_DISTANCE))
local alpha = ((pdistance - NAMETAG_ALPHA_DISTANCE) / NAMETAG_ALPHA_DIFF)
alpha = (alpha < 0) and NAMETAG_ALPHA or NAMETAG_ALPHA-(alpha*NAMETAG_ALPHA)
local scale = math.evalCurve(maxScaleCurve,scale)
local textscale = math.evalCurve(textScaleCurve,scale)
local textalpha = math.evalCurve(textAlphaCurve,alpha)
local outlineThickness = NAMETAG_OUTLINE_THICKNESS*(scale)
--Draw chat icon
if getElementData(player, "isChatting") and chaticon then
-- dxDrawImage(sx - 15 * scale, sy - 40 * scale, 30 * scale, 30 * scale, "img/chat.png")
end
--Draw our text
local r,g,b = 255,255,255
local team = getPlayerTeam(player)
if team then
r,g,b = getTeamColor(team)
end
local h = dxGetFontHeight(0.5 * NfontScale * textscale * NAMETAG_TEXTSIZE_N, Nfont)
local oet = (scale) * NAMETAG_TEXT_BAR_SPACE/2
local w = dxGetTextWidth(getPlayerName(player), 0.5 * NfontScale * textscale * NAMETAG_TEXTSIZE_N, Nfont)
--dxDrawColoredText(getPlayerName(player), sx - w + 1, sy - oet + 1, sx - w + 1, sy + 1, tocolor(0,0,0, math.floor(0.8*getElementAlpha(getPedOccupiedVehicle(player)))),math.floor(0.8*getElementAlpha(getPedOccupiedVehicle(player))), NfontScale * textscale * NAMETAG_TEXTSIZE_N, font, "center", "bottom", false, false, false, true)
local a = getElementAlpha(getPedOccupiedVehicle(player))
dxDrawColoredText(getPlayerNametagText(player), sx - w, sy - oet, sx - w, sy, tocolor(r, g, b, a), {r,g,b,a} , NfontScale * textscale * NAMETAG_TEXTSIZE_N, Nfont, "center", "bottom", false, false, false, true)
--We draw three parts to make the healthbar. First the outline/background
local drawX = sx - NAMETAG_WIDTH*scale*0.65/2
drawY = sy + oet*0.6
local width,height = NAMETAG_WIDTH*scale*0.65, NAMETAG_HEIGHT*scale*0.7
--dxDrawRectangle ( drawX, drawY, width, height, tocolor(0,0,0,alpha) )
--Next the inner background
local health = getElementHealth(vehicle)
health = math.max(health - 250, 0)/750
local p = -510*(health^2)
local r,g = math.max(math.min(p + 255*health + 255, 255), 0), math.max(math.min(p + 765*health, 255), 0)
dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * (health)), 0, 256, 16, "img/healthbar1.png", 0, 0, 0, tocolor(r, g, 0, getElementAlpha(getPedOccupiedVehicle(player))))
dxDrawImageSection(drawX, drawY, width, height, math.floor(256 - 256 * (health)), 0, 256, 16, "img/healthbar2.png", 0, 0, 0, tocolor(255,255,255, getElementAlpha(getPedOccupiedVehicle(player))))
dxDrawImage(drawX - 1, drawY - 1, width + 2, height + 2, "img/healthbar3.png", 0, 0, 0, tocolor(255,255,255,getElementAlpha(getPedOccupiedVehicle(player))))
end
break
end
end
end
)
addEventHandler('onClientResourceStart', g_ResRoot,
function()
for i,player in ipairs(getElementsByType"player") do
if player ~= g_Me then
nametag.create ( player )
end
end
end
)
addEventHandler ( "onClientPlayerJoin", g_Root,
function()
if source == g_Me then return end
setPlayerNametagShowing ( source, false )
nametag.create ( source )
end
)
addEventHandler ( "onClientPlayerQuit", g_Root,
function()
nametag.destroy ( source )
end
)
addEvent ( "onClientScreenFadedOut", true )
addEventHandler ( "onClientScreenFadedOut", g_Root,
function()
bHideNametags = true
end
)
addEvent ( "onClientScreenFadedIn", true )
addEventHandler ( "onClientScreenFadedIn", g_Root,
function()
bHideNametags = false
end
)
function isTheft()
return false
end
setTimer(
function()
if getElementData(getLocalPlayer(), "isChatting" ) ~= isChatBoxInputActive() then
setElementData(getLocalPlayer(), "isChatting", isChatBoxInputActive(), true)
end
end,
50,0 )
addCommandHandler("nametags", function()
enabled = not enabled
end
)
addCommandHandler("chaticons", function()
chaticons = not chaticon
end
)