Adds support for the addition and removal of joysticks.

This commit is contained in:
Markil3
2020-12-13 17:38:52 -07:00
parent 8595a875f4
commit d1105bf5ef

View File

@@ -21,6 +21,7 @@ import com.jme3.font.BitmapText;
import com.jme3.input.Joystick;
import com.jme3.input.JoystickAxis;
import com.jme3.input.JoystickButton;
import com.jme3.input.JoystickConnectionListener;
import com.jme3.input.RawInputListener;
import com.jme3.input.event.JoyAxisEvent;
import com.jme3.input.event.JoyButtonEvent;
@@ -70,7 +71,7 @@ import static markil3.controller.Main.START;
* @author Stephen Gold
*/
public class JoystickPreviewScreen extends BaseAppState
implements RawInputListener
implements RawInputListener, JoystickConnectionListener
{
private static final org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(JoystickPreviewScreen.class);
@@ -529,15 +530,32 @@ public class JoystickPreviewScreen extends BaseAppState
@Override
public void initialize(Application app)
{
int mostButtons = 0;
Joystick joy;
int l = this.getApplication().getInputManager().getJoysticks().length;
this.guiFont =
app.getAssetManager().loadFont("Interface/Fonts/Default.fnt");
this.refLabel = this.guiFont.createLabel("Axis X/Axis Y");
this.gui.attachChild(this.refLabel);
this.updateGamepad();
// this.initNavState();
// this.setFocus(this.playButton);
this.setEnabled(true);
((SimpleApplication) this.getApplication()).getGuiNode()
.attachChild(this.gui);
this.resize(((SimpleApplication) this.getApplication()).getCamera()
.getWidth(),
((SimpleApplication) this.getApplication()).getCamera()
.getHeight());
this.setEnabled(true);
}
private void updateGamepad()
{
int mostButtons = 0;
Joystick joy;
int l = this.getApplication().getInputManager().getJoysticks().length;
if (this.gamepadCont != null)
{
for (Node cont : this.gamepadCont)
@@ -548,9 +566,22 @@ public class JoystickPreviewScreen extends BaseAppState
}
}
}
if (this.labels != null)
{
for (BitmapText[] cont : this.labels)
{
for (BitmapText cont2 : cont)
{
if (cont2 != null)
{
cont2.removeFromParent();
}
}
}
}
for (int i = 0; i < l; i++)
{
joy = app.getInputManager().getJoysticks()[i];
joy = this.getApplication().getInputManager().getJoysticks()[i];
mostButtons = Math.max(joy.getAxisCount() + joy.getButtonCount(),
mostButtons);
}
@@ -573,18 +604,6 @@ public class JoystickPreviewScreen extends BaseAppState
}
}
this.addButtons();
// this.initNavState();
// this.setFocus(this.playButton);
this.setEnabled(true);
((SimpleApplication) this.getApplication()).getGuiNode()
.attachChild(this.gui);
this.resize(((SimpleApplication) this.getApplication()).getCamera()
.getWidth(),
((SimpleApplication) this.getApplication()).getCamera()
.getHeight());
this.setEnabled(true);
}
private void addButtons()
@@ -650,12 +669,16 @@ public class JoystickPreviewScreen extends BaseAppState
protected void onEnable()
{
this.getApplication().getInputManager().addRawInputListener(this);
this.getApplication().getInputManager()
.addJoystickConnectionListener(this);
}
@Override
protected void onDisable()
{
this.getApplication().getInputManager().removeRawInputListener(this);
this.getApplication().getInputManager()
.removeJoystickConnectionListener(this);
}
private void setLabels(Joystick joy)
@@ -928,4 +951,30 @@ public class JoystickPreviewScreen extends BaseAppState
public void onTouchEvent(TouchEvent evt)
{
}
@Override
public void onConnected(Joystick joystick)
{
this.updateGamepad();
this.resize(((SimpleApplication) this.getApplication()).getCamera()
.getWidth(),
((SimpleApplication) this.getApplication()).getCamera()
.getHeight());
}
@Override
public void onDisconnected(Joystick joystick)
{
/**
* TODO - This callback will fire before the joystick is actually
* removed. This is helpful at times, but it does mean we will have a
* blank slot because this code still thinks we have the one that was
* removed.
*/
this.updateGamepad();
this.resize(((SimpleApplication) this.getApplication()).getCamera()
.getWidth(),
((SimpleApplication) this.getApplication()).getCamera()
.getHeight());
}
}