-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
256 lines (201 loc) · 8.85 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
import pygame as pg
import string
class GUI:
BACKGROUND = "back_color"
FOREGROUND = "text_color"
FONT = "font"
BORDER_COLOR = "border_color"
BORDER_SIZE = "border_size"
HOVERED_BACKGROUND = "hover_color"
PRESSED_BACKGROUND = "press_color"
TOGGLED_BACKGROUND = "toggle_color"
default_options = None
default_font = None
@staticmethod
def create_defaults():
font = GUI.default_font or pg.font.SysFont("Comic Sans MS", 20)
GUI.default_options = {
Label.__name__: {
GUI.BACKGROUND: (255, 255, 255),
GUI.FOREGROUND: (0, 0, 0),
GUI.FONT: font,
GUI.BORDER_COLOR: (0, 0, 0),
GUI.BORDER_SIZE: 4
},
Button.__name__: {
GUI.BACKGROUND: (255, 255, 255),
GUI.FOREGROUND: (0, 0, 0),
GUI.FONT: font,
GUI.BORDER_COLOR: (0, 0, 0),
GUI.BORDER_SIZE: 4,
GUI.HOVERED_BACKGROUND: (200, 200, 200),
GUI.PRESSED_BACKGROUND: (100, 100, 100),
},
ToggleButton.__name__: {
GUI.BACKGROUND: (255, 255, 255),
GUI.FOREGROUND: (0, 0, 0),
GUI.FONT: font,
GUI.BORDER_COLOR: (0, 0, 0),
GUI.BORDER_SIZE: 4,
GUI.HOVERED_BACKGROUND: (200, 200, 200),
GUI.PRESSED_BACKGROUND: (100, 100, 100),
GUI.TOGGLED_BACKGROUND: (150, 150, 150)
},
Textbox.__name__: {
GUI.BACKGROUND: (255, 255, 255),
GUI.FOREGROUND: (0, 0, 0),
GUI.FONT: font,
GUI.BORDER_COLOR: (0, 0, 0),
GUI.BORDER_SIZE: 4,
GUI.HOVERED_BACKGROUND: (200, 200, 200),
GUI.PRESSED_BACKGROUND: (100, 100, 100),
}
}
def with_defaults(self, options):
if not GUI.default_options:
GUI.create_defaults()
defaults = GUI.default_options[self.__class__.__name__]
for key in defaults:
if key not in options:
options[key] = defaults[key]
return options
class Label(GUI):
def __init__(self, rect, text, **kwargs):
self.rect = rect
self.text = text
self.rendered_text = None
self.rendered_text_rect = None
self.options = self.with_defaults(kwargs)
self.recreate()
def update(self, event):
return
def render(self, screen):
border = self.options[GUI.BORDER_SIZE]
if border > 0:
pg.draw.rect(screen, self.options[GUI.BORDER_COLOR], self.rect.inflate(border, border))
pg.draw.rect(screen, self.options[GUI.BACKGROUND], self.rect)
screen.blit(self.rendered_text, self.rendered_text_rect)
def set_text(self, text):
self.text = text
self.recreate()
def recreate(self):
self.rendered_text = self.options[GUI.FONT].render(self.text, True, self.options[GUI.FOREGROUND])
self.rendered_text_rect = self.rendered_text.get_rect(center=self.rect.center)
class Textbox(GUI):
valid_text = (string.ascii_letters + string.digits + string.punctuation + " ")
def __init__(self, rect, text, **kwargs):
self.rect = rect
self.text = None
self.text_changed = False
self.buffer = list(text)
self.focused = False
self.draw_cursor = True
self.blink_counter = 0
self.blink_time = 500
self.rendered_text = None
self.rendered_text_rect = None
self.visible_area = None
self.options = self.with_defaults(kwargs)
self.recreate()
def render(self, screen):
border = self.options[GUI.BORDER_SIZE]
if border > 0:
pg.draw.rect(screen, self.options[GUI.BORDER_COLOR], self.rect.inflate(border, border))
pg.draw.rect(screen, self.options[GUI.BACKGROUND], self.rect)
screen.blit(self.rendered_text, self.rendered_text_rect, self.visible_area)
ticks = pg.time.get_ticks()
if ticks - self.blink_counter > self.blink_time:
self.draw_cursor = not self.draw_cursor
self.blink_counter = ticks
if self.draw_cursor and self.focused:
cursor_start = (self.rendered_text_rect.left + self.visible_area.width, self.rendered_text_rect.top)
cursor_end = (self.rendered_text_rect.left + self.visible_area.width, self.rendered_text_rect.bottom)
pg.draw.line(screen, (0, 0, 0), cursor_start, cursor_end, 2)
def update(self, event):
if self.focused and event.type == pg.KEYDOWN:
if event.key in [pg.K_LEFT, pg.K_RIGHT, pg.K_UP, pg.K_DOWN]:
return
elif event.key == pg.K_BACKSPACE and len(self.buffer) > 0:
self.buffer.pop()
self.recreate()
elif event.unicode in self.valid_text:
self.buffer.append(event.unicode)
self.recreate()
elif event.type == pg.MOUSEBUTTONDOWN:
self.focused = self.rect.collidepoint(event.pos)
def set_text(self, text):
self.buffer = list(text)
self.recreate()
def recreate(self):
new_text = "".join(self.buffer)
self.text_changed = new_text != self.text
self.text = new_text
self.rendered_text = self.options[GUI.FONT].render(self.text, True, self.options[GUI.FOREGROUND])
self.rendered_text_rect = self.rendered_text.get_rect(x=self.rect.left + 5, centery=self.rect.centery)
if self.rendered_text_rect.width > self.rect.width - 10:
offset = self.rendered_text_rect.width - (self.rect.width - 10)
self.visible_area = pg.Rect(offset, 0, self.rect.width - 10, self.rendered_text_rect.height)
else:
self.visible_area = self.rendered_text.get_rect()
class Button(GUI):
def __init__(self, rect, text, **kwargs):
self.rect = rect
self.text = text
self.enabled = True
self.hovered = False
self.pressed = False
self.clicked = False
self.rendered_text = None
self.rendered_text_rect = None
self.options = self.with_defaults(kwargs)
self.recreate()
def render(self, screen):
border = self.options[GUI.BORDER_SIZE]
if border > 0:
pg.draw.rect(screen, self.options[GUI.BORDER_COLOR], self.rect.inflate(border, border))
bg_color = self.options[(GUI.PRESSED_BACKGROUND if self.pressed else (
GUI.HOVERED_BACKGROUND if self.hovered else GUI.BACKGROUND))]
pg.draw.rect(screen, bg_color, self.rect)
if not self.enabled:
pg.draw.rect(screen, (100, 100, 100), self.rect)
screen.blit(self.rendered_text, self.rendered_text_rect)
def update(self, event):
if self.enabled:
if event.type == pg.MOUSEMOTION:
self.hovered = self.rect.collidepoint(event.pos)
elif event.type == pg.MOUSEBUTTONDOWN:
self.pressed = self.hovered
self.clicked = False
elif event.type == pg.MOUSEBUTTONUP:
self.clicked = self.hovered and self.pressed
self.pressed = False
def click_handled(self):
self.clicked = False
def set_enabled(self, enabled):
self.enabled = enabled
def set_text(self, text):
self.text = text
self.recreate()
def recreate(self):
self.rendered_text = self.options[GUI.FONT].render(self.text, True, self.options[GUI.FOREGROUND])
self.rendered_text_rect = self.rendered_text.get_rect(center=self.rect.center)
class ToggleButton(Button):
def __init__(self, rect, text, **kwargs):
super().__init__(rect, text, **kwargs)
self.toggled = False
def update(self, event):
super().update(event)
if self.clicked:
self.toggled = not self.toggled
self.click_handled()
def render(self, screen):
border = self.options[GUI.BORDER_SIZE]
if border > 0:
pg.draw.rect(screen, self.options[GUI.BORDER_COLOR], self.rect.inflate(border, border))
bg_color = self.options[GUI.PRESSED_BACKGROUND if self.pressed else (
(GUI.TOGGLED_BACKGROUND if self.toggled else (
GUI.HOVERED_BACKGROUND if self.hovered else GUI.BACKGROUND)))]
pg.draw.rect(screen, bg_color, self.rect)
if not self.enabled:
pg.draw.rect(screen, (100, 100, 100), self.rect)
screen.blit(self.rendered_text, self.rendered_text_rect)