Class that handles input received such as keyboard and mouse.
| 1367 | |
| 1368 | |
| 1369 | class InputControl(object): |
| 1370 | """Class that handles input received such as keyboard and mouse.""" |
| 1371 | |
| 1372 | def __init__(self, name): |
| 1373 | """Initializes input member variables when instance is created.""" |
| 1374 | self.name = name |
| 1375 | self.mouse_pos = (0, 0) |
| 1376 | self.mouse_offset = [0.0, 0.0] |
| 1377 | self.wheel_offset = 0.1 |
| 1378 | self.wheel_amount = 0.025 |
| 1379 | self._steer_cache = 0.0 |
| 1380 | self.control = None |
| 1381 | self._autopilot_enabled = False |
| 1382 | |
| 1383 | # Modules that input will depend on |
| 1384 | self._hud = None |
| 1385 | self._world = None |
| 1386 | |
| 1387 | def start(self, hud, world): |
| 1388 | """Assigns other initialized modules that input module needs.""" |
| 1389 | self._hud = hud |
| 1390 | self._world = world |
| 1391 | |
| 1392 | self._hud.notification("Press 'H' or '?' for help.", seconds=4.0) |
| 1393 | |
| 1394 | def render(self, display): |
| 1395 | """Does nothing. Input module does not need render anything.""" |
| 1396 | |
| 1397 | def tick(self, clock): |
| 1398 | """Executed each frame. Calls method for parsing input.""" |
| 1399 | self.parse_input(clock) |
| 1400 | |
| 1401 | def _parse_events(self): |
| 1402 | """Parses input events. These events are executed only once when pressing a key.""" |
| 1403 | self.mouse_pos = pygame.mouse.get_pos() |
| 1404 | for event in pygame.event.get(): |
| 1405 | if event.type == pygame.QUIT: |
| 1406 | exit_game() |
| 1407 | elif event.type == pygame.KEYUP: |
| 1408 | if self._is_quit_shortcut(event.key): |
| 1409 | exit_game() |
| 1410 | elif event.key == K_h or (event.key == K_SLASH and pygame.key.get_mods() & KMOD_SHIFT): |
| 1411 | self._hud.help.toggle() |
| 1412 | elif event.key == K_TAB: |
| 1413 | # Toggle between hero and map mode |
| 1414 | if self._world.hero_actor is None: |
| 1415 | self._world.select_hero_actor() |
| 1416 | self.wheel_offset = HERO_DEFAULT_SCALE |
| 1417 | self.control = carla.VehicleControl() |
| 1418 | self._hud.notification('Hero Mode') |
| 1419 | else: |
| 1420 | self.wheel_offset = MAP_DEFAULT_SCALE |
| 1421 | self.mouse_offset = [0, 0] |
| 1422 | self.mouse_pos = [0, 0] |
| 1423 | self._world.scale_offset = [0, 0] |
| 1424 | self._world.hero_actor = None |
| 1425 | self._hud.notification('Map Mode') |
| 1426 | elif event.key == K_F1: |