• Center VGUI on all resolutions
    63 replies, posted
Hey, I just noticed today that in other resolutions that my VGUI's are off screen. Is there a way to make a VGUI center in all screens? Or, just make it in the same position on all screens. Thanks.
-snip- Def use the post below me!
panel:SetSize( ... ) panel:Center( ) Where ... is the width and height.
or you could do [lua] local w = ScrW() / 2 local h = ScrH() / 2 panel:SetPos(w-200, h-100) [/lua] and all you do to get the subtracted value is take how wide your panel is divided by 2 so lets say its panel:SetSize(400,200) then it would be SetPos(w-200, h-100) although the one above me is more simple, this lets you center it only on the X or a Y axis, so you can have it at the top centered.
Generic centering thing: [code]panel:SetSize( w, h ) panel:SetPos( parent:GetWide() / 2 - panel:GetWide() /2, parent:GetTall() / 2 - panel:GetTall() / 2 )[/code]
Why not just use panel:Center() like Kogitsune said and finiish with it?
because like i said, this lets you center only on the X or the Y axis or both...more choices available in case he wanted it centered top, or centered left.
Thanks everyone but, it doesn't work. Ok, can someone please explain to me how to make a HUD at the bottom left of a screen, for ALL resolutions. So for a resolution for like, 1440x900, or 1280x768, or any of the others. Also, make sure it is in the same position if the player has his GMod open in any resolution in full screen or in a window. Thanks.
If you wanted something to be 30px off the left hand side and 100px off of the bottom, your positioning would look like this: [code]panel:SetPos( 30, ScrW() - 100 )[/code]
[QUOTE=Entoros;20729752]If you wanted something to be 30px off the left hand side and 100px off of the bottom, your positioning would look like this: [code]panel:SetPos( 30, ScrW() - 100 )[/code][/QUOTE] Ok. That is like something I have been using, but it is not in the same spot in all resolutions. In one, it will be fine, but in another it would be like half off or closer to the middle. I will try it right now. EDIT:Didn't even showup.
That position is positioned absolutely in the sense that it's always 30 px from the left and 100px from the bottom but it's positioned relatively in the sense that it's always from the bottom. It's possible that if you're setting your width/height according to the screen's dimensions that it will be off - in that case, your panel should be positioned relatively as well.
[QUOTE=Entoros;20732888]That position is positioned absolutely in the sense that it's always 30 px from the left and 100px from the bottom but it's positioned relatively in the sense that it's always from the bottom. It's possible that if you're setting your width/height according to the screen's dimensions that it will be off - in that case, your panel should be positioned relatively as well.[/QUOTE] Could my monitor being bigger than what I set the GMod resolution at be causing this problem?
Use ScrW()/1000*? and ScrH()/1000*? to place it on a 1000^2 grid. [editline]12:07AM[/editline] And i believe it's from top left.
[QUOTE=yoBrelliKX;20839812]Use ScrW()/1000*? and ScrH()/1000*? to place it on a 1000^2 grid. [editline]12:07AM[/editline] And i believe it's from top left.[/QUOTE] That is what I have been using, and it still doesn't work. In one resolution, it will be fine, and in another it will be somewhere else on the screen...
The Width and Height of the box should also be scaled.
[QUOTE=yoBrelliKX;20871092]The Width and Height of the box should also be scaled.[/QUOTE] So you mean like GetTall and GetWide?
Like: X = ScrW()/1000*250 Y = X W = ScrW()/1000*500 H = W
[QUOTE=antid2;20704762]because like i said, this lets you center only on the X or the Y axis or both...more choices available in case he wanted it centered top, or centered left.[/QUOTE] [url=http://wiki.garrysmod.com/?title=Panel.CenterHorizontal]CenterHorizontal[/url] [url=http://wiki.garrysmod.com/?title=Panel.CenterVertical]CenterVertical[/url]
[QUOTE=wakeboarderCWB;20728054]Thanks everyone but, it doesn't work. Ok, can someone please explain to me how to make a HUD at the bottom left of a screen, for ALL resolutions. So for a resolution for like, 1440x900, or 1280x768, or any of the others. Also, make sure it is in the same position if the player has his GMod open in any resolution in full screen or in a window. Thanks.[/QUOTE] Example of a HUD [lua] -- Hud from here down good_hud = { }; local function clr( color ) return color.r, color.g, color.b, color.a; end function good_hud:PaintBar( x, y, w, h, colors, value ) self:PaintPanel( x, y, w, h, colors ); x = x + 1; y = y + 1; w = w - 2; h = h - 2; local width = w * math.Clamp( value, 0, 1 ); local shade = 4; surface.SetDrawColor( clr( colors.shade ) ); surface.DrawRect( x, y, width, shade ); surface.SetDrawColor( clr( colors.fill ) ); surface.DrawRect( x, y + shade, width, h - shade ); end function good_hud:PaintPanel( x, y, w, h, colors ) surface.SetDrawColor( clr( colors.border ) ); surface.DrawOutlinedRect( x, y, w, h ); x = x + 1; y = y + 1; w = w - 2; h = h - 2; surface.SetDrawColor( clr( colors.background ) ); surface.DrawRect( x, y, w, h ); end function good_hud:PaintText( x, y, text, font, colors ) surface.SetFont( font ); surface.SetTextPos( x + 1, y + 1 ); surface.SetTextColor( clr( colors.shadow ) ); surface.DrawText( text ); surface.SetTextPos( x, y ); surface.SetTextColor( clr( colors.text ) ); surface.DrawText( text ); end function good_hud:TextSize( text, font ) surface.SetFont( font ); return surface.GetTextSize( text ); end local vars = { font = "TargetID", padding = 10, margin = 35, text_spacing = 2, bar_spacing = 5, bar_height = 16, width = 0.25 }; local colors = { background = { border = Color( 190, 255, 128, 255 ), background = Color( 120, 240, 0, 75 ) }, text = { shadow = Color( 0, 0, 0, 200 ), text = Color( 255, 255, 255, 255 ) }, health_bar = { border = Color( 255, 0, 0, 255 ), background = Color( 255, 0, 0, 75 ), shade = Color( 255, 104, 104, 255 ), fill = Color( 232, 0, 0, 255 ) }, suit_bar = { border = Color( 0, 0, 255, 255 ), background = Color( 0, 0, 255, 75 ), shade = Color( 136, 136, 255, 255 ), fill = Color( 0, 0, 219, 255 ) } }; local function HUDPaint( ) client = client or LocalPlayer( ); -- set a shortcut to the client if( !client:Alive( ) ) then return; end -- don't draw if the client is dead local _, th = good_hud:TextSize( "TEXT", vars.font ); -- get text size( height in this case ) local i = 2; -- shortcut to how many items( bars + text ) we have local width = vars.width * ScrW( ); -- calculate width local bar_width = width - ( vars.padding * i ); -- calculate bar width and element height local height = ( vars.padding * i ) + ( th * i ) + ( vars.text_spacing * i ) + ( vars.bar_height * i ) + vars.bar_spacing; local x = vars.margin; -- get x position of element local y = ScrH( ) - vars.margin - height; -- get y position of element local cx = x + vars.padding; -- get x and y of contents local cy = y + vars.padding; good_hud:PaintPanel( x, y, width, height, colors.background ); -- paint the background panel local by = th + vars.text_spacing; -- calc text position local text = string.format( "Health: %iHP", client:Health( ) ); -- get health text good_hud:PaintText( cx, cy, text, vars.font, colors.text ); -- paint health text and health bar good_hud:PaintBar( cx, cy + by, bar_width, vars.bar_height, colors.health_bar, client:Health( ) / 100 ); by = by + vars.bar_height + vars.bar_spacing; -- increment text position local text = string.format( "Suit: %iSP", client:Armor( ) ); -- get suit text good_hud:PaintText( cx, cy + by, text, vars.font, colors.text ); -- paint suit text and suit bar good_hud:PaintBar( cx, cy + by + th + vars.text_spacing, bar_width, vars.bar_height, colors.suit_bar, client:Armor( ) / 100 ); end hook.Add( "HUDPaint", "PaintOurHud", HUDPaint ); function hidehud(name) for k, v in pairs{"CHudHealth"} do if name == v then return false end end end hook.Add("HUDShouldDraw", "hidehud", hidehud) [/lua]
did you really just copy and paste the wiki?
Ok, now this. Can someone help me place a HUD in the bottom center, so it will be the same on all screens? I got the VGUI's to be the same on all screens, now I want it to be with the HUD. Thanks.
ScrH() - the height of your HUD will place it exactly at the bottom.
[QUOTE=Crazy Quebec;20934861]ScrH() - the height of your HUD will place it exactly at the bottom.[/QUOTE] Ok, how would I make it go higher up? But be the same on all screens. And then the same with width.
Use fractions of ScrW() and ScrH().
Or position everything as if it were on a 640x480 moniter and [b][url=http://wiki.garrysmod.com/?title=G.ScreenScale]G.ScreenScale [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] it.
[QUOTE=Crazy Quebec;20944088]Use fractions of ScrW() and ScrH().[/QUOTE] I do use fractions and stuff, and it is not the same on each screen.
ScrW() - 200 for a HUD that is 200 px wide. If it's not on the left hand of the screen on all resolutions, you coded it wrong.
[QUOTE=Disseminate;20945652]ScrW() - 200 for a HUD that is 200 px wide. If it's not on the left hand of the screen on all resolutions, you coded it wrong.[/QUOTE] I believe that takes the screen width, and places the hud 200 pixels across.
[QUOTE=wakeboarderCWB;20934094]Ok, now this. Can someone help me place a HUD in the bottom center, so it will be the same on all screens? I got the VGUI's to be the same on all screens, now I want it to be with the HUD. Thanks.[/QUOTE] Are we still talking about this? If so panel:Center() panel.y = ScrH() - panel:GetTall() and the code disseminate posted makes the hud appear at 200 pixels less than all the way to the right of the screen. If you already knew that, I apologize, but your sentence didn't make sense.
[QUOTE=yakahughes;20954962]Are we still talking about this? If so panel:Center() panel.y = ScrH() - panel:GetTall() and the code disseminate posted makes the hud appear at 200 pixels less than all the way to the right of the screen. If you already knew that, I apologize, but your sentence didn't make sense.[/QUOTE] No, I got the vgui's all good. But now the HUD is where I am having problems. In one screen, it's fine, but in another it's not in the same pos, or cut off a little.
Sorry, you need to Log In to post a reply to this thread.