OSDN Git Service

introduced event mode for mouse and key input, added JoystickInput (thus added jinput...
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / input / InputHandler.java
1 /*
2  * Copyright (c) 2003-2005 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 com.jme.input;
34
35 import java.util.ArrayList;
36 import java.util.HashMap;
37
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;
42
43 /**
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.
48  * 
49  * @author Mark Powell
50  * @author Jack Lindamood - (javadoc only)
51  * @version $Id: InputHandler.java,v 1.23 2005-10-11 10:41:45 irrisor Exp $
52  */
53 public class InputHandler {
54
55         /** List of keyboard actions. They are performed in update if valid. */
56         protected ArrayList keyActions;
57
58         /** Actions that just get back an event driven action */
59         protected ArrayList buffKeyActions;
60
61         /** List of mouse actions. They are performed in update. */
62         protected ArrayList mouseActions;
63
64         /** The keyboard where valid key actions are taken from in update. */
65         protected KeyBindingManager keyboard;
66
67         /** The mouse where valid mouse actions are taken from in update. */
68         protected Mouse mouse;
69
70         /** setup only when the buffKeyActions list has elements */
71         protected boolean useBufferedKeyboard = false;
72
73         /** event that will be used to call each action this frame */
74         protected InputActionEvent event;
75
76         /** list of all actions that will be executed this frame */
77         protected ArrayList actionList;
78
79         /** list of names of the events called this frame */
80         protected ArrayList eventList;
81
82         /**
83          * Creates a new input handler. By default, there are no keyboard actions or
84          * mouse actions defined.
85          */
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();
93         }
94
95         /**
96          * Sets the keyboard that will receive key inputs by this handler.
97          * 
98          * @param keyboard
99          *            The keyboard to receive key inputs.
100          */
101         public void setKeyBindingManager(KeyBindingManager keyboard) {
102                 this.keyboard = keyboard;
103         }
104
105         /**
106          * Returns the currently assigned keybard to receive key inputs.
107          * 
108          * @return This handler's keyboard.
109          */
110         public KeyBindingManager getKeyBindingManager() {
111                 return keyboard;
112         }
113
114         /**
115          * Sets the mouse to receive mouse inputs from.
116          * 
117          * @param mouse
118          *            This handler's new mouse.
119          */
120         public void setMouse(Mouse mouse) {
121                 this.mouse = mouse;
122         }
123
124         /**
125          * Returns the mouse currently receiving inputs by this handler.
126          * 
127          * @return This handler's mouse.
128          */
129         public Mouse getMouse() {
130                 return mouse;
131         }
132
133         /**
134          * Sets the speed of all key actions currently defined by this handler to
135          * the given value.
136          * 
137          * @param speed
138          *            The new speed for all currently defined key actions.
139          * @see com.jme.input.action.KeyInputAction#setSpeed(float)
140          */
141         public void setKeySpeed(float speed) {
142                 for (int i = 0; i < keyActions.size(); i++) {
143                         ((KeyInputAction) keyActions.get(i)).setSpeed(speed);
144                 }
145         }
146
147         /**
148          * Sets the speed of all mouse actions currently defined by this handler to
149          * the given value.
150          * 
151          * @param speed
152          *            The new speed for all currently defined mouse actions.
153          * @see com.jme.input.action.MouseInputAction#setSpeed(float)
154          */
155         public void setMouseSpeed(float speed) {
156                 for (int i = 0; i < mouseActions.size(); i++) {
157                         ((MouseInputAction) mouseActions.get(i)).setSpeed(speed);
158                 }
159         }
160
161         /**
162          * Adds a keyboard input action to be polled by this handler during update.
163          * 
164          * @param inputAction
165          *            The input action to be added
166          */
167         public void addAction(KeyInputAction inputAction) {
168                 keyActions.add(inputAction);
169         }
170
171         /**
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
174          * key.
175          * 
176          * @param keyIdent
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
180          * @param action
181          *            An AbstractInputAction that is performed on the keyInputValue.
182          */
183         public void addKeyboardAction(String keyIdent, int keyInputValue,
184                         KeyInputAction action) {
185
186                 if (keyboard == null) {
187                         KeyBindingManager keyboard = KeyBindingManager
188                                         .getKeyBindingManager();
189                         keyboard.setKeyInput(KeyInput.get());
190                         setKeyBindingManager(keyboard);
191                 }
192                 keyboard.set(keyIdent, keyInputValue);
193                 action.setKey(keyIdent);
194                 addAction(action);
195         }
196
197         /**
198          * Used to set actions which will be called based on event driven keyboard
199          * actions
200          * 
201          * @param keyInputAction
202          *            AbstractInputAction that is performed on key event the key
203          *            value of the action will be changed dynamically
204          */
205         public void addBufferedKeyAction(KeyInputAction keyInputAction) {
206             if (keyboard == null) {
207                         KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();
208                         keyboard.setKeyInput(KeyInput.get());
209                         setKeyBindingManager(keyboard);
210             }
211                 buffKeyActions.add(keyInputAction);
212                 useBufferedKeyboard = true;
213         }
214
215         /**
216          * Adds a mouse input action to be polled by this handler during update.
217          * 
218          * @param mouseAction
219          *            The input action to be added
220          */
221         public void addAction(MouseInputAction mouseAction) {
222                 mouseActions.add(mouseAction);
223         }
224
225         /**
226          * Removes a keyboard input action from the list of keyActions that are
227          * polled during update.
228          * 
229          * @param inputAction
230          *            The action to remove.
231          */
232         public void removeAction(KeyInputAction inputAction) {
233                 keyActions.remove(inputAction);
234         }
235
236         /**
237          * Clears all keyboard actions currently stored.
238          */
239         public void clearKeyboardActions() {
240                 keyActions.clear();
241         }
242
243         /**
244          * Clears all mouse actions currently stored.
245          */
246         public void clearMouseActions() {
247                 mouseActions.clear();
248         }
249
250         /**
251          * Removes a mouse input action from the list of mouseActions that are
252          * polled during update.
253          * 
254          * @param mouseAction
255          *            The action to remove.
256          */
257         public void removeAction(MouseInputAction mouseAction) {
258                 mouseActions.remove(mouseAction);
259         }
260
261         /**
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.
264          * 
265          * @param time
266          *            The time to pass to every key and mouse action that is active.
267          */
268         public void update(float time) {
269                 actionList.clear();
270                 eventList.clear();
271                 if (keyboard != null) {
272                         event.setKeys(keyboard.getKeyInput());
273                         if (useBufferedKeyboard) {
274                                 keyboard.update();
275                                 KeyInput kInput = keyboard.getKeyInput();
276                                 while (kInput.next()) {
277                                         boolean keyPressed = kInput.state();
278                                         if (keyPressed) {
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));
284                                                 }
285                                         }
286                                 }
287                         } else {
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))
293                                                                 .getKey());
294                                                 actionList.add(keyActions.get(i));
295                                         }
296                                 }
297                         }
298                 } else {
299                         event.setKeys(null);
300                 }
301
302                 if (mouse != null) {
303                         event.setMouse(mouse.getMouseInput());
304                         mouse.update();
305                         for (int i = 0; i < mouseActions.size(); i++) {
306                                 eventList.add(((InputAction) mouseActions.get(i)).getKey());
307                                 actionList.add(mouseActions.get(i));
308                         }
309                 } else {
310                         event.setMouse(null);
311                 }
312
313                 event.setTime(time);
314                 event.setEventList(eventList);
315
316                 for (int i = 0; i < actionList.size(); i++) {
317                         ((InputAction) actionList.get(i)).performAction(event);
318                 }
319
320         }
321
322     public static float getFloatProp(HashMap props, String key, float defaultVal) {
323         if (props == null || props.get(key) == null)
324             return defaultVal;
325         else
326             return Float.parseFloat(props.get(key).toString());
327     }
328
329     public static int getIntProp(HashMap props, String key, int defaultVal) {
330         if (props == null || props.get(key) == null)
331             return defaultVal;
332         else
333             return Integer.parseInt(props.get(key).toString());
334     }
335
336     public static boolean getBooleanProp(HashMap props, String key, boolean defaultVal) {
337         if (props == null || props.get(key) == null)
338             return defaultVal;
339         else
340             return "true".equalsIgnoreCase(props.get(key).toString());
341     }
342
343     public static Object getObjectProp(HashMap props, String key, Object defaultVal) {
344         if (props == null || props.get(key) == null)
345             return defaultVal;
346         else
347             return props.get(key);
348     }
349 }