8000 Route to controller setup when joystick present without saved mapping by jmahmood · Pull Request #28 · jmahmood/CardBrick · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions cardbrick-py/cardbrick/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,12 @@ def __init__(self, storage, service, audio, settings, paths=None,
self._init_display()
pygame.display.set_caption("CardBrick — Spanish Practice")
pygame.mouse.set_visible(False)
# In pygame 2 the SDL device closes again when a Joystick object
# is garbage collected, and SDL only delivers button/hat events
# for open devices — these references are what keeps input alive.
self._joysticks = {}
for i in range(pygame.joystick.get_count()):
pygame.joystick.Joystick(i).init()
self._open_joystick(i)

self.font_big = _load_font(38)
self.font = _load_font(26)
Expand Down Expand Up @@ -280,6 +284,20 @@ def _card_count(self):
def run(self):
if self.initial_state:
state = self.initial_state
elif self.input_map.source == "defaults" and \
pygame.joystick.get_count() > 0:
# A joystick is present but never calibrated: the built-in
# DEFAULT_BUTTONS guess is only a starting point and is
# frequently wrong for a given device's raw SDL button
# order. Landing in Parent Menu with a wrong guess strands
# the child/parent on a screen that doesn't respond to any
# button — calibration has to come first so south_button
# (etc.) actually means something. Keyboard-only setups
# (desktop testing) skip this: KEYBOARD_SEMANTICS is fixed
# and never needs calibration.
log.warning("joystick present with no saved mapping — "
"starting in controller setup")
state = "INPUT_DIAG"
elif self._card_count() == 0:
# Nothing imported yet: route straight to setup/parent mode.
log.warning("no cards in database — starting in parent mode")
Expand Down Expand Up @@ -314,6 +332,16 @@ def run(self):

# -- input ---------------------------------------------------------------------

def _open_joystick(self, device_index):
"""Open a joystick and retain the reference (see __init__)."""
try:
joy = pygame.joystick.Joystick(device_index)
except pygame.error as exc:
log.error("could not open joystick %d: %s", device_index, exc)
return None
self._joysticks[joy.get_instance_id()] = joy
return joy

def poll(self):
"""Next semantic action, or None.

Expand All @@ -331,11 +359,12 @@ def poll(self):
if event.type == pygame.QUIT:
raise QuitApp
if event.type == pygame.JOYDEVICEADDED:
joy = pygame.joystick.Joystick(event.device_index)
joy.init()
log.info("joystick connected: %r", joy.get_name())
joy = self._open_joystick(event.device_index)
if joy:
log.info("joystick connected: %r", joy.get_name())
continue
if event.type == pygame.JOYDEVICEREMOVED:
self._joysticks.pop(event.instance_id, None)
log.warning("joystick disconnected")
continue
action = self.input.translate(event)
Expand Down Expand Up @@ -1537,7 +1566,7 @@ def screen_input_diagnostic(self):
if abs(event.value) > 0.5:
raw = f"axis {event.axis} value={event.value:+.2f}"
elif event.type == pygame.JOYDEVICEADDED:
pygame.joystick.Joystick(event.device_index).init()
self._open_joystick(event.device_index)
raw = "joystick connected"
if raw is not None:
semantic = self.input.translate(event)
Expand Down Expand Up @@ -1626,7 +1655,7 @@ def screen_calibrate(self):
log.info("calibration cancelled")
return "INPUT_DIAG"
if event.type == pygame.JOYDEVICEADDED:
pygame.joystick.Joystick(event.device_index).init()
self._open_joystick(event.device_index)
if event.type == pygame.JOYHATMOTION and \
event.value != (0, 0):
if semantic.startswith("dpad_"):
Expand Down
6 changes: 4 additions & 2 deletions cardbrick-py/cardbrick/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ def __init__(self, storage, scheduler, audio, deck=None,
self.screen = pygame.display.set_mode((SCREEN_W, SCREEN_H), flags)
pygame.display.set_caption("CardBrick")
pygame.mouse.set_visible(False)
for i in range(pygame.joystick.get_count()):
pygame.joystick.Joystick(i).init()
# Keep references: in pygame 2 a garbage-collected Joystick
# object closes the SDL device and its events stop arriving.
self._joysticks = [pygame.joystick.Joystick(i)
for i in range(pygame.joystick.get_count())]

self.font_big = _load_font(34)
self.font = _load_font(24)
Expand Down
Loading
0