summaryrefslogtreecommitdiff
path: root/cameltris/screens/pause.py
blob: 4f9a515611a9b285a98275a52ceb9f330561b9b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
""" Module that defines the pause screen class """

import contextlib
import sys
from typing import Callable, NoReturn

import pygame

from .base_screen import Screen
from ..controller import Controller, KeyboardController, JoystickController, Input
from ..misc import UnPause


def handle_input_pressed(controller: Controller, event: pygame.event.Event):
    """ Function that checks the input pressed and triggers the relevant action """
    if controller.get_input_down(event) == Input.PAUSE:
        raise UnPause()


class Pause(Screen):
    """ Represents the pause screen """
    def __init__(self, screen: pygame.surface.Surface):
        self.event_handler: dict[int, Callable[[pygame.event.Event], None]] = {}
        self.screen = screen

        def exit_(_) -> NoReturn:
            sys.exit()

        self.event_handler[pygame.QUIT] = exit_
        self.event_handler[pygame.JOYBUTTONDOWN] = lambda evt: handle_input_pressed(JoystickController(evt.joy), evt)
        self.event_handler[pygame.KEYDOWN] = lambda evt: handle_input_pressed(KeyboardController(pygame.key), evt)

    def refresh(self) -> None:
        pass

    def oneframe(self) -> None:
        try:
            for event in pygame.event.get():
                with contextlib.suppress(KeyError):
                    self.event_handler[event.type](event)
        except UnPause:
            pygame.event.clear()
            raise