I am developing a platformer in Pygame with the eventual hope of releasing it on Steam.
I am currently trying to add input support for multiple controllers.
To do this, I am using the Pygame joystick module, which allows a connected controller to send input events to my event queue (the event types are JOYBUTTONDOWN, JOYBUTTONUP, and JOYAXISMOTION).
I want to have input support for at least keyboards, Xbox 360 controllers, Xbox One controllers, and PS4 controllers.
The problem, of course, is that these controllers all use different button IDs.
One way I thought of getting around this was to call a function in my event processing code which could convert input from any of these devices to a more abstract event tag. For example, keyboard presses of "a", Xbox 360 controller presses of left on the D-pad, and negative motion on the seventh axis of a PS4 controller would all set an abstract "mapped_event" attribute to "left". I could then use this "mapped_event" attribute in my player sprite's process_event() method, which moves him based on event input.
Here's a sketch of how that conversion code would look:
def map_event(event): # KEYBOARD BUTTON PRESSES if event.type == pg.KEYDOWN: if event.key = K_RIGHT: mapped_event = "right" elif event.key == K_LEFT: mapped_event = "left" # CONTROLLER BUTTON PRESSES elif event.type == pg.JOYBUTTONDOWN: if joystick == "Xbox360 controller": if event.button == 3: mapped_event = "right" elif event.button == 2: mapped_event = "left" # CONTROLLER AXIS MOTION elif event.type == pg.JOYAXISMOTION: if joystick == "Xbox360 controller": if event.axis == 1: if event.value == 1: mapped_event = "left" elif event.value == -1: mapped_event = "right" elif joystick == "PS4 controller": if event.axis == 3: if event.value == 1: mapped_event = "left" elif event.value == -1: mapped_event = "right" return mapped_event
From what I can tell, this is the way a lot of people recommend getting around the inconsistent controller issue. For example, I think this is what is meant when people speak about an "intermediate layer" of input processing and "mapping from real buttons to game concepts". Please correct me if I'm interpretting this wrong.
My question is:
How can I extend this method, (or an alternative), to also support all controllers which use, say, XInput/DirectX (according to this answer, a lot of aftermarket controllers use these), while handling multiple OSes?
I am open to using a different framework for processing joystick events, provided the language is Python.