-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gui.py
89 lines (74 loc) · 3.04 KB
/
Gui.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import pygame
class Button:
def __init__(self, text, width, height, pos, win, font):
self.win = win
self.font = font
self.pressed = False
# top rectangle
self.top_rect = pygame.Rect(pos, (width, height))
self.top_color = "#475F77"
# text
self.text_surface = self.font.render(text, True, "#FFFFFF")
self.text_rect = self.text_surface.get_rect(
center=self.top_rect.center)
def draw(self, event_list):
pygame.draw.rect(self.win, self.top_color,
self.top_rect, border_radius=6)
self.win.blit(self.text_surface, self.text_rect)
self.is_clicked(event_list)
def is_clicked(self, event_list):
mouse_pos = pygame.mouse.get_pos()
self.pressed = False
if self.top_rect.collidepoint(mouse_pos):
self.top_color = "#D74B4B"
for event in event_list:
if event.type == pygame.MOUSEBUTTONDOWN:
if pygame.mouse.get_pressed()[0]:
self.pressed = True
else:
self.top_color = "#00FFFF"
def check_pressed(self):
return self.pressed == True
class DropDown():
def __init__(self, color_menu, color_option, x, y, w, h, font, main, options):
self.color_menu = color_menu
self.color_option = color_option
self.rect = pygame.Rect(x, y, w, h)
self.font = font
self.main = main
self.options = options
self.draw_menu = False
self.menu_active = False
self.active_option = -1
def draw(self, surf):
pygame.draw.rect(surf, self.color_menu[self.menu_active], self.rect, 0)
msg = self.font.render(self.main, 1, (0, 0, 0))
surf.blit(msg, msg.get_rect(center=self.rect.center))
if self.draw_menu:
for i, text in enumerate(self.options):
rect = self.rect.copy()
rect.y += (i+1) * self.rect.height
pygame.draw.rect(
surf, self.color_option[1 if i == self.active_option else 0], rect, 0)
msg = self.font.render(text, 1, (0, 0, 0))
surf.blit(msg, msg.get_rect(center=rect.center))
def update(self, event_list):
mpos = pygame.mouse.get_pos()
self.menu_active = self.rect.collidepoint(mpos)
self.active_option = -1
for i in range(len(self.options)):
rect = self.rect.copy()
rect.y += (i+1) * self.rect.height
if rect.collidepoint(mpos):
self.active_option = i
break
if not self.menu_active and self.active_option == -1:
self.draw_menu = False
for event in event_list:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.menu_active:
self.draw_menu = not self.draw_menu
elif self.draw_menu and self.active_option >= 0:
self.draw_menu = False
return self.active_option
return -1