de.rico.engine.input
Class InputManager

java.lang.Object
  extended by de.rico.engine.input.InputManager
All Implemented Interfaces:
java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.MouseWheelListener, java.util.EventListener

public class InputManager
extends java.lang.Object
implements java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.MouseWheelListener

This class is used as an input manager. It is fairly collaborating with the InputAction class, because the input events registered by this class are mapped to actions of the InputAction class.
Note: This class was mostly taken by the book "Developing Games In Java" by David Brackeen

Author:
David Brackeen, slight additions (some methods, more comments) by Frank Bruns

Field Summary
static java.awt.Cursor INVISIBLE_CURSOR
          An invisible cursor.
static int MOUSE_BUTTON_1
          This is usually the left mouse button.
static int MOUSE_BUTTON_2
          This is usually the middle mouse button.
static int MOUSE_BUTTON_3
          This is usually the right mouse button.
static int MOUSE_MOVE_DOWN
           
static int MOUSE_MOVE_LEFT
           
static int MOUSE_MOVE_RIGHT
           
static int MOUSE_MOVE_UP
           
static int MOUSE_WHEEL_DOWN
           
static int MOUSE_WHEEL_UP
           
 
Constructor Summary
InputManager(java.awt.Component comp)
          Creates a new InputManager for the specified component.
 
Method Summary
 void assignKey(InputAction inputAction, int keyCode)
          Assigns a specific key to an InputAction.
 void assignMouse(InputAction inputAction, int mouseCode)
          Assigns a mouse action to an object of InputAction.
 void clearAssignments(InputAction inputAction)
          Clears all assigned mouse and key actions from an input action
 InputAction[] getKeyActions()
          Returns the key actions assigned to the input manager.
static java.lang.String getKeyName(int keyCode)
          Returns the name of a key code.
 java.util.List getMaps(InputAction gameCode)
          Returns a List of names of the keys and mouse actions mapped to this GameAction.
 InputAction[] getMouseActions()
          Returns the mouse actions assigned to the input manager.
static int getMouseButtonCode(java.awt.event.MouseEvent e)
          Returns the mouse code for the button specified in this MouseEvent.
static java.lang.String getMouseName(int mouseCode)
          Returns the name of a mouse code.
 int getMouseX()
          Gets the x position of the mouse.
 int getMouseY()
          Gets the y position of the mouse.
 boolean isEnabledRelativeMouseMode()
          Returns whether or not relative mouse mode is enabled.
 void keyPressed(java.awt.event.KeyEvent e)
           
 void keyReleased(java.awt.event.KeyEvent e)
           
 void keyTyped(java.awt.event.KeyEvent e)
           
 void mouseClicked(java.awt.event.MouseEvent e)
           
 void mouseDragged(java.awt.event.MouseEvent e)
           
 void mouseEntered(java.awt.event.MouseEvent e)
           
 void mouseExited(java.awt.event.MouseEvent e)
           
 void mouseMoved(java.awt.event.MouseEvent e)
           
 void mousePressed(java.awt.event.MouseEvent e)
           
 void mouseReleased(java.awt.event.MouseEvent e)
           
 void mouseWheelMoved(java.awt.event.MouseWheelEvent e)
           
 void resetAllGameActions()
          Resets all GameActions so they appear like they haven't been pressed.
 void setCursor(java.awt.Cursor cursor)
          Sets the cursor on this InputManager's input component.
 void setEnabledRelativeMouseMode(boolean mode)
          Sets whether realtive mouse mode is enabled or not.
 void setMouseLocation(int x, int y)
          Sets the mouse location to the specified coordinates.
 void setMouseX(int x)
          Sets the mouse location to the specified coordinate.
 void setMouseY(int y)
          Sets the mouse location to the specified coordinate.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INVISIBLE_CURSOR

public static final java.awt.Cursor INVISIBLE_CURSOR
An invisible cursor.


MOUSE_MOVE_LEFT

public static final int MOUSE_MOVE_LEFT
See Also:
Constant Field Values

MOUSE_MOVE_RIGHT

public static final int MOUSE_MOVE_RIGHT
See Also:
Constant Field Values

MOUSE_MOVE_UP

public static final int MOUSE_MOVE_UP
See Also:
Constant Field Values

MOUSE_MOVE_DOWN

public static final int MOUSE_MOVE_DOWN
See Also:
Constant Field Values

MOUSE_WHEEL_UP

