surface.DrawPoly on buttons.

Ok so i tried to use surface.DrawPoly on a button. It simply doesnt want to work for me.

http://cloud-2.steampowered.com/ugc/558737235087458367/A4CBA94FBA5A78C192C79AC40F3BA0421F8EF177/

Global :


tex = surface.GetTextureID( "cs_italy/black" )
tbl = { {}, {}, {}, {} }
tbl[1]["x"] = ScrW() - 800
tbl[1]["y"] = ScrH() - 820
tbl[2]["x"] = ScrW() - 820
tbl[2]["y"] = ScrH() - 800
tbl[3]["x"] = ScrW() - 800
tbl[3]["y"] = ScrH() - 820


Button :


		local button4 = vgui.Create("DButton", buyhealthframe)
				button4:SetText( "Buy" )
				button4:SetPos( 525, 35 )
				button4:SetSize( 90, 40 )
				button4.DoClick = function () 
				frame:Close()
				greybox:Close()
				greybox2:Close()
				buyhealthframe:Close()
				RunConsoleCommand("buyscorestreakhealth")
				end
				button4.Paint = function()
		surface.SetDrawColor( 255, 0, 0, 255 )
		surface.SetTexture( tex )
		surface.DrawPoly( tbl )
			end

I’m not sure what your problem but two things
people can avoid (for whatever reason) the console command by aliasing it and secondly you’re drawing a triangle, judging by your posts you want to draw a trapezoid of sorts.

Here, try this function:

[lua]function surface.DrawTrapezoid( x, y, w, h, col, a)
local s = {
{ x = x, y = y, u = 0, v = 0},
{ x = x+w, y = y, u = 1, v = 0},
{ x = x+w+a, y = y+h, u = 0, v = 1},
{ x = x+a, y = y+h, u = 1, v = 1}
}

surface.SetMaterial(Material("vgui/white"))
surface.SetDrawColor(col)
surface.DrawPoly(s)

end[/lua]

Isn’t exactly what you need, but with small modifying it shouldn’t be a problem to make one side straight.

If they alias it they are stupid since they lose access to buy powerups. Thanks for function i look at it

Your problem is that you use ScrW() when you setup the poly. Please be aware that drawing inside buttons use relative positions, and not global positions. So position 0, 0 would be the top left of your button, and the width/height of the button would be the bottom right.

For example:

[lua]
if BUTTON then
BUTTON:Remove()
end

BUTTON = vgui.Create(“DButton”)
BUTTON:SetPos(200, 200)
BUTTON:SetSize(128, 64)
BUTTON.tblTopTriangle = {}
BUTTON.tblBottomTriangle = {}

– This function is called whenever the button size/layout changes.
– This is useful because we have to recreate our triangles to fit the button whenever the size changes.
function BUTTON:PerformLayout()
self.tblTopTriangle = {
{x = 0, y = 0},
{x = self:GetWide(), y = 0},
{x = self:GetWide() * 0.5, y = self:GetTall() * 0.5},
}
self.tblBottomTriangle = {
{x = 0, y = self:GetTall()},
{x = self:GetWide(), y = self:GetTall()},
{x = self:GetWide() * 0.5, y = self:GetTall() * 0.5},
}
end

function BUTTON:Paint()
surface.SetDrawColor(Color(0, 200, 0, 255))
surface.DrawRect(0, 0, self:GetWide(), self:GetTall())
surface.SetDrawColor(Color(255, 0, 0, 255))
surface.DrawPoly(self.tblTopTriangle)
surface.DrawPoly(self.tblBottomTriangle)
end
[/lua]

How this example looks:

Thanks alot know i understand why it did dissapear

@Maurits this is kinda off topic but may I know how long you’ve been coding for?
You and Robotboy (and many else) seem to know the answer for every problem, so I’m just wondering.

Maurits own maurits.tv thats 3-4 year old.

On topic : How would i the buttons look like this :

Create a button and use PANEL.Paint, and create the trapezoid.

Example:
[lua]
local button = vgui.Create(“DButton”, parentPanel)
button:SetPos(x, y)
button:SetSize(w, h)
button.Paint = function()
//Draw trapezoid here
//Draw BUY text after trapezoid
end
button.DoClick = function()
//Do whatever when the button is clicked
end
[/lua]

Pretty sure he needs (wants) the code itself for the buttons, drawpoly tables and all.

No i just want a tip how to make the trapezoid

Oh well…

It has been around 6 years ago when I first got into programming, but many years were nothing more than messing around. It’s only 2 years ago when I decided that programming wasn’t that bad of a career and dedicated more time to it.

Weird… never disabled or touched the private message setting.

Thanks for the info, I’ve been wanting to learn coding since I got my first PC and constantly tried but was too confused, now that I’m bit of older (17 in a month) I finally understand the logic and some of the math behind it, still learning though.

Anyway thanks for the answer!

Private messages is disabled by default

Is that the draw trapezoid function you took out of my hud? :stuck_out_tongue:

Actually yeah, it’s very useful to me so I thought it’d be useful to others too
If you’re not okay with me sharing it then let me know, I’ll edit my post.

Its fine to share it, was just wondering. Just a function. :slight_smile:

Ilyaaa enable private messages

Because apparently trying things on your own isn’t any fun

Code:
[lua]
surface.CreateFont( “BuyFont”,
{
font = “Coolvetica”,
size = 36,
weight = 400
}
)
tex = surface.GetTextureID( “vgui/white” )
surface.SetDrawColor( 133, 0, 0, 255 )
tbl = { {}, {}, {}, {} }
tbl[1][“x”] = 30
tbl[1][“y”] = 10
tbl[2][“x”] = 130
tbl[2][“y”] = 10
tbl[3][“x”] = 130
tbl[3][“y”] = 50
tbl[4][“x”] = 10
tbl[4][“y”] = 50
surface.SetTexture( tex )
surface.DrawPoly( tbl )
draw.SimpleText( “BUY”, “BuyFont”, 45, 15, Color(255,255,255,255) )
[/lua]

Now just put that in button form. (See my last post)

Love you. Works perfect