Skip to content

Commit

Permalink
Merge pull request #46 from ll7/ll7/issue45
Browse files Browse the repository at this point in the history
Improve text rendering in RobotSF simulation
  • Loading branch information
ll7 authored Aug 27, 2024
2 parents c17f338 + c336325 commit cb72a65
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions robot_sf/render/sim_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
PED_ACTION_COLOR = (255, 50, 50)
ROBOT_GOAL_COLOR = (0, 204, 102)
ROBOT_LIDAR_COLOR = (238, 160, 238, 128)
TEXT_COLOR = (0, 0, 0)
TEXT_COLOR = (255, 255, 255) # White text
TEXT_BACKGROUND = (0, 0, 0, 180) # Semi-transparent black background
TEXT_OUTLINE_COLOR = (0, 0, 0) # Black outline


@dataclass
Expand Down Expand Up @@ -413,13 +415,27 @@ def _add_text(self, timestep: int, state: VisualizableSimState):
f'RobotGoal: {state.action.robot_goal}',
'(Press h for help)',
]

# Create a surface for the text background
max_width = max(self.font.size(line)[0] for line in text_lines)
text_height = len(text_lines) * self.font.get_linesize()
text_surface = pygame.Surface((max_width + 10, text_height + 10), pygame.SRCALPHA)
text_surface.fill(TEXT_BACKGROUND)

for i, text in enumerate(text_lines):
text_surface = self.font.render(text, False, TEXT_COLOR)
pos = (
self._timestep_text_pos[0],
self._timestep_text_pos[1] + i * self.font.get_linesize()
)
self.screen.blit(text_surface, pos)
text_render = self.font.render(text, True, TEXT_COLOR)
text_outline = self.font.render(text, True, TEXT_OUTLINE_COLOR)

pos = (5, i * self.font.get_linesize() + 5)

# Draw text outline
for dx, dy in [(-1, -1), (-1, 1), (1, -1), (1, 1)]:
text_surface.blit(text_outline, (pos[0] + dx, pos[1] + dy))

# Draw main text
text_surface.blit(text_render, pos)

self.screen.blit(text_surface, self._timestep_text_pos)

def _add_help_text(self):
text_lines = [
Expand All @@ -437,13 +453,25 @@ def _add_help_text(self):
text_surface = self.font.render(text_lines[1], False, TEXT_COLOR)
width = text_surface.get_width() + 10

max_width = max(self.font.size(line)[0] for line in text_lines)
text_height = len(text_lines) * self.font.get_linesize()
text_surface = pygame.Surface((max_width + 10, text_height + 10), pygame.SRCALPHA)
text_surface.fill(TEXT_BACKGROUND)

for i, text in enumerate(text_lines):
text_surface = self.font.render(text, False, TEXT_COLOR)
pos = (
self.width - width,
self._timestep_text_pos[1] + i * self.font.get_linesize()
)
self.screen.blit(text_surface, pos)
text_render = self.font.render(text, True, TEXT_COLOR)
text_outline = self.font.render(text, True, TEXT_OUTLINE_COLOR)

pos = (5, i * self.font.get_linesize() + 5)

# Draw text outline
for dx, dy in [(-1, -1), (-1, 1), (1, -1), (1, 1)]:
text_surface.blit(text_outline, (pos[0] + dx, pos[1] + dy))

# Draw main text
text_surface.blit(text_render, pos)

self.screen.blit(text_surface, (self.width - max_width - 10, self._timestep_text_pos[1]))

def _draw_grid(
self,
Expand Down

0 comments on commit cb72a65

Please sign in to comment.