|
10 | 10 | from functools import partialmethod |
11 | 11 |
|
12 | 12 |
|
| 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 | + |
13 | 122 | class Coord(NamedTuple): |
14 | 123 | """ |
15 | 124 | Helper class for managing coordinate values. |
@@ -112,115 +221,3 @@ def __add__(self, other: Direction) -> Coord: |
112 | 221 |
|
113 | 222 | def flip(self): |
114 | 223 | 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() |
0 commit comments