-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation.py
More file actions
82 lines (64 loc) · 2.57 KB
/
animation.py
File metadata and controls
82 lines (64 loc) · 2.57 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
from PIL import Image, ImageFont, ImageDraw
from colours import BLACK, WHITE
class Animation(object):
def __init__(self, buffer, update_rate=15):
self.buffer = buffer
self.update_interval = 1. / float(update_rate)
class CircleAnimation(Animation):
def __init__(self, buffer, colour, start, end, waiting_for=None):
super().__init__(buffer)
self.pos = list(start)
self.colour = colour
self.start = list(start)
self.end = list(end)
self.waiting_for = waiting_for
def should_update(self):
if self.waiting_for is None:
return True
return self.waiting_for.has_finished()
def has_finished(self):
return self.pos == self.end
def update_pos(self):
for i in range(2):
if self.pos[i] < self.end[i]:
self.pos[i] += 1
elif self.pos[i] > self.end[i]:
self.pos[i] -= 1
def update(self):
if self.pos != self.start:
self.update_pos()
self.buffer.paste(BLACK, [0, 0, self.buffer.size[0], self.buffer.size[1]])
if self.has_finished():
return
d = ImageDraw.Draw(self.buffer)
d.ellipse([self.pos[0]-4, self.pos[1]-4, self.pos[0]+4, self.pos[1]+4], fill=self.colour)
if self.pos == self.start:
self.update_pos()
class TextAnimation(Animation):
def __init__(self, buffer, input_text):
super().__init__(buffer)
self.angle = 0
self.text = Image.new("1", (32, 8), BLACK) # TODO: minimum necessary size
font = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 12)
draw = ImageDraw.Draw(self.text)
draw.text((-2, -2), input_text, font=font, fill=WHITE)
def should_update(self):
return self.angle < self.buffer.size[1]
def has_finished(self):
return not self.should_update()
def update(self):
self.angle += 1
self.buffer.paste(BLACK, [0, 0, self.buffer.size[0], self.buffer.size[1]])
self.buffer.paste(self.text.resize((self.buffer.size[0], self.angle)), [0, 0])
class BackgroundAnimation(Animation):
def __init__(self, buffer, colour):
super().__init__(buffer)
self.colour = colour
self.drawn = False
def should_update(self):
return not self.drawn
def has_finished(self):
return self.drawn
def update(self):
self.buffer.paste(self.colour, [0, 0, self.buffer.size[0], self.buffer.size[1]])
self.drawn = True