1
0
Fork 0

Switch boards after keypress, wait some time etc.

This commit is contained in:
Michał Rudowicz 2024-09-08 17:17:55 +02:00
parent 0ab21fabc0
commit c330fa0bfe
1 changed files with 44 additions and 4 deletions

48
menu.py
View File

@ -2,6 +2,9 @@
import math
import sys
from time import sleep
from threading import Thread
from queue import Queue, Empty
from abc import abstractmethod
from typing import Tuple, Callable, TypedDict, List
@ -53,6 +56,23 @@ class Button:
self.cbk()
class MessageBoard(IBoard):
def __init__(self, screen_rect: pygame.Rect, text: str):
self.text = text
self.font = pygame.freetype.SysFont(name="Sans", size=30)
text_rect = self.font.get_rect(self.text)
self.text_pos = pygame.Rect((screen_rect.left + (screen_rect.width - text_rect.width)/2),
(screen_rect.top + (screen_rect.height - text_rect.height)/2),
text_rect.width, text_rect.height)
def handle_event(self, _):
pass
def draw(self, screen: pygame.Surface):
screen.fill("purple")
self.font.render_to(screen, self.text_pos, self.text, fgcolor="black")
class MenuBoard(IBoard):
BUTTON_MARGINS = 10
@ -103,15 +123,34 @@ class App:
self.screen = pygame.display.set_mode((info.current_w, info.current_h), flags=pygame.FULLSCREEN)
self.clock = pygame.time.Clock()
self.running = False
buttons = list(map(lambda t: ButtonDef(text=f"{t}", cbk=lambda: print(t)), list(range(btns))))
buttons = list(map(lambda t: ButtonDef(text=f"{t}", cbk=self.button_press_handler(str(t))), list(range(btns))))
buttons.append(ButtonDef(text="Exit", cbk=self.quit))
self.board: IBoard = MenuBoard(pygame.Rect(0, 0, info.current_w, info.current_h), buttons)
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons)
self.task_q = Queue()
def get_screen_rect(self) -> pygame.Rect:
return pygame.Rect(0, 0, self.screen.get_width(), self.screen.get_height())
def button_press_handler(self, text: str) -> Callable[[], None]:
def impl():
previous_board = self.board
def thr_fun():
def end_thr():
self.board = previous_board
sleep(5)
self.task_q.put(end_thr)
process_thr = Thread(target=thr_fun)
process_thr.start()
self.board = MessageBoard(self.get_screen_rect(), f"Wait: {text}")
return impl
def loop(self):
self.running = True
while self.running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
try:
self.task_q.get_nowait()()
except Empty:
pass
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
@ -125,6 +164,7 @@ class App:
pygame.quit()
def quit(self):
self.board = MessageBoard(self.get_screen_rect(), "Exiting...")
self.running = False