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

Commit 9fc7a23

Browse files
committed
Swipe animation working
1 parent 7557cff commit 9fc7a23

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

src/animate.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import tkinter as tk
44
import operator
55
import time
6-
from . import widget
76
from typing import NamedTuple, Callable, TypeVar, Generator, Tuple
87
from enum import Enum
98
from dataclasses import dataclass
109
from functools import partialmethod, partial
1110

11+
from . import widget
12+
from .cache import ImageTk
13+
1214

1315
class Coord(NamedTuple):
1416
"""
@@ -235,7 +237,7 @@ def __eq__(self):
235237

236238

237239
class Window(widget.PrimaryCanvas):
238-
animation_speed = 3
240+
animation_speed = 4
239241
_current = None
240242

241243
def init(self):
@@ -244,34 +246,41 @@ def init(self):
244246
def __coord(self, id):
245247
return Coord(*self.coords(id)[:2])
246248

247-
def clear(self):
248-
if self._current is not None:
249-
self.delete(self._current)
250-
self.update()
249+
def __set_image(self, view: ImageTk.PhotoImage, coord: Coord):
250+
return self.create_image(
251+
coord, image=view, anchor='nw'
252+
)
251253

252-
def __set(self, view: tk.Widget, coord: Coord):
254+
def __set_widget(self, view: tk.Widget, coord: Coord):
253255
return self.create_window(
254256
coord, window=view, anchor='nw'
255257
)
256258

257-
def set_view(self, view: tk.Widget):
258-
self.clear()
259-
self._current = self.__set(view, self.origin)
259+
def __set(self, view, coord, viewtype):
260+
if viewtype == 'image':
261+
return self.__set_image(view, coord)
262+
else:
263+
return self.__set_widget(view, coord)
264+
265+
def set_view(self, view: tk.Widget, viewtype='image'):
266+
print(self.origin)
267+
self._current = self.__set(view, self.origin, viewtype)
260268

261-
def change_view(self, view: tk.Widget, direction: Direction):
269+
def change_view(self, view: tk.Widget, direction: Direction, viewtype='image'):
262270
if not isinstance(direction, Direction):
263271
direction = Direction[direction.upper()] # Cast string for convenience
264272

265273
if direction in (Direction.UP, Direction.DOWN):
266-
dist = self.winfo_height()
274+
dist = Coord(0, self.winfo_height())
267275
elif direction in (Direction.LEFT, Direction.RIGHT):
268-
dist = self.winfo_width()
276+
dist = Coord(self.winfo_width(), 0)
269277
else:
270278
raise NotImplementedError
279+
271280
edge = direction * dist
272281
end = self.origin + edge
273282
beg = self.origin - edge
274-
wid = self.__set(view, beg)
283+
wid = self.__set(view, beg, viewtype)
275284

276285
self.animater.clear()
277286
self.animater.add_motion(self._current, end, speed=self.animation_speed)
@@ -282,4 +291,4 @@ def change_view(self, view: tk.Widget, direction: Direction):
282291

283292
@property
284293
def origin(self):
285-
return Coord(self.winfo_x(), self.winfo_y())
294+
return Coord(0, 0)

src/front.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ class Front(widget.PrimaryFrame):
99

1010
cachesize = 10
1111

12+
# Quick fix to keep a reference count on the
13+
# last image, making sure the garbage collector
14+
# doesn't delete it before the animation ends
15+
_last = None
16+
1217
def __next(self):
1318
data: dict = self.cache.pop()
1419
data.pop('jumpscare') # not using it for now
@@ -22,13 +27,13 @@ def __next(self):
2227
def __load(self, name, image, data):
2328
self.title.config(text=name)
2429
self.bio.load(data)
25-
26-
self.image = widget.PrimaryLabel(self.window, image=image)
30+
self._last = self.image
31+
self.image = image
2732
self.update()
2833

2934
def __change_image(self, direction: Direction):
3035
self.__next()
31-
self.window.change_view(self.image, direction)
36+
self.window.change_view(self.image, direction, viewtype='image')
3237

3338
def init(self):
3439
self.title = widget.PrimaryLabel(self)
@@ -57,6 +62,7 @@ def init(self):
5762

5863
self._loop = asyncio.get_event_loop()
5964
self.cache = Cache(self.window)
65+
self.cache
6066

6167
def cmd_dislike(self):
6268
self.__change_image('LEFT')

0 commit comments

Comments
 (0)