-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.py
159 lines (127 loc) · 5.11 KB
/
menu.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import pygame
pygame.init()
WINDOW_SIZE = (1000, 1000)
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("ToGoodToGo - Scrapper")
BG_COLOR = (25, 25, 25)
TEXT_COLOR = (48, 127, 145)
OUTLINE_COLOR = (48, 127, 145)
HEADERS_COLOR = (28, 213, 255)
# example shops list from TgtgClient
shops = [
"Shop 0",
"Shop 1",
"Shop 2",
"Shop 3",
"Shop 4",
"Shop 5",
"Shop 6",
"Shop 7",
"Shop 8",
"Shop 9",
"Shop 10",
]
class Menu:
def __init__(self, shops):
# x and y position of mouse for buttons
self.x = 0
self.y = 0
# running state
self.running = True
# available shops list
self.shops = shops
# pressable buttons
self.buttons = []
# chosen buttons
self.chosen = []
# chosen stores names
self.chosen_stores = []
# cooldown
self.cooldown = 100
self.last = pygame.time.get_ticks()
def draw_menu(self):
# set color to bg of menu
screen.fill(BG_COLOR)
# set font
font = pygame.font.SysFont('Sans', 32)
header = font.render('Choose Your favourite shops', False, HEADERS_COLOR, BG_COLOR)
continue_button = font.render('Continue', False, HEADERS_COLOR, BG_COLOR)
headerRect = header.get_rect()
headerRect.center = (WINDOW_SIZE[0] / 2, 50)
continueRect = continue_button.get_rect()
continueRect.center = (WINDOW_SIZE[0] / 2, WINDOW_SIZE[1] - 50)
screen.blit(header, headerRect)
screen.blit(continue_button, continueRect)
font = pygame.font.SysFont('Calibri', 24)
if len(self.shops) <= 15:
x_spacing = WINDOW_SIZE[0] / 2
else:
x_spacing = WINDOW_SIZE[0] / 4
y_spacing = 120
counter = 0
for shop in self.shops:
if shop not in self.chosen_stores:
text_color = TEXT_COLOR
else:
text_color = (19, 191, 36)
temp = font.render(shop, False, text_color, BG_COLOR)
tempRect = temp.get_rect()
tempRect.center = (x_spacing, y_spacing)
screen.blit(temp, tempRect)
self.buttons.append(tempRect)
y_spacing += 50
counter += 1
if counter == 16:
y_spacing = 120
x_spacing = WINDOW_SIZE[0] / 4 * 3
offset = 8
for text in self.buttons:
if text in self.chosen:
outline_color = (19, 191, 36)
else:
outline_color = OUTLINE_COLOR
#top horizontal lines
pygame.draw.line(screen, color = outline_color,\
start_pos = (text.bottomleft[0] - offset, text.topleft[1] - offset),\
end_pos = (text.bottomright[0] + offset, text.topleft[1] - offset))
#bottom horizontal lines
pygame.draw.line(screen, color = outline_color,\
start_pos = (text.bottomleft[0] - offset, text.bottomleft[1] + offset),\
end_pos = (text.bottomright[0] + offset, text.bottomleft[1] + offset))
#left vertical lines
pygame.draw.line(screen, color = outline_color,\
start_pos = (text.bottomleft[0] - offset, text.topleft[1] - offset),\
end_pos = (text.bottomleft[0] - offset, text.bottomleft[1] + offset))
#right vertical lines
pygame.draw.line(screen, color = outline_color,\
start_pos = (text.bottomright[0] + offset, text.topright[1] - offset),\
end_pos = (text.bottomright[0] + offset, text.bottomright[1] + offset))
pygame.display.update()
def runMenu(shops):
menu = Menu(shops)
while menu.running:
menu.x, menu.y = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
menu.running = False
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
for button in menu.buttons:
if menu.x > button.left and menu.x < button.right and menu.y > button.top and menu.y < button.bottom:
now = pygame.time.get_ticks()
if now - menu.last >= menu.cooldown:
menu.last = now
if button not in menu.chosen:
menu.chosen.append(button)
menu.chosen_stores.append(shops[menu.buttons.index(button)])
else:
menu.chosen.remove(button)
menu.chosen_stores.remove(shops[menu.buttons.index(button)])
if menu.x > 436 and menu.x < 565 and menu.y > 932 and menu.y < 968:
menu.running = False
pygame.quit()
try:
menu.draw_menu()
except:
pass
return menu.chosen_stores