snakeware

Python-based Linux distro for x86-64 and RPi
git clone git://squid-tech.com/snakeware.git
Log | Files | Refs | README

commit 2580c00db167a945e138a745ee2425dd9c450c7a
parent 8cf4b9b68bea78e749539ec9891a5a87ee82f2b4
Author: Josh Moore <jxm5210@rit.edu>
Date:   Thu, 28 May 2020 16:01:29 -0400

Implement focus for pong game

Diffstat:
Msnakewm/apps/games/pong/pongwindow.py | 15++++++++++-----
Msnakewm/wm.py | 10++++++++++
2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/snakewm/apps/games/pong/pongwindow.py b/snakewm/apps/games/pong/pongwindow.py @@ -11,7 +11,7 @@ from .pong import PongGame class PongWindow(UIWindow): def __init__(self, position, ui_manager): super().__init__(pygame.Rect(position, (320, 240)), ui_manager, - window_display_title='Super Awesome Pong!', + window_display_title='pong', object_id='#pong_window') game_surface_size = self.get_container().get_size() @@ -24,9 +24,14 @@ class PongWindow(UIWindow): self.pong_game = PongGame(game_surface_size) - #self.is_active = False + self.is_active = False + + def focus(self): self.is_active = True + def unfocus(self): + self.is_active = False + def process_event(self, event): handled = super().process_event(event) if (event.type == pygame.USEREVENT and @@ -34,14 +39,14 @@ class PongWindow(UIWindow): event.ui_object_id == "#pong_window.#title_bar" and event.ui_element == self.title_bar): handled = True - event_data = {'user_type': 'pong_window_selected', + event_data = {'user_type': 'window_selected', 'ui_element': self, 'ui_object_id': self.most_specific_combined_id} window_selected_event = pygame.event.Event(pygame.USEREVENT, event_data) pygame.event.post(window_selected_event) - #if self.is_active: - handled = self.pong_game.process_event(event) + if self.is_active: + handled = self.pong_game.process_event(event) return handled def update(self, time_delta): diff --git a/snakewm/wm.py b/snakewm/wm.py @@ -18,6 +18,9 @@ class SnakeWM: BG = None MANAGER = None + # currently focused window + FOCUS = None + # dict that will contain the apps directory structure APPS = {} # reference to the root app menu object @@ -129,6 +132,13 @@ class SnakeWM: running = False return pygame.quit() + elif event.type == pygame.USEREVENT: + if event.user_type == 'window_selected': + if self.FOCUS is not None: + self.FOCUS.unfocus() + self.FOCUS = event.ui_element + self.FOCUS.focus() + self.MANAGER.process_events(event) self.MANAGER.update(delta)