| 479 | |
| 480 | |
| 481 | class HUD(object): |
| 482 | def __init__(self, width, height): |
| 483 | self.dim = (width, height) |
| 484 | font = pygame.font.Font(pygame.font.get_default_font(), 20) |
| 485 | font_name = 'courier' if os.name == 'nt' else 'mono' |
| 486 | fonts = [x for x in pygame.font.get_fonts() if font_name in x] |
| 487 | default_font = 'ubuntumono' |
| 488 | mono = default_font if default_font in fonts else fonts[0] |
| 489 | mono = pygame.font.match_font(mono) |
| 490 | self._font_mono = pygame.font.Font(mono, 12 if os.name == 'nt' else 14) |
| 491 | self._notifications = FadingText(font, (width, 40), (0, height - 40)) |
| 492 | self.help = HelpText(pygame.font.Font(mono, 16), width, height) |
| 493 | self.server_fps = 0 |
| 494 | self.frame = 0 |
| 495 | self.simulation_time = 0 |
| 496 | self._show_info = True |
| 497 | self._info_text = [] |
| 498 | self._server_clock = pygame.time.Clock() |
| 499 | |
| 500 | def on_world_tick(self, timestamp): |
| 501 | self._server_clock.tick() |
| 502 | self.server_fps = self._server_clock.get_fps() |
| 503 | self.frame = timestamp.frame |
| 504 | self.simulation_time = timestamp.elapsed_seconds |
| 505 | |
| 506 | def tick(self, world, clock): |
| 507 | self._notifications.tick(world, clock) |
| 508 | if not self._show_info: |
| 509 | return |
| 510 | t = world.player.get_transform() |
| 511 | v = world.player.get_velocity() |
| 512 | c = world.player.get_control() |
| 513 | compass = world.imu_sensor.compass |
| 514 | heading = 'N' if compass > 270.5 or compass < 89.5 else '' |
| 515 | heading += 'S' if 90.5 < compass < 269.5 else '' |
| 516 | heading += 'E' if 0.5 < compass < 179.5 else '' |
| 517 | heading += 'W' if 180.5 < compass < 359.5 else '' |
| 518 | colhist = world.collision_sensor.get_collision_history() |
| 519 | collision = [colhist[x + self.frame - 200] for x in range(0, 200)] |
| 520 | max_col = max(1.0, max(collision)) |
| 521 | collision = [x / max_col for x in collision] |
| 522 | vehicles = world.world.get_actors().filter('vehicle.*') |
| 523 | self._info_text = [ |
| 524 | 'Server: % 16.0f FPS' % self.server_fps, |
| 525 | 'Client: % 16.0f FPS' % clock.get_fps(), |
| 526 | '', |
| 527 | 'Vehicle: % 20s' % get_actor_display_name(world.player, truncate=20), |
| 528 | 'Map: % 20s' % world.map.name, |
| 529 | 'Simulation time: % 12s' % datetime.timedelta(seconds=int(self.simulation_time)), |
| 530 | '', |
| 531 | 'Speed: % 15.0f km/h' % (3.6 * math.sqrt(v.x**2 + v.y**2 + v.z**2)), |
| 532 | u'Compass:% 17.0f\N{DEGREE SIGN} % 2s' % (compass, heading), |
| 533 | 'Accelero: (%5.1f,%5.1f,%5.1f)' % (world.imu_sensor.accelerometer), |
| 534 | 'Gyroscop: (%5.1f,%5.1f,%5.1f)' % (world.imu_sensor.gyroscope), |
| 535 | 'Location:% 20s' % ('(% 5.1f, % 5.1f)' % (t.location.x, t.location.y)), |
| 536 | 'GNSS:% 24s' % ('(% 2.6f, % 3.6f)' % (world.gnss_sensor.lat, world.gnss_sensor.lon)), |
| 537 | 'Height: % 18.0f m' % t.location.z, |
| 538 | ''] |