added tooltips

This commit is contained in:
Louis Heredero 2024-04-16 21:20:07 +02:00
parent 0c30f4ea24
commit 1ddb840e60
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
2 changed files with 35 additions and 2 deletions

View File

@ -10,6 +10,7 @@ class MapDisplay:
PATH_WIDTH = 5 PATH_WIDTH = 5
SEGMENT_SIZE = 20 SEGMENT_SIZE = 20
MIN_SEGMENT_LENGTH = 5 MIN_SEGMENT_LENGTH = 5
APP_NAME = "Train Journey - Map Display"
def __init__(self, def __init__(self,
surf: pygame.Surface, surf: pygame.Surface,
@ -25,7 +26,8 @@ class MapDisplay:
self.max_lat: float = max_lat self.max_lat: float = max_lat
self.cities: list[tuple[Vec2, str, str]] = cities 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]: def real_to_screen(self, lon: float, lat: float) -> tuple[float, float]:
x = (lon - self.min_lon) / (self.max_lon - self.min_lon) x = (lon - self.min_lon) / (self.max_lon - self.min_lon)
@ -116,12 +118,13 @@ class MapDisplay:
pygame.image.save(self.surf, path) pygame.image.save(self.surf, path)
print(f"Saved as {path}") print(f"Saved as {path}")
pygame.display.set_caption(f"{self.APP_NAME} - {clock.get_fps():.2f}fps")
self.render() self.render()
pygame.display.flip() pygame.display.flip()
clock.tick(30) clock.tick(30)
def init_interactive(self) -> None: def init_interactive(self) -> None:
pass self._tooltip_surf = pygame.Surface(self.surf.get_size(), pygame.SRCALPHA)
def render(self) -> None: def render(self) -> None:
pass pass

View File

@ -10,6 +10,8 @@ from vec import Vec2
class SpeedMapDisplay(MapDisplay): class SpeedMapDisplay(MapDisplay):
APP_NAME = "Train Journey - Speed Map Display"
def __init__(self, def __init__(self,
surf: pygame.Surface, surf: pygame.Surface,
min_lon: float, min_lon: float,
@ -61,14 +63,42 @@ class SpeedMapDisplay(MapDisplay):
def render(self) -> None: def render(self) -> None:
self.surf.fill((0, 0, 0)) self.surf.fill((0, 0, 0))
self._tooltip_surf.fill((0, 0, 0, 0))
self.draw_cities() self.draw_cities()
if self._path is not None: if self._path is not None:
self.draw_path(self._path) 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: def set_path(self, path: Path) -> None:
self._path = path 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__': if __name__ == '__main__':
name = "data_28-03" name = "data_28-03"