diff --git a/map_display.py b/map_display.py index fb714cb..651649d 100644 --- a/map_display.py +++ b/map_display.py @@ -10,6 +10,7 @@ class MapDisplay: PATH_WIDTH = 5 SEGMENT_SIZE = 20 MIN_SEGMENT_LENGTH = 5 + APP_NAME = "Train Journey - Map Display" def __init__(self, surf: pygame.Surface, @@ -25,7 +26,8 @@ class MapDisplay: self.max_lat: float = max_lat self.cities: list[tuple[Vec2, str, str]] = cities - self.font = pygame.font.SysFont("ubuntu", 20) + 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) @@ -116,12 +118,13 @@ class MapDisplay: 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: - pass + self._tooltip_surf = pygame.Surface(self.surf.get_size(), pygame.SRCALPHA) def render(self) -> None: pass diff --git a/speed_map_display.py b/speed_map_display.py index b5d8769..ae28d4d 100644 --- a/speed_map_display.py +++ b/speed_map_display.py @@ -10,6 +10,8 @@ from vec import Vec2 class SpeedMapDisplay(MapDisplay): + APP_NAME = "Train Journey - Speed Map Display" + def __init__(self, surf: pygame.Surface, min_lon: float, @@ -61,14 +63,42 @@ class SpeedMapDisplay(MapDisplay): def render(self) -> None: self.surf.fill((0, 0, 0)) + self._tooltip_surf.fill((0, 0, 0, 0)) self.draw_cities() if self._path is not None: self.draw_path(self._path) + mpos = Vec2(*pygame.mouse.get_pos()) + self.tooltip_nearest(mpos) + self.surf.blit(self._tooltip_surf, (0, 0)) def set_path(self, path: Path) -> None: self._path = path + def tooltip_nearest(self, mpos: Vec2) -> None: + closest = None + closest_i = 0 + min_dist = 0 + + for i, pt in enumerate(self._path.points): + pt = Vec2(*self.real_to_screen(pt.x, pt.y)) + dist = (pt - mpos).mag + if i == 0 or dist < min_dist: + closest = pt + closest_i = i + min_dist = dist + + pt = closest + speed = self._path.extra_data[closest_i] + col = (200, 150, 50) + pygame.draw.circle(self.surf, col, (pt.x, pt.y), 2 * self.PATH_WIDTH, 2) + pygame.draw.line(self.surf, col, (pt.x, pt.y), (mpos.x, mpos.y), 2) + txt = self.font.render(f"{speed:.1f} km/h", True, (150, 200, 255)) + txt_size = Vec2(*txt.get_size()) + txt_pos = mpos - txt_size.scale(Vec2(0.5, 1)) + pygame.draw.rect(self._tooltip_surf, (0, 0, 0, 150), (txt_pos.x, txt_pos.y, txt_size.x, txt_size.y)) + self._tooltip_surf.blit(txt, (txt_pos.x, txt_pos.y)) + if __name__ == '__main__': name = "data_28-03"