-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
352 lines (204 loc) · 9.43 KB
/
ui.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
from misc import (
timer,
sim_to_screen,
screen_to_sim,
pos_in_bounds
)
import pygame
# UI ELEMENTS ===============================================================
class Button:
def __init__(self, text, bounds, is_selected=False, img=None, func=None, args=None, surf=None, font=None):
self.text = text
self.bounds = bounds
self.is_selected = is_selected
self.has_updated = False
#surface
self.surf = surf
self.font = font
#image
self.img = img
#colors for display
self.color1 = (0,0,0)
self.color2 = (55,55,55)
self.color3 = (255,255,255)
self.color = self.color3 if self.is_selected else self.color1
self.text_color = self.color1 if self.is_selected else self.color3
#func is function object
self.func = func
self.args = args
def interact(self, position=(0,0), pressed=False):
self.has_updated = False
if self.is_selected:
return
#hovering over button
if pos_in_bounds(position, self.bounds) :
if pressed:
#call function with args
if self.func is not None:
self.func(*self.args)
self.select()
self.color = self.color3
self.text_color = self.color1
self.has_updated = True
else:
self.color = self.color2
self.text_color = self.color3
else:
self.color = self.color1
self.text_color = self.color3
def draw(self):
x_max, x_min, y_max, y_min = self.bounds
width = x_max - x_min
height = y_max - y_min
#draw filled rectangle
pygame.draw.rect(self.surf, self.color, (x_min,y_min, width, height), width=0)
#draw bounding rectangle
pygame.draw.rect(self.surf, (255,255,255), (x_min, y_min, width, height), width=2)
if self.img is None:
#draw text
text = self.font.render(self.text , True, self.text_color)
self.surf.blit(text, (x_min + 10 , y_min + height/6 ))
def select(self):
self.is_selected = True
def deselect(self):
self.is_selected = False
class ButtonArray:
def __init__(self, buttons=[]):
self.buttons = buttons
self.currently_selected = None
self.has_updated = False
for btn in self.buttons:
if btn.is_selected:
self.currently_selected = btn
def interact(self, position=(0,0), pressed=False):
self.has_updated = False
#handle button interactions
for btn in self.buttons:
btn.interact(position, pressed)
if btn.has_updated:
self.currently_selected = btn
self.has_updated = True
#resolve selections
for btn in self.buttons:
if btn != self.currently_selected:
btn.deselect()
def draw(self):
#draw every button
for btn in self.buttons:
btn.draw()
## KEYBOARD INPUT ==================================================================
class KeyboardInput:
def __init__(self, text, value, bounds, surf=None, font=None):
self.text = text
self.bounds = bounds
#surface
self.surf = surf
self.font = font
self.is_selected = False
#colors for display
self.color1 = (0,0,0)
self.color2 = (55,55,55)
self.color3 = (255,255,255)
self.color = self.color1
self.text_color = self.color3
#value
self.value_str = str(value)
self.value = value
self.key_event_dict = {pygame.K_1 : "1",
pygame.K_2 : "2",
pygame.K_3 : "3",
pygame.K_4 : "4",
pygame.K_5 : "5",
pygame.K_6 : "6",
pygame.K_7 : "7",
pygame.K_8 : "8",
pygame.K_9 : "9",
pygame.K_0 : "0",
pygame.K_e : "e",
pygame.K_PERIOD : ".",
pygame.K_MINUS : "-",
}
def select(self):
self.is_selected = True
def deselect(self):
self.is_selected = False
def interact(self, position=(0,0), pressed=False, events=[]):
if self.is_selected:
self.color = self.color3
self.text_color = self.color1
if pressed:
self.deselect()
self.color = self.color1
self.text_color = self.color3
for event in events:
if event in self.key_event_dict:
self.value_str += self.key_event_dict[event]
if event == pygame.K_BACKSPACE and len(self.value_str) > 0:
self.value_str = self.value_str[:-1]
else:
self.color = self.color1
self.text_color = self.color3
if pos_in_bounds(position, self.bounds):
if pressed:
self.select()
self.color = self.color3
self.text_color = self.color1
if not self.is_selected:
self.color = self.color2
self.text_color = self.color3
def get_value(self):
if len(self.value_str) > 0:
try:
self.value = eval(self.value_str)
return self.value
except:
return self.value
else:
return self.value
def draw(self):
x_max, x_min, y_max, y_min = self.bounds
width = x_max - x_min
height = y_max - y_min
#draw filled rectangle
pygame.draw.rect(self.surf, self.color, (x_min,y_min, width, height), width=0)
#draw bounding rectangle
pygame.draw.rect(self.surf, (255,255,255), (x_min, y_min, width, height), width=2)
#draw text
text = self.font.render(self.text + self.value_str , True, self.text_color)
self.surf.blit(text, (x_min + 10 , y_min + height/6 ))
class Menu:
def __init__(self, interactables, descriptions, title, background):
self.interactables = interactables
self.descriptions = descriptions
self.title = title
self.background = background
class Grid:
"""
draw grid for cartesian coordinate system
"""
def __init__(self, sim_bounds, nx, ny, surf, font):
self.sim_bounds = sim_bounds
self.nx = nx
self.ny = ny
self.surf = surf
self.font = font
self.color = (55, 55, 55)
def draw(self):
x_max, x_min, y_max, y_min = self.sim_bounds
res_x, res_y = self.surf.get_size()
for n in range(self.nx):
x = (n + 1) / (self.nx + 1) * res_x
x_sim ,_ = screen_to_sim(x, 0, self.sim_bounds, (res_x, res_y))
#draw line
pygame.draw.line(self.surf, self.color, (x, 0), (x, res_y), 2)
#draw text
text = self.font.render(f"{round(x_sim, 3)}" , True, self.color)
self.surf.blit(text, (x+5, 0))
for n in range(self.ny):
y = (n + 1) / (self.ny + 1) * res_y
_, y_sim = screen_to_sim(0, y, self.sim_bounds, (res_x, res_y))
#draw line
pygame.draw.line(self.surf, self.color, (0, y), (res_x, y), 2)
#draw text
text = self.font.render(f"{round(y_sim, 3)}" , True, self.color)
self.surf.blit(text, (5, y))