Changing the color of a button in a DScrollPanel list? (noob question)
5 replies, posted
Oi m8s.. need some help here. How would I go about changing the color of the DButtons I created here everytime I clicked on that button?
All of the buttons are in a DScrollPanel by the way. Thanks in advance... :smile::smile:
[code]
--cl_init
for k, v in pairs(panel1table) do
local name = v
v = vgui.Create( "DButton", DScrollPanel1 )
v:SetFont( "hud_exo18" )
v:SetText( "["..k.."]".. name )
v:SetTextColor( Color( 0, 0, 0, 255 ) )
v:SetPos( 0, k * 45 - 45 )
v:SetSize( 330, 45 )
v.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, Color( Something like v.colorselected or something? ) ) end
v.DoClick = function( ply )
end
end
[/code]
[editline]1st October 2016[/editline]
Now that I think about it, this should have probably gone in the "Problem that don't need their own thread" thread. :ohno: Sorry about that to those who are anal about that sort of thing
What you're looking for is BUTTON.Paint = function(self) code end)
[editline]2nd October 2016[/editline]
Sorry, didn't read your code properly, you could check if the specific button is selected by storing it on itself I.e DoClick then store that specific button, then if that specific button == true then just paint, else do paint if not clicked.
[code]
--cl_init
for k, v in pairs(panel1table) do
local name = v
v = vgui.Create( "DButton", DScrollPanel1 )
v:SetFont( "hud_exo18" )
v:SetText( "["..k.."]".. name )
v:SetTextColor( Color( 0, 0, 0, 255 ) )
v:SetPos( 0, k * 45 - 45 )
v:SetSize( 330, 45 )
v.DoClick = function( ply )
v.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, Color(10,10,10,250) ) end
end
end
[/code]
[QUOTE=Vissaben;51144056][code]
--cl_init
for k, v in pairs(panel1table) do
local name = v
v = vgui.Create( "DButton", DScrollPanel1 )
v:SetFont( "hud_exo18" )
v:SetText( "["..k.."]".. name )
v:SetTextColor( Color( 0, 0, 0, 255 ) )
v:SetPos( 0, k * 45 - 45 )
v:SetSize( 330, 45 )
v.DoClick = function( ply )
v.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, Color(10,10,10,250) ) end
end
end
[/code][/QUOTE]
Wrong, he wants to change the color with an click.
If you want them to change their colors only on click
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/DButton/IsDown]DButton:IsDown[/url]
[code]DButton.Paint = function(self, w, h)
if self:IsDown() then
draw.RoundedBox(0, 0, 0, w, h, color_white)
else
draw.RoundedBox(0, 0, 0, w, h, color_black)
end
end[/code]
Or this, if you want to change colors on each click
[code]
DButton.colour = color_white
DButton.DoClick = function(self)
self.colour = ColorRand()
end
DButton.Paint = function(self, w, h)
draw.RoundedBox(0, 0, 0, w, h, self.colour)
end[/code]
[QUOTE=iJohnny;51144878]If you want them to change their colors only on click
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/DButton/IsDown]DButton:IsDown[/url]
[code]DButton.Paint = function(self, w, h)
if self:IsDown() then
draw.RoundedBox(0, 0, 0, w, h, color_white)
else
draw.RoundedBox(0, 0, 0, w, h, color_black)
end
end[/code]
Or this, if you want to change colors on each click
[code]
DButton.colour = color_white
DButton.DoClick = function(self)
self.colour = ColorRand()
end
DButton.Paint = function(self, w, h)
draw.RoundedBox(0, 0, 0, w, h, self.colour)
end[/code][/QUOTE]
Apologies for the super late reply, haven't really been messing with GLua much. :pudge: I know that the code you gave me there changes colors of each button, but how would I go about setting the other colors while changing the current one when DoClick() is ran. (v in this instance) So far, I have tried using a for loop to loop through the table and set all of the v.Colours to white, but it doesn't seem to work.
[code]
local O = Color(255, 150, 250, 255)
local OT = Color(255, 150, 250, 0)
for k, v in pairs(panel1table) do
local vtable = {} -- !(NEW)
local name = v
local selected = nil
v = vgui.Create( "DButton", DScrollPanel1 )
v.Colour = OT
v:SetFont( "hud_exo18" )
v:SetText( "["..k.."]".. string.upper(string.TrimLeft( name, "cw_" )) )
v:SetTextColor( Color( 0, 0, 0, 255 ) )
v:SetPos( 0, k * 45 - 45 )
v:SetSize( 330, 45 )
for i=1, #panel1table do
table.insert( vtable, v )
end
v.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, self.Colour ) end
v.DoClick = function( self )
primarySelected = name
selected = self
self.Colour = O
for i=1, #vtable do
if !selected == vtable[i] then -- if not equal to self then run thru and turn everything into white
vtable[i].Colour = OT
end
end
end
end
[/code]
[editline]9th October 2016[/editline]
I think I am inserting the same DButton into the table(vtable) multiple times(however much #panel1table is to be exact)...
[editline]9th October 2016[/editline]
Yea, that is definitely what seems to be happening :goodjob:
Sorry, you need to Log In to post a reply to this thread.