This repository has been archived by the owner on Jan 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
356 lines (273 loc) · 11.1 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
'''
Author: Kyle Charters
Description:
The gui module contains graphical user interface utilities
Contents:
- Label
- AlignedLabel
- Button
- TextButton
- ImageButton
- TextMenu
- ImageMenu
Notes:
None
'''
import pygame
import file
import utility
class Label(object):
def __init__(self, font, text, antialias, color, location):
'''
Description:
This gui element holds text
Parameters:
font: The text's font
text: The text to display
antialias: Whether or not to use antialiasing on the text
color: The color of the text
location: The location of the text
Notes:
This is essentially a class based wrapper for the pygame font.render method
'''
self.font = font
self.text = text
self.antialias = antialias
self.color = color
self.location = location
self.label = font.render(text, antialias, color)
def width(self):
return self.label.get_width()
def height(self):
return self.label.get_height()
def setLocation(self, location):
self.location = location
def setText(self, text):
self.text = text
self.label = self.font.render(text, self.antialias, self.color)
def setAntialias(self, antialias):
self.antialias = antialias
self.label = self.font.render(self.text, antialias, self.color)
def setColor(self, color):
self.color = color
self.label = self.font.render(self.text, self.antialias, color)
def render(self, surface):
surface.blit(self.label, self.location)
class AlignedLabel(Label):
def __init__(self, font, text, antialias, color, alignment, location, yoffset=0, xoffset=0):
'''
Description:
This gui element holds text, and supports different alignments from the point
Parameters:
font: The text's font
text: The text to display
antialias: Whether or not to use antialiasing on the text
color: The color of the text
alignment: The alignment of the text
location: The location of the text
yoffset: The offset of location on the y-axis
xoffset: The offset of location on the x-axis
Notes:
This class extends Label
Alignment from point:
0 = Text on right
1 = Text in middle
2 = Text on left
'''
super().__init__(font, text, antialias, color, location)
self.yoffset = yoffset
self.xoffset = xoffset
self.setAlignment(alignment)
def setText(self, text):
super().setText(text)
self.setAlignment(self.alignment)
def setLocation(self, location):
super().setLocation(location)
self.setAlignment(self.alignment)
def setAlignment(self, alignment):
self.alignment = alignment
self.xlocation = self.location[0]
self.ylocation = self.location[1] - (self.label.get_height() / 2) + self.yoffset
if self.alignment == 0:
self.xlocation += self.xoffset
elif self.alignment == 1:
self.xlocation -= (self.label.get_width() / 2) - self.xoffset
elif self.alignment == 2:
self.xlocation -= self.label.get_width() - self.xoffset
def render(self, surface):
surface.blit(self.label, (self.xlocation, self.ylocation))
class Button(object):
def __init__(self, rect):
'''
Description:
This gui element detects mouse collision in a specified area
Parameters:
rect: The rectangle that the button occupies
Notes:
This class does not render anything
'''
self.rect = pygame.Rect(rect)
self.selected = False
def setLocation(self, x, y):
self.rect.x = x
self.rect.y = y
def move(self, x, y):
self.rect = self.rect.move(x, y)
def setDimension(self, width, height):
self.rect.width = width
self.rect.height = height
def update(self, mousepos):
self.selected = self.rect.collidepoint(mousepos)
return self.selected
class TextButton(Button):
def __init__(self, font, text, color, highlightcolor, rect):
'''
Description:
This gui element detects mouse collision in a specified area, and renders a solid rectangle with text in the middle
Parameters:
font: The text's font
text: The text to display
color: The color of the button
highlightccolor: The color of the button when the mouse collides with the rectangle
rect: The rectangle that the button occupies
Notes:
None
'''
super().__init__(rect)
self.label = AlignedLabel(font, text, True, (0, 0, 0), 1, self.rect.center)
self.color = color
self.highlightcolor = highlightcolor
def getText(self):
return self.label.text
def setText(self, text):
self.label.setText(text)
def setCol(self, color, highlightcolor):
self.color = color
self.highlightcolor = highlightcolor
def setLocation(self, x, y):
super().setLocation(x, y)
self.label.setLocation(self.rect.center)
def move(self, x, y):
super().move(x, y)
self.label.setLocation(self.rect.center)
def setDimension(self, dimension):
super().setDimension(dimension)
self.label.setDimension(self.rect.center)
def render(self, surface):
if self.selected:
pygame.draw.rect(surface, self.highlightcolor, self.rect)
else:
pygame.draw.rect(surface, self.color, self.rect)
self.label.render(surface)
class ImageButton(Button):
def __init__(self, image, rect):
'''
Description:
This gui element detects mouse collision in a specified area, and renders an image
Parameters:
image: The image to display
rect: The rectangle that the button occupies
Notes:
None
'''
super().__init__(rect)
self.setImage(image)
#Creates a blank white texture with a transparency of 80
self.selection = pygame.Surface(self.rect.size)
self.selection.set_alpha(80)
self.selection.fill((255, 255, 255))
def setImage(self, image):
#Image value can either be string or already loaded image
if isinstance(image, str):
self.image = file.loadImage(image)
else:
self.image = image
self.image = utility.aspect(self.image, self.rect.size)
def setDimension(self, dimension):
super().setDimension(self, dimension)
self.image = utility.aspect(self.image, self.rect.size)
def render(self, surface):
surface.blit(self.image, self.rect.topleft)
if self.selected:
surface.blit(self.selection, self.rect.topleft)
class TextMenu(object):
def __init__(self, parentdim, font, buttondim, spacing, offset, buttons):
'''
Description:
This gui element creates a list of buttons from the center of the parent dimension and down
Parameters:
parentdim: The dimensions of the parent
font: The text's font
buttondim: The dimensions of the buttons
spacing: The distance between buttons
offset: The vertical offset
buttons: A list of 3 element tuples that make up the buttons
Notes:
The 3 element button tuples are:
(Text, Color, HighlightColor)
'''
self.buttons = []
for position, button in enumerate(buttons):
self.buttons.append(TextButton(font, button[0], button[1], button[2],
((parentdim[0] / 2) - (buttondim[0] / 2),
(parentdim[1] / 2) + ((buttondim[1] + spacing) * position) + offset,
buttondim[0], buttondim[1])))
def setDimensions(self, buttondim, parentdim, spacing, offset):
for position in range(len(self.buttons)):
self.getButton(position).setLocation(
(parentdim[0] / 2) - (buttondim[0] / 2),
(parentdim[1] / 2) + ((buttondim[1] + spacing) * position) + offset)
def scroll(self, amount):
for button in self.buttons:
button.move(0, amount)
def getButton(self, pos):
return self.buttons[pos]
def getSize(self):
return len(self.buttons)
def update(self, mousepos):
result = None
for position, button in enumerate(self.buttons):
if button.update(mousepos):
result = position
return result
def render(self, surface):
for button in self.buttons:
button.render(surface)
class ImageMenu(object):
def __init__(self, parentrect, buttondim, columns, images):
'''
Description:
This gui element creates a list of buttons from the center of the parent dimension and down
Parameters:
parentrect: The dimensions of the parent
buttondim: The dimensions of the buttons
columns: The amount of columns inside teh menu
buttons: A list of images / strings
Notes:
None
'''
self.rect = pygame.Rect(parentrect)
self.buttondim = buttondim
self.columns = columns
self.spacing = (self.rect.width - buttondim * columns) / (columns + 1)
self.setImages(images)
def setImages(self, images):
#The operator // means integer division, it automatically finds the floor of the number
self.rect.height = self.spacing + (self.spacing + self.buttondim) * (max(len(images), self.columns) // self.columns)
self.buttons = []
for position, image in enumerate(images):
x = self.rect.left + self.spacing + (self.spacing + self.buttondim) * (position % self.columns)
y = self.rect.top + self.spacing + (self.spacing + self.buttondim) * (position // self.columns)
self.buttons.append(ImageButton(image, (x, y, self.buttondim, self.buttondim)))
def scroll(self, amount):
for button in self.buttons:
button.move(0, amount)
def update(self, mousepos):
result = None
for position, button in enumerate(self.buttons):
if button.update(mousepos):
result = position
return result
def render(self, surface):
for button in self.buttons:
button.render(surface)