소스 검색

Updated code example to 1.8.

mitm 7 년 전
부모
커밋
878f2bfe6e
1개의 변경된 파일96개의 추가작업 그리고 89개의 파일을 삭제
  1. 96 89
      src/docs/asciidoc/jme3/beginner/hello_input_system.adoc

+ 96 - 89
src/docs/asciidoc/jme3/beginner/hello_input_system.adoc

@@ -1,17 +1,18 @@
 = jMonkeyEngine 3 Tutorial (5) - Hello Input System
-:author: 
-:revnumber: 
+:author:
+:revnumber:
 :revdate: 2016/03/17 20:48
 :keywords: input, intro, beginner, documentation, keyinput, click
 :relfileprefix: ../../
 :imagesdir: ../..
+:experimental:
 ifdef::env-github,env-browser[:outfilesuffix: .adoc]
 
 
 Previous: <<jme3/beginner/hello_main_event_loop#,Hello Update Loop>>,
 Next: <<jme3/beginner/hello_material#,Hello Material>>
 
-By default, SimpleApplication sets up a camera control that allows you to steer the camera with the WASD keys, the arrow keys, and the mouse. You can use it as a flying first-person camera right away. But what if you need a third-person camera, or you want keys to trigger special game actions? 
+By default, SimpleApplication sets up a camera control that allows you to steer the camera with the kbd:[W] kbd:[A] kbd:[S] kbd:[D] keys, the arrow keys, and the mouse. You can use it as a flying first-person camera right away. But what if you need a third-person camera, or you want keys to trigger special game actions?
 
 Every game has its custom keybindings, and this tutorial explains how you define them. We first define the key presses and mouse events, and then we define the actions they should trigger.
 
@@ -36,75 +37,81 @@ 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 mousebuttons to actions
+ */
 public class HelloInput extends SimpleApplication {
 
-  public static void main(String[] args) {
-    HelloInput app = new HelloInput();
-    app.start();
-  }
-  protected Geometry player;
-  Boolean isRunning=true;
-
-  @Override
-  public void simpleInitApp() {
-    Box b = new Box(1, 1, 1);
-    player = new Geometry("Player", b);
-    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
-    mat.setColor("Color", ColorRGBA.Blue);
-    player.setMaterial(mat);
-    rootNode.attachChild(player);
-    initKeys(); // load my custom keybinding
-  }
+    public static void main(String[] args) {
+        HelloInput app = new HelloInput();
+        app.start();
+    }
+    protected Geometry player;
+    Boolean isRunning = true;
+
+    @Override
+    public void simpleInitApp() {
+        Box b = new Box(1, 1, 1);
+        player = new Geometry("Player", b);
+        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
+        mat.setColor("Color", ColorRGBA.Blue);
+        player.setMaterial(mat);
+        rootNode.attachChild(player);
+        initKeys(); // load my custom keybinding
+    }
 
-  /** Custom Keybinding: Map named actions to inputs. */
-  private void initKeys() {
-    // You can map one or several inputs to one named action
-    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.
-    inputManager.addListener(actionListener,"Pause");
-    inputManager.addListener(analogListener,"Left", "Right", "Rotate");
-    
-  }
+    /**
+     * Custom Keybinding: Map named actions to inputs.
+     */
+    private void initKeys() {
+        // You can map one or several inputs to one named action
+        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.
+        inputManager.addListener(actionListener, "Pause");
+        inputManager.addListener(analogListener, "Left", "Right", "Rotate");
 
-  private ActionListener actionListener = new ActionListener() {
-    public void onAction(String name, boolean keyPressed, float tpf) {
-      if (name.equals("Pause") && !keyPressed) {
-        isRunning = !isRunning;
-      }
     }
-  };
 
-  private AnalogListener analogListener = new AnalogListener() {
-    public void onAnalog(String name, float value, float tpf) {
-      if (isRunning) {
-        if (name.equals("Rotate")) {
-          player.rotate(0, value*speed, 0);
+    private final ActionListener actionListener = new ActionListener() {
+        @Override
+        public void onAction(String name, boolean keyPressed, float tpf) {
+            if (name.equals("Pause") && !keyPressed) {
+                isRunning = !isRunning;
+            }
         }
-        if (name.equals("Right")) {
-          Vector3f v = player.getLocalTranslation();
-          player.setLocalTranslation(v.x + value*speed, v.y, v.z);
+    };
+
+    private final 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);
+                }
+                if (name.equals("Right")) {
+                    Vector3f v = player.getLocalTranslation();
+                    player.setLocalTranslation(v.x + value * speed, v.y, v.z);
+                }
+                if (name.equals("Left")) {
+                    Vector3f v = player.getLocalTranslation();
+                    player.setLocalTranslation(v.x - value * speed, v.y, v.z);
+                }
+            } else {
+                System.out.println("Press P to unpause.");
+            }
         }
-        if (name.equals("Left")) {
-          Vector3f v = player.getLocalTranslation();
-          player.setLocalTranslation(v.x - value*speed, v.y, v.z);
-        }
-      } else {
-        System.out.println("Press P to unpause.");
-      }
-    }
-  };
+    };
 }
 
 ----
 
 Build and run the example.
 
-*  Press the Spacebar or click to rotate the cube. 
+*  Press the Spacebar or click to rotate the cube.
 *  Press the J and K keys to move the cube.
 *  Press P to pause and unpause the game. While paused, the game should not respond to any input, other than `P`.
 
@@ -123,10 +130,10 @@ For example, the “Rotate action can be triggered by a click and by pressing th
 Have a look at the code:
 
 .  You register the mapping named “Rotate to the Spacebar key trigger. +
-`new KeyTrigger(KeyInput.KEY_SPACE)`). 
+`new KeyTrigger(KeyInput.KEY_SPACE)`).
 .  In the same line, you also register “Rotate to an alternative mouse click trigger. +
 `new MouseButtonTrigger(MouseInput.BUTTON_LEFT)`
-.  You map the `Pause`, `Left`, `Right` mappings to the P, J, K keys, respectively. 
+.  You map the `Pause`, `Left`, `Right` mappings to the P, J, K keys, respectively.
 +
 [source,java]
 ----
@@ -161,13 +168,13 @@ This code goes into the `simpleInitApp()` method. But since we will likely add m
 
 You have mapped action names to input triggers. Now you specify the actions themselves.
 
-The two important methods here are the `ActionListener` with its `onAction()` method, and the `AnalogListener` with its `onAnalog()` method. In these two methods, you test for each named mapping, and call the game action you want to trigger. 
+The two important methods here are the `ActionListener` with its `onAction()` method, and the `AnalogListener` with its `onAnalog()` method. In these two methods, you test for each named mapping, and call the game action you want to trigger.
 
-In this example, we trigger the following actions: 
+In this example, we trigger the following actions:
 
-.  The _Rotate_ mapping triggers the action `player.rotate(0, value, 0)`. 
-.  The _Left_ and _Right_ mappings increase and decrease the player's x coordinate. 
-.  The _Pause_ mapping flips a boolean `isRunning`. 
+.  The _Rotate_ mapping triggers the action `player.rotate(0, value, 0)`.
+.  The _Left_ and _Right_ mappings increase and decrease the player's x coordinate.
+.  The _Pause_ mapping flips a boolean `isRunning`.
 .  We also want to check the boolean `isRunning` before any action (other than unpausing) is executed.
 
 [source,java]
@@ -215,7 +222,7 @@ You can also combine both listeners into one, the engine will send the appropria
         isRunning = !isRunning;
       }
     }
