diff options
Diffstat (limited to 'src/splitscreen_duo/input.py')
| -rw-r--r-- | src/splitscreen_duo/input.py | 50 |
1 files changed, 43 insertions, 7 deletions
diff --git a/src/splitscreen_duo/input.py b/src/splitscreen_duo/input.py index 29f97cb..f5a8233 100644 --- a/src/splitscreen_duo/input.py +++ b/src/splitscreen_duo/input.py @@ -1,4 +1,7 @@ import pygame +import os + +ROTATION_ENABLED = os.getenv("DEVELOPMENT", "") in ["", "0"] class Input: @@ -6,6 +9,7 @@ class Input: pygame.joystick.init() self.controller = None + self.instance = os.getenv("INSTANCE", "Unknown") if pygame.joystick.get_count() > 0: self.controller = pygame.joystick.Joystick(0) @@ -17,14 +21,18 @@ class Input: self.prev_axis_states = {"x": 0, "y": 0} self.prev_hat_states = (0, 0) # D-pad - def get_input(self, event): + def get_input(self, event, rotation_angle=0, is_joint_mode=False): + input_action = None + if self.controller: - controller_input = self._get_controller_input(event) + input_action = self._get_controller_input(event) + + if input_action: + return self._apply_rotation(input_action, rotation_angle, is_joint_mode) - if controller_input: - return controller_input + input_action = self._get_keyboard_input(event) - return self._get_keyboard_input(event) + return self._apply_rotation(input_action, rotation_angle, is_joint_mode) def _get_keyboard_input(self, event): if event.type == pygame.KEYDOWN: @@ -54,10 +62,10 @@ class Input: y = self.controller.get_axis(1) # Y-axis if x <= -0.1: - return "RIGHT" + return "LEFT" if x >= 0.1: - return "LEFT" + return "RIGHT" if y <= -0.1: return "UP" @@ -122,3 +130,31 @@ class Input: self.prev_button_states = self.button_states.copy() return None + + def _apply_rotation(self, action, rotation_angle, is_joint_mode): + if not action or not ROTATION_ENABLED or is_joint_mode or rotation_angle == 0: + return action + + # Primary: 90 degrees, right is down + if self.instance == "primary" and rotation_angle == 90: + if action == "RIGHT": + return "DOWN" + elif action == "LEFT": + return "UP" + elif action == "UP": + return "LEFT" + elif action == "DOWN": + return "RIGHT" + + # Secondary, -90 degrees, left is down + elif self.instance == "secondary" and rotation_angle == -90: + if action == "RIGHT": + return "UP" + elif action == "LEFT": + return "DOWN" + elif action == "UP": + return "RIGHT" + elif action == "DOWN": + return "LEFT" + + return action |