mirror of https://git.sr.ht/~michalr/menu
Initial screensaver support
This commit is contained in:
parent
c483d92a66
commit
607057868d
35
menu.py
35
menu.py
|
@ -4,7 +4,7 @@ import math
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
from enum import Enum, auto
|
from enum import Enum, auto
|
||||||
from time import sleep
|
from time import sleep, strftime
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from queue import Queue, Empty
|
from queue import Queue, Empty
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
|
@ -41,6 +41,26 @@ class IBoard:
|
||||||
raise NotImplementedError()
|
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:
|
class Button:
|
||||||
PRESS_OFFSET = 2
|
PRESS_OFFSET = 2
|
||||||
|
|
||||||
|
@ -140,6 +160,9 @@ class MenuBoard(IBoard):
|
||||||
|
|
||||||
|
|
||||||
class App:
|
class App:
|
||||||
|
FPS = 30
|
||||||
|
TICKS_UNTIL_SCREENSAVER = 1 * FPS
|
||||||
|
|
||||||
def __init__(self, urls: List[Action], bg_color: str):
|
def __init__(self, urls: List[Action], bg_color: str):
|
||||||
pygame.init()
|
pygame.init()
|
||||||
info = pygame.display.Info()
|
info = pygame.display.Info()
|
||||||
|
@ -151,6 +174,8 @@ class App:
|
||||||
buttons.append(ButtonDef(text="Exit", cbk=self.quit))
|
buttons.append(ButtonDef(text="Exit", cbk=self.quit))
|
||||||
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, bg_color)
|
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, bg_color)
|
||||||
self.task_q = Queue()
|
self.task_q = Queue()
|
||||||
|
self.screensaver = ClockScreensaver()
|
||||||
|
self.screensaver_ticks = self.TICKS_UNTIL_SCREENSAVER
|
||||||
|
|
||||||
def get_screen_rect(self) -> pygame.Rect:
|
def get_screen_rect(self) -> pygame.Rect:
|
||||||
return pygame.Rect(0, 0, self.screen.get_width(), self.screen.get_height())
|
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():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
self.running = False
|
self.running = False
|
||||||
|
if self.screensaver_ticks != 0:
|
||||||
self.board.handle_event(event)
|
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.board.draw(self.screen)
|
||||||
|
self.screensaver_ticks -= 1
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
self.clock.tick(30)
|
self.clock.tick(self.FPS)
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
def quit(self):
|
def quit(self):
|
||||||
|
|
Loading…
Reference in New Issue