Skip to content
This repository was archived by the owner on Mar 31, 2020. It is now read-only.

Commit 7f573ca

Browse files
committed
Linting fix
1 parent 9747c1e commit 7f573ca

File tree

2 files changed

+111
-116
lines changed

2 files changed

+111
-116
lines changed

src/animate.py

Lines changed: 109 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,115 @@
1010
from functools import partialmethod
1111

1212

13+
class Animater:
14+
"""
15+
Manager for executing animations.
16+
17+
example::
18+
19+
```
20+
motion = Motion(...)
21+
window = Animater(...)
22+
window.add_motion(motion)
23+
```
24+
"""
25+
_motions = set()
26+
27+
def __init__(self, canvas: tk.Canvas):
28+
self.canvas = canvas
29+
30+
def start(self):
31+
while self._motions:
32+
self.run()
33+
34+
def run(self):
35+
for motion in self._motions.copy():
36+
try:
37+
move = next(motion)
38+
move()
39+
except StopIteration:
40+
self._motions.remove(motion)
41+
42+
def add(self, motion: Motion):
43+
self._motions.add(motion.start())
44+
45+
def add_motion(self, id: int, end: Coord, **kwargs):
46+
motion = Motion(self.canvas, id, end, **kwargs)
47+
self.add(motion)
48+
49+
def clear(self):
50+
self._motions.clear()
51+
52+
@property
53+
def running(self):
54+
return bool(self._motions)
55+
56+
57+
@dataclass
58+
class Motion:
59+
"""
60+
61+
"""
62+
canvas: tk.Canvas
63+
id: int
64+
end: Coord
65+
66+
speed: int = 1
67+
68+
def __iter__(self):
69+
return self.start()
70+
71+
def __key(self):
72+
return self.id
73+
74+
def __hash__(self):
75+
return hash(self.__key())
76+
77+
def __eq__(self):
78+
return isinstance(self, type(other)) and self.__key() == other.__key()
79+
80+
def start(self) -> Generator[Callable]:
81+
"""
82+
The entry point for generating move commands.
83+
"""
84+
self.time = time.time()
85+
self.start = self.current
86+
self.distance = self.start.distance(self.end)
87+
self.speed = self.speed ** 3
88+
while self.current != self.end:
89+
yield self.move
90+
91+
def move(self):
92+
self.canvas.move(self.id, *self.increment)
93+
self.canvas.update_idletasks()
94+
95+
@property
96+
def time(self):
97+
return time.time() - self._time
98+
99+
@time.setter
100+
def time(self, val):
101+
self._time = val
102+
103+
@property
104+
def increment(self):
105+
mult = (self.time * self.speed) / self.distance
106+
point = (self.end - self.start) * mult + self.start
107+
108+
if point.distance(self.end) > self.journey:
109+
return self.end - self.current
110+
else:
111+
return point - self.current
112+
113+
@property
114+
def current(self):
115+
return Coord(*self.canvas.coords(self.id))
116+
117+
@property
118+
def journey(self):
119+
return self.current.distance(self.end)
120+
121+
13122
class Coord(NamedTuple):
14123
"""
15124
Helper class for managing coordinate values.
@@ -112,115 +221,3 @@ def __add__(self, other: Direction) -> Coord:
112221

113222
def flip(self):
114223
return Direction(Coord(0, 0) - self.value)
115-
116-
117-
class Animater:
118-
"""
119-
Manager for executing animations.
120-
121-
example::
122-
123-
```
124-
motion = Motion(...)
125-
window = Animater(...)
126-
window.add_motion(motion)
127-
```
128-
"""
129-
_motions = set()
130-
131-
def __init__(self, canvas: tk.Canvas):
132-
self.canvas = canvas
133-
134-
def start(self):
135-
while self._motions:
136-
print(self._motions)
137-
self.run()
138-
139-
def run(self):
140-
for motion in self._motions.copy():
141-
try:
142-
move = next(motion)
143-
move()
144-
self.canvas.update()
145-
except StopIteration:
146-
self._motions.remove(motion)
147-
148-
def add(self, motion: Motion):
149-
self._motions.add(motion.start())
150-
151-
def add_motion(self, id: int, end: Coord, **kwargs):
152-
motion = Motion(self.canvas, id, end, **kwargs)
153-
self.add(motion)
154-
155-
def clear(self):
156-
self._motions.clear()
157-
158-
@property
159-
def running(self):
160-
return bool(self._motions)
161-
162-
163-
@dataclass
164-
class Motion:
165-
"""
166-
167-
"""
168-
canvas: tk.Canvas
169-
id: int
170-
end: Coord
171-
172-
speed: int = 1
173-
174-
def start(self) -> Generator[Callable]:
175-
"""
176-
The entry point for generating move commands.
177-
"""
178-
self.time = time.time()
179-
self.start = self.current
180-
self.distance = self.start.distance(self.end)
181-
self.speed = self.speed ** 3
182-
while self.current != self.end:
183-
yield self.move
184-
185-
def move(self):
186-
self.canvas.move(self.id, *self.increment)
187-
self.canvas.update_idletasks()
188-
189-
@property
190-
def time(self):
191-
return time.time() - self._time
192-
193-
@time.setter
194-
def time(self, val):
195-
self._time = val
196-
197-
@property
198-
def increment(self):
199-
mult = (self.time * self.speed) / self.distance
200-
point = (self.end - self.start) * mult + self.start
201-
202-
if point.distance(self.end) > self.journey:
203-
return self.end - self.current
204-
else:
205-
return point - self.current
206-
207-
208-
@property
209-
def current(self):
210-
return Coord(*self.canvas.coords(self.id))
211-
212-
@property
213-
def journey(self):
214-
return self.current.distance(self.end)
215-
216-
def __iter__(self):
217-
return self.start()
218-
219-
def __key(self):
220-
return self.id
221-
222-
def __hash__(self):
223-
return hash(self.__key())
224-
225-
def __eq__(self):
226-
return isinstance(self, type(other)) and self.__key() == other.__key()

src/view.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
from __future__ import annotations
22

3-
import tkinter as tk
4-
from typing import TypeVar
53
from enum import Enum
6-
from PIL import ImageTk
74

85
from . import widget
96
from .animate import Coord, Animater, Direction
107

118

129
class Window(widget.PrimaryCanvas):
1310
animation_speed = 10
14-
current = None
1511
views = {}
12+
current = None
1613

1714
def init(self):
1815
self.animater = Animater(self)
@@ -55,6 +52,7 @@ def change_view(self, view: View, direction: Direction = None):
5552
return
5653
if not isinstance(direction, Direction):
5754
direction = Direction[direction.upper()] # Cast string for convenience
55+
5856
self.animater.clear()
5957

6058
last = self.current

0 commit comments

Comments
 (0)