-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.py
315 lines (208 loc) · 8.62 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
"""
this classes are for the GUI (BUTTON , FRAME , Levels_FRAME)
"""
import pygame
import os
font_path = os.path.join("src/fonts", "OCRAEXT.ttf")
font_size = 23
class Button():
def __init__(self, x, y, text,font_size):
self.x = x
self.y = y
self.text = text
self.image = pygame.image.load('src/img/GUI/button.png').convert_alpha()
self.holding_image=pygame.image.load('src/img/GUI/button_holding.png').convert_alpha()
self.locked_image=pygame.image.load('src/img/GUI/button_locked.png').convert_alpha()
self.current_image=self.image
self.holding=False
self.custum_rect=False
self.locked=False
self.font = pygame.font.Font(font_path, font_size)
def get_text(self):
return self.text
def lock(self):
self.image=self.locked_image
self.holding_image=self.locked_image
self.locked=True
def conifig_button(self,image,holding_image,custum_rect):
self.image=image
self.holding_image=holding_image
self.custum_rect=custum_rect
def change_images(self,image,hold_image):
self.image=image
self.holding_image=hold_image
def change_location(self,x,y):
self.x=x
self.y=y
def scale(self, w, h):
self.image = pygame.transform.scale(self.image, (w, h))
def get_rect(self):
rect = self.image.get_rect()
if not self.custum_rect:
rect.center = (self.x, self.y)
return rect
else:
rect.center=self.custum_rect
return rect
def render_text(self,holding):
if not self.locked:
if holding:
self.current_image=self.holding_image
button_text = self.font.render(self.text, True, (255, 255, 255))
else:
button_text = self.font.render(self.text, True, (0, 0, 0))
self.current_image=self.image
else:
self.current_image=self.holding_image
button_text = self.font.render(self.text, True, (255, 255, 255))
button_text_rect = button_text.get_rect()
button_text_rect.center = self.get_rect().center
return button_text, button_text_rect
def place(self, screen):
self.chek_hold()
button_text, button_text_rect = self.render_text(self.holding)
screen.blit(self.current_image, self.get_rect())
screen.blit(button_text, button_text_rect)
def get_width(self):
return self.image.get_width()
def chek_hold(self):
mouse=pygame.mouse.get_pos()
if self.get_rect().collidepoint(mouse):
self.holding=True
else:
self.holding=False
class Frame:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width -100
self.height = height -60
self.image = pygame.image.load('src/img/GUI/LabelFrame.png').convert_alpha()
self.buttons = []
self.text = ""
self.selected_button=None
self.default_lines=[]
self.font = pygame.font.Font(font_path, font_size)
self.bold_font=pygame.font.Font(font_path, font_size)
self.bold_font.set_bold(True)
def write(self, text):
self.text = text
def add_line(self,text,x,y,blod:bool,color,center:bool):
line = {'text': text, 'position_x': x, 'position_y': y,'blod': blod, 'color' : color, 'center':center}
self.default_lines.append(line)
def get_rect(self):
# print(self.image)
rect = self.image.get_rect()
rect.topleft = (self.x, self.y)
return rect
def add_button(self,button):
self.buttons.append(button)
def render_text(self):
words = self.text.split()
lines = []
current_line = ""
line_spacing = 5
for word in words:
test_line = current_line + " " + word if current_line else word
test_size = self.font.size(test_line)
if test_size[0] <= self.width:
current_line = test_line
else:
lines.append(current_line)
current_line = word
lines.append(current_line)
rendered_lines = [self.font.render(line, True, (255, 255, 255)) for line in lines]
text_height = sum(line.get_height() for line in rendered_lines) + (line_spacing * (len(rendered_lines) - 1))
y_offset = (self.height - text_height) // 2
text_rects = []
current_y = self.y + y_offset
for line in rendered_lines:
text_rects.append(line.get_rect(topleft=(self.x+50, current_y)))
current_y += line.get_height() + line_spacing
return rendered_lines, text_rects
def confing(self,image):
self.image=image
def draw(self, screen):
rendered_lines, text_rects = self.render_text()
screen.blit(self.image, self.get_rect())
for line, text_rect in zip(rendered_lines, text_rects):
screen.blit(line, text_rect)
for line in self.default_lines:
if not line['blod']:
line_txt=self.font.render(line['text'],True,line['color'])
else:
line_txt=self.bold_font.render(line['text'],True,line['color'])
line_rect=line_txt.get_rect()
if line['center']:
line_rect.center=(line['position_x'],line['position_y'])
else:
line_rect.topleft=(line['position_x'],line['position_y'])
screen.blit(line_txt,line_rect)
def draw_buttons(self,screen):
self.selected_button=None
for button in self.buttons:
if button.holding:
self.selected_button=button
button.place(screen)
def get_selected_btn(self):
return 0
def get_buttons(self):
return self.buttons
class Levels_Frame:
def __init__(self, x, y, width, height, rows, cols,levels):
self.x = x
self.y = y
self.width = width
self.height = height
self.rows = rows
self.cols = cols
self.image = pygame.image.load('src/img/GUI/LabelFrame.png').convert_alpha()
self.level_image=pygame.image.load('src/img/GUI/level.png').convert_alpha()
self.locked_image=pygame.image.load('src/img/GUI/level_locked2.png').convert_alpha()
self.hold_image=pygame.image.load('src/img/GUI/level_hold.png').convert_alpha()
self.levels=[]
self.image_width = 600 // cols
self.image_height = 225 // rows
self.buttons=[]
self.selected_button=None
for level in levels:
self.add_level(level)
for row in range(self.rows):
for col in range(self.cols):
index = row * self.cols + col
if index < len(self.levels):
center_rect=(self.x + col * (self.image_width + 10)+95, self.y + row * (self.image_height + 10)+70)
image=self.level_image
imag_rect=image.get_rect()
imag_rect.center=(center_rect)
level_button=Button(0,0,self.levels[index].get_number(),22)
level_button.conifig_button(self.level_image,self.hold_image,center_rect)
if not self.levels[index].locked:
level_button.conifig_button(self.level_image,self.hold_image,center_rect)
else:
level_button=Button(0,0,'',22)
level_button.conifig_button(self.locked_image,self.locked_image,center_rect)
self.buttons.append(level_button)
def add_level(self, level):
self.levels.append(level)
def get_buttons(self):
return self.buttons
def get_rect(self,index):
rect = self.buttons[index].get_rect()
return rect
def chek_hold(self,rect):
mouse=pygame.mouse.get_pos()
if rect.collidepoint(mouse):
return True
else:
return False
def draw(self, screen):
screen.blit(self.image, (self.x, self.y))
def get_selected_btn(self):
return self.selected_button
def draw_buttons(self,screen):
self.selected_button=None
for button in self.buttons:
if button.holding:
self.selected_button=button
button.place(screen)