Skip to content

Conversation

@ghost
Copy link

@ghost ghost commented May 11, 2019

Further to thread: https://hub.jmonkeyengine.org/t/gamepad-disconnect-re-connect/41822

This PR changes the following:

  • You can add joysticks AFTER the game is loaded.
  • You don’t need to plug the joystick in BEFORE the game starts.
  • You are notified if a joystick is added or removed.
  • No errors are thrown if a joystick is removed.
Connection listeners can be added, removed and cleared via the InputManager.

getInputManager().addJoystickConnectionListener(new JoystickConnectionListener(){

            @Override
            public void connectionChanged(int joystickId, JoystickState action) {
                System.out.println(action == JoystickState.CONNECTED
                        ? "Joystick Connected:" + joystickId
                        : "Joystick Disconnected:" + joystickId
                );
            }
        });

Fixes bug causing NullPointerException when removing joysticks.
Allows adding joysticks after the application has started.
@Ali-RS
Copy link
Member

Ali-RS commented May 11, 2019

Thanks @jayfella
Can you please fix this little thing:

private static final Logger LOGGER = Logger.getLogger(InputManager.class.getName());

it should be

private static final Logger logger = Logger.getLogger(GlfwJoystickInput.class.getName());

@pspeed42
Copy link
Contributor

Also, on quick glance, it's a little weird to embed lwjgl3-specific magic numbers into a core class rather than translating them into something more general in the implementation layer. That means every other joystick layer that wanted to implement this would have to mimic lwjgl3 constants. Just define the Connected/Disconnected enum (enum values should not be all caps) and translate that in the glfw layer.

@tonihele
Copy link
Contributor

tonihele commented May 11, 2019 via email

@ghost
Copy link
Author

ghost commented May 11, 2019

Ok.

  • changed the logger to the correct class
  • removed magic number from the enum constant.
  • The LWJGL3 context now translates its event ID to a JoystickState enum constant.
  • Removed InputManager field and use RawInputListener instead.

@tonihele the listener code in the comments is just an example written here of the JoystickConnectionListener entry point - unless I missed a line somwehere?

@pspeed42
Copy link
Contributor

The first time I read JoystickState I wondered why you were making an app state. That's an unfortunate name collision. Not really sure I have a better idea (though a boolean "connected" might also work instead of an enum.).

Hmmm...

@Ali-RS
Copy link
Member

Ali-RS commented May 12, 2019

Yes, I also think a boolean would be better here.

@ghost
Copy link
Author

ghost commented May 12, 2019

Yep. Makes sense. Done.

@ghost
Copy link
Author

ghost commented May 13, 2019

Are we all good with this now?

@Ali-RS
Copy link
Member

Ali-RS commented May 13, 2019

Looks fine to me.

@pspeed42
Copy link
Contributor

It's going to sound like I'm being nit-picky but this is going to become part of the public API and we will be stuck with it forever. So I gave it a little more thought and I'm sorry I didn't do that earlier when you were already making changes.

JME seems to follow a couple different patterns: 1) pass an event object that can be interrogated and have fewer listener methods (for example, we don't have separate pressed/released button listener methods), or 2) pass the raw event pieces and have more methods... (for example: ClientStateListener.onConnect()/onDisconnect(), ConnectionListener.connectionAdded()/connectionRemoved())... sometimes both. (CinematicListener.onPlay()/onPause()/onStop())

In that light, even if we only keep one method on the JoystickConnectionListener, I think it should pass the Joystick and not the joystick ID. It would better fit all of the other joystick listener methods to pass the actual object instead of making the listener look it up. It's also nice that one of the parameters would be strong-type tied to joysticks just from an idiomatic perspective for some wierdo who tries to implement a lot of interfaces on one object.

I don't have a strong opinion on JoystickConnectionListener.onConnect()/onDisconnect() versus a boolean... just something to think about. I think the Joystick versus ID thing is important, though.

Use separate connection methods (onConnected/onDisconnected).
@ghost
Copy link
Author

ghost commented May 13, 2019

No not at all. I'd rather it be as it should be. Referencing the joystick makes sense - from a user point of view the code seems a lot more descriptive.

getInputManager().addJoystickConnectionListener(new JoystickConnectionListener() {

            @Override
            public void onConnected(Joystick joystick) {
                System.out.println("Joystick connected: " + joystick.getName());
            }

            @Override
            public void onDisconnected(Joystick joystick) {
                System.out.println("Joystick disconnected: " + joystick.getName());
            }
        });

I actually went with separate methods because I think in the case of buttons it usually follows the same pattern (move left, move -left or whatever) but in a connection state it's a separate flow direction completely - it would need to follow a recovery or pause phase on disconnection. In my experience each method should only do one thing to keep code maintainable - and this way keeps in line with that.

@pspeed42
Copy link
Contributor

I agree. I like separate methods better for all of the reasons you listed... I just didn't want to send you back to the drawing board if you weren't feeling it. It wasn't worth the argument when I really really wanted to have the Joystick parameter. :)

Changes look good to me... I'll let them percolate a little longer in case someone else wants to review the latest changes.

@Ali-RS Ali-RS requested a review from stephengold May 22, 2019 16:47
@Ali-RS
Copy link
Member

Ali-RS commented May 22, 2019

@stephengold are you alright with this PR?

@stephengold
Copy link
Member

I'm unfamiliar with JME's support for joysticks and therefore unqualified to review this PR.

@stephengold stephengold removed their request for review May 22, 2019 17:05
@pspeed42 pspeed42 merged commit 33ade6b into jMonkeyEngine:master May 22, 2019
@pspeed42
Copy link
Contributor

I guess no one else but me and Jayfella are going to look at it so might as well just push it through. Open the gates, guys, I think I'm just going to start blind-merging everything. lol.

@empirephoenix
Copy link
Contributor

Master is not stable, so go ahead, in the worst case it can always be reverted :)

@stephengold stephengold added this to the v3.3.0 milestone Jun 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants