added methods for interactive mode

This commit is contained in:
Louis Heredero 2024-04-16 19:05:52 +02:00
parent 369db54b5a
commit 0c30f4ea24
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
2 changed files with 51 additions and 6 deletions

View File

@ -27,9 +27,6 @@ class MapDisplay:
self.font = pygame.font.SysFont("ubuntu", 20)
for city in self.cities:
self.draw_city(*city)
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)
@ -65,6 +62,10 @@ class MapDisplay:
pygame.draw.lines(self.surf, (255, 255, 255), True, points)
def draw_cities(self) -> None:
for city in self.cities:
self.draw_city(*city)
def draw_city(self, pos: Vec2, name: str, label_side: str) -> None:
pos2 = Vec2(*self.real_to_screen(pos.x, pos.y))
@ -94,3 +95,33 @@ 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}")
self.render()
pygame.display.flip()
clock.tick(30)
def init_interactive(self) -> None:
pass
def render(self) -> None:
pass

View File

@ -1,3 +1,5 @@
from typing import Optional
import pygame
from gps_loader import GPSLoader
@ -23,6 +25,8 @@ class SpeedMapDisplay(MapDisplay):
self.max_speed_col: tuple[int, int, int] = max_speed_col
self.segment_threshold: float = segment_threshold
self._path: Optional[Path] = None
def draw_path(self, path: Path) -> None:
min_speed = min(path.extra_data)
max_speed = max(path.extra_data)
@ -55,6 +59,16 @@ class SpeedMapDisplay(MapDisplay):
return r, g, b
def render(self) -> None:
self.surf.fill((0, 0, 0))
self.draw_cities()
if self._path is not None:
self.draw_path(self._path)
def set_path(self, path: Path) -> None:
self._path = path
if __name__ == '__main__':
name = "data_28-03"
@ -78,6 +92,6 @@ if __name__ == '__main__':
cities,
(255, 0, 0), (0, 255, 0), 155)
display.draw_path(path)
pygame.display.flip()
input()
display.set_path(path)
display.mainloop()