1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package jme3test.helloworld; 34 35 import com.jme3.app.SimpleApplication; 36 import com.jme3.input.KeyInput; 37 import com.jme3.input.MouseInput; 38 import com.jme3.input.controls.ActionListener; 39 import com.jme3.input.controls.AnalogListener; 40 import com.jme3.input.controls.KeyTrigger; 41 import com.jme3.input.controls.MouseButtonTrigger; 42 import com.jme3.material.Material; 43 import com.jme3.math.ColorRGBA; 44 import com.jme3.math.Vector3f; 45 import com.jme3.scene.Geometry; 46 import com.jme3.scene.shape.Box; 47 48 /** Sample 5 - how to map keys and mousebuttons to actions */ 49 public class HelloInput extends SimpleApplication { 50 main(String[] args)51 public static void main(String[] args) { 52 HelloInput app = new HelloInput(); 53 app.start(); 54 } 55 protected Geometry player; 56 Boolean isRunning=true; 57 58 @Override simpleInitApp()59 public void simpleInitApp() { 60 Box b = new Box(Vector3f.ZERO, 1, 1, 1); 61 player = new Geometry("Player", b); 62 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 63 mat.setColor("Color", ColorRGBA.Blue); 64 player.setMaterial(mat); 65 rootNode.attachChild(player); 66 initKeys(); // load my custom keybinding 67 } 68 69 /** Custom Keybinding: Map named actions to inputs. */ initKeys()70 private void initKeys() { 71 /** You can map one or several inputs to one named mapping. */ 72 inputManager.addMapping("Pause", new KeyTrigger(keyInput.KEY_P)); 73 inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_J)); 74 inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K)); 75 inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE), // spacebar! 76 new MouseButtonTrigger(MouseInput.BUTTON_LEFT) ); // left click! 77 /** Add the named mappings to the action listeners. */ 78 inputManager.addListener(actionListener, new String[]{"Pause"}); 79 inputManager.addListener(analogListener, new String[]{"Left", "Right", "Rotate"}); 80 } 81 82 /** Use this listener for KeyDown/KeyUp events */ 83 private ActionListener actionListener = new ActionListener() { 84 public void onAction(String name, boolean keyPressed, float tpf) { 85 if (name.equals("Pause") && !keyPressed) { 86 isRunning = !isRunning; 87 } 88 } 89 }; 90 91 /** Use this listener for continuous events */ 92 private AnalogListener analogListener = new AnalogListener() { 93 public void onAnalog(String name, float value, float tpf) { 94 if (isRunning) { 95 if (name.equals("Rotate")) { 96 player.rotate(0, value, 0); 97 } 98 if (name.equals("Right")) { 99 player.move((new Vector3f(value, 0,0)) ); 100 } 101 if (name.equals("Left")) { 102 player.move(new Vector3f(-value, 0,0)); 103 } 104 } else { 105 System.out.println("Press P to unpause."); 106 } 107 } 108 }; 109 110 } 111