forked from Be1zebub/Small-GLua-Things
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle-button.lua
61 lines (51 loc) · 2.03 KB
/
circle-button.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- https://github.com/Be1zebub/Small-GLua-Things/blob/master/example/circle-button.lua
-- Circle button example
-- DrawPoly rendering & hover collision
-- in fact, using this example, you can make any shapes - for example, a hexagon or a triangle.
-- eg hexagon math is the same as circle, but 6 vertex's & 30deg rotation - to check collision, just make sure cursorPos withIn
-- other primitives implementation is even simpler
local math_Approach, sin, cos, rad = math.Approach, math.sin, math.cos, math.rad
local function GeneratePolyCircle(x, y, radius, quality)
local circle = {}
local tmp = 0
local s, c
for i = 1, quality do
tmp = rad(i * 360) / quality
s = sin(tmp)
c = cos(tmp)
circle[i] = {
x = x + c * radius,
y = y + s * radius,
u = (c + 1) / 2,
v = (s + 1) / 2
}
end
return circle
end
local poly_circle = GeneratePolyCircle(32, 32, 32, 64)
concommand.Add("circlebtn_exapmle", function()
local btn = vgui.Create("DButton")
btn:SetText("")
btn:SetSize(64, 64)
btn:Center()
btn:MakePopup()
btn.DoClick = function(self)
if not self.CircleHover then return end
self:Remove()
end
btn.DrawAlpha = 0
btn.Paint = function(self, w, h)
self.DrawAlpha = math_Approach(self.DrawAlpha, self.CircleHover and 255 or 50, FrameTime()*650)
draw.NoTexture()
surface.SetDrawColor(ColorAlpha(color_white, self.DrawAlpha))
surface.DrawPoly(poly_circle)
local _, szy = draw.SimpleText("Circle Button", "DermaDefault", w/2, h/2, Color(0, 0, 0), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.SimpleText("Example", "DermaDefault", w/2, h/2 +szy, Color(0, 0, 0), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end
local radius = btn:GetWide()/2
btn.Think = function(self)
local curx, cury = self:CursorPos()
self.CircleHover = math.Distance(curx, cury, radius, radius) < radius
self:SetCursor(self.CircleHover and "hand" or "arrow")
end
end)