1
0
Fork 0

Events after screensaver should be ignored for a short time

This commit is contained in:
Michał Rudowicz 2024-09-10 18:55:12 +02:00
parent c20557cc32
commit 75659d8142
1 changed files with 13 additions and 5 deletions

18
menu.py
View File

@ -162,6 +162,7 @@ class MenuBoard(IBoard):
class App:
FPS = 30
SHOW_REQUEST_MESSAGE_FOR_AT_LEAST_S = 5
IGNORE_EVENTS_FOR_TICKS_AFTER_SCREENSAVER = 5
def __init__(self, actions: List[Action], theme: Dict, fullscreen: bool, screensaver_delay: int):
pygame.init()
@ -178,7 +179,6 @@ class App:
self.task_q = Queue()
self.screensaver = ClockScreensaver()
self.TICKS_UNTIL_SCREENSAVER = screensaver_delay * self.FPS
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())
@ -241,24 +241,32 @@ class App:
def loop(self):
self.running = True
needs_redrawing = True
# Block event handling for some time after screensaver is disabled to prevent
# user from clicking somewhere they don't intend to.
ticks_after_screensaver = 0
# how many ticks left until the screensaver should be engaged
screensaver_ticks = self.TICKS_UNTIL_SCREENSAVER
while self.running:
try:
self.task_q.get_nowait()()
except Empty:
pass
ticks_after_screensaver = max(ticks_after_screensaver-1, 0)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if self.screensaver_ticks != 0:
if screensaver_ticks != 0 and ticks_after_screensaver == 0:
self.board.handle_event(event)
self.screensaver_ticks = self.TICKS_UNTIL_SCREENSAVER
screensaver_ticks = self.TICKS_UNTIL_SCREENSAVER
needs_redrawing = True
if self.screensaver_ticks == 0:
if screensaver_ticks == 0:
self.screensaver.draw(self.screen, False)
ticks_after_screensaver = self.IGNORE_EVENTS_FOR_TICKS_AFTER_SCREENSAVER
else:
self.board.draw(self.screen, needs_redrawing)
self.screensaver_ticks -= 1
screensaver_ticks -= 1
needs_redrawing = False
pygame.display.flip()