-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
190 lines (161 loc) · 6.86 KB
/
gui.py
File metadata and controls
190 lines (161 loc) · 6.86 KB
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
import pygame
pygame.init()
FONT = pygame.font.Font(None, 24)
class Widget:
def __init__(self, rect):
self.rect = pygame.Rect(rect)
self.visible = True
self.focus = False
def draw(self, surface):
pass
def handle_event(self, event):
return False
class Button(Widget):
def __init__(self, rect, text, callback):
super().__init__(rect)
self.text = text
self.callback = callback
self.hovered = False
def draw(self, surface):
color = (200, 100, 100) if self.hovered else (150, 50, 50)
pygame.draw.rect(surface, color, self.rect, border_radius=5)
txt_surf = FONT.render(self.text, True, (255, 255, 255))
txt_rect = txt_surf.get_rect(center=self.rect.center)
surface.blit(txt_surf, txt_rect)
def handle_event(self, event):
if event.type == pygame.MOUSEMOTION:
self.hovered = self.rect.collidepoint(event.pos)
elif event.type == pygame.MOUSEBUTTONDOWN:
if self.hovered and event.button == 1:
self.callback()
return True
return False
class Label(Widget):
def __init__(self, rect, text, color=(255, 255, 255)):
super().__init__(rect)
self.text = text
self.color = color
def draw(self, surface):
txt_surf = FONT.render(self.text, True, self.color)
surface.blit(txt_surf, self.rect.topleft)
class InputBox(Widget):
def __init__(self, rect, text=''):
super().__init__(rect)
self.text = text
self.active = False
self.color_inactive = (100, 100, 100)
self.color_active = (255, 255, 255)
def draw(self, surface):
color = self.color_active if self.active else self.color_inactive
pygame.draw.rect(surface, color, self.rect, 2)
txt_surf = FONT.render(self.text, True, color)
surface.blit(txt_surf, (self.rect.x + 5, self.rect.y + 5))
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
self.active = self.rect.collidepoint(event.pos)
elif event.type == pygame.KEYDOWN and self.active:
if event.key == pygame.K_RETURN:
self.active = False
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
return False
class Slider(Widget):
def __init__(self, rect, min_val, max_val, initial, callback=None):
super().__init__(rect)
self.min = min_val
self.max = max_val
self.value = initial
self.callback = callback
self.knob_rect = pygame.Rect(0, 0, 10, rect[3])
self.dragging = False
self.update_knob_pos()
def update_knob_pos(self):
ratio = (self.value - self.min) / (self.max - self.min)
self.knob_rect.centerx = self.rect.x + int(ratio * self.rect.w)
self.knob_rect.centery = self.rect.centery
def draw(self, surface):
pygame.draw.rect(surface, (100, 100, 100), self.rect)
pygame.draw.rect(surface, (200, 200, 200), self.knob_rect)
val_text = FONT.render(f"{self.value:.2f}", True, (255, 255, 255))
surface.blit(val_text, (self.rect.right + 10, self.rect.y))
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN and self.knob_rect.collidepoint(event.pos):
self.dragging = True
elif event.type == pygame.MOUSEBUTTONUP:
self.dragging = False
elif event.type == pygame.MOUSEMOTION and self.dragging:
x = min(max(event.pos[0], self.rect.x), self.rect.x + self.rect.w)
ratio = (x - self.rect.x) / self.rect.w
self.value = self.min + ratio * (self.max - self.min)
self.update_knob_pos()
if self.callback:
self.callback(self.value)
return False
class CheckBox(Widget):
def __init__(self, rect, text, checked=False, callback=None):
super().__init__(rect)
self.checked = checked
self.callback = callback
self.text = text
self.box_rect = pygame.Rect(rect[0], rect[1], rect[3], rect[3])
def draw(self, surface):
pygame.draw.rect(surface, (200, 200, 200), self.box_rect, 2)
if self.checked:
pygame.draw.line(surface, (255, 255, 255), self.box_rect.topleft, self.box_rect.bottomright, 2)
pygame.draw.line(surface, (255, 255, 255), self.box_rect.topright, self.box_rect.bottomleft, 2)
txt_surf = FONT.render(self.text, True, (255, 255, 255))
surface.blit(txt_surf, (self.box_rect.right + 10, self.box_rect.y))
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN and self.box_rect.collidepoint(event.pos):
self.checked = not self.checked
if self.callback:
self.callback(self.checked)
return False
class Dropdown(Widget):
def __init__(self, rect, options, callback=None):
super().__init__(rect)
self.options = options
self.selected = options[0] if options else ''
self.expanded = False
self.callback = callback
def draw(self, surface):
pygame.draw.rect(surface, (100, 100, 100), self.rect)
txt = FONT.render(self.selected, True, (255, 255, 255))
surface.blit(txt, (self.rect.x + 5, self.rect.y + 5))
if self.expanded:
for i, opt in enumerate(self.options):
opt_rect = pygame.Rect(self.rect.x, self.rect.bottom + i * self.rect.height, self.rect.w, self.rect.h)
pygame.draw.rect(surface, (60, 60, 60), opt_rect)
opt_txt = FONT.render(opt, True, (255, 255, 255))
surface.blit(opt_txt, (opt_rect.x + 5, opt_rect.y + 5))
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.expanded = not self.expanded
elif self.expanded:
for i, opt in enumerate(self.options):
opt_rect = pygame.Rect(self.rect.x, self.rect.bottom + i * self.rect.height, self.rect.w, self.rect.h)
if opt_rect.collidepoint(event.pos):
self.selected = opt
self.expanded = False
if self.callback:
self.callback(opt)
break
else:
self.expanded = False
return False
class GUIManager:
def __init__(self):
self.widgets = []
def add(self, widget):
self.widgets.append(widget)
def handle_event(self, event):
for widget in self.widgets:
if widget.visible and widget.handle_event(event):
break
def draw(self, surface):
for widget in self.widgets:
if widget.visible:
widget.draw(surface)