diff --git a/mopidy_touchscreen/input_manager.py b/mopidy_touchscreen/input_manager.py index 0a8d085..7e47842 100644 --- a/mopidy_touchscreen/input_manager.py +++ b/mopidy_touchscreen/input_manager.py @@ -6,10 +6,11 @@ import pygame logger = logging.getLogger(__name__) -class TouchManager(): +class InputManager(): click = 1 swipe = 2 long_click = 3 + key = 4 long_click_min_time = 0.3 @@ -17,6 +18,7 @@ class TouchManager(): down = 1 left = 2 right = 3 + enter = 4 def __init__(self, size): self.down_pos = (0, 0) @@ -26,32 +28,57 @@ class TouchManager(): self.min_swipe_move = self.screen_size[1] / 3 self.down_button = -1 self.down_time = -1 + self.last_key = -1 def event(self, event): if event.type == pygame.MOUSEBUTTONUP: if event.button == 4: - touch_event = TouchEvent(TouchManager.swipe, self.down_pos, - self.up_pos, True) - touch_event.direction = TouchManager.up + touch_event = InputEvent(InputManager.swipe, self.down_pos, + self.up_pos, True, InputManager.up) return touch_event elif event.button == 5: - touch_event = TouchEvent(TouchManager.swipe, self.down_pos, - self.up_pos, True) - touch_event.direction = TouchManager.down + touch_event = InputEvent(InputManager.swipe, self.down_pos, + self.up_pos, True, InputManager.down) return touch_event elif event.button == 1 and self.down_button == 1: touch_event = self.mouse_up(event) return touch_event elif event.button == 3 and self.down_button == 3: - touch_event = TouchEvent(TouchManager.long_click, - self.down_pos, self.up_pos, None) + touch_event = InputEvent(InputManager.long_click, + self.down_pos, self.up_pos, None, None) return touch_event else: return None elif event.type == pygame.MOUSEBUTTONDOWN: self.mouse_down(event) return None + elif event.type == pygame.KEYDOWN: + self.key_down(event) + return None + elif event.type == pygame.KEYUP: + return self.key_up(event) + + def key_down(self, event): + self.last_key = event.key + self.down_time = time.time() + + def key_up(self, event): + if self.last_key == event.key: + if self.last_key == pygame.K_DOWN: + direction = InputManager.down + elif self.last_key == pygame.K_UP: + direction = InputManager.up + elif self.last_key == pygame.K_LEFT: + direction = InputManager.left + elif self.last_key == pygame.K_RIGHT: + direction = InputManager.right + elif self.last_key == pygame.K_RETURN: + direction = InputManager.enter + else: + return None + if direction is not None: + return InputEvent(InputManager.key, None, None, None, direction) def mouse_down(self, event): self.down_pos = event.pos @@ -62,35 +89,37 @@ class TouchManager(): self.up_pos = event.pos if abs(self.down_pos[0] - self.up_pos[0]) < self.max_move_margin: if abs(self.down_pos[1] - self.up_pos[1]) < self.max_move_margin: - if time.time() - TouchManager.long_click_min_time > \ + if time.time() - InputManager.long_click_min_time > \ self.down_time: - return TouchEvent(TouchManager.long_click, self.down_pos, - self.up_pos, None) + return InputEvent(InputManager.long_click, self.down_pos, + self.up_pos, None, None) else: - return TouchEvent(TouchManager.click, self.down_pos, - self.up_pos, None) + return InputEvent(InputManager.click, self.down_pos, + self.up_pos, None, None) elif abs(self.down_pos[1] - self.up_pos[1]) > self.min_swipe_move: - return TouchEvent(TouchManager.swipe, self.down_pos, - self.up_pos, True) + return InputEvent(InputManager.swipe, self.down_pos, + self.up_pos, True, None) elif self.down_pos[1] - self.up_pos[1] < self.max_move_margin: if abs(self.down_pos[0] - self.up_pos[0]) > self.min_swipe_move: - return TouchEvent(TouchManager.swipe, self.down_pos, - self.up_pos, False) + return InputEvent(InputManager.swipe, self.down_pos, + self.up_pos, False, None) -class TouchEvent(): - def __init__(self, event_type, down_pos, current_pos, vertical): +class InputEvent(): + def __init__(self, event_type, down_pos, current_pos, vertical, direction): self.type = event_type self.down_pos = down_pos self.current_pos = current_pos - if event_type is TouchManager.swipe: + if event_type is InputManager.swipe and direction is None: if vertical: if self.down_pos[1] < self.current_pos[1]: - self.direction = TouchManager.down + self.direction = InputManager.down else: - self.direction = TouchManager.up + self.direction = InputManager.up else: if self.down_pos[0] < self.current_pos[0]: - self.direction = TouchManager.right + self.direction = InputManager.right else: - self.direction = TouchManager.left + self.direction = InputManager.left + else: + self.direction = direction diff --git a/mopidy_touchscreen/library_screen.py b/mopidy_touchscreen/library_screen.py index 422f753..6450086 100644 --- a/mopidy_touchscreen/library_screen.py +++ b/mopidy_touchscreen/library_screen.py @@ -3,7 +3,7 @@ import logging import mopidy.models from .list_view import ListView -from .touch_manager import TouchManager +from .input_manager import InputManager logger = logging.getLogger(__name__) @@ -48,7 +48,7 @@ class LibraryScreen(): def touch_event(self, touch_event): clicked = self.list_view.touch_event(touch_event) if clicked is not None: - if touch_event.type == TouchManager.long_click: + if touch_event.type == InputManager.long_click: if self.current_directory is not None: if clicked == 0: self.go_up_directory() diff --git a/mopidy_touchscreen/list_view.py b/mopidy_touchscreen/list_view.py index d2f5b1d..47de24a 100644 --- a/mopidy_touchscreen/list_view.py +++ b/mopidy_touchscreen/list_view.py @@ -1,7 +1,7 @@ import logging from .screen_objects import ScreenObjectsManager, ScrollBar, TouchAndTextItem -from .touch_manager import TouchManager +from .input_manager import InputManager logger = logging.getLogger(__name__) @@ -60,8 +60,8 @@ class ListView(): self.screen_objects.render(surface) def touch_event(self, touch_event): - if touch_event.type == TouchManager.click \ - or touch_event.type == TouchManager.long_click: + if touch_event.type == InputManager.click \ + or touch_event.type == InputManager.long_click: objects = self.screen_objects.get_touch_objects_in_pos( touch_event.current_pos) if objects is not None: @@ -73,10 +73,10 @@ class ListView(): self.move_to(direction) else: return int(key) - elif touch_event.type == TouchManager.swipe: - if touch_event.direction == TouchManager.up: + elif touch_event.type == InputManager.swipe: + if touch_event.direction == InputManager.up: self.move_to(-1) - elif touch_event.direction == TouchManager.down: + elif touch_event.direction == InputManager.down: self.move_to(1) # Scroll to direction diff --git a/mopidy_touchscreen/main_screen.py b/mopidy_touchscreen/main_screen.py index 6f123f0..93cbde1 100644 --- a/mopidy_touchscreen/main_screen.py +++ b/mopidy_touchscreen/main_screen.py @@ -11,7 +11,7 @@ import pygame from .screen_objects import (Progressbar, ScreenObjectsManager, TextItem, TouchAndTextItem) -from .touch_manager import TouchManager +from .input_manager import InputManager logger = logging.getLogger(__name__) @@ -211,7 +211,7 @@ class MainScreen(): (size, size)) def touch_event(self, event): - if event.type == TouchManager.click: + if event.type == InputManager.click: objects = self.touch_text_manager.get_touch_objects_in_pos( event.current_pos) if objects is not None: @@ -224,19 +224,19 @@ class MainScreen(): self.core.playback.previous() elif key == "next": self.core.playback.next() - elif event.type == TouchManager.swipe: - if event.direction == TouchManager.left: + elif event.type == InputManager.swipe: + if event.direction == InputManager.left: self.core.playback.next() - elif event.direction == TouchManager.right: + elif event.direction == InputManager.right: self.core.playback.previous() - elif event.direction == TouchManager.up: + elif event.direction == InputManager.up: volume = self.core.playback.volume.get() + 10 if volume > 100: volume = 100 self.manager.backend.tell( {'action': 'volume', 'value': volume}) self.manager.volume_changed(volume) - elif event.direction == TouchManager.down: + elif event.direction == InputManager.down: volume = self.core.playback.volume.get() - 10 if volume < 0: volume = 0 diff --git a/mopidy_touchscreen/screen_manager.py b/mopidy_touchscreen/screen_manager.py index 05cea25..b5841ee 100644 --- a/mopidy_touchscreen/screen_manager.py +++ b/mopidy_touchscreen/screen_manager.py @@ -12,7 +12,7 @@ from .main_screen import MainScreen from .menu_screen import MenuScreen from .playlist_screen import PlaylistScreen from .screen_objects import Progressbar, ScreenObjectsManager, TouchAndTextItem -from .touch_manager import TouchManager +from .input_manager import InputManager from .tracklist import Tracklist logger = logging.getLogger(__name__) @@ -40,7 +40,7 @@ class ScreenManager(): except: traceback.print_exc() self.track = None - self.touch_manager = TouchManager(size) + self.input_manager = InputManager(size) self.screen_objects_manager = ScreenObjectsManager() # Top bar @@ -147,17 +147,17 @@ class ScreenManager(): self.screens[0].track_playback_ended(tl_track, time_position) def event(self, event): - touch_event = self.touch_manager.event(event) - if touch_event is not None: - if touch_event.type == TouchManager.click: + event = self.input_manager.event(event) + if event is not None: + if event.type == InputManager.click: objects = self.screen_objects_manager.get_touch_objects_in_pos( - touch_event.current_pos) + event.current_pos) if objects is not None: for key in objects: if key == "volume": manager = self.screen_objects_manager volume = manager.get_touch_object(key) - pos = touch_event.current_pos + pos = event.current_pos value = volume.get_pos_value(pos) self.backend.tell( {'action': 'volume', 'value': value}) @@ -186,7 +186,7 @@ class ScreenManager(): self.screens[4].check_connection() elif key[:-1] == "menu_": self.change_screen(int(key[-1:])) - self.screens[self.current_screen].touch_event(touch_event) + self.screens[self.current_screen].touch_event(event) def volume_changed(self, volume): if not self.core.playback.mute.get():