Making a function apply to all Buttons

Is there any easier way to creating vgui elements and painting them?
I wanted to see if I could just set all the colors of DButtons with a few lines of code instead of having to do it individually for each button.
Also same thing for PANEL:OnCursorEntered could I draw an additional rect on top of ALL buttons with one OnCursorEntered?
I know how to do it individually for each button, but it’s very tedious and annoying.

Are you creating each of your buttons manually or in a loop?

[editline]14th May 2016[/editline]

Either way, you could try something like this:



local function ButtonPaintThing( self, w, h )
    surface.SetDrawColor( 255, 0, 255, 255 ) -- for example
    surface.DrawRect( 0, 0, w, h ) -- for example
end

-- then repeat something like this for all your buttons:
somebutton.Paint = ButtonPaintThing


[editline]14th May 2016[/editline]

It’s not much better than doing it manually, but it’s slightly simpler


local DepositButton = vgui.Create("DButton", Frame)
	DepositButton:SetSize(150,30)
	DepositButton:SetPos(80, 220)
	DepositButton:SetText("")
	DepositButton.Paint = function(me, w, h)
		surface.SetDrawColor(Theme)
		surface.DrawRect(0,0,w,h)
	end
	local WithdrawlButton = vgui.Create("DButton", Frame)
	WithdrawlButton:SetSize(150,30)
	WithdrawlButton:SetPos(Frame:GetWide() / 2 + 20, 220)
	WithdrawlButton:SetText("")
	WithdrawlButton.Paint = function(me, w, h)
		surface.SetDrawColor(Theme)
		surface.DrawRect(0,0,w,h)
	end

I’ve been painting each button like this and plan to paint a lot.

[editline]14th May 2016[/editline]

How would I do the same for the OnCursorEntered?

[editline]14th May 2016[/editline]

wait nevermind, same thing applies. Thank you.

[editline]14th May 2016[/editline]

Nevermind it didn’t work because of two things. The arguments on cursor entered does not take (self, w, h) also due to the fact if you just do something like
DepositButton.OnCursorEntered = function()
surface.SetDrawColor(color)
surface.DrawRect(0,0,DepositButton:GetWide(), DepositButton:GetTall())
it won’t actually be painting over the Button.

I had to do this, to get what I wanted. This is a better example of something very tedious and unpleasant.


	DepositButton.Paint = function(self, w, h)
		surface.SetDrawColor(Theme)
		surface.DrawRect(0,0,w,h)
		surface.SetDrawColor(255,255,255,20)
		surface.DrawRect(0,0,w,h / 3)
		draw.DrawText( "Deposit", "Simple", self:GetWide() / 2, 5, color_white, TEXT_ALIGN_CENTER )
	end
	DepositButton.OnCursorEntered = function()
	DepositButton.Paint = function(self, w, h)
		surface.SetDrawColor(Theme)
		surface.DrawRect(0,0,w,h)
		surface.SetDrawColor(255,255,255,20)
		surface.DrawRect(0,0,w,h / 3)
		draw.DrawText( "Deposit", "Simple", self:GetWide() / 2, 5, color_white, TEXT_ALIGN_CENTER )
		surface.SetDrawColor(255,255,255,30)
		surface.DrawRect(0,0,w,h)
		end
	end
	DepositButton.OnCursorExited = function()
	DepositButton.Paint = function(self, w, h)
		surface.SetDrawColor(Theme)
		surface.DrawRect(0,0,w,h)
		surface.SetDrawColor(255,255,255,20)
		surface.DrawRect(0,0,w,h / 3)
		draw.DrawText( "Deposit", "Simple", self:GetWide() / 2, 5, color_white, TEXT_ALIGN_CENTER )
		end
	end
	

Nooooo don’t do that. Remove the cursor shit, you don’t need those. In your paint function just check if self:IsHovered() to see if the cursor is hovering in your button. You can also use self:IsDown() to check if the button is being clicked.

Thank you thank you thank you, I didn’t know that existed.