aboutsummaryrefslogtreecommitdiff
path: root/src/splitscreen_duo/input.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/splitscreen_duo/input.py')
-rw-r--r--src/splitscreen_duo/input.py91
1 files changed, 44 insertions, 47 deletions
diff --git a/src/splitscreen_duo/input.py b/src/splitscreen_duo/input.py
index b42cd7d..a947315 100644
--- a/src/splitscreen_duo/input.py
+++ b/src/splitscreen_duo/input.py
@@ -2,27 +2,31 @@ import pygame
class Input:
- def __init__(self, debug=True):
- self.debug = debug
+ def __init__(self, debug=False):
+ pygame.joystick.init()
+
self.controller = None
- self.prev_button_states = {}
- self.prev_axis_states = {"x": 0, "y": 0}
- self.prev_hat_states = (0, 0)
- if not self.debug:
- pygame.joystick.init()
+ if pygame.joystick.get_count() > 0:
+ self.controller = pygame.joystick.Joystick(0)
- if pygame.joystick.get_count() > 0:
- self.controller = pygame.joystick.Joystick(0)
- self.controller.init()
+ self.controller.init()
+
+ self.button_states = {} # Current states
+ self.prev_button_states = {} # Previous states
+ self.prev_axis_states = {"x": 0, "y": 0}
+ self.prev_hat_states = (0, 0) # D-pad
def get_input(self, event):
- if self.debug:
- return self.get_keyboard_input(event)
- else:
- return self.get_controller_input(event)
+ if self.controller:
+ controller_input = self._get_controller_input(event)
+
+ if controller_input:
+ return controller_input
- def get_keyboard_input(self, event):
+ return self._get_keyboard_input(event)
+
+ def _get_keyboard_input(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
return "UP"
@@ -44,60 +48,51 @@ class Input:
return None
- def get_controller_input(self, event):
- if not self.controller:
- return None
-
+ def _get_controller_input(self, event):
+ # Analog stick
if event.type == pygame.JOYAXISMOTION:
- if event.axis == 0: # X-axis (left/right)
- prev_x = self.prev_axis_states["x"]
+ if event.axis == 0: # X-axis
curr_x = event.value
- self.prev_axis_states["x"] = curr_x
-
- if prev_x > -0.5 and curr_x < -0.5: # Transition to left
- return "LEFT"
- if prev_x < 0.5 and curr_x > 0.5: # Transition to right
+ if curr_x <= -0.1:
return "RIGHT"
+ if curr_x >= 0.1:
+ return "LEFT"
- elif event.axis == 1: # Y-axis (up/down)
- prev_y = self.prev_axis_states["y"]
+ elif event.axis == 1: # Y-axis
curr_y = event.value
- self.prev_axis_states["y"] = curr_y
- if prev_y > -0.5 and curr_y < -0.5: # Transition to up
+ if curr_y <= -0.1:
return "UP"
-
- if prev_y < 0.5 and curr_y > 0.5: # Transition to down
+ if curr_y >= 0.1:
return "DOWN"
-
+ # D-pad
elif event.type == pygame.JOYHATMOTION:
- # D-pad
prev_hat = self.prev_hat_states
curr_hat = event.value
self.prev_hat_states = curr_hat
- if prev_hat[0] == 0 and curr_hat[0] == -1: # Transition to left
+ if prev_hat[0] == 0 and curr_hat[0] == -1:
return "LEFT"
- if prev_hat[0] == 0 and curr_hat[0] == 1: # Transition to right
+ if prev_hat[0] == 0 and curr_hat[0] == 1:
return "RIGHT"
- if prev_hat[1] == 0 and curr_hat[1] == 1: # Transition to up
+ if prev_hat[1] == 0 and curr_hat[1] == 1:
return "UP"
- if prev_hat[1] == 0 and curr_hat[1] == -1: # Transition to down
+ if prev_hat[1] == 0 and curr_hat[1] == -1:
return "DOWN"
-
+ # Buttons
elif event.type == pygame.JOYBUTTONDOWN:
button = event.button
+ self.button_states[button] = True
- if button not in self.prev_button_states:
- self.prev_button_states[button] = False
-
- if not self.prev_button_states[button]:
- self.prev_button_states[button] = True
-
+ # Debounced
+ if (
+ button not in self.prev_button_states
+ or not self.prev_button_states[button]
+ ):
if button == 0:
return "DOWN"
@@ -116,7 +111,9 @@ class Input:
if button == 5:
return "SELECT"
elif event.type == pygame.JOYBUTTONUP:
- if event.button in self.prev_button_states:
- self.prev_button_states[event.button] = False
+ button = event.button
+ self.button_states[button] = False
+
+ self.prev_button_states = self.button_states.copy()
return None