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

Commit 11d0f4f

Browse files
committed
Project structure overhaul
1 parent 0408d2b commit 11d0f4f

9 files changed

Lines changed: 301 additions & 13 deletions

File tree

res/settings.ini

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[DEFAULT]
2-
main.title = Cat Tinder
2+
main.title = App
33
main.geometry = 400x500
44
main.background = black
55
cachesize = 10
@@ -15,5 +15,6 @@ back.background = blue
1515
bio.text = Bio
1616
bio.background = blue
1717

18-
[DEV]
19-
main.title = DEV MODE CAT TINDER
18+
19+
[APP]
20+
title = App

res/widgets.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[base]
2+
3+
[primary]
4+
5+
[secondary]
6+

src/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
RES: Path = ROOT / 'res'
66

77
SETTINGS: Path = RES / 'settings.ini'
8+
THEME: Path = RES / 'widgets.ini'
89
IMAGES: Path = RES / 'images'
910
SOUNDS: Path = RES / 'sounds'

src/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .mainwindow import Tinder
1+
from .mainwindow import App
22

33
if __name__ == "__main__":
4-
Tinder().start()
4+
App().mainloop()

src/animate.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tkinter as tk
44
import operator
55
import time
6+
from . import widget
67
from typing import NamedTuple, Callable, TypeVar, Generator, Tuple, Iterable
78
from enum import Enum
89
from dataclasses import dataclass
@@ -148,7 +149,6 @@ def add_motion(self, id: int, endpoints: Iterable[Coord], **kwargs):
148149
if not isinstance(endpoints, Iterable):
149150
endpoints = (endpoints,)
150151

151-
endpoints = (Coord(*point) for point in endpoints) # Reinforce type
152152
motion = Motion(self.canvas, id, endpoints, **kwargs)
153153
self.add(motion)
154154

@@ -214,7 +214,6 @@ def frame(increment: Coord, count: int):
214214
for _ in range(count):
215215
move(*increment)
216216
self.canvas.master.update_idletasks()
217-
# self.canvas.master.update()
218217

219218
for end in self.endpoints:
220219
start = Coord(*self.canvas.coords(self.id)[:2])
@@ -236,3 +235,53 @@ def __hash__(self):
236235

237236
def __eq__(self):
238237
return isinstance(self, type(other)) and self.__key() == other.__key()
238+
239+
240+
class Window(widget.PrimaryCanvas):
241+
origin = Coord(0, 0)
242+
animation_speed = 2
243+
current = None
244+
245+
def __init__(self, *args, **kwargs):
246+
super().__init__(*args, **kwargs)
247+
248+
self.animater = Animater(self)
249+
250+
def __coord(self, id):
251+
return Coord(*self.coords(id)[:2])
252+
253+
def clear(self):
254+
if self.current is not None:
255+
self.delete(self.current)
256+
self.update()
257+
258+
def set_view(self, view: tk.Widget):
259+
self.clear()
260+
self.current = self.create_window(self.origin, view)
261+
262+
def change_view(self, view: tk.Widget, direction: Direction):
263+
if self.current is None:
264+
self.set_view(view)
265+
return
266+
267+
if not isinstance(direction, Direction):
268+
direction = Direction[direction] # Cast string for convenience
269+
270+
if direction in (Direction.UP, Direction.DOWN):
271+
edge = self.winfo_screenheight()
272+
elif direction in (Direction.LEFT, Direction.RIGHT):
273+
edge = self.winfo_screenwidth()
274+
else:
275+
raise NotImplementedError
276+
277+
pos = self.__coord(self.current)
278+
end = pos + edge
279+
beg = pos - edge
280+
wid = self.create_window(beg, view)
281+
282+
self.animater.clear()
283+
self.animater.add_motion(self.current, end)
284+
self.animater.add_motion(wid, self.origin)
285+
286+
self.animater.start()
287+
self.current = wid

src/cache.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def __init__(self, root):
2323
# get settings
2424
cp = configparser.ConfigParser()
2525
cp.read(str(SETTINGS))
26-
cp.read('settings.ini')
2726

2827
# for now, let's just look up the DEV settings
2928
# can change this later

