-
Notifications
You must be signed in to change notification settings - Fork 3
/
draw_buttons.py
63 lines (49 loc) · 2.96 KB
/
draw_buttons.py
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
62
63
#All Credits for the following piece of code goes to Glenn Mackintosh
#The following code has been shared by him on the stackoverflow thread "https://stackoverflow.com/questions/61523241/pygame-button-with-rounded-corners-border-radius-argument-does-not-work"
#Thanks to Glenn Mackintosh for sharing this code snippet
import pygame
import pygame.gfxdraw
def draw_rounded_rect(surface, rect, color, corner_radius):
''' Draw a rectangle with rounded corners.
Would prefer this:
pygame.draw.rect(surface, color, rect, border_radius=corner_radius)
but this option is not yet supported in my version of pygame so do it ourselves.
We use anti-aliased circles to make the corners smoother
'''
if rect.width < 2 * corner_radius or rect.height < 2 * corner_radius:
raise ValueError(f"Both height (rect.height) and width (rect.width) must be > 2 * corner radius ({corner_radius})")
# need to use anti aliasing circle drawing routines to smooth the corners
pygame.gfxdraw.aacircle(surface, rect.left+corner_radius, rect.top+corner_radius, corner_radius, color)
pygame.gfxdraw.aacircle(surface, rect.right-corner_radius-1, rect.top+corner_radius, corner_radius, color)
pygame.gfxdraw.aacircle(surface, rect.left+corner_radius, rect.bottom-corner_radius-1, corner_radius, color)
pygame.gfxdraw.aacircle(surface, rect.right-corner_radius-1, rect.bottom-corner_radius-1, corner_radius, color)
pygame.gfxdraw.filled_circle(surface, rect.left+corner_radius, rect.top+corner_radius, corner_radius, color)
pygame.gfxdraw.filled_circle(surface, rect.right-corner_radius-1, rect.top+corner_radius, corner_radius, color)
pygame.gfxdraw.filled_circle(surface, rect.left+corner_radius, rect.bottom-corner_radius-1, corner_radius, color)
pygame.gfxdraw.filled_circle(surface, rect.right-corner_radius-1, rect.bottom-corner_radius-1, corner_radius, color)
rect_tmp = pygame.Rect(rect)
rect_tmp.width -= 2 * corner_radius
rect_tmp.center = rect.center
pygame.draw.rect(surface, color, rect_tmp)
rect_tmp.width = rect.width
rect_tmp.height -= 2 * corner_radius
rect_tmp.center = rect.center
pygame.draw.rect(surface, color, rect_tmp)
def draw_bordered_rounded_rect(surface, rect, color, border_color, corner_radius, border_thickness):
if corner_radius < 0:
raise ValueError(f"border radius ({corner_radius}) must be >= 0")
rect_tmp = pygame.Rect(rect)
center = rect_tmp.center
if border_thickness:
if corner_radius <= 0:
pygame.draw.rect(surface, border_color, rect_tmp)
else:
draw_rounded_rect(surface, rect_tmp, border_color, corner_radius)
rect_tmp.inflate_ip(-2*border_thickness, -2*border_thickness)
inner_radius = corner_radius - border_thickness + 1
else:
inner_radius = corner_radius
if inner_radius <= 0:
pygame.draw.rect(surface, color, rect_tmp)
else:
draw_rounded_rect(surface, rect_tmp, color, inner_radius)