mirror of https://git.sr.ht/~michalr/menu
Switch boards after keypress, wait some time etc.
This commit is contained in:
parent
0ab21fabc0
commit
c330fa0bfe
48
menu.py
48
menu.py
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
import math
|
import math
|
||||||
import sys
|
import sys
|
||||||
|
from time import sleep
|
||||||
|
from threading import Thread
|
||||||
|
from queue import Queue, Empty
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from typing import Tuple, Callable, TypedDict, List
|
from typing import Tuple, Callable, TypedDict, List
|
||||||
|
|
||||||
|
@ -53,6 +56,23 @@ class Button:
|
||||||
self.cbk()
|
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):
|
class MenuBoard(IBoard):
|
||||||
BUTTON_MARGINS = 10
|
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.screen = pygame.display.set_mode((info.current_w, info.current_h), flags=pygame.FULLSCREEN)
|
||||||
self.clock = pygame.time.Clock()
|
self.clock = pygame.time.Clock()
|
||||||
self.running = False
|
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))
|
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):
|
def loop(self):
|
||||||
self.running = True
|
self.running = True
|
||||||
while self.running:
|
while self.running:
|
||||||
# poll for events
|
try:
|
||||||
# pygame.QUIT event means the user clicked X to close your window
|
self.task_q.get_nowait()()
|
||||||
|
except Empty:
|
||||||
|
pass
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
self.running = False
|
self.running = False
|
||||||
|
@ -125,6 +164,7 @@ class App:
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
def quit(self):
|
def quit(self):
|
||||||
|
self.board = MessageBoard(self.get_screen_rect(), "Exiting...")
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue