Controller-Fix: Joystick-Objekte wurden nach dem Namens-Auslesen freigegeben, dadurch lieferte SDL keine Events mehr. Jetzt offen halten + Hotplug. Neu: tools/controller_check.py zur Diagnose, Tests fuer Tastenbelegung (62).
This commit is contained in:
1 parent
a088c23a09
commit
5237b0a39c
7 files changed
+343
-17
No files matched your search
+48
-14
@@ -56,6 +56,53 @@ HAT_ACTIONS: dict[tuple[int, int], Action] = {
|
||||
#: Analogstick: ab diesem Ausschlag gilt eine Richtung als gewollt
|
||||
AXIS_DEADZONE = 0.6
|
||||
|
||||
#: Offene Joystick-Objekte. Sie müssen am Leben bleiben: gibt Python das
|
||||
#: Objekt frei, schliesst pygame das Gerät – und SDL liefert danach **keine**
|
||||
#: Joystick-Events mehr. Genau das war der Grund, weshalb ein erkannter
|
||||
#: Controller im Spiel nichts bewirkte.
|
||||
_OPEN_CONTROLLERS: list[pygame.joystick.Joystick] = []
|
||||
|
||||
|
||||
def open_controllers() -> list[str]:
|
||||
"""Öffnet alle angeschlossenen Controller und hält sie offen.
|
||||
|
||||
Nach ``pygame.init()`` aufrufen, und erneut, wenn ein Controller an- oder
|
||||
abgesteckt wird (``JOYDEVICEADDED`` / ``JOYDEVICEREMOVED``).
|
||||
"""
|
||||
_OPEN_CONTROLLERS.clear()
|
||||
names: list[str] = []
|
||||
if not pygame.joystick.get_init():
|
||||
try:
|
||||
pygame.joystick.init()
|
||||
except pygame.error:
|
||||
return names
|
||||
try:
|
||||
count = pygame.joystick.get_count()
|
||||
except pygame.error:
|
||||
return names
|
||||
for index in range(count):
|
||||
try:
|
||||
stick = pygame.joystick.Joystick(index)
|
||||
stick.init()
|
||||
except pygame.error:
|
||||
continue
|
||||
_OPEN_CONTROLLERS.append(stick)
|
||||
names.append(stick.get_name())
|
||||
return names
|
||||
|
||||
|
||||
def opened_controllers() -> list[pygame.joystick.Joystick]:
|
||||
"""Die aktuell offenen Controller (für Diagnose-Tools)."""
|
||||
return list(_OPEN_CONTROLLERS)
|
||||
|
||||
|
||||
def controller_names() -> list[str]:
|
||||
return [stick.get_name() for stick in _OPEN_CONTROLLERS]
|
||||
|
||||
|
||||
def is_device_event(event: pygame.event.Event) -> bool:
|
||||
return event.type in (pygame.JOYDEVICEADDED, pygame.JOYDEVICEREMOVED)
|
||||
|
||||
|
||||
def actions_from_event(event: pygame.event.Event) -> list[Action]:
|
||||
"""Übersetzt ein pygame-Event in null bis eine Aktion."""
|
||||
@@ -79,21 +126,8 @@ def actions_from_event(event: pygame.event.Event) -> list[Action]:
|
||||
return []
|
||||
|
||||
|
||||
def connected_controllers() -> list[str]:
|
||||
"""Namen aller angeschlossenen Controller (leer, wenn keiner da ist)."""
|
||||
names = []
|
||||
for index in range(pygame.joystick.get_count()):
|
||||
try:
|
||||
stick = pygame.joystick.Joystick(index)
|
||||
stick.init()
|
||||
names.append(stick.get_name())
|
||||
except pygame.error:
|
||||
continue
|
||||
return names
|
||||
|
||||
|
||||
def describe_controller_setup() -> str:
|
||||
names = connected_controllers()
|
||||
names = controller_names()
|
||||
if not names:
|
||||
return "Controller: keiner erkannt (Tastatur: Space=Snap, ←=Drehen, ↑=OnTop, Esc=Ende)"
|
||||
return "Controller: " + ", ".join(names)
|
||||
|
||||
Reference in new issue
Block a user