diff --git a/controls/__init__.py b/controls/__init__.py index bf2d4fe..bae5b71 100644 --- a/controls/__init__.py +++ b/controls/__init__.py @@ -1,3 +1,2 @@ -from . import gamepad from . import joystick from . import keyboard \ No newline at end of file diff --git a/controls/gamepad.py b/controls/gamepad.py deleted file mode 100644 index 203015a..0000000 --- a/controls/gamepad.py +++ /dev/null @@ -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 \ No newline at end of file diff --git a/controls/joystick.py b/controls/joystick.py index e7268f6..6f64e34 100644 --- a/controls/joystick.py +++ b/controls/joystick.py @@ -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") \ No newline at end of file + pass + + def rumble(self): + pass \ No newline at end of file diff --git a/gamepad.png b/gamepad.png new file mode 100644 index 0000000..cefbea0 Binary files /dev/null and b/gamepad.png differ diff --git a/joystick.png b/joystick.png new file mode 100644 index 0000000..e644d5d Binary files /dev/null and b/joystick.png differ diff --git a/keyboard.png b/keyboard.png new file mode 100644 index 0000000..7c98c60 Binary files /dev/null and b/keyboard.png differ diff --git a/logitech-t310.png b/logitech-t310.png new file mode 100644 index 0000000..b8f27e5 Binary files /dev/null and b/logitech-t310.png differ diff --git a/ps5-dualsense.png b/ps5-dualsense.png new file mode 100644 index 0000000..88b0617 Binary files /dev/null and b/ps5-dualsense.png differ diff --git a/ps5-dualsense.webp b/ps5-dualsense.webp new file mode 100644 index 0000000..380c55f Binary files /dev/null and b/ps5-dualsense.webp differ diff --git a/testing/controls.py b/testing/controls.py new file mode 100644 index 0000000..5f50919 --- /dev/null +++ b/testing/controls.py @@ -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() diff --git a/controls/joy.py b/testing/joystick.py similarity index 100% rename from controls/joy.py rename to testing/joystick.py