I am wondering if there is a more efficient way to get the text in my health and armor bars to always be in the middle.
[CODE]hook.Add( "HUDPaint", "firstHUD", function()
local ply = LocalPlayer()
local screenX = 1600
local screenY = 900
local xMult = ScrW() / screenX
local yMult = ScrH() / screenY
local health = ply:Health() / 100
if health > 1 then health = 1 end
local armor = ply:Armor() / 100
if armor > 1 then armor = 1 end
local white = Color( 255, 255, 255,255 )
local black = Color( 0, 0, 0,255 )
local h_xpos = 122
if ply:Health() > 99 then h_xpos = h_xpos - 9
elseif ply:Health() > 9 and ply:Health() < 100 then h_xpos = h_xpos - 6
else h_xpos = 122
end
local a_xpos = 122
if ply:Armor() > 99 then a_xpos = a_xpos - 9
elseif ply:Armor() > 9 and ply:Armor() < 100 then a_xpos = a_xpos - 6
else a_xpos = 122
end
//Background//
draw.RoundedBox( 6, 25 * xMult, 800 * yMult, 200 * xMult, 75 * yMult, Color( 25, 25, 25, 250))
draw.RoundedBox( 4, 28 * xMult, 803 * yMult, 194 * xMult, 69 * yMult, Color( 75, 75, 75, 255))
//Health//
draw.RoundedBox( 4, 37 * xMult, 810 * yMult, 174 * xMult, 25 * yMult, Color( 75, 25, 25, 250))
if health > 0 then
draw.RoundedBox( 4, 39 * xMult, 812 * yMult, 170 * xMult * health, 21 * yMult, Color( 175, 25, 25, 255))
end
draw.SimpleTextOutlined( ply:Health(), "TargetID", h_xpos * xMult, 815 * yMult, white, 200 * xMult, 75 * yMult, 1 , black)
//Armor//
draw.RoundedBox( 4, 37 * xMult, 840 * yMult, 174 * xMult, 25 * yMult, Color( 25, 25, 50, 250))
if armor > 0 then
draw.RoundedBox( 4, 39 * xMult, 842 * yMult, 170 * xMult * armor, 21 * yMult, Color( 25, 25, 150, 255))
end
draw.SimpleTextOutlined( ply:Armor(), "TargetID", a_xpos * xMult, 845 * yMult, white, 200 * xMult, 75 * yMult, 1 , black)
end)
hook.Add( "HUDShouldDraw", "HideHUD", function(name)
for k, v in pairs( {"CHudHealth", "CHudBattery"}) do
if name == v then return false end
end
end)[/CODE]
[IMG]http://i.imgur.com/FCHzfvV.jpg[/IMG]
Dont know about that, but you shouldnt call variables that doesnt change in HUDPaint like Black white width etc. (Call xmult because if client changes res, it will be badly positioned.)
You could easily simplify some of the work. For text, there's local _w, _h = surface.GetTextSize( _text ); but it requires you to call surface.SetFont( "xxx" ); prior to using it.
Basically, with that, and knowing that the origin is top-left, ie 0, 0 ... you'd use _w / 2 = center of text. _h / 2; = middle of text.
The basic idea is:
[code] surface.SetFont( "Default" );
surface.SetTextColor( color_white );
local _textW, _textH = surface.GetTextSize( _health );
surface.SetTextPos( _x + ( _w / 2 ) - ( _textW / 2 ), _y + ( _h / 2 ) - ( _textH / 2 ) );
surface.DrawText( _health );[/code]
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/proper_hud_creation.lua.html[/url]
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/understanding_hardcoding_of_screensizes.lua.html[/url]
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/basic_healthbar.lua.html[/url] Edit: Updated this hud tutorial to include centered text.
Basically since you know that top-left is 0, 0... You can add half of the width of the bar, then you need to offset the text so subtracting half of the width of text brings it to the center-line alone the horizontal axis. For vertical, you simply to the same thing. Start + half-bar - half-text..
Hopefully this helps.
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/creating_shapes_using_poly.lua.html[/url]
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/tilted_rectangle_poly_as_health_meter.lua.html[/url]
[code]draw.SimpleTextOutlined( ply:Health(), "TargetID", LengthOfBar/2, HeightOfBar/2, white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1 , black)[/code]
Should work. Just make sure to replace length of bar with how long your health bars are
Edit: typed on phone, spacing is crap lol.
Sorry, you need to Log In to post a reply to this thread.