2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
33 package com.jme.input;
35 import com.jme.input.action.KeyNodeBackwardAction;
36 import com.jme.input.action.KeyNodeForwardAction;
37 import com.jme.input.action.KeyNodeLookDownAction;
38 import com.jme.input.action.KeyNodeLookUpAction;
39 import com.jme.input.action.KeyNodeRotateLeftAction;
40 import com.jme.input.action.KeyNodeRotateRightAction;
41 import com.jme.input.action.KeyNodeStrafeLeftAction;
42 import com.jme.input.action.KeyNodeStrafeRightAction;
43 import com.jme.input.action.NodeMouseLook;
44 import com.jme.math.Vector3f;
45 import com.jme.scene.Spatial;
48 * <code>NodeHandler</code> defines an InputHandler that sets
49 * a node that can be controlled via keyboard and mouse inputs. By default the
50 * commands are, WSAD moves the node forward, backward and strafes. The
51 * arrow keys rotate and tilt the node and the mouse also rotates and tilts
54 * @version $Id: NodeHandler.java,v 1.15 2006/03/05 10:43:57 irrisor Exp $
56 public class NodeHandler extends InputHandler {
59 * Constructor instantiates a new <code>NodeHandler</code> object. The
60 * application is set for the use of the exit action. The node is set to
61 * control, while the api defines which input api is to be used.
62 * @param node the node to control.
65 public NodeHandler(Spatial node, String api) { //todo: remove parameter api
69 setActions(node, 0.5f, 0.01f );
73 * Constructor instantiates a new <code>NodeHandler</code> object. The
74 * application is set for the use of the exit action. The node is set to
75 * control, while the api defines which input api is to be used.
76 * @param node the node to control.
77 * @param keySpeed action speed for key actions (move)
78 * @param mouseSpeed action speed for mouse actions (rotate)
80 public NodeHandler(Spatial node, float keySpeed, float mouseSpeed) {
83 setUpMouse(node, mouseSpeed );
84 setActions(node, keySpeed, mouseSpeed );
89 * <code>setKeyBindings</code> binds the keys to use for the actions.
91 private void setKeyBindings() {
92 KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();
94 keyboard.set("forward", KeyInput.KEY_W);
95 keyboard.set("backward", KeyInput.KEY_S);
96 keyboard.set("strafeLeft", KeyInput.KEY_A);
97 keyboard.set("strafeRight", KeyInput.KEY_D);
98 keyboard.set("lookUp", KeyInput.KEY_UP);
99 keyboard.set("lookDown", KeyInput.KEY_DOWN);
100 keyboard.set("turnRight", KeyInput.KEY_RIGHT);
101 keyboard.set("turnLeft", KeyInput.KEY_LEFT);
106 * <code>setUpMouse</code> sets the mouse look object.
107 * @param node the node to use for rotations.
110 private void setUpMouse( Spatial node, float mouseSpeed ) {
111 RelativeMouse mouse = new RelativeMouse("Mouse Input");
112 mouse.registerWithInputHandler( this );
114 NodeMouseLook mouseLook = new NodeMouseLook(mouse, node, 0.1f);
115 mouseLook.setSpeed( mouseSpeed );
116 mouseLook.setLockAxis(new Vector3f(node.getLocalRotation().getRotationColumn(1).x,
117 node.getLocalRotation().getRotationColumn(1).y,
118 node.getLocalRotation().getRotationColumn(1).z));
119 addAction(mouseLook);
124 * <code>setActions</code> sets the keyboard actions with the corresponding
126 * @param node the node to control.
130 private void setActions( Spatial node, float moveSpeed, float turnSpeed ) {
131 addAction( new KeyNodeForwardAction( node, moveSpeed ), "forward", true );
132 addAction( new KeyNodeBackwardAction( node, moveSpeed ), "backward", true );
133 addAction( new KeyNodeStrafeLeftAction( node, moveSpeed ), "strafeLeft", true );
134 addAction( new KeyNodeStrafeRightAction( node, moveSpeed ), "strafeRight", true );
135 addAction( new KeyNodeLookUpAction( node, turnSpeed ), "lookUp", true );
136 addAction( new KeyNodeLookDownAction( node, turnSpeed ), "lookDown", true );
137 KeyNodeRotateRightAction rotateRight = new KeyNodeRotateRightAction( node, turnSpeed );
138 rotateRight.setLockAxis( node.getLocalRotation().getRotationColumn( 1 ) );
139 addAction( rotateRight, "turnRight", true );
140 KeyNodeRotateLeftAction rotateLeft = new KeyNodeRotateLeftAction( node, turnSpeed );
141 rotateLeft.setLockAxis( node.getLocalRotation().getRotationColumn( 1 ) );
142 addAction( rotateLeft, "turnLeft", true );