aboutsummaryrefslogtreecommitdiff
path: root/src/splitscreen_duo/input.py
diff options
context:
space:
mode:
authorZoltan Szabatin <[email protected]>2025-03-05 20:16:08 -0800
committerZoltan Szabatin <[email protected]>2025-03-05 20:16:08 -0800
commit424198608dc0f7f4246b13fa6420c1244719de3f (patch)
tree5b4de0498208ef3e68a6f33dca11fc3d8503823d /src/splitscreen_duo/input.py
parentfeat: Add Pico-specific modules (diff)
downloadsplitscreen-duo-424198608dc0f7f4246b13fa6420c1244719de3f.tar.xz
splitscreen-duo-424198608dc0f7f4246b13fa6420c1244719de3f.zip
feat(Input): Debounce buttons
Diffstat (limited to 'src/splitscreen_duo/input.py')
-rw-r--r--src/splitscreen_duo/input.py70
1 files changed, 50 insertions, 20 deletions
diff --git a/src/splitscreen_duo/input.py b/src/splitscreen_duo/input.py
index 769a5e5..b42cd7d 100644
--- a/src/splitscreen_duo/input.py
+++ b/src/splitscreen_duo/input.py
@@ -5,6 +5,9 @@ class Input:
def __init__(self, debug=True):
self.debug = debug
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()
@@ -47,46 +50,73 @@ class Input:
if event.type == pygame.JOYAXISMOTION:
if event.axis == 0: # X-axis (left/right)
- if event.value < -0.5: # Joystick left
+ prev_x = self.prev_axis_states["x"]
+ 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 event.value > 0.5: # Joystick right
+ if prev_x < 0.5 and curr_x > 0.5: # Transition to right
return "RIGHT"
+
elif event.axis == 1: # Y-axis (up/down)
- if event.value < -0.5: # Joystick up
+ prev_y = self.prev_axis_states["y"]
+ curr_y = event.value
+ self.prev_axis_states["y"] = curr_y
+
+ if prev_y > -0.5 and curr_y < -0.5: # Transition to up
return "UP"
- if event.value > 0.5: # Joystick down
+ if prev_y < 0.5 and curr_y > 0.5: # Transition to down
return "DOWN"
+
elif event.type == pygame.JOYHATMOTION:
- if event.value[0] == -1: # D-pad left
+ # 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
return "LEFT"
- if event.value[0] == 1: # D-pad right
+ if prev_hat[0] == 0 and curr_hat[0] == 1: # Transition to right
return "RIGHT"
- if event.value[1] == 1: # D-pad up
+ if prev_hat[1] == 0 and curr_hat[1] == 1: # Transition to up
return "UP"
- if event.value[1] == -1: # D-pad down
+ if prev_hat[1] == 0 and curr_hat[1] == -1: # Transition to down
return "DOWN"
+
elif event.type == pygame.JOYBUTTONDOWN:
- if event.button == 0:
- return "DOWN"
+ button = event.button
- if event.button == 1:
- return "LEFT"
+ if button not in self.prev_button_states:
+ self.prev_button_states[button] = False
- if event.button == 2:
- return "UP"
+ if not self.prev_button_states[button]:
+ self.prev_button_states[button] = True
- if event.button == 3:
- return "RIGHT"
+ if button == 0:
+ return "DOWN"
- if event.button == 4:
- return "SELECT"
+ if button == 1:
+ return "LEFT"
+
+ if button == 2:
+ return "UP"
+
+ if button == 3:
+ return "RIGHT"
+
+ if button == 4:
+ return "START"
- if event.button == 5:
- return "START"
+ 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
return None