public static final int MOUSE_WHEEL_UP
See Also:
Constant Field Values

MOUSE_WHEEL_DOWN

public static final int MOUSE_WHEEL_DOWN
See Also:
Constant Field Values

MOUSE_BUTTON_1

public static final int MOUSE_BUTTON_1
This is usually the left mouse button.

See Also:
Constant Field Values

MOUSE_BUTTON_2

public static final int MOUSE_BUTTON_2
This is usually the middle mouse button.

See Also:
Constant Field Values

MOUSE_BUTTON_3

public static final int MOUSE_BUTTON_3
This is usually the right mouse button.

See Also:
Constant Field Values
Constructor Detail

InputManager

public InputManager(java.awt.Component comp)
Creates a new InputManager for the specified component. This adds several listeners to the component, such as KeyListener, MouseListener, MouseMotionListener or MouseWheelListener

Parameters:
comp - GUI component to assign the input manager to
Method Detail

getKeyActions

public InputAction[] getKeyActions()
Returns the key actions assigned to the input manager.

Returns:
assigned key actions

getMouseActions

public InputAction[] getMouseActions()
Returns the mouse actions assigned to the input manager.

Returns:
assigned mouse actions

setCursor

public void setCursor(java.awt.Cursor cursor)
Sets the cursor on this InputManager's input component.

Parameters:
cursor - cursor to use on the component

setEnabledRelativeMouseMode

public void setEnabledRelativeMouseMode(boolean mode)
Sets whether realtive mouse mode is enabled or not. For relative mouse mode, the mouse is "locked" in the center of the screen, and only the changed in mouse movement is measured. In normal mode, the mouse is free to move about the screen.

Parameters:
mode - set relative mouse mode enabled? --> true=yes, false=no

isEnabledRelativeMouseMode

public boolean isEnabledRelativeMouseMode()
Returns whether or not relative mouse mode is enabled.

Returns:
is relative mouse mode enabled? --> true=yes, false=no

assignKey

public void assignKey(InputAction inputAction,
                      int keyCode)
Assigns a specific key to an InputAction.

Parameters:
inputAction - an InputAction object
keyCode - key code like KeyEvent.VK_A

assignMouse

public void assignMouse(InputAction inputAction,
                        int mouseCode)
Assigns a mouse action to an object of InputAction.

Parameters:
inputAction - an InputAction object
mouseCode - mouse code like InputManager.MOUSE_MOVE_LEFT

clearAssignments

public void clearAssignments(InputAction inputAction)
Clears all assigned mouse and key actions from an input action

Parameters:
inputAction - an InputAction object

getMaps

public java.util.List getMaps(InputAction gameCode)
Returns a List of names of the keys and mouse actions mapped to this GameAction. Each entry in the List is a String.

Returns:
list of names of the input manager's game actions

resetAllGameActions

public void resetAllGameActions()
Resets all GameActions so they appear like they haven't been pressed.


getKeyName

public static java.lang.String getKeyName(int keyCode)
Returns the name of a key code.

Returns:
name of the key code

getMouseName

public static java.lang.String getMouseName(int mouseCode)
Returns the name of a mouse code.

Returns:
name of the mouse code

getMouseX

public int getMouseX()
Gets the x position of the mouse.

Returns:
x coordinate

getMouseY

public int getMouseY()
Gets the y position of the mouse.

Returns:
y coordinate

setMouseX

public void setMouseX(int x)
Sets the mouse location to the specified coordinate.

Parameters:
x - x-coordinate for the mouse location

setMouseY

public void setMouseY(int y)
Sets the mouse location to the specified coordinate.

Parameters:
y - y-coordinate for the mouse location

setMouseLocation

public void setMouseLocation(int x,
                             int y)
Sets the mouse location to the specified coordinates.

Parameters:
x - x-coordinate for the mouse location
y - y-coordinate for the mouse location

getMouseButtonCode

public static int getMouseButtonCode(java.awt.event.MouseEvent e)
Returns the mouse code for the button specified in this MouseEvent.

Returns:
mouse code for the button specified in this MouseEvent

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
Specified by:
keyReleased in interface java.awt.event.KeyListener

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
Specified by:
keyTyped in interface java.awt.event.KeyListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Specified by:
mouseExited in interface java.awt.event.MouseListener

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener

mouseWheelMoved

public void mouseWheelMoved(java.awt.event.MouseWheelEvent e)
Specified by:
mouseWheelMoved in interface java.awt.event.MouseWheelListener