-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
71 lines (58 loc) · 2.09 KB
/
controller.py
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
import threading
import logging
import safe_exit
from menu import TileMenu, ListMenu
from model import Model
from touch import Touch
from view import View
logging.basicConfig(level=logging.INFO)
# logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
class Controller:
def __init__(self):
self.view = View()
self.model = Model(self.view)
self.touch = Touch(
rotate=90,
touch_event_gesture_end_callback=self.gesture_handler,
touch_event_tap_end_callback=self.tap_handler,
)
self.exit_event = threading.Event()
safe_exit.register(self.cleanup)
def cleanup(self):
logger.info("cleaning up")
self.exit_event.set()
def tap_handler(self, touch_event):
if isinstance(self.model.current_menu, TileMenu):
self.tile_menu_controller(touch_event)
return
if isinstance(self.model.current_menu, ListMenu):
self.list_menu_controller(touch_event)
return
def gesture_handler(self, touch_event):
if (
touch_event.touch_points[0].x < touch_event.touch_points[-1].x
and self.model.current_menu.parent is not None
):
logger.info("back swipe")
try:
# cleanup if function exists
self.model.current_menu.cleanup()
except AttributeError:
pass
self.model.current_menu.parent.callback()
def tile_menu_controller(self, touch_event):
for i, touch_target in enumerate(
self.model.current_menu.child_locations,
):
if touch_target.contains_point(touch_event.touch_points[0]):
self.model.current_menu.children[i].callback()
return
def list_menu_controller(self, touch_event):
for button in self.model.current_menu.buttons:
if button.location.contains_point(touch_event.touch_points[0]):
button.callback()
return
controller = Controller()
controller.exit_event.wait()
logger.info("Goodbye!")