Hi ive been having an issue with dbuttons where i want to add a convar
I want to make it so once button is clicked the text goes green which ive done
and make it stay green when a convar is turned on for example example_test 1
and when you press the button again the green button turns back to white so example_test 0
would anyone have any idea on how to do this?
[CODE]local derma1 = vgui.Create( "DFrame" )
derma1:SetPos( 5, 5 )
derma1:SetSize( 300, 300 )
derma1:SetTitle( "Name window" )
derma1:SetVisible( true )
derma1:SetDraggable( false )
derma1:ShowCloseButton( true )
derma1:MakePopup()
local DPanel = vgui.Create( "DPanel", derma1 )
DPanel:SetPos( 10, 30 )
DPanel:SetSize( 125, 35 )
DPanel.Paint = function(s , w , h)
draw.RoundedBox(4,0,0,w , h,Color(50, 50, 100, 255))
end
local DButton = vgui.Create( "DButton", DPanel )
DButton:SetPos( 5, 5 )
DButton:SetText( "single" )
DButton:SetSize( 115, 25 )
DButton:SetTextColor( Color( 255, 255, 255, 255 ) )
DButton.Paint = function(s , w , h)
draw.RoundedBox(4,0,0,w , h,Color(5, 5, 40, 255))
end
DButton.OnCursorEntered = function()
print( "You hovered over this button" )
DButton:SetTextColor( Color( 255, 255, 0, 255 ) )
end
DButton.OnCursorExited = function()
print( "You left the button area" )
DButton:SetTextColor( Color( 255, 255, 255, 255 ) )
end
DButton.DoClick = function()
print( "Button was clicked!" )
surface.PlaySound("UI/buttonclick.wav")
DButton:SetTextColor( Color( 0, 255, 0, 255 ) )
end
[/CODE]
i added a derma so its easier to work with.
dont mind the button .DoClick and .OnCursorExited print strings i did that cause i wanted to test shit lol :p
I would like to also know if someone could make the same thing but as a dcombo box with the same colors at the button above ^
Could do it like this,
[lua]
local DButton = vgui.Create("DButton", DPanel)
DButton:SetPos(5, 5)
DButton:SetSize(115, 25)
DButton:SetText("")
DButton.Paint = function(s, w, h)
draw.RoundedBox(4, 0, 0, w, h, Color(5, 5, 40, 255))
draw.SimpleText("Single", "TargetID", w / 2, h / 2, GetConVarNumber("example_test") == 1 and color_green or color_white, 1, 1)
end
DButton.DoClick = function()
if GetConVarNumber("example_test") >= 1 then
LocalPlayer():ConCommand("example_test 0")
else
LocalPlayer():ConCommand("example_test 1")
end
end
[/lua]
[QUOTE=Its Pawsative;51588312]Could do it like this,
[lua]
local DButton = vgui.Create("DButton", DPanel)
DButton:SetPos(5, 5)
DButton:SetSize(115, 25)
DButton:SetText("")
DButton.Paint = function(s, w, h)
draw.RoundedBox(4, 0, 0, w, h, Color(5, 5, 40, 255))
draw.SimpleText("Single", "TargetID", w / 2, h / 2, GetConVarNumber("example_test") == 1 and color_green or color_white, 1, 1)
end
DButton.DoClick = function()
if GetConVarNumber("example_test") >= 1 then
LocalPlayer():ConCommand("example_test 0")
else
LocalPlayer():ConCommand("example_test 1")
end
end
[/lua][/QUOTE]
Theres simpler approaches which doesnt include setting a convar.
You should consider making it a [URL="http://wiki.garrysmod.com/page/Category:DCheckBox"]DCheckBox[/URL] and using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Panel/SetConVar]Panel:SetConVar[/url]
[QUOTE=sannys;51588409]You should consider making it a [URL="http://wiki.garrysmod.com/page/Category:DCheckBox"]DCheckBox[/URL] and using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Panel/SetConVar]Panel:SetConVar[/url][/QUOTE]
Nah wont work ive tried and cant seem to make a simple button to make a convar go from example_test 1 to example_test 2 on the same button :/
Ive used this but when i do this multiple times it stuffs up my custom dpanel boxes ive made only works for 1 button for some reason :/
[CODE]local toggle = false
local derma1 = vgui.Create( "DFrame" )
derma1:SetPos( 5, 5 )
derma1:SetSize( 300, 300 )
derma1:SetTitle( "Name window" )
derma1:SetVisible( true )
derma1:SetDraggable( false )
derma1:ShowCloseButton( true )
derma1:MakePopup()
local DPanel = vgui.Create( "DPanel", derma1 )
DPanel:SetPos( 10, 30 )
DPanel:SetSize( 125, 35 )
DPanel.Paint = function(s , w , h)
draw.RoundedBox(4,0,0,w , h,Color(50, 50, 100, 255))
end
local DButton = vgui.Create( "DButton", DPanel )
DButton:SetPos( 5, 5 )
DButton:SetText( "single" )
DButton:SetSize( 115, 25 )
DButton:SetTextColor( Color( 255, 0, 0, 255 ) )
DButton.Paint = function(s , w , h)
draw.RoundedBox(4,0,0,w , h,Color(5, 5, 40, 255))
end
DButton.OnCursorEntered = function()
print( "You hovered over this button" )
DButton:SetTextColor( Color( 255, 255, 0, 255 ) )
if toggle then
DButton:SetTextColor( Color( 100, 255, 100, 255 ) )
else
DButton:SetTextColor( Color( 255, 100, 100, 255 ) )
end
end
DButton.OnCursorExited = function()
print( "You left the button area" )
if toggle then
DButton:SetTextColor( Color( 0, 255, 0, 255 ) )
else
DButton:SetTextColor( Color( 255, 0, 0, 255 ) )
end
end
DButton.DoClick = function()
toggle = !toggle
if toggle then
GetConVar("xeno_aimbot_toggle"):SetInt(1)
else
GetConVar("xeno_aimbot_toggle"):SetInt(0)
end
print( "Button was clicked!" )
surface.PlaySound("UI/buttonclick.wav")
if toggle then
DButton:SetTextColor( Color( 100, 255, 100, 255 ) )
else
DButton:SetTextColor( Color( 255, 100, 100, 255 ) )
end
end[/CODE]
[QUOTE=liamproctor99;51588781]Nah wont work ive tried and cant seem to make a simple button to make a convar go from example_test 1 to example_test 2 on the same button :/
Ive used this but when i do this multiple times it stuffs up my custom dpanel boxes ive made only works for 1 button for some reason :/
[CODE]local toggle = false
local derma1 = vgui.Create( "DFrame" )
derma1:SetPos( 5, 5 )
derma1:SetSize( 300, 300 )
derma1:SetTitle( "Name window" )
derma1:SetVisible( true )
derma1:SetDraggable( false )
derma1:ShowCloseButton( true )
derma1:MakePopup()
local DPanel = vgui.Create( "DPanel", derma1 )
DPanel:SetPos( 10, 30 )
DPanel:SetSize( 125, 35 )
DPanel.Paint = function(s , w , h)
draw.RoundedBox(4,0,0,w , h,Color(50, 50, 100, 255))
end
local DButton = vgui.Create( "DButton", DPanel )
DButton:SetPos( 5, 5 )
DButton:SetText( "single" )
DButton:SetSize( 115, 25 )
DButton:SetTextColor( Color( 255, 0, 0, 255 ) )
DButton.Paint = function(s , w , h)
draw.RoundedBox(4,0,0,w , h,Color(5, 5, 40, 255))
end
DButton.OnCursorEntered = function()
print( "You hovered over this button" )
DButton:SetTextColor( Color( 255, 255, 0, 255 ) )
if toggle then
DButton:SetTextColor( Color( 100, 255, 100, 255 ) )
else
DButton:SetTextColor( Color( 255, 100, 100, 255 ) )
end
end
DButton.OnCursorExited = function()
print( "You left the button area" )
if toggle then
DButton:SetTextColor( Color( 0, 255, 0, 255 ) )
else
DButton:SetTextColor( Color( 255, 0, 0, 255 ) )
end
end
DButton.DoClick = function()
toggle = !toggle
if toggle then
GetConVar("xeno_aimbot_toggle"):SetInt(1)
else
GetConVar("xeno_aimbot_toggle"):SetInt(0)
end
print( "Button was clicked!" )
surface.PlaySound("UI/buttonclick.wav")
if toggle then
DButton:SetTextColor( Color( 100, 255, 100, 255 ) )
else
DButton:SetTextColor( Color( 255, 100, 100, 255 ) )
end
end[/CODE][/QUOTE]
This only works for 1 button because the only button you are applying those functions to are [B]DButton[/B].
[QUOTE=liamproctor99;51588781]Nah wont work ive tried and cant seem to make a simple button to make a convar go from example_test 1 to example_test 2 on the same button :/[/QUOTE]
That's exactly what the example does on the SetConVar page. You did not try at all. The only person you're fooling is yourself.
[QUOTE=sannys;51591015]That's exactly what the example does on the SetConVar page. You did not try at all. The only person you're fooling is yourself.[/QUOTE]
Yeah my bad i tried it on another derma so see if it could work but it doesn't switch between the 2 convars if i were to make more buttons i need to make more = toggle such as
1st button = toggle
2nd button = toggle 2
ext
it works and i have solved my problem, thank you anyway :*
Sorry, you need to Log In to post a reply to this thread.