src/front.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from . import widget
2+
from .animate import Window, Direction
3+
4+
5+
class Front(widget.PrimaryFrame):
6+
7+
_cache: list = None
8+
9+
def __next(self):
10+
data: dict = self.cache.pop()
11+
data.pop('jumpscare') # not using it for now
12+
name = data.pop('name')
13+
image = data.pop('image')
14+
self.__load(name, image, data)
15+
16+
def __load(self, name, image, data):
17+
self.title.config(text=name)
18+
self.bio.load(data)
19+
20+
test = tk.Frame(self.window)
21+
self.image = widget.PrimaryLabel(test, image=image)
22+
self.image.pack()
23+
self.image = test
24+
self.update()
25+
26+
def __change_image(self, direction: Direction):
27+
self.__next()
28+
self.window.change_view(self.image, direction)
29+
30+
def init(self):
31+
self.title = widget.PrimaryLabel(self)
32+
self.window = Window(self)
33+
self.commandbar = widget.SecondaryFrame(self)
34+
35+
self.bio = Bio(self.window)
36+
self.image = None
37+
38+
self.btn_dislike = widget.PrimaryButton(
39+
self.commandbar, text='Nope', bg='red', command=self.cmd_dislike
40+
)
41+
self.btn_bio = widget.SecondaryButton(
42+
self.commandbar, text='Bio', command=self.cmd_bio
43+
)
44+
self.btn_like = widget.PrimaryButton(
45+
self.commandbar, text='Yep', bg='green', command=self.cmd_like
46+
)
47+
self.title.pack()
48+
self.window.pack()
49+
self.commandbar.pack()
50+
51+
self.btn_dislike.pack(side='left')
52+
self.btn_bio.pack(side='left')
53+
self.btn_like.pack(side='left')
54+
55+
def cmd_dislike(self):
56+
self.__change_image('LEFT')
57+
58+
def cmd_like(self):
59+
self.__change_image('RIGHT')
60+
61+
def cmd_bio(self):
62+
self.window.change_view(self.bio, 'UP')
63+
64+
@property
65+
def cache(self):
66+
if self._cache is None:
67+
return AttributeError('cache has not been set.')
68+
return self._cache
69+
70+
@cache.setter
71+
def cache(self, data: list):
72+
self._cache = data
73+
# Pump the well
74+
self.__next()
75+
self.window.set_view(self.image)
76+
77+
78+
class Bio(widget.PrimaryFrame):
79+
80+
def __make_item(self, name, value):
81+
item = widget.SecondaryFrame(self)
82+
name = widget.SecondaryLabel(item, text=name)
83+
value = widget.SecondaryLabel(item, text=value)
84+
name.pack(side='left')
85+
value.pack(side='left')
86+
return item
87+
88+
def load(self, data: dict):
89+
for name, val in data.items():
90+
item = self.__make_item(name, val)
91+
item.pack()

src/mainwindow.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,41 @@
44
from pygame import mixer
55
from .cache import Cache
66
from .animate import Animater, Coord
7+
from .front import Front
78

89
from . import SETTINGS
910

1011

12+
parser = configparser.ConfigParser()
13+
parser.read(SETTINGS)
14+
15+
16+
class App(tk.Tk):
17+
appconfig = parser['APP']
18+
19+
def __init__(self, *args, **kwds):
20+
title = self.appconfig.pop('title')
21+
super().__init__(*args, **kwds)
22+
self.title = title
23+
mixer.init()
24+
25+
self.loop = asyncio.get_event_loop()
26+
self.cache = Cache(self)
27+
28+
self.geometry = '400x500'
29+
self.minsize(400, 500)
30+
self.maxsize(400, 500)
31+
32+
self.front = Front(self)
33+
self.front.pack(fill='both')
34+
35+
self.__cache()
36+
self.front.cache = self.cache.cats
37+
38+
def __cache(self):
39+
self.loop.run_until_complete(self.cache.refill())
40+
41+
1142
class Tinder:
1243
'''The main class for the application.'''
1344

@@ -44,13 +75,12 @@ def __init__(self):
4475
self.jumpscare = False
4576
self.loop = asyncio.get_event_loop()
4677
self.cache = Cache(self.root)
78+
self.window = Animater(self.root)
4779

4880
def start(self):
4981
'''Starts the Tinder application'''
5082

51-
# getting a cache of cat info
52-
self.loop.run_until_complete(self.cache.refill())
53-
83+
# getting a cache of cat inf
5484
# starting the program loop
5585
self.new_image()
5686

@@ -89,7 +119,7 @@ def new_image(self, cat=None):
89119
item.pack_forget()
90120

91121
# make a new Frame
92-
self.frame = tk.Frame(self.root, bg="black")
122+
self.frame = tk.Frame(self.window, bg="black")
93123

94124
# if a dict wasn't passed to the function, get a dict from self.cats
95125
if not cat:
@@ -213,7 +243,7 @@ def get_bio():
213243
command=back_to_photo).pack(side=tk.BOTTOM)
214244

215245
# packing the frame
216-
self.bioid = self.window.create_window((0, 500), window=self.frame, anchor='nw')
246+
self.bioid = self.window.create_window((0, 0), window=self.frame, anchor='nw')
217247
end = Coord(0, 0)
218248
self.window.add_motion(self.bioid, (end,), speed=3)
219249
self.window.pack(fill='both', expand=True)
@@ -225,6 +255,7 @@ def get_bio():
225255
command=get_bio).pack(side=tk.BOTTOM)
226256

227257
# packing the frame
258+
self.window.create_window((0, 0))
228259
self.frame.pack()
229260

230261
# starting the main tkinter loop

0 commit comments

Comments
 (0)