|
@@ -29,9 +29,8 @@ import com.jme3.input.controls.AnalogListener;
|
|
|
import com.jme3.input.controls.KeyTrigger;
|
|
|
import com.jme3.input.controls.MouseButtonTrigger;
|
|
|
|
|
|
-/**
|
|
|
- * Sample 5 - how to map keys and mousebuttons to actions
|
|
|
- */
|
|
|
+/** Sample 5 - how to map keys and mouse buttons to actions */
|
|
|
+
|
|
|
public class HelloInput extends SimpleApplication {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
@@ -39,8 +38,8 @@ public class HelloInput extends SimpleApplication {
|
|
|
app.start();
|
|
|
}
|
|
|
|
|
|
- protected Geometry player;
|
|
|
- private boolean isRunning = true;
|
|
|
+ private Geometry player;
|
|
|
+ private Boolean isRunning = true;
|
|
|
|
|
|
@Override
|
|
|
public void simpleInitApp() {
|
|
@@ -53,23 +52,22 @@ public class HelloInput extends SimpleApplication {
|
|
|
initKeys(); // load my custom keybinding
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Custom Keybinding: Map named actions to inputs.
|
|
|
- */
|
|
|
+ /** Custom Keybinding: Map named actions to inputs. */
|
|
|
private void initKeys() {
|
|
|
- // You can map one or several inputs to one named action
|
|
|
+ /* You can map one or several inputs to one named mapping. */
|
|
|
inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
|
|
|
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_J));
|
|
|
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));
|
|
|
inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE),
|
|
|
new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
|
|
|
- // Add the names to the action listener.
|
|
|
+ /* Add the named mappings to the action listeners. */
|
|
|
inputManager.addListener(actionListener, "Pause");
|
|
|
inputManager.addListener(analogListener, "Left", "Right", "Rotate");
|
|
|
|
|
|
}
|
|
|
|
|
|
- private final ActionListener actionListener = new ActionListener() {
|
|
|
+ /** Use this listener for KeyDown/KeyUp events */
|
|
|
+ final private ActionListener actionListener = new ActionListener() {
|
|
|
@Override
|
|
|
public void onAction(String name, boolean keyPressed, float tpf) {
|
|
|
if (name.equals("Pause") && !keyPressed) {
|
|
@@ -78,20 +76,19 @@ public class HelloInput extends SimpleApplication {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- private final AnalogListener analogListener = new AnalogListener() {
|
|
|
+ /** Use this listener for continuous events */
|
|
|
+ final private AnalogListener analogListener = new AnalogListener() {
|
|
|
@Override
|
|
|
public void onAnalog(String name, float value, float tpf) {
|
|
|
if (isRunning) {
|
|
|
if (name.equals("Rotate")) {
|
|
|
- player.rotate(0, value * speed, 0);
|
|
|
+ player.rotate(0, value, 0);
|
|
|
}
|
|
|
if (name.equals("Right")) {
|
|
|
- Vector3f v = player.getLocalTranslation();
|
|
|
- player.setLocalTranslation(v.x + value * speed, v.y, v.z);
|
|
|
+ player.move((new Vector3f(value, 0,0)) );
|
|
|
}
|
|
|
if (name.equals("Left")) {
|
|
|
- Vector3f v = player.getLocalTranslation();
|
|
|
- player.setLocalTranslation(v.x - value * speed, v.y, v.z);
|
|
|
+ player.move(new Vector3f(-value, 0,0));
|
|
|
}
|
|
|
} else {
|
|
|
System.out.println("Press P to unpause.");
|
|
@@ -131,7 +128,7 @@ Have a look at the code:
|
|
|
[source,java]
|
|
|
----
|
|
|
|
|
|
- // You can map one or several inputs to one named action
|
|
|
+ /* You can map one or several inputs to one named mapping. */
|
|
|
inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
|
|
|
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_J));
|
|
|
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));
|
|
@@ -148,7 +145,7 @@ Now you need to register your trigger mappings.
|
|
|
[source,java]
|
|
|
----
|
|
|
|
|
|
- // Add the names to the action listener.
|
|
|
+ /* Add the named mappings to the action listeners. */
|
|
|
inputManager.addListener(actionListener,"Pause");
|
|
|
inputManager.addListener(analogListener,"Left", "Right", "Rotate");
|
|
|
|
|
@@ -173,8 +170,8 @@ In this example, we trigger the following actions:
|
|
|
[source,java]
|
|
|
----
|
|
|
|
|
|
-
|
|
|
- private final ActionListener actionListener = new ActionListener() {
|
|
|
+ /** Use this listener for KeyDown/KeyUp events */
|
|
|
+ final private ActionListener actionListener = new ActionListener() {
|
|
|
@Override
|
|
|
public void onAction(String name, boolean keyPressed, float tpf) {
|
|
|
if (name.equals("Pause") && !keyPressed) {
|
|
@@ -183,20 +180,19 @@ In this example, we trigger the following actions:
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- private final AnalogListener analogListener = new AnalogListener() {
|
|
|
+ /** Use this listener for continuous events */
|
|
|
+ final private AnalogListener analogListener = new AnalogListener() {
|
|
|
@Override
|
|
|
public void onAnalog(String name, float value, float tpf) {
|
|
|
if (isRunning) {
|
|
|
if (name.equals("Rotate")) {
|
|
|
- player.rotate(0, value * speed, 0);
|
|
|
+ player.rotate(0, value, 0);
|
|
|
}
|
|
|
if (name.equals("Right")) {
|
|
|
- Vector3f v = player.getLocalTranslation();
|
|
|
- player.setLocalTranslation(v.x + value * speed, v.y, v.z);
|
|
|
+ player.move((new Vector3f(value, 0,0)) );
|
|
|
}
|
|
|
if (name.equals("Left")) {
|
|
|
- Vector3f v = player.getLocalTranslation();
|
|
|
- player.setLocalTranslation(v.x - value * speed, v.y, v.z);
|
|
|
+ player.move(new Vector3f(-value, 0,0));
|
|
|
}
|
|
|
} else {
|
|
|
System.out.println("Press P to unpause.");
|
|
@@ -225,15 +221,13 @@ private class MyCombinedListener implements AnalogListener, ActionListener {
|
|
|
public void onAnalog(String name, float value, float tpf) {
|
|
|
if (isRunning) {
|
|
|
if (name.equals("Rotate")) {
|
|
|
- player.rotate(0, value * speed, 0);
|
|
|
+ player.rotate(0, value, 0);
|
|
|
}
|
|
|
if (name.equals("Right")) {
|
|
|
- Vector3f v = player.getLocalTranslation();
|
|
|
- player.setLocalTranslation(v.x + value * speed, v.y, v.z);
|
|
|
+ player.move((new Vector3f(value, 0,0)) );
|
|
|
}
|
|
|
if (name.equals("Left")) {
|
|
|
- Vector3f v = player.getLocalTranslation();
|
|
|
- player.setLocalTranslation(v.x - value * speed, v.y, v.z);
|
|
|
+ player.move(new Vector3f(-value, 0,0));
|
|
|
}
|
|
|
} else {
|
|
|
System.out.println("Press P to unpause.");
|