Skip to content

Commit

Permalink
Pass through event registrations to the current gamepad
Browse files Browse the repository at this point in the history
Fix issue where registering and unregistering an event on the current gamepad didn't work on-the-fly.
The listeners were only hooked up once as a whole but adding/removing individual events at runtime was not supported properly which also made it a matter of execution order because events would have to be hooked up before the first Gamepad was detected.
  • Loading branch information
steffen-wilke committed Dec 18, 2021
1 parent 9a1eda2 commit b3c47f9
Showing 1 changed file with 126 additions and 14 deletions.
140 changes: 126 additions & 14 deletions core/src/main/java/de/gurkenlabs/litiengine/input/GamepadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,129 @@ public boolean isPressed(String gamepadComponent) {
return current != null && current.isPressed(gamepadComponent);
}

/** DON'T CALL THIS EXPLICITLY! THE LITIENGINE WILL MANAGE THE LIFECYCLE OF THIS INSTANCE. */
@Override
public void onPoll(GamepadPollListener listener) {
super.onPoll(listener);
if (this.current() != null) {
this.current().onPoll(listener);
}
}

@Override
public void onPressed(GamepadPressedListener listener) {
super.onPressed(listener);
if (this.current() != null) {
this.current().onPressed(listener);
}
}

@Override
public void onReleased(GamepadReleasedListener listener) {
super.onReleased(listener);
if (this.current() != null) {
this.current().onReleased(listener);
}
}

@Override
public void onPoll(String identifier, GamepadPollListener listener) {
super.onPoll(identifier, listener);
if (this.current() != null) {
this.current().onPoll(identifier, listener);
}
}

@Override
public void onPressed(String identifier, GamepadPressedListener listener) {
super.onPressed(identifier, listener);

if (this.current() != null) {
this.current().onPressed(identifier, listener);
}
}

@Override
public void onReleased(String identifier, GamepadReleasedListener listener) {
super.onReleased(identifier, listener);
if (this.current() != null) {
this.current().onReleased(identifier, listener);
}
}

@Override
public void clearEventListeners() {
super.clearEventListeners();

if (this.current() != null) {
this.current().clearEventListeners();
}
}

@Override
public void removePollListener(String identifier, GamepadPollListener listener) {
super.removePollListener(identifier, listener);

if (this.current() != null) {
this.current().removePollListener(identifier, listener);
}
}

@Override
public void removePressedListener(String identifier, GamepadPressedListener listener) {
super.removePressedListener(identifier, listener);

if (this.current() != null) {
this.current().removePressedListener(identifier, listener);
}
}

@Override
public void removeReleasedListener(String identifier, GamepadReleasedListener listener) {
super.removeReleasedListener(identifier, listener);

if (this.current() != null) {
this.current().removeReleasedListener(identifier, listener);
}
}

@Override
public void removePollListener(GamepadPollListener listener) {
super.removePollListener(listener);

if (this.current() != null) {
this.current().removePollListener(listener);
}
}

@Override
public void removePressedListener(GamepadPressedListener listener) {
super.removePressedListener(listener);

if (this.current() != null) {
this.current().removePressedListener(listener);
}
}

@Override
public void removeReleasedListener(GamepadReleasedListener listener) {
super.removeReleasedListener(listener);

if (this.current() != null) {
this.current().removeReleasedListener(listener);
}
}

/**
* DON'T CALL THIS EXPLICITLY! THE LITIENGINE WILL MANAGE THE LIFECYCLE OF THIS INSTANCE.
*/
@Override
public void start() {
this.hotPlugThread.start();
}

/** DON'T CALL THIS EXPLICITLY! THE LITIENGINE WILL MANAGE THE LIFECYCLE OF THIS INSTANCE. */
/**
* DON'T CALL THIS EXPLICITLY! THE LITIENGINE WILL MANAGE THE LIFECYCLE OF THIS INSTANCE.
*/
@Override
public void terminate() {
int totalWait = 0;
Expand All @@ -213,7 +329,9 @@ public void terminate() {
this.hotPlugThread.interrupt();
}

/** DON'T CALL THIS EXPLICITLY! THE LITIENGINE WILL MANAGE THE LIFECYCLE OF GAMEPADS. */
/**
* DON'T CALL THIS EXPLICITLY! THE LITIENGINE WILL MANAGE THE LIFECYCLE OF GAMEPADS.
*/
void remove(final Gamepad gamepad) {
if (gamepad == null) {
return;
Expand Down Expand Up @@ -263,22 +381,19 @@ private static void hackTheShitOutOfJInput() {
}

private void hookupToGamepad(final Gamepad pad) {
for (final Map.Entry<String, Collection<GamepadPollListener>> entry :
this.componentPollListeners.entrySet()) {
for (final Map.Entry<String, Collection<GamepadPollListener>> entry : this.componentPollListeners.entrySet()) {
for (final GamepadPollListener listener : entry.getValue()) {
pad.onPoll(entry.getKey(), listener);
}
}

for (final Map.Entry<String, Collection<GamepadPressedListener>> entry :
this.componentPressedListeners.entrySet()) {
for (final Map.Entry<String, Collection<GamepadPressedListener>> entry : this.componentPressedListeners.entrySet()) {
for (final GamepadPressedListener listener : entry.getValue()) {
pad.onPressed(entry.getKey(), listener);
}
}

for (final Map.Entry<String, Collection<GamepadReleasedListener>> entry :
this.componentReleasedListeners.entrySet()) {
for (final Map.Entry<String, Collection<GamepadReleasedListener>> entry : this.componentReleasedListeners.entrySet()) {
for (final GamepadReleasedListener listener : entry.getValue()) {
pad.onReleased(entry.getKey(), listener);
}
Expand All @@ -302,11 +417,8 @@ private void updateGamepads() {
try {
hackTheShitOutOfJInput();
// update plugged in gamepads
for (int i = 0;
i < ControllerEnvironment.getDefaultEnvironment().getControllers().length;
i++) {
final Controller controller =
ControllerEnvironment.getDefaultEnvironment().getControllers()[i];
for (int i = 0; i < ControllerEnvironment.getDefaultEnvironment().getControllers().length; i++) {
final Controller controller = ControllerEnvironment.getDefaultEnvironment().getControllers()[i];
final Type type = controller.getType();

if (type.equals(Type.KEYBOARD) || type.equals(Type.MOUSE) || type.equals(Type.UNKNOWN)) {
Expand Down

0 comments on commit b3c47f9

Please sign in to comment.