From 477f479712f2fde4abbef21abd44387bb72a2d07 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 17 Apr 2024 17:35:07 +0200 Subject: [PATCH] abstracted Display class --- display.py | 43 +++++++++++++++++++++++++++++++++++++++++++ map_display.py | 40 ++++------------------------------------ 2 files changed, 47 insertions(+), 36 deletions(-) create mode 100644 display.py diff --git a/display.py b/display.py new file mode 100644 index 0000000..2dea115 --- /dev/null +++ b/display.py @@ -0,0 +1,43 @@ +from typing import Optional + +import pygame + + +class Display: + APP_NAME = "Train Journey" + + def __init__(self, surf: pygame.Surface): + self.surf: pygame.Surface = surf + self.font: pygame.font.Font = pygame.font.SysFont("ubuntu", 20) + self._tooltip_surf: Optional[pygame.Surface] = None + + def mainloop(self) -> None: + running = True + + self.init_interactive() + + clock = pygame.time.Clock() + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + running = False + + elif event.key == pygame.K_s and event.mod & pygame.KMOD_CTRL: + path = "/tmp/image.jpg" + pygame.image.save(self.surf, path) + print(f"Saved as {path}") + + pygame.display.set_caption(f"{self.APP_NAME} - {clock.get_fps():.2f}fps") + self.render() + pygame.display.flip() + clock.tick(30) + + def init_interactive(self) -> None: + self._tooltip_surf = pygame.Surface(self.surf.get_size(), pygame.SRCALPHA) + + def render(self) -> None: + pass diff --git a/map_display.py b/map_display.py index 651649d..a3bf7cd 100644 --- a/map_display.py +++ b/map_display.py @@ -2,11 +2,12 @@ from typing import Optional import pygame +from display import Display from path import Path from vec import Vec2 -class MapDisplay: +class MapDisplay(Display): PATH_WIDTH = 5 SEGMENT_SIZE = 20 MIN_SEGMENT_LENGTH = 5 @@ -19,16 +20,14 @@ class MapDisplay: min_lat: float, max_lat: float, cities: list[tuple[Vec2, str, str]]): - self.surf: pygame.Surface = surf + + super().__init__(surf) self.min_lon: float = min_lon self.max_lon: float = max_lon self.min_lat: float = min_lat self.max_lat: float = max_lat self.cities: list[tuple[Vec2, str, str]] = cities - self.font: pygame.font.Font = pygame.font.SysFont("ubuntu", 20) - self._tooltip_surf: Optional[pygame.Surface] = None - def real_to_screen(self, lon: float, lat: float) -> tuple[float, float]: x = (lon - self.min_lon) / (self.max_lon - self.min_lon) y = (lat - self.min_lat) / (self.max_lat - self.min_lat) @@ -97,34 +96,3 @@ class MapDisplay: pygame.draw.line(self.surf, (255, 255, 255), (pos2.x, pos2.y), (line_end.x, line_end.y)) self.surf.blit(label, (label_pos.x, label_pos.y)) - - def mainloop(self) -> None: - running = True - - self.init_interactive() - - clock = pygame.time.Clock() - while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - elif event.type == pygame.KEYDOWN: - if event.key == pygame.K_ESCAPE: - running = False - - elif event.key == pygame.K_s and event.mod & pygame.KMOD_CTRL: - path = "/tmp/image.jpg" - pygame.image.save(self.surf, path) - print(f"Saved as {path}") - - pygame.display.set_caption(f"{self.APP_NAME} - {clock.get_fps():.2f}fps") - self.render() - pygame.display.flip() - clock.tick(30) - - def init_interactive(self) -> None: - self._tooltip_surf = pygame.Surface(self.surf.get_size(), pygame.SRCALPHA) - - def render(self) -> None: - pass