information about buttons and axes and allow slightly easier hook-up of stanard triggers. The events have also been expanded to include the actual JoystickAxis and JoystickButton objects. Changes to the JInput joystick support coming next commit. Android joystick stuff was modified to compile but is totally broken with this change until the code can be properly updated. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9760 75d07b2b-3a1a-0410-a2c5-0572b91ccdca3.0
parent
8daa039ecc
commit
81837959b1
@ -0,0 +1,219 @@ |
||||
package com.jme3.input; |
||||
|
||||
import java.util.*; |
||||
|
||||
import com.jme3.input.controls.JoyAxisTrigger; |
||||
import com.jme3.input.controls.JoyButtonTrigger; |
||||
|
||||
/** |
||||
* A joystick represents a single joystick that is installed in the system. |
||||
* |
||||
* @author Kirill Vainer, Paul Speed |
||||
*/ |
||||
public abstract class AbstractJoystick implements Joystick { |
||||
|
||||
private InputManager inputManager; |
||||
private JoyInput joyInput; |
||||
private int joyId; |
||||
private String name; |
||||
|
||||
private List<JoystickAxis> axes = new ArrayList<JoystickAxis>(); |
||||
private List<JoystickButton> buttons = new ArrayList<JoystickButton>(); |
||||
|
||||
/** |
||||
* Creates a new joystick instance. Only used internally. |
||||
*/ |
||||
protected AbstractJoystick(InputManager inputManager, JoyInput joyInput, |
||||
int joyId, String name) { |
||||
this.inputManager = inputManager; |
||||
this.joyInput = joyInput; |
||||
this.joyId = joyId; |
||||
this.name = name; |
||||
} |
||||
|
||||
protected InputManager getInputManager() { |
||||
return inputManager; |
||||
} |
||||
|
||||
protected JoyInput getJoyInput() { |
||||
return joyInput; |
||||
} |
||||
|
||||
protected void addAxis( JoystickAxis axis ) { |
||||
axes.add(axis); |
||||
} |
||||
|
||||
protected void addButton( JoystickButton button ) { |
||||
buttons.add(button); |
||||
} |
||||
|
||||
/** |
||||
* Rumbles the joystick for the given amount/magnitude. |
||||
* |
||||
* @param amount The amount to rumble. Should be between 0 and 1. |
||||
*/ |
||||
@Override |
||||
public void rumble(float amount){ |
||||
joyInput.setJoyRumble(joyId, amount); |
||||
} |
||||
|
||||
/** |
||||
* Assign the mapping name to receive events from the given button index |
||||
* on the joystick. |
||||
* |
||||
* @param mappingName The mapping to receive joystick button events. |
||||
* @param buttonId The button index. |
||||
* |
||||
* @see Joystick#getButtonCount() |
||||
* @deprecated Use JoystickButton.assignButton() instead. |
||||
*/ |
||||
@Override |
||||
public void assignButton(String mappingName, int buttonId){ |
||||
if (buttonId < 0 || buttonId >= getButtonCount()) |
||||
throw new IllegalArgumentException(); |
||||
|
||||
inputManager.addMapping(mappingName, new JoyButtonTrigger(joyId, buttonId)); |
||||
} |
||||
|
||||
/** |
||||
* Assign the mappings to receive events from the given joystick axis. |
||||
* |
||||
* @param positiveMapping The mapping to receive events when the axis is negative |
||||
* @param negativeMapping The mapping to receive events when the axis is positive |
||||
* @param axisId The axis index. |
||||
* |
||||
* @see Joystick#getAxisCount() |
||||
* @deprecated Use JoystickAxis.assignAxis() instead. |
||||
*/ |
||||
@Override |
||||
public void assignAxis(String positiveMapping, String negativeMapping, int axisId){ |
||||
|
||||
// For backwards compatibility
|
||||
if( axisId == JoyInput.AXIS_POV_X ) { |
||||
axisId = getPovXAxis().getAxisId(); |
||||
} else if( axisId == JoyInput.AXIS_POV_Y ) { |
||||
axisId = getPovYAxis().getAxisId(); |
||||
} |
||||
|
||||
inputManager.addMapping(positiveMapping, new JoyAxisTrigger(joyId, axisId, false)); |
||||
inputManager.addMapping(negativeMapping, new JoyAxisTrigger(joyId, axisId, true)); |
||||
} |
||||
|
||||
/** |
||||
* Returns the JoystickAxis with the specified name. |
||||
* |
||||
* @param name The name of the axis to search for as returned by JoystickAxis.getName(). |
||||
*/ |
||||
@Override |
||||
public JoystickAxis getAxis(String name) { |
||||
for( JoystickAxis axis : axes ) { |
||||
if( axis.getName().equals(name) ) |
||||
return axis; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* Returns a read-only list of all joystick axes for this Joystick. |
||||
*/ |
||||
@Override |
||||
public List<JoystickAxis> getAxes() { |
||||
return Collections.unmodifiableList(axes); |
||||
} |
||||
|
||||
/** |
||||
* Returns the number of axes on this joystick. |
||||
* |
||||
* @return the number of axes on this joystick. |
||||
*/ |
||||
@Override |
||||
public int getAxisCount() { |
||||
return axes.size(); |
||||
} |
||||
|
||||
/** |
||||
* Returns the JoystickButton with the specified name. |
||||
* |
||||
* @param name The name of the button to search for as returned by JoystickButton.getName(). |
||||
*/ |
||||
@Override |
||||
public JoystickButton getButton(String name) { |
||||
for( JoystickButton b : buttons ) { |
||||
if( b.getName().equals(name) ) |
||||
return b; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* Returns a read-only list of all joystick buttons for this Joystick. |
||||
*/ |
||||
@Override |
||||
public List<JoystickButton> getButtons() { |
||||
return Collections.unmodifiableList(buttons); |
||||
} |
||||
|
||||
/** |
||||
* Returns the number of buttons on this joystick. |
||||
* |
||||
* @return the number of buttons on this joystick. |
||||
*/ |
||||
@Override |
||||
public int getButtonCount() { |
||||
return buttons.size(); |
||||
} |
||||
|
||||
/** |
||||
* Returns the name of this joystick. |
||||
* |
||||
* @return the name of this joystick. |
||||
*/ |
||||
@Override |
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
/** |
||||
* Returns the joyId of this joystick. |
||||
* |
||||
* @return the joyId of this joystick. |
||||
*/ |
||||
@Override |
||||
public int getJoyId() { |
||||
return joyId; |
||||
} |
||||
|
||||
/** |
||||
* Gets the index number for the X axis on the joystick. |
||||
* |
||||
* <p>E.g. for most gamepads, the left control stick X axis will be returned. |
||||
* |
||||
* @return The axis index for the X axis for this joystick. |
||||
* |
||||
* @see Joystick#assignAxis(java.lang.String, java.lang.String, int) |
||||
*/ |
||||
@Override |
||||
public int getXAxisIndex(){ |
||||
return getXAxis().getAxisId(); |
||||
} |
||||
|
||||
/** |
||||
* Gets the index number for the Y axis on the joystick. |
||||
* |
||||
* <p>E.g. for most gamepads, the left control stick Y axis will be returned. |
||||
* |
||||
* @return The axis index for the Y axis for this joystick. |
||||
* |
||||
* @see Joystick#assignAxis(java.lang.String, java.lang.String, int) |
||||
*/ |
||||
@Override |
||||
public int getYAxisIndex(){ |
||||
return getYAxis().getAxisId(); |
||||
} |
||||
|
||||
@Override |
||||
public String toString(){ |
||||
return "Joystick[name=" + name + ", id=" + joyId + ", buttons=" + getButtonCount() |
||||
+ ", axes=" + getAxisCount() + "]"; |
||||
} |
||||
} |
@ -0,0 +1,111 @@ |
||||
package com.jme3.input; |
||||
|
||||
import com.jme3.input.controls.JoyAxisTrigger; |
||||
|
||||
/** |
||||
* Default implementation of the JoystickAxis interface. |
||||
* |
||||
* @author Paul Speed |
||||
*/ |
||||
public class DefaultJoystickAxis implements JoystickAxis { |
||||
|
||||
private InputManager inputManager; |
||||
private Joystick parent; |
||||
private int axisIndex; |
||||
private String name; |
||||
private String logicalId; |
||||
private boolean isAnalog; |
||||
private boolean isRelative; |
||||
private float deadZone; |
||||
|
||||
/** |
||||
* Creates a new joystick axis instance. Only used internally. |
||||
*/ |
||||
public DefaultJoystickAxis(InputManager inputManager, Joystick parent, |
||||
int axisIndex, String name, String logicalId, |
||||
boolean isAnalog, boolean isRelative, float deadZone ) { |
||||
this.inputManager = inputManager; |
||||
this.parent = parent; |
||||
this.axisIndex = axisIndex; |
||||
this.name = name; |
||||
this.logicalId = logicalId; |
||||
this.isAnalog = isAnalog; |
||||
this.isRelative = isRelative; |
||||
this.deadZone = deadZone; |
||||
} |
||||
|
||||
/** |
||||
* Assign the mappings to receive events from the given joystick axis. |
||||
* |
||||
* @param positiveMapping The mapping to receive events when the axis is negative |
||||
* @param negativeMapping The mapping to receive events when the axis is positive |
||||
*/ |
||||
public void assignAxis(String positiveMapping, String negativeMapping){ |
||||
inputManager.addMapping(positiveMapping, new JoyAxisTrigger(parent.getJoyId(), axisIndex, false)); |
||||
inputManager.addMapping(negativeMapping, new JoyAxisTrigger(parent.getJoyId(), axisIndex, true)); |
||||
} |
||||
|
||||
/** |
||||
* Returns the joystick to which this axis object belongs. |
||||
*/ |
||||
public Joystick getJoystick() { |
||||
return parent; |
||||
} |
||||
|
||||
/** |
||||
* Returns the name of this joystick. |
||||
* |
||||
* @return the name of this joystick. |
||||
*/ |
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
/** |
||||
* Returns the logical identifier of this joystick axis. |
||||
* |
||||
* @return the logical identifier of this joystick. |
||||
*/ |
||||
public String getLogicalId() { |
||||
return logicalId; |
||||
} |
||||
|
||||
/** |
||||
* Returns the axisId of this joystick axis. |
||||
* |
||||
* @return the axisId of this joystick axis. |
||||
*/ |
||||
public int getAxisId() { |
||||
return axisIndex; |
||||
} |
||||
|
||||
/** |
||||
* Returns true if this is an analog axis, meaning the values |
||||
* are a continuous range instead of 1, 0, and -1. |
||||
*/ |
||||
public boolean isAnalog() { |
||||
return isAnalog; |
||||
} |
||||
|
||||
/** |
||||
* Returns true if this axis presents relative values. |
||||
*/ |
||||
public boolean isRelative() { |
||||
return isRelative; |
||||
} |
||||
|
||||
/** |
||||
* Returns the suggested dead zone for this axis. Values less than this |
||||
* can be safely ignored. |
||||
*/ |
||||
public float getDeadZone() { |
||||
return deadZone; |
||||
} |
||||
|
||||
@Override |
||||
public String toString(){ |
||||
return "JoystickAxis[name=" + name + ", parent=" + parent.getName() + ", id=" + axisIndex |
||||
+ ", logicalId=" + logicalId + ", isAnalog=" + isAnalog |
||||
+ ", isRelative=" + isRelative + ", deadZone=" + deadZone + "]"; |
||||
} |
||||
} |
@ -0,0 +1,77 @@ |
||||
package com.jme3.input; |
||||
|
||||
import com.jme3.input.controls.JoyButtonTrigger; |
||||
|
||||
/** |
||||
* Default implementation of the JoystickButton interface. |
||||
* |
||||
* @author Paul Speed |
||||
*/ |
||||
public class DefaultJoystickButton implements JoystickButton { |
||||
|
||||
private InputManager inputManager; |
||||
private Joystick parent; |
||||
private int buttonIndex; |
||||
private String name; |
||||
private String logicalId; |
||||
|
||||
public DefaultJoystickButton( InputManager inputManager, Joystick parent, int buttonIndex, |
||||
String name, String logicalId ) { |
||||
this.inputManager = inputManager; |
||||
this.parent = parent; |
||||
this.buttonIndex = buttonIndex; |
||||
this.name = name; |
||||
this.logicalId = logicalId; |
||||
} |
||||
|
||||
/** |
||||
* Assign the mapping name to receive events from the given button index |
||||
* on the joystick. |
||||
* |
||||
* @param mappingName The mapping to receive joystick button events. |
||||
*/ |
||||
public void assignButton(String mappingName) { |
||||
inputManager.addMapping(mappingName, new JoyButtonTrigger(parent.getJoyId(), buttonIndex)); |
||||
} |
||||
|
||||
/** |
||||
* Returns the joystick to which this axis object belongs. |
||||
*/ |
||||
public Joystick getJoystick() { |
||||
return parent; |
||||
} |
||||
|
||||
/** |
||||
* Returns the name of this joystick. |
||||
* |
||||
* @return the name of this joystick. |
||||
*/ |
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
/** |
||||
* Returns the logical identifier of this joystick axis. |
||||
* |
||||
* @return the logical identifier of this joystick. |
||||
*/ |
||||
public String getLogicalId() { |
||||
return logicalId; |
||||
} |
||||
|
||||
/** |
||||
* Returns the unique buttonId of this joystick axis within a given |
||||
* InputManager context. |
||||
* |
||||
* @return the buttonId of this joystick axis. |
||||
*/ |
||||
public int getButtonId() { |
||||
return buttonIndex; |
||||
} |
||||
|
||||
@Override |
||||
public String toString(){ |
||||
return "JoystickButton[name=" + getName() + ", parent=" + parent.getName() + ", id=" + getButtonId() |
||||
+ ", logicalId=" + getLogicalId() + "]"; |
||||
} |
||||
} |
@ -0,0 +1,72 @@ |
||||
package com.jme3.input; |
||||
|
||||
import com.jme3.input.controls.JoyAxisTrigger; |
||||
import com.jme3.input.controls.JoyButtonTrigger; |
||||
|
||||
/** |
||||
* Represents a single axis of a Joystick. |
||||
* |
||||
* @author Paul Speed |
||||
*/ |
||||
public interface JoystickAxis { |
||||
|
||||
public static final String X_AXIS = "X Axis"; |
||||
public static final String Y_AXIS = "Y Axis"; |
||||
public static final String Z_AXIS = "Z Axis"; |
||||
public static final String Z_ROTATION = "Z Rotation"; |
||||
|
||||
public static final String POV_X = "JME:POV_X"; |
||||
public static final String POV_Y = "JME:POV_Y"; |
||||
|
||||
/** |
||||
* Assign the mappings to receive events from the given joystick axis. |
||||
* |
||||
* @param positiveMapping The mapping to receive events when the axis is negative |
||||
* @param negativeMapping The mapping to receive events when the axis is positive |
||||
*/ |
||||
public void assignAxis(String positiveMapping, String negativeMapping); |
||||
|
||||
/** |
||||
* Returns the joystick to which this axis object belongs. |
||||
*/ |
||||
public Joystick getJoystick(); |
||||
|
||||
/** |
||||
* Returns the name of this joystick. |
||||
* |
||||
* @return the name of this joystick. |
||||
*/ |
||||
public String getName(); |
||||
|
||||
/** |
||||
* Returns the logical identifier of this joystick axis. |
||||
* |
||||
* @return the logical identifier of this joystick. |
||||
*/ |
||||
public String getLogicalId(); |
||||
|
||||
/** |
||||
* Returns the unique axisId of this joystick axis within a given |
||||
* InputManager context. |
||||
* |
||||
* @return the axisId of this joystick axis. |
||||
*/ |
||||
public int getAxisId(); |
||||
|
||||
/** |
||||
* Returns true if this is an analog axis, meaning the values |
||||
* are a continuous range instead of 1, 0, and -1. |
||||
*/ |
||||
public boolean isAnalog(); |
||||
|
||||
/** |
||||
* Returns true if this axis presents relative values. |
||||
*/ |
||||
public boolean isRelative(); |
||||
|
||||
/** |
||||
* Returns the suggested dead zone for this axis. Values less than this |
||||
* can be safely ignored. |
||||
*/ |
||||
public float getDeadZone(); |
||||
} |
@ -0,0 +1,47 @@ |
||||
package com.jme3.input; |
||||
|
||||
import com.jme3.input.controls.JoyAxisTrigger; |
||||
import com.jme3.input.controls.JoyButtonTrigger; |
||||
|
||||
/** |
||||
* Represents a single button of a Joystick. |
||||
* |
||||
* @author Paul Speed |
||||
*/ |
||||
public interface JoystickButton { |
||||
|
||||
/** |
||||
* Assign the mapping name to receive events from the given button index |
||||
* on the joystick. |
||||
* |
||||
* @param mappingName The mapping to receive joystick button events. |
||||
*/ |
||||
public void assignButton(String mappingName); |
||||
|
||||
/** |
||||
* Returns the joystick to which this axis object belongs. |
||||
*/ |
||||
public Joystick getJoystick(); |
||||
|
||||
/** |
||||
* Returns the name of this joystick. |
||||
* |
||||
* @return the name of this joystick. |
||||
*/ |
||||
public String getName(); |
||||
|
||||
/** |
||||
* Returns the logical identifier of this joystick axis. |
||||
* |
||||
* @return the logical identifier of this joystick. |
||||
*/ |
||||
public String getLogicalId(); |
||||
|
||||
/** |
||||
* Returns the unique buttonId of this joystick axis within a given |
||||
* InputManager context. |
||||
* |
||||
* @return the buttonId of this joystick axis. |
||||
*/ |
||||
public int getButtonId(); |
||||
} |
Loading…
Reference in new issue