-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicons.py
More file actions
500 lines (404 loc) · 16.1 KB
/
icons.py
File metadata and controls
500 lines (404 loc) · 16.1 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import pygame
import updateComments
import linkComments
BLACK = (0, 0, 0)
LIGHT_BLUE = (0, 255, 255)
DARK_BLUE = (0, 100, 100)
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
ORANGE = (255, 100, 0)
WHITE = (240, 240, 240)
if not pygame.font.get_init():
pygame.font.init()
font = pygame.font.SysFont('chalkduster.ttf', 18)
def xy_place_in_desks(place, desk_layout):
x, y = place // desk_layout[0], place % desk_layout[0]
print(x, y)
return x, y
def screen_position_from_xy_and_size(x, y, desk_layout, height_width):
def rescale(t, real_length, given_length):
return t * real_length / given_length
return [rescale(z, r, g) for z, r, g in zip((x, y), height_width, desk_layout)]
def pos_from_place_in_desks(place, desk_layout, height_width):
return screen_position_from_xy_and_size(*xy_place_in_desks(place, desk_layout),
height_width, desk_layout)
def deltas(xy0, xy1):
return xy0[0] - xy1[0], xy0[1] - xy1[1]
def distance2(xy0, xy1):
dx, dy = deltas(xy0, xy1)
return dx ** 2 + dy ** 2
def nearest_desk(loadsa_desks, xy):
desks = {d: distance2(d.home, xy) for d in loadsa_desks}
return min(desks, key=desks.get)
class ParentDesk(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.home = (1e10, 1e10)
self.sliding = False
self.is_swapping = False
self.text_editor_active = False
def __str__(self):
return "ParentDesk"
def update(self, *args, **kwargs):
pass
def move(self, *args, **kwargs):
pass
def clicked(self, *args, **kwargs):
return UnclickedDesk(), *args
def unclicked(self, *args, **kwargs):
return UnclickedDesk()
def bothered(self, other):
pass
def unbothered(self):
pass
def check_collisions(self, *args, **kwargs):
return self
def check_new(self, *args, **kwargs):
return self
def append(self, selected_desks):
return selected_desks
def handle_keydown(self, event, selected_desks):
pass
def student_name(self):
return ""
def set_student_name(self, name):
raise TypeError(f"{type(self).__name__} cannot be assigned a student")
def is_absent(self):
return False
def toggle_absent(self):
return self
class UnclickedDesk(ParentDesk):
def __init__(self):
super().__init__()
def __str__(self):
return "UnclickedDesk"
class UnselectedDesk(ParentDesk):
def __init__(self):
super().__init__()
def __str__(self):
return "UnselectedDesk"
class Desk(ParentDesk):
def __init__(self, place, name, desk_layout, height_width, desk_id=None):
super().__init__()
self.color_default = YELLOW
self.color = self.color_default
self.color_selected = LIGHT_BLUE
self.desk_layout = desk_layout
self.height_width = height_width
self.size = (height_width[0] // desk_layout[0], height_width[1] // desk_layout[1])
self.place = place
self.desk_id = desk_id
self.pos = screen_position_from_xy_and_size(*place, desk_layout, height_width)
self.home = self.pos
self.target_for_sliding = self.pos
self.name_img = font.render(name, True, (0, 0, 0))
self.button_down = False
self.rect = pygame.Rect(*self.pos, *self.size)
self.sliding = False
self.changing_position = False
def __str__(self):
student_name = self.student_name()
if student_name:
return "Desk: " + student_name
return type(self).__name__
def update(self, surface):
if self.sliding:
self.slide()
self.rect = pygame.Rect(*self.pos, *self.size)
if not hasattr(surface, "blit"):
return
pygame.draw.rect(surface, self.color, self.rect)
center_pos = (self.rect.left + self.rect.width // 2, self.rect.top + self.rect.height // 2)
name_pos = (center_pos[0] - self.name_img.get_width() // 2, center_pos[1] - self.name_img.get_height() // 2)
surface.blit(self.name_img, name_pos)
def bothered(self, target):
self.is_swapping = True
self.color = ORANGE
self.target_for_sliding = target.home
self.sliding = True
return self
def unbothered(self):
self.is_swapping = False
self.color = self.color_default
self.target_for_sliding = self.home
self.sliding = True
self.changing_position = False
def move(self, dx, dy):
self.pos = (self.pos[0] + dx, self.pos[1] + dy)
def slide(self):
dx, dy = deltas(self.target_for_sliding, self.pos)
if dx ** 2 + dy ** 2 < 36:
self.pos = self.target_for_sliding
self.sliding = False
else:
self.move(dx / 10, dy / 10)
def check_collisions(self, desks, selected_desk):
collisions = pygame.sprite.spritecollide(self, desks, False)
collisions.remove(self)
near_desks = [c for c in collisions if not c.sliding]
# print(near_desks)
return self.check_new(near_desks, selected_desk, self.pos)
def check_new(self, desks, selected_desk, xy):
return nearest_desk([selected_desk] + desks, xy)
def unclicked(self, other):
self.button_down = False
self.sliding = True
if self.changing_position:
self.home, other.home = other.home, self.home
self.desk_id, other.desk_id = other.desk_id, self.desk_id
self.unbothered()
other.unbothered()
return UnclickedDesk()
@classmethod
def create_desk(cls, place, name, desk_layout, height_width, desk_id=None):
if name == 'empty':
return EmptyDesk(place, desk_layout, height_width, desk_id=desk_id)
else:
return FilledDesk(place, name, desk_layout, height_width, desk_id=desk_id)
def _copy_state_to(self, other):
other.pos = self.pos
other.home = self.home
other.target_for_sliding = self.target_for_sliding
other.button_down = self.button_down
other.sliding = self.sliding
other.changing_position = self.changing_position
other.rect = pygame.Rect(*other.pos, *other.size)
return other
class FilledDesk(Desk):
def __init__(self, place, name, desk_layout, height_width, desk_id=None):
super().__init__(place, name, desk_layout, height_width, desk_id=desk_id)
self.name = name
self.absent_today = False
def student_name(self):
return self.name
def set_student_name(self, name):
if not name:
return self._copy_state_to(
EmptyDesk(self.place, self.desk_layout, self.height_width, desk_id=self.desk_id)
)
self.name = name
self.name_img = font.render(name, True, (0, 0, 0))
return self
def is_absent(self):
return self.absent_today
def toggle_absent(self):
self.absent_today = not self.absent_today
self._apply_attendance_colors()
return self
def mark_absent(self):
self.absent_today = True
self._apply_attendance_colors()
return self
def mark_present(self):
self.absent_today = False
self._apply_attendance_colors()
return self
def _apply_attendance_colors(self):
if self.absent_today:
self.color_default = WHITE
self.color_selected = WHITE
self.color = WHITE
else:
self.color_default = YELLOW
self.color_selected = LIGHT_BLUE
self.color = YELLOW
def clicked(self, other):
self.color = RED
self.button_down = True
self.sliding = False
return self, other
def append(self, selected_desks):
if self in selected_desks:
selected_desks.discard(self)
self.color = self.color_default
else:
selected_desks.add(self)
self.color = self.color_selected
return selected_desks
class EmptyDesk(Desk):
def __init__(self, place, desk_layout, height_width, desk_id=None):
super().__init__(place, "", desk_layout, height_width, desk_id=desk_id)
self.color_default = DARK_BLUE
self.color = self.color_default
self.color_selected = self.color_default
# TODO make the empty desk smaller - will require Desk.pos being center
def set_student_name(self, name):
if not name:
return self
return self._copy_state_to(
FilledDesk(self.place, name, self.desk_layout, self.height_width, desk_id=self.desk_id)
)
class Button(pygame.sprite.Sprite):
def __init__(self, pos, size, text):
super().__init__()
self.pos = pos
self.size = size
self.color_unclicked = YELLOW
self.color_clicked = RED
self.color_default = YELLOW
self.color_selected = LIGHT_BLUE
self.rect = pygame.Rect(*self.pos, *self.size)
self.fade_from_1_to_0 = 0.
self.text_img = font.render(text, True, (0, 0, 0))
self.text_pos = self.text_img.get_rect(center=self.rect.center)
self.text_editor_active = False
self.text = text
# self.text_rect = self.rect.inflate(-50, -50).move(0, 20)
self.cursor_visible = True
self.cursor_blink_interval_millis = 500
self.cursor_blink_timer = 0
def update(self, surface):
if not hasattr(surface, "blit"):
return
if self.fade_from_1_to_0 < 0.01:
color = self.color_unclicked
else:
f = self.fade_from_1_to_0
color = [int(u * (1 - f) + c * f) for u, c in zip(self.color_unclicked, self.color_clicked)]
self.fade_from_1_to_0 *= 0.95
pygame.draw.rect(surface, color, self.rect)
surface.blit(self.text_img, self.text_pos)
if self.text_editor_active:
pygame.draw.rect(surface, WHITE, self.rect)
self.text_img = font.render(self.text, True, (0, 0, 0))
self.text_pos = self.text_img.get_rect(center=self.rect.center)
surface.blit(self.text_img, self.text_pos)
self.draw_cursor(surface, self.text_pos)
def draw_cursor(self, surface, text_pos):
if pygame.time.get_ticks() - self.cursor_blink_timer >= self.cursor_blink_interval_millis:
self.cursor_visible = not self.cursor_visible
self.cursor_blink_timer = pygame.time.get_ticks()
if self.cursor_visible:
cursor_pos = text_pos.right + 1
cursor_width = 2
cursor_rect = pygame.Rect(cursor_pos, text_pos.top, cursor_width, text_pos.height)
pygame.draw.rect(surface, BLACK, cursor_rect)
def handle_keydown(self, event, selected_desks):
self.text_editor_active = True
if event.key == pygame.K_RETURN:
self.clicked(selected_desks)
self.text_editor_active = False
self.text = ""
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
def clicked(self, selected_desks):
self.fade_from_1_to_0 = 1.
self.render_immediate_feedback()
for desk in selected_desks:
desk.color = desk.color_default
return self, set()
def set_text(self, text):
self.text = text
self.text_img = font.render(text, True, (0, 0, 0))
self.text_pos = self.text_img.get_rect(center=self.rect.center)
def render_immediate_feedback(self):
surface = pygame.display.get_surface()
if surface is None:
return
pygame.draw.rect(surface, self.color_clicked, self.rect)
surface.blit(self.text_img, self.text_pos)
pygame.display.update(self.rect)
class ButtonWithComments(Button):
def __init__(self, pos, size, text, comments_path):
super().__init__(pos, size, text)
self.comments_path = comments_path
self.linked_button = None
class PositiveButton(ButtonWithComments):
def __init__(self, pos, size, comments_path):
super().__init__(pos, size, "+", comments_path)
def clicked(self, selected_desks):
updateComments.add_positive_comments(self.comments_path,
[desk.student_name() for desk in selected_desks if desk.student_name()],
self.linked_button.text)
self.linked_button.fade_from_1_to_0 = 1.
super().clicked(selected_desks)
return UnclickedDesk(), set()
class NegativeButton(ButtonWithComments):
def __init__(self, pos, size, comments_path):
super().__init__(pos, size, "-", comments_path)
def clicked(self, selected_desks):
updateComments.add_negative_comments(self.comments_path,
[desk.student_name() for desk in selected_desks if desk.student_name()],
self.linked_button.text)
self.linked_button.fade_from_1_to_0 = 1.
super().clicked(selected_desks)
return UnclickedDesk(), set()
class SuggestFocusButton(ButtonWithComments):
def __init__(self, pos, size, desks, comments_path, config_path, course):
super().__init__(pos, size, "suggestions", comments_path)
self.desks = desks
self.config_path = config_path
self.course = course
def clicked(self, selected_desks):
self.fade_from_1_to_0 = 1.
self.render_immediate_feedback()
students_for_comments = linkComments.get_students_needing_comments_from_config_path(
self.config_path, self.course)
selected_count = len(selected_desks)
target_count = selected_count + 1
ordered_desks = self._ordered_suggested_desks(students_for_comments)
for desk in selected_desks:
desk.color = desk.color_default
selected_desks = set(ordered_desks[:target_count])
for desk in selected_desks:
desk.color = desk.color_selected
return self, selected_desks
def _ordered_suggested_desks(self, students_for_comments):
desks_by_student = {
desk.student_name(): desk
for desk in self.desks
if desk.student_name() and not desk.is_absent()
}
return [
desks_by_student[student_name]
for student_name in students_for_comments
if student_name in desks_by_student
]
class TextButtonLinkedToFile(Button):
def __init__(self, pos, size, text, file, count):
if text is None:
with open(file, 'r') as f:
lines = f.read().split("\n")
text = lines[count]
super().__init__(pos, size, text)
self.file = file
self.count = count
self.linked_buttons = []
def write_text(self):
with open(self.file, 'r') as f:
lines = f.read().split("\n")
num_missing = self.count - len(lines) + 1
if num_missing > 0:
lines = lines + [""]*num_missing
lines[self.count] = self.text
with open(self.file, 'w') as f:
f.write("\n".join(lines))
def clicked(self, selected_desks):
super().clicked(selected_desks)
if self.text_editor_active:
self.text_editor_active = False
if self.file:
self.write_text()
else:
self.text_editor_active = True
return self, selected_desks
def handle_keydown(self, event, selected_desks):
self.text_editor_active = True
if event.key == pygame.K_RETURN:
self.clicked(selected_desks)
self.text_editor_active = False
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
class ButtonWhichCallsFunction(Button):
def __init__(self, pos, size, text, fn, config_file):
super().__init__(pos, size, text)
self.fn = fn
self.config_file = config_file
def clicked(self, selected_desks):
button, selected_desks = super().clicked(selected_desks)
self.fn(self.config_file)
return button, selected_desks