processing / processing-android

Processing mode and core library to create Android apps with Processing
http://android.processing.org
779 stars 293 forks source link

[Feature Request] Add support for input from game controllers #715

Open SableRaf opened 1 year ago

SableRaf commented 1 year ago

It would be great if Processing for Android could wrap the APIs for detecting and processing user input from game controllers that are already included in the Android framework. They are not currently easily accessible in Processing for Android. Making these APIs accessible in Processing would enable developers to create interactive experiences using game controllers on Android devices, including the increasingly popular Android-based gaming handhelds from the likes of Anbernic or Retroid but also any kind of Android smartphone or tablet with a bluetooth controller plugged in.

ezgif com-resize Edit: An example of a Processing sketch running on an Anbernic 353V

References:

SableRaf commented 1 year ago

I have posted a detailed description of the issue here hoping for answers:

https://stackoverflow.com/questions/74763835/handle-inputs-from-a-gamepad-with-processing-for-android

Edit: here's the relevant part of the message:

I’m trying to use Processing for Android to get inputs from a physical game controller (with buttons and joysticks).

I am using the Android documentation on controller actions as a guide.

Tl;dr: I'm struggling to find how to override the necessary callbacks (onGenericMotionEvent(), onKeyDown(), onKeyUp()) in the context of Processing since it instantiates the Activity and View itself.

Here's what I have managed so far:

  • Compile Processing examples to the device (specifically the RG353V, an Android-based handheld gaming console from Anbernic)
  • Use android.view.InputDevice to make a list of input devices and output their names to the console
  • Filter the input devices that are game controllers
  • Register an input device listener to receive notifications about when input devices are added, removed or changed using inputManager.registerInputDeviceListener()
  • Get the main activity with Activity activity = getActivity()
  • Get the root view with ViewGroup rootView = activity.findViewById(android.R.id.content); I now want to capture button/joystick events but this is where my trouble starts.

All the examples I found involve implementing a custom view and overriding onGenericMotionEvent() for joystick events, and onKeyDown()/onKeyUp() for button events.

Only Processing instantiates the main Activity and View on its own, so I don't know to override those callbacks. Is it even possible to override a method on an instance of a class?

I was hoping to find something similar to registerInputDeviceListener() in the InputManager class or registerListener() in the SensorManager class, but I haven't found a method that would allow to register callbacks for button and joystick input in the InputDevice class in the same way.