2023-05-11 14:01:02 -04:00
|
|
|
import pygame
|
|
|
|
import importlib.util
|
2023-05-11 17:59:06 -04:00
|
|
|
import pocket_friends.game_files.io.gpio_handler as gpio_handler
|
2023-05-11 14:01:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
class InputHandler:
|
2023-05-11 15:31:36 -04:00
|
|
|
"""
|
|
|
|
Class that is implemented into surfaces in order to control the
|
|
|
|
pressing of buttons on both the real hardware and on a keyboard.
|
|
|
|
"""
|
|
|
|
|
2023-05-11 14:01:02 -04:00
|
|
|
def __init__(self, pygame_clock):
|
|
|
|
self.clock = pygame_clock
|
2023-05-11 15:31:36 -04:00
|
|
|
|
|
|
|
# If not on actual hardware, fake the GPIO in order to get it working correctly.
|
2023-05-11 14:01:02 -04:00
|
|
|
try:
|
|
|
|
importlib.util.find_spec('RPi.GPIO')
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
self.on_hardware = True
|
|
|
|
except ImportError:
|
2023-05-11 17:59:06 -04:00
|
|
|
import pocket_friends.game_files.io.fake_gpio as GPIO
|
2023-05-11 14:01:02 -04:00
|
|
|
self.on_hardware = False
|
2023-05-11 15:31:36 -04:00
|
|
|
|
2023-05-11 14:01:02 -04:00
|
|
|
self.last_input_tick = 0
|
|
|
|
|
|
|
|
def create_event(self, pressed_button):
|
|
|
|
"""
|
|
|
|
Creates a pygame event with a given keyboard code
|
|
|
|
:param pressed_button:
|
|
|
|
"""
|
|
|
|
# Register a button click so long as the last button click happened no less than two frames ago
|
|
|
|
if pygame.time.get_ticks() - self.last_input_tick > self.clock.get_time() * 2 or not self.on_hardware:
|
|
|
|
pygame.event.post(pygame.event.Event(pygame.KEYDOWN, {'key': pressed_button}))
|
|
|
|
pygame.event.post(pygame.event.Event(pygame.KEYUP, {'key': pressed_button}))
|
|
|
|
self.last_input_tick = pygame.time.get_ticks()
|
|
|
|
|
|
|
|
def handle_gpio(self):
|
|
|
|
"""
|
|
|
|
Handles getting GPIO button presses and making a pygame event when a press is detected.
|
|
|
|
"""
|
2023-05-11 17:59:06 -04:00
|
|
|
for pressed_button in gpio_handler.BUTTONS:
|
|
|
|
code = gpio_handler.BUTTONS.get(pressed_button)
|
2023-05-11 14:01:02 -04:00
|
|
|
|
|
|
|
# Check if a button has been pressed. If it has, create a pygame event for it.
|
2023-05-11 17:59:06 -04:00
|
|
|
if gpio_handler.get_press(code):
|
2023-05-11 14:01:02 -04:00
|
|
|
self.create_event(code)
|
|
|
|
|
|
|
|
def keyboard_handler(self):
|
|
|
|
"""
|
|
|
|
Simulates key presses to GPIO button presses. Also handles quitting the game.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Checks if a corresponding keyboard key has been pressed. If it has, emulate a button press.
|
|
|
|
for keyboard_event in pygame.event.get():
|
|
|
|
if keyboard_event.type == pygame.QUIT:
|
|
|
|
running = False
|
|
|
|
if keyboard_event.type == pygame.KEYDOWN:
|
|
|
|
if keyboard_event.key == pygame.K_a:
|
2023-05-11 17:59:06 -04:00
|
|
|
self.create_event(gpio_handler.BUTTONS.get('a'))
|
2023-05-11 14:01:02 -04:00
|
|
|
if keyboard_event.key == pygame.K_b:
|
2023-05-11 17:59:06 -04:00
|
|
|
self.create_event(gpio_handler.BUTTONS.get('b'))
|
2023-05-11 14:01:02 -04:00
|
|
|
if keyboard_event.key == pygame.K_PERIOD:
|
2023-05-11 17:59:06 -04:00
|
|
|
self.create_event(gpio_handler.BUTTONS.get('j_i'))
|
2023-05-11 14:01:02 -04:00
|
|
|
if keyboard_event.key == pygame.K_RIGHT:
|
2023-05-11 17:59:06 -04:00
|
|
|
self.create_event(gpio_handler.BUTTONS.get('j_r'))
|
2023-05-11 14:01:02 -04:00
|
|
|
if keyboard_event.key == pygame.K_LEFT:
|
2023-05-11 17:59:06 -04:00
|
|
|
self.create_event(gpio_handler.BUTTONS.get('j_l'))
|
2023-05-11 14:01:02 -04:00
|
|
|
if keyboard_event.key == pygame.K_DOWN:
|
2023-05-11 17:59:06 -04:00
|
|
|
self.create_event(gpio_handler.BUTTONS.get('j_d'))
|
2023-05-11 14:01:02 -04:00
|
|
|
if keyboard_event.key == pygame.K_UP:
|
2023-05-11 17:59:06 -04:00
|
|
|
self.create_event(gpio_handler.BUTTONS.get('j_u'))
|
2023-05-11 14:01:02 -04:00
|
|
|
if keyboard_event.key == pygame.K_ESCAPE:
|
|
|
|
running = False
|
|
|
|
|
|
|
|
def update(self):
|
2023-05-11 15:31:36 -04:00
|
|
|
"""
|
|
|
|
Run the input handler and check for inputs.
|
|
|
|
"""
|
2023-05-11 14:01:02 -04:00
|
|
|
if self.on_hardware:
|
|
|
|
self.handle_gpio()
|
|
|
|
else:
|
|
|
|
self.keyboard_handler()
|