-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractive_Tools.py
363 lines (326 loc) · 15.4 KB
/
Interactive_Tools.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
357
358
359
360
361
362
from pygame import gfxdraw
import pygame
from numpy import clip, interp, round
class Label:
def __init__(self, display, font, label, value, position, text_color=(0,0,0),background_color=(220, 220, 220)):
self.display = display
self.font = font
self.position = position
self.text_color = text_color
self.background_color = background_color
self.x, self.y = position[0], position[1]
self.label = font.render(label,True, self.text_color, self.background_color)
self.label_rect = self.label.get_rect()
self.label_rect.bottomleft = self.position
self.value = font.render(str(round(value, 1)),True, self.text_color, background_color)
self.value_rect = self.value.get_rect()
self.value_rect.bottomright = (self.x+80, self.y)
def draw(self, value):
self.display.blit(self.label, self.label_rect)
self.value = self.font.render(str(round(value, 1)),True, self.text_color, self.background_color)
self.value_rect = self.value.get_rect()
self.value_rect.bottomright = (self.x+80, self.y)
self.display.blit(self.value, self.value_rect)
class KeyLabel:
def __init__(self, display, font, label, color, position, radius=4, text_color=(0,0,0),background_color=(220, 220, 220)):
self.display = display
self.font = font
self.color = color
self.position = position
self.x, self.y = self.position[0]+80, self.position[1]-8
self.radius = radius
self.text_color = text_color
self.background_color = background_color
self.label = font.render(label,True, self.text_color, self.background_color)
self.label_rect = self.label.get_rect()
self.label_rect.bottomleft = self.position
def draw(self):
self.display.blit(self.label, self.label_rect)
pygame.draw.circle(self.display, self.color, (self.x, self.y), self.radius)
class Slider:
def __init__(self, display, font, label, position, width=200, height=22, valueRange=(2,10000), initial_value=3, buttonRadius=10, buttonColor=(80,80,80), sliderBarColor=(200,200,200), textColor = (0,0,0), textBGColor=(220, 220, 220), append_text="", value_datatype='int'):
self.display = display
self.font = font
self.position = position
self.width = width
self.height = height
self.valueRange = valueRange
self.buttonRadius = buttonRadius
self.buttonColor = buttonColor
self.sliderBarColor = sliderBarColor
self.textColor = textColor
self.textBGColor = textBGColor
self.append_text = append_text
self.value_datatype = value_datatype
self.sliderBar = pygame.Rect(position, (width, height))
self.minXBarPosition = self.position[0] + self.buttonRadius
self.maxXBarPosition = self.position[0] + self.width - self.buttonRadius - 1
self.minYBarPosition = self.position[1] + self.height / 2 - self.buttonRadius
self.maxYBarPosition = self.position[1] + self.height / 2 + self.buttonRadius + 1
self.centerX = int(interp(initial_value, self.valueRange, (self.minXBarPosition, self.maxXBarPosition)))
self.centerY = int(position[1] + (height/2))
self.pressed = False
self.value = initial_value
self.label = self.font.render(label, True, self.textColor, self.textBGColor)
self.labelRect = self.label.get_rect()
self.labelRect.bottomright = (self.minXBarPosition-15, self.minYBarPosition + self.height - 2)
value = self.font.render(str(int(self.value)), True, self.textColor, self.textBGColor)
self.valueRect = value.get_rect()
self.valueRect.bottomleft = (self.maxXBarPosition+15, self.minYBarPosition + self.height - 3)
def draw(self):
pygame.draw.rect(self.display, self.sliderBarColor, self.sliderBar, border_radius=10)
gfxdraw.circle(self.display, self.centerX, self.centerY, self.buttonRadius, self.buttonColor)
gfxdraw.filled_circle(self.display, self.centerX, self.centerY, self.buttonRadius, self.buttonColor)
self.display.blit(self.label, self.labelRect)
value = self.font.render(str(self.value)+self.append_text, True, self.textColor, self.textBGColor)
self.display.blit(value, self.valueRect)
return self.check_click()
def check_click(self):
action=False
if pygame.mouse.get_pressed()[0]:
mouseX, mouseY = pygame.mouse.get_pos()
if self.pressed:
self.centerX = clip(mouseX, self.minXBarPosition, self.maxXBarPosition)
if self.value_datatype == 'int':
self.value = int(interp(self.centerX, (self.minXBarPosition, self.maxXBarPosition), self.valueRange))
else:
self.value = round(interp(self.centerX, (self.minXBarPosition, self.maxXBarPosition), self.valueRange), 1)
action = True
else:
if self.minXBarPosition < mouseX < self.maxXBarPosition and self.minYBarPosition < mouseY < self.maxYBarPosition:
self.centerX = clip(mouseX, self.minXBarPosition, self.maxXBarPosition)
self.pressed = True
action = True
if self.value_datatype == 'int':
self.value = int(interp(self.centerX, (self.minXBarPosition, self.maxXBarPosition), self.valueRange))
else:
self.value = round(interp(self.centerX, (self.minXBarPosition, self.maxXBarPosition), self.valueRange), 1)
else:
if self.pressed:
action=True
self.pressed = False
return action
class Toggle:
def __init__(self, display, font, label, position, width=40, height=22, initial_value=False, buttonRadius=10, buttonColor=(80,80,80), toggleBarColorOff=(200,200,200), toggleBarColorOn=(28, 217, 28), textColor = (0,0,0), textBGColor=(220, 220, 220)):
self.display = display
self.font = font
self.label = label
self.position = position
self.width = width
self.height = height
self.buttonRadius = buttonRadius
self.buttonColor = buttonColor
self.toggleBarColorOff = toggleBarColorOff
self.toggleBarColorOn = toggleBarColorOn
self.textColor = textColor
self.textBGColor = textBGColor
self.toggleBar = pygame.Rect(position, (width, height))
self.minXBarPosition = self.position[0] + self.buttonRadius
self.maxXBarPosition = self.position[0] + self.width - self.buttonRadius - 1
self.minYBarPosition = self.position[1] + self.height / 2 - self.buttonRadius
self.maxYBarPosition = self.position[1] + self.height / 2 + self.buttonRadius + 1
self.value = initial_value
if self.value:
self.centerX = self.maxXBarPosition
self.color = self.toggleBarColorOn
else:
self.centerX = self.minXBarPosition
self.color = self.toggleBarColorOff
self.centerY = int(position[1] + (height/2))
self.pressed = False
label = self.font.render(self.label, True, self.textColor, self.textBGColor)
self.labelRect = label.get_rect()
self.labelRect.bottomright = (self.minXBarPosition-20, self.minYBarPosition + self.height - 2)
def draw(self):
pygame.draw.rect(self.display, self.color, self.toggleBar, border_radius=10)
gfxdraw.circle(self.display, self.centerX, self.centerY, self.buttonRadius, self.buttonColor)
gfxdraw.filled_circle(self.display, self.centerX, self.centerY, self.buttonRadius, self.buttonColor)
label = self.font.render(self.label, True, self.textColor, self.textBGColor)
self.display.blit(label, self.labelRect)
return self.check_click()
def check_click(self):
action = False
if pygame.mouse.get_pressed()[0]:
mouseX, mouseY = pygame.mouse.get_pos()
if self.toggleBar.collidepoint((mouseX, mouseY)):
if not self.pressed:
if self.value:
self.value = False
self.centerX = self.minXBarPosition
self.color = self.toggleBarColorOff
else:
self.value = True
self.centerX = self.maxXBarPosition
self.color = self.toggleBarColorOn
self.pressed = True
else:
if self.pressed:
self.pressed = False
action = True
return action
class Button:
"""tutorial: https://www.youtube.com/watch?v=8SzTzvrWaAA"""
def __init__(self, display, font, text, width, height, position, elevation=3):
self.display = display
self.font = font
self.text = text
self.elevation = elevation
self.dynamic_elevation = elevation
self.original_y_position = position[1]
self.top_rect = pygame.Rect(position, (width, height))
self.top_color = '#34BF49'
self.bottom_rect = pygame.Rect(position, (width, height))
self.bottom_color = '#297829'
self.text_surf = self.font.render(text, True, '#FFFFFF')
self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)
self.pressed = False
def draw(self):
self.top_rect.y = self.original_y_position - self.dynamic_elevation
self.text_rect.center = self.top_rect.center
self.bottom_rect.midtop = self.top_rect.midtop
self.bottom_rect.height = self.top_rect.height + self.dynamic_elevation
pygame.draw.rect(self.display, self.bottom_color, self.bottom_rect)
pygame.draw.rect(self.display, self.top_color, self.top_rect)
self.display.blit(self.text_surf, self.text_rect)
return self.check_click()
def check_click(self):
action = False
mouse_position = pygame.mouse.get_pos()
if self.top_rect.collidepoint(mouse_position):
self.top_color = '#419E41'
if pygame.mouse.get_pressed()[0]:
self.dynamic_elevation = 0
self.pressed = True
else:
if self.pressed:
# Do stuff
self.dynamic_elevation = self.elevation
action = True
self.pressed = False
else:
self.top_color = '#34BF49'
return action
class StartButton(Button):
def __init__(self, display, font, text, pressed_text, width, height, position, paused=True):
Button.__init__(self, display, font, text, width, height, position)
self.pressed_text = pressed_text
self.paused = paused
def pause(self):
if self.paused:
self.text_surf = self.font.render(self.pressed_text, True, '#FFFFFF')
self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)
else:
self.text_surf = self.font.render(self.text, True, '#FFFFFF')
self.text_rect = self.text_surf.get_rect(center=self.top_rect.center)
self.paused = not self.paused
def check_click(self):
mouse_position = pygame.mouse.get_pos()
if self.top_rect.collidepoint(mouse_position):
self.top_color = '#419E41'
if pygame.mouse.get_pressed()[0]:
self.dynamic_elevation = 0
self.pressed = True
else:
if self.pressed:
# Do stuff
self.pause()
self.dynamic_elevation = self.elevation
self.pressed = False
else:
self.top_color = '#34BF49'
class ResetButton(Button):
def __init__(self, display, font, text, width, height, position, populate):
Button.__init__(self, display, font, text, width, height, position)
self.populate = populate
def reset(self, space, particles, population, initially_infected):
if len(particles) != 0:
for particle in particles:
space.remove(particle.body, particle.shape)
return self.populate(population, initially_infected)
def check_click(self):
action = False
mouse_position = pygame.mouse.get_pos()
if self.top_rect.collidepoint(mouse_position):
self.top_color = '#419E41'
if pygame.mouse.get_pressed()[0]:
self.dynamic_elevation = 0
self.pressed = True
else:
if self.pressed:
self.dynamic_elevation = self.elevation
action = True
self.pressed = False
else:
self.top_color = '#34BF49'
return action
class HoldButton(Button):
def __init__(self, display, font, text, width, height, position, pressed=False, elevation=3):
Button.__init__(self, display, font, text, width, height, position, elevation=elevation)
self.pressed = pressed
self.disable = False
def disable_click(self):
self.disable = True
def check_click(self):
action = False
mouse_position = pygame.mouse.get_pos()
if self.top_rect.collidepoint(mouse_position):
if not self.pressed:
if pygame.mouse.get_pressed()[0]:
self.pressed = True
action = True
if self.disable:
self.pressed = False
self.disable = False
else:
if not self.pressed:
self.top_color = '#34BF49'
else:
self.top_color = '#419E41'
return action
class ActiveButton:
def __init__(self, display, font, texts, widths, height, position, elevation=3):
self.x, self.y = position
self.button1 = HoldButton(display, font, texts[0], widths[0], height, position, True, elevation=elevation)
self.button2 = HoldButton(display, font, texts[1], widths[1], height, (self.x + widths[0], self.y), elevation=elevation)
self.button3 = HoldButton(display, font, texts[2], widths[2], height, (self.x + widths[0] + widths[1], self.y), elevation=elevation)
self.mode = 0
def draw(self):
action = False
if self.button1.draw():
self.mode = 0
self.button2.disable_click()
self.button3.disable_click()
action = True
if self.button2.draw():
self.mode = 1
self.button1.disable_click()
self.button3.disable_click()
action = True
if self.button3.draw():
self.mode = 2
self.button1.disable_click()
self.button2.disable_click()
action = True
return action
class SaveButton(Button):
def __init__(self, display, font, text, width, height, position, save_simulation):
Button.__init__(self, display, font, text, width, height, position)
self.save_simulation = save_simulation
def save(self):
self.save_simulation()
def check_click(self):
action = False
mouse_position = pygame.mouse.get_pos()
if self.top_rect.collidepoint(mouse_position):
self.top_color = '#419E41'
if pygame.mouse.get_pressed()[0]:
self.dynamic_elevation = 0
self.pressed = True
else:
if self.pressed:
self.dynamic_elevation = self.elevation
action = True
self.pressed = False
else:
self.top_color = '#34BF49'
return action