1
0
Fork 0

Initial screensaver support

This commit is contained in:
Michał Rudowicz 2024-09-09 19:56:38 +02:00
parent c483d92a66
commit 607057868d
1 changed files with 35 additions and 4 deletions

35
menu.py
View File

@ -4,7 +4,7 @@ import math
import argparse
import json
from enum import Enum, auto
from time import sleep
from time import sleep, strftime
from threading import Thread
from queue import Queue, Empty
from abc import abstractmethod
@ -41,6 +41,26 @@ class IBoard:
raise NotImplementedError()
class Screensaver(IBoard):
def handle_event(self, _):
pass
class ClockScreensaver(Screensaver):
def __init__(self):
self.font = pygame.freetype.SysFont(name="Sans", size=20)
def draw(self, screen: pygame.Surface):
text = strftime("%H:%M:%S")
screen.fill("black")
pygame.Rect(0, 0, screen.get_width(), screen.get_height())
text_rect = self.font.get_rect(text)
text_pos = pygame.Rect((screen.get_width() - text_rect.width)/2,
(screen.get_height() - text_rect.height)/2,
text_rect.width, text_rect.height)
self.font.render_to(screen, text_pos, text, fgcolor="pink")
class Button:
PRESS_OFFSET = 2
@ -140,6 +160,9 @@ class MenuBoard(IBoard):
class App:
FPS = 30
TICKS_UNTIL_SCREENSAVER = 1 * FPS
def __init__(self, urls: List[Action], bg_color: str):
pygame.init()
info = pygame.display.Info()
@ -151,6 +174,8 @@ class App:
buttons.append(ButtonDef(text="Exit", cbk=self.quit))
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, bg_color)
self.task_q = Queue()
self.screensaver = ClockScreensaver()
self.screensaver_ticks = self.TICKS_UNTIL_SCREENSAVER
def get_screen_rect(self) -> pygame.Rect:
return pygame.Rect(0, 0, self.screen.get_width(), self.screen.get_height())
@ -205,13 +230,19 @@ class App:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if self.screensaver_ticks != 0:
self.board.handle_event(event)
self.screensaver_ticks = self.TICKS_UNTIL_SCREENSAVER
if self.screensaver_ticks == 0:
self.screensaver.draw(self.screen)
else:
self.board.draw(self.screen)
self.screensaver_ticks -= 1
pygame.display.flip()
self.clock.tick(30)
self.clock.tick(self.FPS)
pygame.quit()
def quit(self):