Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions jme3-core/src/main/java/com/jme3/input/JoystickAxis.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,14 @@ public interface JoystickAxis {
public static final String Z_ROTATION = "rz";
public static final String LEFT_TRIGGER = "rx";
public static final String RIGHT_TRIGGER = "ry";

// Note: the left/right trigger bit may be a bit controversial in
// the sense that this is one case where XBox controllers make a lot
// more sense.
// I've seen the following mappings for various things:
//
// Axis | XBox | Non-Xbox (generally) (includes actual Sony PS4 controllers)
// --------------+-------+---------------
// left trigger | z | rx (also button 6)
// right trigger | rz | ry (also button 7)
// left stick x | x | x
// left stick y | y | y
// right stick x | rx | z
// right stick y | ry | rz
//
// The issue is that in all cases I've seen, the XBox controllers will
// use the name "xbox" somewhere in their name. The Non-XBox controllers
// never mention anything uniform... even the PS4 controller only calls
// itself "Wireless Controller". In that light, it seems easier to make
// the default the ugly case and the "XBox" way the exception because it
// can more easily be identified.

public static final String AXIS_XBOX_LEFT_TRIGGER = LEFT_TRIGGER;
public static final String AXIS_XBOX_RIGHT_TRIGGER = RIGHT_TRIGGER;
public static final String AXIS_XBOX_LEFT_THUMB_STICK_X = X_AXIS;
public static final String AXIS_XBOX_LEFT_THUMB_STICK_Y = Y_AXIS;
public static final String AXIS_XBOX_RIGHT_THUMB_STICK_X = Z_AXIS;
public static final String AXIS_XBOX_RIGHT_THUMB_STICK_Y = Z_ROTATION;


public static final String POV_X = "pov_x";
public static final String POV_Y = "pov_y";
Expand Down
20 changes: 20 additions & 0 deletions jme3-core/src/main/java/com/jme3/input/JoystickButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ public interface JoystickButton {
public static final String BUTTON_14 = "14";
public static final String BUTTON_15 = "15";


public static final String BUTTON_XBOX_A = BUTTON_2;
public static final String BUTTON_XBOX_B = BUTTON_1;
public static final String BUTTON_XBOX_X = BUTTON_3;
public static final String BUTTON_XBOX_Y = BUTTON_0;
public static final String BUTTON_XBOX_LB = BUTTON_4;
public static final String BUTTON_XBOX_RB = BUTTON_5;
public static final String BUTTON_XBOX_LT = BUTTON_6;
public static final String BUTTON_XBOX_RT = BUTTON_7;
public static final String BUTTON_XBOX_BACK = BUTTON_8;
public static final String BUTTON_XBOX_START = BUTTON_9;
public static final String BUTTON_XBOX_L3 = BUTTON_10;
public static final String BUTTON_XBOX_R3 = BUTTON_11;

public static final String BUTTON_XBOX_DPAD_UP = BUTTON_12;
public static final String BUTTON_XBOX_DPAD_DOWN = BUTTON_13;
public static final String BUTTON_XBOX_DPAD_LEFT = BUTTON_14;
public static final String BUTTON_XBOX_DPAD_RIGHT = BUTTON_15;


/**
* Assign the mapping name to receive events from the given button index
* on the joystick.
Expand Down
50 changes: 50 additions & 0 deletions jme3-core/src/main/java/com/jme3/system/AppSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ public final class AppSettings extends HashMap<String, Object> {
defaults.put("WindowYPosition", 0);
defaults.put("WindowXPosition", 0);
defaults.put("X11PlatformPreferred", false);
defaults.put("XboxLikeControllerLayout", true);
defaults.put("TriggerToButtonThreshold", 0.5f);
// defaults.put("Icons", null);
}

Expand Down Expand Up @@ -1612,4 +1614,52 @@ public void setX11PlatformPreferred(boolean preferred) {
public boolean isX11PlatformPreferred() {
return getBoolean("X11PlatformPreferred");
}

/**
* Enable/disable automatic normalization of gamepad mappings to an Xbox-like layout,
* when possible.
*
* <p>
* Depending on the platform and controller model, this setting might have no effect.
* It is primarily intended to provide a consistent default button/axis layout across common
* controllers.
*
* @param enabled true to enable, false to disable (default: true)
*/
public void setXboxLikeControllerLayout(boolean enabled){
putBoolean("XboxLikeControllerLayout", enabled);
}

/**
* Whether automatic normalization of controller mappings to an Xbox-like layout is enabled.
*
* @return true if enabled, otherwise false
*/
public boolean isXboxLikeControllerLayout(){
return getBoolean("XboxLikeControllerLayout");
}

/**
* Sets the threshold above which an analog trigger should also generate a button-press event.
* If the value is set to -1, the trigger will never generate button-press events.
*
* <p>
* This is intended to normalize behavior between controllers that expose triggers as analog
* axes and controllers that expose triggers as digital buttons.
*
* @param threshold the trigger threshold in the range [0, 1] (default: 0.5f)
*/
public void setTriggerToButtonThreshold(float threshold) {
putFloat("TriggerToButtonThreshold", threshold);
}

/**
* Gets the threshold above which an analog trigger should also generate a button-press event.
*
* @return the trigger threshold in the range [0, 1] (default: 0.5f)
* @see #setTriggerToButtonThreshold(float)
*/
public float getTriggerToButtonThreshold() {
return getFloat("TriggerToButtonThreshold");
}
}
49 changes: 26 additions & 23 deletions jme3-examples/src/main/java/jme3test/input/TestJoystick.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,35 +255,39 @@ public GamepadView() {
attachChild(rightStick);

// A "standard" mapping... fits a majority of my game pads
addButton( JoystickButton.BUTTON_0, 371, 512 - 176, 42, 42 );
addButton( JoystickButton.BUTTON_1, 407, 512 - 212, 42, 42 );
addButton( JoystickButton.BUTTON_2, 371, 512 - 248, 42, 42 );
addButton( JoystickButton.BUTTON_3, 334, 512 - 212, 42, 42 );
addButton( JoystickButton.BUTTON_XBOX_Y, 371, 512 - 176, 42, 42 );
addButton( JoystickButton.BUTTON_XBOX_B, 407, 512 - 212, 42, 42 );
addButton( JoystickButton.BUTTON_XBOX_A, 371, 512 - 248, 42, 42 );
addButton( JoystickButton.BUTTON_XBOX_X, 334, 512 - 212, 42, 42 );

// Front buttons Some of these have the top ones and the bottoms ones flipped.
addButton( JoystickButton.BUTTON_4, 67, 512 - 111, 95, 21 );
addButton( JoystickButton.BUTTON_5, 348, 512 - 111, 95, 21 );
addButton( JoystickButton.BUTTON_6, 67, 512 - 89, 95, 21 );
addButton( JoystickButton.BUTTON_7, 348, 512 - 89, 95, 21 );
addButton( JoystickButton.BUTTON_XBOX_LB, 67, 512 - 111, 95, 21 );
addButton( JoystickButton.BUTTON_XBOX_RB, 348, 512 - 111, 95, 21 );
addButton( JoystickButton.BUTTON_XBOX_LT, 67, 512 - 89, 95, 21 );
addButton( JoystickButton.BUTTON_XBOX_RT, 348, 512 - 89, 95, 21 );

// Select and start buttons
addButton( JoystickButton.BUTTON_8, 206, 512 - 198, 48, 30 );
addButton( JoystickButton.BUTTON_9, 262, 512 - 198, 48, 30 );
addButton( JoystickButton.BUTTON_XBOX_BACK, 206, 512 - 198, 48, 30 );
addButton( JoystickButton.BUTTON_XBOX_START, 262, 512 - 198, 48, 30 );

// Joystick push buttons
addButton( JoystickButton.BUTTON_10, 147, 512 - 300, 75, 70 );
addButton( JoystickButton.BUTTON_11, 285, 512 - 300, 75, 70 );
addButton( JoystickButton.BUTTON_XBOX_L3, 147, 512 - 300, 75, 70 );
addButton( JoystickButton.BUTTON_XBOX_R3, 285, 512 - 300, 75, 70 );

// Fake button highlights for the POV axes
//
// +Y
// -X +X
// -Y
//
addButton( "POV +Y", 96, 512 - 174, 40, 38 );
addButton( "POV +X", 128, 512 - 208, 40, 38 );
addButton( "POV -Y", 96, 512 - 239, 40, 38 );
addButton( "POV -X", 65, 512 - 208, 40, 38 );
// addButton( "POV +Y", 96, 512 - 174, 40, 38 );
// addButton( "POV +X", 128, 512 - 208, 40, 38 );
// addButton( "POV -Y", 96, 512 - 239, 40, 38 );
// addButton( "POV -X", 65, 512 - 208, 40, 38 );
addButton( JoystickButton.BUTTON_XBOX_DPAD_UP, 96, 512 - 174, 40, 38 );
addButton( JoystickButton.BUTTON_XBOX_DPAD_RIGHT, 128, 512 - 208, 40, 38 );
addButton( JoystickButton.BUTTON_XBOX_DPAD_DOWN, 96, 512 - 239, 40, 38 );
addButton( JoystickButton.BUTTON_XBOX_DPAD_LEFT, 65, 512 - 208, 40, 38 );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

binding dpad buttons is more intuitive, however the POV axis is still supported


resetPositions();
}
Expand All @@ -296,29 +300,28 @@ private void addButton( String name, float x, float y, float width, float height

public void setAxisValue( JoystickAxis axis, float value ) {

System.out.println( "Axis:" + axis.getName() + "(id:" + axis.getLogicalId() + ")=" + value );
if( axis == axis.getJoystick().getXAxis() ) {
if( axis == axis.getJoystick().getAxis(JoystickAxis.AXIS_XBOX_LEFT_THUMB_STICK_X)){
setXAxis(value);
} else if( axis == axis.getJoystick().getYAxis() ) {
} else if( axis == axis.getJoystick().getAxis(JoystickAxis.AXIS_XBOX_LEFT_THUMB_STICK_Y)){
setYAxis(-value);
} else if( axis == axis.getJoystick().getAxis(JoystickAxis.Z_AXIS) ) {
} else if( axis == axis.getJoystick().getAxis(JoystickAxis.AXIS_XBOX_RIGHT_THUMB_STICK_X)) {
// Note: in the above condition, we could check the axis name, but
// I have at least one joystick that reports 2 "Z Axis" axes.
// In this particular case, the first one is the right one so
// a name based lookup will find the proper one. It's a problem
// because the erroneous axis sends a constant stream of values.
setZAxis(value);
} else if( axis == axis.getJoystick().getAxis(JoystickAxis.Z_ROTATION) ) {
} else if( axis == axis.getJoystick().getAxis(JoystickAxis.AXIS_XBOX_RIGHT_THUMB_STICK_Y) ) {
setZRotation(-value);
} else if( axis == axis.getJoystick().getAxis(JoystickAxis.LEFT_TRIGGER) ) {
} else if( axis == axis.getJoystick().getAxis(JoystickAxis.AXIS_XBOX_LEFT_TRIGGER) ) {
if( axis.getJoystick().getButton(JoystickButton.BUTTON_6) == null ) {
// left/right triggers sometimes only show up as axes
boolean pressed = value != 0;
if( pressed != buttons.get(JoystickButton.BUTTON_6).isDown() ) {
setButtonValue(JoystickButton.BUTTON_6, pressed);
}
}
} else if( axis == axis.getJoystick().getAxis(JoystickAxis.RIGHT_TRIGGER) ) {
} else if( axis == axis.getJoystick().getAxis(JoystickAxis.AXIS_XBOX_RIGHT_TRIGGER) ) {
if( axis.getJoystick().getButton(JoystickButton.BUTTON_7) == null ) {
// left/right triggers sometimes only show up as axes
boolean pressed = value != 0;
Expand Down
Loading