| 191 | |
| 192 | |
| 193 | class HUD(object): |
| 194 | def __init__(self, width, height): |
| 195 | self.dim = (width, height) |
| 196 | font = pygame.font.Font(pygame.font.get_default_font(), 20) |
| 197 | font_name = 'courier' if os.name == 'nt' else 'mono' |
| 198 | fonts = [x for x in pygame.font.get_fonts() if font_name in x] |
| 199 | default_font = 'ubuntumono' |
| 200 | mono = default_font if default_font in fonts else fonts[0] |
| 201 | mono = pygame.font.match_font(mono) |
| 202 | self._font_mono = pygame.font.Font(mono, 12 if os.name == 'nt' else 14) |
| 203 | self._notifications = FadingText(font, (width, 40), (0, height - 40)) |
| 204 | self.help = HelpText(pygame.font.Font(mono, 24), width, height) |
| 205 | self.server_fps = 0 |
| 206 | self.frame = 0 |
| 207 | self.simulation_time = 0 |
| 208 | self._show_info = True |
| 209 | self._info_text = [] |
| 210 | self._server_clock = pygame.time.Clock() |
| 211 | |
| 212 | def on_world_tick(self, timestamp): |
| 213 | self._server_clock.tick() |
| 214 | self.server_fps = self._server_clock.get_fps() |
| 215 | self.frame = timestamp.frame |
| 216 | self.simulation_time = timestamp.elapsed_seconds |
| 217 | |
| 218 | def tick(self, world, clock): |
| 219 | self._notifications.tick(world, clock) |
| 220 | if not self._show_info: |
| 221 | return |
| 222 | t = world.player.get_transform() |
| 223 | v = world.player.get_velocity() |
| 224 | c = world.player.get_control() |
| 225 | heading = 'N' if abs(t.rotation.yaw) < 89.5 else '' |
| 226 | heading += 'S' if abs(t.rotation.yaw) > 90.5 else '' |
| 227 | heading += 'E' if 179.5 > t.rotation.yaw > 0.5 else '' |
| 228 | heading += 'W' if -0.5 > t.rotation.yaw > -179.5 else '' |
| 229 | colhist = world.collision_sensor.get_collision_history() |
| 230 | collision = [colhist[x + self.frame - 200] for x in range(0, 200)] |
| 231 | max_col = max(1.0, max(collision)) |
| 232 | collision = [x / max_col for x in collision] |
| 233 | vehicles = world.world.get_actors().filter('vehicle.*') |
| 234 | self._info_text = [ |
| 235 | 'Server: % 16.0f FPS' % self.server_fps, |
| 236 | 'Client: % 16.0f FPS' % clock.get_fps(), |
| 237 | '', |
| 238 | 'Vehicle: % 20s' % get_actor_display_name(world.player, truncate=20), |
| 239 | 'Map: % 20s' % world.map.name, |
| 240 | 'Simulation time: % 12s' % datetime.timedelta(seconds=int(self.simulation_time)), |
| 241 | '', |
| 242 | 'Speed: % 15.0f km/h' % (3.6 * math.sqrt(v.x ** 2 + v.y ** 2 + v.z ** 2)), |
| 243 | u'Heading:% 16.0f\N{DEGREE SIGN} % 2s' % (t.rotation.yaw, heading), |
| 244 | 'Location:% 20s' % ('(% 5.1f, % 5.1f)' % (t.location.x, t.location.y)), |
| 245 | 'GNSS:% 24s' % ('(% 2.6f, % 3.6f)' % (world.gnss_sensor.lat, world.gnss_sensor.lon)), |
| 246 | 'Height: % 18.0f m' % t.location.z, |
| 247 | ''] |
| 248 | if isinstance(c, carla.VehicleControl): |
| 249 | self._info_text += [ |
| 250 | ('Throttle:', c.throttle, 0.0, 1.0), |