2 * Copyright (c) 2003-2005 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 java.util.ArrayList;
36 import java.util.HashMap;
38 import com.jme.input.action.InputAction;
39 import com.jme.input.action.InputActionEvent;
40 import com.jme.input.action.KeyInputAction;
41 import com.jme.input.action.MouseInputAction;
44 * <code>InputHandler</code> handles mouse and key inputs. Inputs are added
45 * and whenever update is called whenever action needs to take place (usually
46 * every frame). Mouse actions are performed every update call. Keyboard actions
47 * are performed only if the correct key is pressed.
50 * @author Jack Lindamood - (javadoc only)
51 * @version $Id: InputHandler.java,v 1.23 2005-10-11 10:41:45 irrisor Exp $
53 public class InputHandler {
55 /** List of keyboard actions. They are performed in update if valid. */
56 protected ArrayList keyActions;
58 /** Actions that just get back an event driven action */
59 protected ArrayList buffKeyActions;
61 /** List of mouse actions. They are performed in update. */
62 protected ArrayList mouseActions;
64 /** The keyboard where valid key actions are taken from in update. */
65 protected KeyBindingManager keyboard;
67 /** The mouse where valid mouse actions are taken from in update. */
68 protected Mouse mouse;
70 /** setup only when the buffKeyActions list has elements */
71 protected boolean useBufferedKeyboard = false;
73 /** event that will be used to call each action this frame */
74 protected InputActionEvent event;
76 /** list of all actions that will be executed this frame */
77 protected ArrayList actionList;
79 /** list of names of the events called this frame */
80 protected ArrayList eventList;
83 * Creates a new input handler. By default, there are no keyboard actions or
84 * mouse actions defined.
86 public InputHandler() {
87 keyActions = new ArrayList();
88 mouseActions = new ArrayList();
89 buffKeyActions = new ArrayList();
90 event = new InputActionEvent();
91 actionList = new ArrayList();
92 eventList = new ArrayList();
96 * Sets the keyboard that will receive key inputs by this handler.
99 * The keyboard to receive key inputs.
101 public void setKeyBindingManager(KeyBindingManager keyboard) {
102 this.keyboard = keyboard;
106 * Returns the currently assigned keybard to receive key inputs.
108 * @return This handler's keyboard.
110 public KeyBindingManager getKeyBindingManager() {
115 * Sets the mouse to receive mouse inputs from.
118 * This handler's new mouse.
120 public void setMouse(Mouse mouse) {
125 * Returns the mouse currently receiving inputs by this handler.
127 * @return This handler's mouse.
129 public Mouse getMouse() {
134 * Sets the speed of all key actions currently defined by this handler to
138 * The new speed for all currently defined key actions.
139 * @see com.jme.input.action.KeyInputAction#setSpeed(float)
141 public void setKeySpeed(float speed) {
142 for (int i = 0; i < keyActions.size(); i++) {
143 ((KeyInputAction) keyActions.get(i)).setSpeed(speed);
148 * Sets the speed of all mouse actions currently defined by this handler to
152 * The new speed for all currently defined mouse actions.
153 * @see com.jme.input.action.MouseInputAction#setSpeed(float)
155 public void setMouseSpeed(float speed) {
156 for (int i = 0; i < mouseActions.size(); i++) {
157 ((MouseInputAction) mouseActions.get(i)).setSpeed(speed);
162 * Adds a keyboard input action to be polled by this handler during update.
165 * The input action to be added
167 public void addAction(KeyInputAction inputAction) {
168 keyActions.add(inputAction);
172 * Binds to the key an action and an identification string. The
173 * identification maps to the key and the action will receive updates on the
177 * A string identifying this key/action purpose. IE "jump_key"
178 * @param keyInputValue
179 * A key that will fire this action. IE KeyInput.KEY_SPACE
181 * An AbstractInputAction that is performed on the keyInputValue.
183 public void addKeyboardAction(String keyIdent, int keyInputValue,
184 KeyInputAction action) {
186 if (keyboard == null) {
187 KeyBindingManager keyboard = KeyBindingManager
188 .getKeyBindingManager();
189 keyboard.setKeyInput(KeyInput.get());
190 setKeyBindingManager(keyboard);
192 keyboard.set(keyIdent, keyInputValue);
193 action.setKey(keyIdent);
198 * Used to set actions which will be called based on event driven keyboard
201 * @param keyInputAction
202 * AbstractInputAction that is performed on key event the key
203 * value of the action will be changed dynamically
205 public void addBufferedKeyAction(KeyInputAction keyInputAction) {
206 if (keyboard == null) {
207 KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();
208 keyboard.setKeyInput(KeyInput.get());
209 setKeyBindingManager(keyboard);
211 buffKeyActions.add(keyInputAction);
212 useBufferedKeyboard = true;
216 * Adds a mouse input action to be polled by this handler during update.
219 * The input action to be added
221 public void addAction(MouseInputAction mouseAction) {
222 mouseActions.add(mouseAction);
226 * Removes a keyboard input action from the list of keyActions that are
227 * polled during update.
230 * The action to remove.
232 public void removeAction(KeyInputAction inputAction) {
233 keyActions.remove(inputAction);
237 * Clears all keyboard actions currently stored.
239 public void clearKeyboardActions() {
244 * Clears all mouse actions currently stored.
246 public void clearMouseActions() {
247 mouseActions.clear();
251 * Removes a mouse input action from the list of mouseActions that are
252 * polled during update.
255 * The action to remove.
257 public void removeAction(MouseInputAction mouseAction) {
258 mouseActions.remove(mouseAction);
262 * Checks all key and mouse actions to see if they are valid commands. If
263 * so, performAction is called on the command with the given time.
266 * The time to pass to every key and mouse action that is active.
268 public void update(float time) {
271 if (keyboard != null) {
272 event.setKeys(keyboard.getKeyInput());
273 if (useBufferedKeyboard) {
275 KeyInput kInput = keyboard.getKeyInput();
276 while (kInput.next()) {
277 boolean keyPressed = kInput.state();
279 for (int i = 0; i < buffKeyActions.size(); i++) {
280 ((InputAction) buffKeyActions.get(i)).setKey(kInput.getKeyName(kInput.key()));
281 ((KeyInputAction) buffKeyActions.get(i)).setKeyChar(kInput.keyChar());
282 eventList.add(((InputAction) buffKeyActions.get(i)).getKey());
283 actionList.add(buffKeyActions.get(i));
288 for (int i = 0; i < keyActions.size(); i++) {
289 if (keyboard.isValidCommand(((InputAction) keyActions
290 .get(i)).getKey(), ((KeyInputAction) keyActions
291 .get(i)).allowsRepeats())) {
292 eventList.add(((InputAction) keyActions.get(i))
294 actionList.add(keyActions.get(i));
303 event.setMouse(mouse.getMouseInput());
305 for (int i = 0; i < mouseActions.size(); i++) {
306 eventList.add(((InputAction) mouseActions.get(i)).getKey());
307 actionList.add(mouseActions.get(i));
310 event.setMouse(null);
314 event.setEventList(eventList);
316 for (int i = 0; i < actionList.size(); i++) {
317 ((InputAction) actionList.get(i)).performAction(event);
322 public static float getFloatProp(HashMap props, String key, float defaultVal) {
323 if (props == null || props.get(key) == null)
326 return Float.parseFloat(props.get(key).toString());
329 public static int getIntProp(HashMap props, String key, int defaultVal) {
330 if (props == null || props.get(key) == null)
333 return Integer.parseInt(props.get(key).toString());
336 public static boolean getBooleanProp(HashMap props, String key, boolean defaultVal) {
337 if (props == null || props.get(key) == null)
340 return "true".equalsIgnoreCase(props.get(key).toString());
343 public static Object getObjectProp(HashMap props, String key, Object defaultVal) {
344 if (props == null || props.get(key) == null)
347 return props.get(key);