Controls testing. /JL

This commit is contained in:
2025-03-22 09:03:16 +01:00
parent 864cf88c42
commit 15b1de2ad7
11 changed files with 128 additions and 56 deletions

View File

@@ -1,3 +1,2 @@
from . import gamepad
from . import joystick
from . import keyboard

View File

@@ -1,25 +0,0 @@
"""
Class for grabbing gamepad signals
"""
from controls.controlsbase import ControlsBase
import pygame
import gamepad
class GamePadControls(ControlsBase):
def __init__(self):
gamapadType = gamepad.ps5
def left(self):
pass
def right(self):
pass
def up(self):
pass
def down(self):
pass
def pause(self):
pass

View File

@@ -1,44 +1,23 @@
"""
Class for grabbing joystick signals
"""
from controls.controlsbase import ControlsBase
import pygame
import pyjoystick
from pyjoystick.pygame import Key, Joystick, run_event_loop
class JoystickControls(ControlsBase):
def __init__(self):
mngr = pyjoystick.ThreadEventManager(event_loop=run_event_loop,
handle_key_event=self.handle_key_event)
mngr.start()
def handle_key_event(self, key):
if key.keytype != Key.HAT:
if key.value != pygame.K_P:
return
if key.value == Key.HAT_UP:
self.up()
elif key.value == Key.HAT_DOWN:
self.down()
if key.value == Key.HAT_LEFT:
self.left()
elif key.value == Key.HAT_RIGHT:
self.right()
elif key.value == Key.KEY_P:
self.pause()
pass
def left(self):
print("Joystick moved left")
pass
def right(self):
print("Joystick moved right")
pass
def up(self):
print("Joystick moved up")
pass
def down(self):
print("Joystick moved down")
pass
def pause(self):
print("Paused")
pass
def rumble(self):
pass

BIN
gamepad.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 327 KiB

BIN
joystick.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 245 KiB

BIN
keyboard.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 115 KiB

BIN
logitech-t310.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
ps5-dualsense.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 665 KiB

BIN
ps5-dualsense.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

119
testing/controls.py Normal file
View File

@@ -0,0 +1,119 @@
import pygame
from abc import ABC, abstractmethod
# Initialize pygame
pygame.init()
# Screen settings
WIDTH, HEIGHT = 600, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Select Control Method")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
# Font
font = pygame.font.Font(None, 36)
# Load images (replace with actual image paths)
def load_image(path, max_size):
image = pygame.image.load(path).convert_alpha() # Use convert_alpha for transparency
image_rect = image.get_rect()
scale_factor = min(max_size[0] / image_rect.width, max_size[1] / image_rect.height)
new_size = (int(image_rect.width * scale_factor), int(image_rect.height * scale_factor))
return pygame.transform.smoothscale(image, new_size)
keyboard_image = load_image("keyboard.png", (100, 100))
#joystick_image = load_image("ps5-dualsense.png", (100, 100))
joystick_image = load_image("gamepad.png", (100, 100))
# Abstract Control Class
class Control(ABC):
@abstractmethod
def handle_input(self, event):
pass
class KeyboardControl(Control):
def handle_input(self, event):
if event.type == pygame.KEYDOWN:
print("Keyboard key pressed")
class JoystickControl(Control):
def __init__(self, joystick_id):
self.joystick = pygame.joystick.Joystick(joystick_id)
self.joystick.init()
def handle_input(self, event):
if event.type == pygame.JOYBUTTONDOWN and event.joy == self.joystick.get_id():
print(f"Joystick {self.joystick.get_id()} button pressed")
# Options
options = ["Keyboard"]
option_images = {"Keyboard": keyboard_image}
pygame.joystick.init()
joystick_count = pygame.joystick.get_count()
joystick_controls = {}
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()
joystick_name = joystick.get_name()
options.append(joystick_name)
joystick_controls[joystick_name] = JoystickControl(i)
option_images[joystick_name] = joystick_image
selected_index = 0
# Control mapping
control_classes = {"Keyboard": KeyboardControl}
control_classes.update(joystick_controls)
selected_control = KeyboardControl()
running = True
while running:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN and selected_index < len(options) - 1:
selected_index += 1
elif event.key == pygame.K_UP and selected_index > 0:
selected_index -= 1
elif event.key == pygame.K_RETURN:
selected_control = control_classes[options[selected_index]]() if options[selected_index] == "Keyboard" else joystick_controls[options[selected_index]]
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
for i in range(len(options)):
if 100 <= x <= 300 and 100 + i * 40 <= y <= 140 + i * 40:
selected_index = i
selected_control = control_classes[options[selected_index]]() if options[selected_index] == "Keyboard" else joystick_controls[options[selected_index]]
break
# Pass input to selected control method
selected_control.handle_input(event)
# Render label
label = font.render("Controls:", True, BLACK)
screen.blit(label, (50, 50))
# Render list
for i, option in enumerate(options):
color = RED if i == selected_index else BLACK
text = font.render(option, True, color)
screen.blit(text, (100, 100 + i * 40))
# Render selected item image
selected_option = options[selected_index]
if selected_option in option_images:
image = option_images[selected_option]
image_rect = image.get_rect(center=(500, 200)) # Keep aspect ratio
screen.blit(image, image_rect.topleft)
pygame.display.flip()
pygame.quit()