abstracted Display class
This commit is contained in:
parent
1ddb840e60
commit
477f479712
43
display.py
Normal file
43
display.py
Normal file
@ -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
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user