Shorten Code

Is there any way to shorten code like this? Feels so repetitive and makes code a jumbled mess.



closeB.Paint = function(self)
	surface.SetDrawColor(Color(0,0,0,100))
	surface.DrawRect(0, 0, self:GetWide(), self:GetTall())
		
	surface.SetDrawColor(Color(200,200,200,255))
	surface.DrawOutlinedRect(0, 0, self:GetWide(), self:GetTall())

	draw.SimpleTextOutlined("X", "MelonPett", self:GetWide()/2, self:GetTall()/2, Color(51,204,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0))

end
closeB.OnCursorEntered = function()
	closeB.Paint = function(self)
		surface.SetDrawColor(Color(255,90,90,100))
		surface.DrawRect(0, 0, self:GetWide(), self:GetTall())
		
		surface.SetDrawColor(Color(200,200,200,255))
		surface.DrawOutlinedRect(0, 0, self:GetWide(), self:GetTall())

		draw.SimpleTextOutlined("X", "MelonPett", self:GetWide()/2, self:GetTall()/2, Color(51,204,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0))

	end
end
closeB.OnCursorExited = function()
	closeB.Paint = function(self)
		surface.SetDrawColor(Color(0,0,0,100))
		surface.DrawRect(0, 0, self:GetWide(), self:GetTall())
		
		surface.SetDrawColor(Color(200,200,200,255))
		surface.DrawOutlinedRect(0, 0, self:GetWide(), self:GetTall())

		draw.SimpleTextOutlined("X", "MelonPett", self:GetWide()/2, self:GetTall()/2, Color(51,204,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0))

	end
end



[lua]
function paint(self)
surface.SetDrawColor(Color(0,0,0,100))
surface.DrawRect(0, 0, self:GetWide(), self:GetTall())

surface.SetDrawColor(Color(200,200,200,255))
surface.DrawOutlinedRect(0, 0, self:GetWide(), self:GetTall())

draw.SimpleTextOutlined("X", "MelonPett", self:GetWide()/2, self:GetTall()/2, Color(51,204,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0))

end

closeB.Paint = function(self)
paint(self)
end
closeB.OnCursorEntered = function()
closeB.Paint = function(self)
paint(self)
end
end
closeB.OnCursorExited = function()
closeB.Paint = function(self)
paint(self)
end
end
[/lua]



local col_cold, col_hot = Color(0, 0, 0, 100), Color(255, 90, 90, 100)
local rect_col, text_col = Color(200, 200, 200, 255), Color(51,204,255,255)
closeB.Paint = function(self, w, h)
	surface.SetDrawColor(self:IsHovered() and col_hot or col_col)
	surface.DrawRect(0, 0, w, h)
		
	surface.SetDrawColor(rect_col)
	surface.DrawOutlinedRect(0, 0, w, h)

	draw.SimpleTextOutlined("X", "MelonPett", w/2, h/2, text_col, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, color_black)
end


You’re both life-savers. Thank you!

I’ll mention something else. vgui itself can be quite repetitive so I use a helper-function… vgui.CreateEx; it works the same as the original except it adds a third options, _options, which lets you define everything in 1 line. Most of the controls are without Set and all lower-case.

Some of them can be shortened / simplified…
fullscreen = true instead of something like size = { w = ScrW( ), h = ScrH( ) }
center = true instead of something like pos = { x = ScrW( ) / 2 - w / 2, y = ScrH( ) / 2 - h / 2 } - some of which may be in the same table / inaccessible…

or simply x = 100, y = 100 instead of pos = { x = 100, y = 100 } – same with w / h

vgui.CreateEx( “DFrame”, nil, { center = true, fullscreen = true, makepopup = true, } );

It’s easy enough to make one tailored to your needs. Alternatively, it’s easy to create helper-functions added to the Panel meta-table to create objects such as Text Areas, Buttons, etc…

I will be releasing this soon and a meta-table “extension” for a lot of elements to be added and managed…