-    
+
     public void onAnalog(String name, float value, float tpf) {
       if (isRunning) {
         if (name.equals("Rotate")) {
@@ -236,7 +243,7 @@ You can also combine both listeners into one, the engine will send the appropria
   }
 // ...
 inputManager.addListener(combinedListener, new String[]{"Pause", "Left", "Right", "Rotate"});
-  
+
 ----
 
 It's okay to use only one of the two Listeners, and not implement the other one, if you are not using this type of interaction. In the following, we have a closer look how to decide which of the two listeners is best suited for which situation.
@@ -248,18 +255,18 @@ Technically, every input can be either an “analog or a “digital action. Here
 
 Mappings registered to the *AnalogListener* are triggered repeatedly and gradually.
 
-*  Parameters: 
+*  Parameters:
 ..  JME gives you access to the name of the triggered action.
 ..  JME gives you access to a gradual value showing the strength of that input. In the case of a keypress that will be the tpf value for which it was pressed since the last frame. For other inputs such as a joystick which give analogue control though then the value will also indicate the strength of the input premultiplied by tpf. For an example on this go to <<jme3/beginner/hello_input_system/timekeypressed#,jMonkeyEngine 3 Tutorial (5) - Hello Input System - Variation over time key is pressed>>
 
 
 In order to see the total time that a key has been pressed for then the incoming value can be accumulated. The analogue listener may also need to be combined with an action listener so that you are notified when the key is released.
 
-*  Example: Navigational events (e.g. Left, Right, Rotate, Run, Strafe), situations where you interact continuously. 
+*  Example: Navigational events (e.g. Left, Right, Rotate, Run, Strafe), situations where you interact continuously.
 
 Mappings registered to the *ActionListener* are digital either-or actions – “Pressed or released? On or off?
 
-*  Parameters: 
+*  Parameters:
 ..  JME gives you access to the name of the triggered action.
 ..  JME gives you access to a boolean whether the key is pressed or not.
 
@@ -284,33 +291,33 @@ You can find the list of input constants in the files `src/core/com/jme3/input/K
 [cols="2", options="header"]
 |===
 
-a| Trigger 
-a| Code 
+a| Trigger
+a| Code
 
-a| Mouse button: Left Click 
-a| MouseButtonTrigger(MouseInput.BUTTON_LEFT) 
+a| Mouse button: Left Click
+a| MouseButtonTrigger(MouseInput.BUTTON_LEFT)
 
-a| Mouse button: Right Click 
-a| MouseButtonTrigger(MouseInput.BUTTON_RIGHT) 
+a| Mouse button: Right Click
+a| MouseButtonTrigger(MouseInput.BUTTON_RIGHT)
 
-a| Keyboard: Characters and Numbers 
-a| KeyTrigger(KeyInput.KEY_X) 
+a| Keyboard: Characters and Numbers
+a| KeyTrigger(KeyInput.KEY_X)
 
-<a| Keyboard: Spacebar  
-a| KeyTrigger(KeyInput.KEY_SPACE) 
+<a| Keyboard: Spacebar
+a| KeyTrigger(KeyInput.KEY_SPACE)
 
-a| Keyboard: Return, Enter 
+a| Keyboard: Return, Enter
 <a| KeyTrigger(KeyInput.KEY_RETURN), +
-KeyTrigger(KeyInput.KEY_NUMPADENTER)  
+KeyTrigger(KeyInput.KEY_NUMPADENTER)
 
-a| Keyboard: Escape 
-a| KeyTrigger(KeyInput.KEY_ESCAPE) 
+a| Keyboard: Escape
+a| KeyTrigger(KeyInput.KEY_ESCAPE)
 
-a| Keyboard: Arrows 
+a| Keyboard: Arrows
 a| KeyTrigger(KeyInput.KEY_UP), +
 KeyTrigger(KeyInput.KEY_DOWN) +
 KeyTrigger(KeyInput.KEY_LEFT), +
-KeyTrigger(KeyInput.KEY_RIGHT) 
+KeyTrigger(KeyInput.KEY_RIGHT)
 
 |===
 
@@ -323,15 +330,15 @@ If you don't recall an input constant during development, you benefit from an ID
 
 .  Add mappings for moving the player (box) up and down with the H and L keys!
 .  Switch off the flyCam and override the WASD keys.
-**  Tip: Use <<jme3/faq#how-do-i-switch-between-third-person-and-first-person-view,flyCam.setEnabled(false);>> 
+**  Tip: Use <<jme3/faq#how-do-i-switch-between-third-person-and-first-person-view,flyCam.setEnabled(false);>>
 
 .  Modify the mappings so that you can also trigger the up an down motion with the mouse scroll wheel!
 **  Tip: Use `new MouseAxisTrigger(MouseInput.AXIS_WHEEL, true)`
 
-.  In which situation would it be better to use variables instead of literals for the MouseInput/KeyInput definitions? 
+.  In which situation would it be better to use variables instead of literals for the MouseInput/KeyInput definitions?
 [source,java]
 ----
-int usersPauseKey = KeyInput.KEY_P; 
+int usersPauseKey = KeyInput.KEY_P;
 ...
 inputManager.addMapping("Pause",  new KeyTrigger(usersPauseKey));