-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
231 lines (177 loc) · 6.74 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
import json
import pygame
import time
from font_manager import FontManager
from i18n import I18n
from path import Path
class GUI:
MENUS = ["main", "pause"]
def __init__(self, manager):
self.mgr = manager
self.menus = {}
self._menu = self.MENUS[0]
self.visible = True
self.load_menus()
def load_menus(self):
for m in self.MENUS:
with open(Path("assets", "menus", f"{m}.json"), "r", encoding="utf-8") as f:
self.menus[m] = Menu(json.load(f))
def render(self, surf):
if self.visible:
self.get_menu().render(surf)
def get_menu(self):
return self.menus[self._menu]
def set_menu(self, menu):
self._menu = menu
def on_mouse_down(self, event):
if not self.visible: return
if event.button == 1:
self.get_menu().on_mouse_down(event)
def on_mouse_up(self, 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)
def hide(self):
self.visible = False
def show(self):
self.visible = True
class Menu:
def __init__(self, data):
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):
self.width, self.height = surf.get_size()
for c in self.components:
c.render(surf)
def on_mouse_down(self, 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):
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:
COLOR = (103, 170, 25)
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):
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):
if not self.visible: return
font = FontManager.get("arial", 30)
txt = font.render(I18n.get(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:
COLOR = (255, 255, 255)
def __init__(self, menu, x, y, txt="", font_family="arial", size=30, align="center", bold=False, color=COLOR, **kwargs):
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):
txt = self.font.render(I18n.get(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:
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):
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):
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):
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):
if len(txt) > self.maxlen:
txt = txt[:10]
self.txt = txt