-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
365 lines (273 loc) · 10.8 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#Snaildash is a small game created in the scope of a school project
#Copyright (C) 2022 Louis HEREDERO & Mathéo BENEY
import json
import os
import time
import pygame
from font_manager import FontManager
class GUI:
"""Class managing menus"""
MENUS = ["main", "waiting", "credits", "breakdown", "tutorial", "nameinput"]
def __init__(self):
"""Initializes a GUI instance"""
self.menus = {}
self._menu = self.MENUS[0]
self.visible = True
self.load_menus()
def load_menus(self):
"""Loads all menu files"""
for m in self.MENUS:
with open(os.path.join("assets", "menus", f"{m}.json"), "r", encoding="utf-8") as f:
self.menus[m] = Menu(json.load(f))
def render(self, surf):
"""Renders the active menu
Args:
surf (pygame.Surface): window surface
"""
if self.visible:
self.get_menu().render(surf)
def get_menu(self):
"""Gets the active menu
Returns:
Menu: active menu
"""
return self.menus[self._menu]
def set_menu(self, menu):
"""Sets the active menu
Args:
menu (str): new active menu name
"""
self._menu = menu
def on_mouse_down(self, event):
"""Handles a pygame.MOUSEBUTTONDOWN event
Args:
event (pygame.EVENT): the pygame event
"""
if not self.visible: return
if event.button == 1:
self.get_menu().on_mouse_down(event)
def on_mouse_up(self, event):
"""Handles a pygame.MOUSEBUTTONUP event
Args:
event (pygame.EVENT): the pygame event
"""
if not self.visible: return
if event.button == 1:
self.get_menu().on_mouse_up(event)
def on_key_down(self, event):
if not self.visible: return
self.get_menu().on_key_down(event)
class Menu:
"""Class representing a menu, composed of texts and buttons"""
def __init__(self, data):
"""Initializes a Menu instance
Args:
data (list[dict]): list of components
"""
self.width, self.height = 0, 0
self.components = []
for c in data:
type_ = c["type"]
if type_ == "button":
cls = Button
elif type_ == "text":
cls = Text
elif type_ == "input":
cls = Input
else:
continue
self.components.append(cls(self, **c))
def render(self, surf):
"""Renders this menu
Args:
surf (pygame.Surface): window surface
"""
self.width, self.height = surf.get_size()
for c in self.components:
c.render(surf)
def on_mouse_down(self, event):
"""Handles a pygame.MOUSEBUTTONDOWN event
Args:
event (pygame.EVENT): the pygame event
"""
for c in self.components:
if not isinstance(c, Button): continue
if not c.visible: continue
x,y,w,h = c.rect
if x <= event.pos[0] < x+w and y <= event.pos[1] < y+h:
c.pressed = True
break
def on_mouse_up(self, event):
"""Handles a pygame.MOUSEBUTTONDOWN event
Args:
event (pygame.EVENT): the pygame event
"""
for c in self.components:
if not isinstance(c, Button): continue
if not c.visible: continue
x,y,w,h = c.rect
if x <= event.pos[0] < x+w and y <= event.pos[1] < y+h:
if c.pressed:
pygame.event.post(pygame.event.Event(pygame.USEREVENT+1, name=c.name))
c.pressed = False
def on_key_down(self, event):
for c in self.components:
if not isinstance(c, Input): continue
if not c.visible: continue
c.handle_event(event)
class Button:
"""Class representing a clickable button"""
COLOR = (133, 255, 255)
TXT_COLOR = (0, 0, 0)
def __init__(self, menu, x, y, txt="", name="", width=1, margin=None, pos="relative", color=TXT_COLOR, bg=COLOR, **kwargs):
"""Initializes a Button instance
Args:
menu (Menu): parent menu
x (float): x position factor
y (float): y position factor
txt (str, optional): displayed text. Defaults to "".
name (str, optional): named passed when the button is clicked. Defaults to "".
width (float, optional): width factor. Defaults to 1.
margin (int, optional): if not None, margin in pixels and overrides width. Defaults to None.
pos (str, optional): type of positioning. Either "relative" or "absolute". Defaults to "relative".
color (tuple[int, int, int], optional): text color. Defaults to TXT_COLOR.
bg (tuple[int, int, int], optional): background color. Defaults to COLOR.
"""
self.menu = menu
self.x = x
self.y = y
self.txt = txt
self.name = name
self.width = width
self.margin = margin
self.pos = pos
self.rect = [0,0,0,0]
self.pressed = False
self.color = color
self.bg = bg
self.visible = True
def render(self, surf):
"""Renders this button
Args:
surf (pygame.Surface): window surface
"""
if not self.visible: return
font = FontManager.get("arial", 30)
txt = font.render(self.txt, True, self.color)
width = self.menu.width*self.width
height = txt.get_height()+20
if self.margin is not None:
width = txt.get_width()+2*self.margin
height = txt.get_height()+self.margin
if self.pos == "relative":
x = self.menu.width*self.x - width/2
y = self.menu.height*self.y - height/2
elif self.pos == "absolute":
x = self.x if self.x >= 0 else self.menu.width+self.x-width
y = self.y if self.y >= 0 else self.menu.height+self.y-height
else:
return
self.rect = [x, y, width, height]
pygame.draw.rect(surf, self.bg, self.rect)
surf.blit(txt, [x+width/2-txt.get_width()/2, y+height/2-txt.get_height()/2])
class Text:
"""Class representing a text element"""
COLOR = (255, 255, 255)
def __init__(self, menu, x, y, txt="", font_family="arial", size=30, align="center", bold=False, color=COLOR, **kwargs):
"""Initializes a Text instance
Args:
menu (Menu): parent menu
x (float): x position factor
y (float): y position facor
txt (str, optional): text content. Defaults to "".
font_family (str, optional): font family. Defaults to "arial".
size (int, optional): font size. Defaults to 30.
align (str, optional): text alignment. One of: "left", "center" or "right". Defaults to "center".
bold (bool, optional): whether the text is bold. Defaults to False.
color (tuple[int, int, int], optional): text color. Defaults to COLOR.
"""
self.menu = menu
self.x = x
self.y = y
self.txt = txt
self.font = FontManager.get(font_family, size, bold=bold)
self.align = align
self.color = color
def render(self, surf):
"""Renders this text
Args:
surf (pygame.Surface): window surface
"""
txt = self.font.render(self.txt, True, self.color)
x = self.menu.width*self.x
y = self.menu.height*self.y
y = y-txt.get_height()/2
if self.align == "right":
x -= txt.get_width()
elif self.align == "center":
x -= txt.get_width()/2
surf.blit(txt, [x, y])
class Input:
"""Class representing an input element"""
COLOR = (255, 255, 255)
VALID = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
def __init__(self, menu, x, y, txt="", font_family="arial", size=30, align="center", color=COLOR, maxlen=10, width=0.3, height=0.1, **kwargs):
"""Initializes an Input instance
Args:
menu (Menu): parent menu
x (float): x position factor
y (float): y position facor
txt (str, optional): text content. Defaults to "".
font_family (str, optional): font family. Defaults to "arial".
size (int, optional): font size. Defaults to 30.
align (str, optional): text alignment. One of: "left", "center" or "right". Defaults to "center".
bold (bool, optional): whether the text is bold. Defaults to False.
color (tuple[int, int, int], optional): text color. Defaults to COLOR.
"""
self.menu = menu
self.x = x
self.y = y
self.txt = txt
self.font = FontManager.get(font_family, size)
self.align = align
self.color = color
self.maxlen = maxlen
self.width = width
self.height = height
self.visible = True
def render(self, surf):
"""Renders this text
Args:
surf (pygame.Surface): window surface
"""
txt = self.font.render(self.txt, True, self.color)
x = self.menu.width*self.x
y = self.menu.height*self.y
bx = x - self.width*self.menu.width/2
by = y - self.height*self.menu.height/2
y = y-txt.get_height()/2
if self.align == "right":
x -= txt.get_width()
elif self.align == "center":
x -= txt.get_width()/2
pygame.draw.rect(surf, self.color, (bx, by, self.width*self.menu.width, self.height*self.menu.height), width = 2)
surf.blit(txt, [x, y])
if int(time.time()*2)%2 == 0:
pygame.draw.rect(surf, self.color, [x + txt.get_width(), y, 3, txt.get_height()])
def handle_event(self, event):
"""Handles a pygame.KEYDOWN event
Args:
event (pygame.EVENT): the pygame event
"""
if event.key == pygame.K_BACKSPACE:
self.set_txt(self.txt[:-1])
elif event.unicode in self.VALID:
self.set_txt(self.txt + event.unicode)
def set_txt(self, txt):
"""Sets the current text
Args:
txt (str): new text
"""
if len(txt) > self.maxlen:
txt = txt[:10]
self.txt = txt