com.brackeen.javagamebook.input
Class InputManager

java.lang.Object
  extended by com.brackeen.javagamebook.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

The InputManager manages input of key and mouse events. Events are mapped to GameActions.


Field Summary
private  java.awt.Point centerLocation
           
private  java.awt.Component comp
           
static java.awt.Cursor INVISIBLE_CURSOR
          An invisible cursor.
private  boolean isRecentering
           
private  GameAction[] keyActions
           
static int MOUSE_BUTTON_1
           
static int MOUSE_BUTTON_2
           
static int MOUSE_BUTTON_3
           
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
           
private  GameAction[] mouseActions
           
private  java.awt.Point mouseLocation
           
private static int NUM_KEY_CODES
           
private static int NUM_MOUSE_CODES
           
private  java.awt.Robot robot
           
 
Constructor Summary
InputManager(java.awt.Component comp)
          Creates a new InputManager that listens to input from the specified component.
 
Method Summary
 void clearAllMaps()
          Clears all mapped keys and mouse actions.
 void clearMap(GameAction gameAction)
          Clears all mapped keys and mouse actions to this GameAction.
private  GameAction getKeyAction(java.awt.event.KeyEvent e)
           
static java.lang.String getKeyName(int keyCode)
          Gets the name of a key code.
 java.util.List getMaps(GameAction gameCode)
          Gets a List of names of the keys and mouse actions mapped to this GameAction.
private  GameAction getMouseButtonAction(java.awt.event.MouseEvent e)
           
static int getMouseButtonCode(java.awt.event.MouseEvent e)
          Gets the mouse code for the button specified in this MouseEvent.
static java.lang.String getMouseName(int mouseCode)
          Gets 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 isRelativeMouseMode()
          Returns whether or not relative mouse mode is on.
 void keyPressed(java.awt.event.KeyEvent e)
           
 void keyReleased(java.awt.event.KeyEvent e)
           
 void keyTyped(java.awt.event.KeyEvent e)
           
 void mapToKey(GameAction gameAction, int keyCode)
          Maps a GameAction to a specific key.
 void mapToMouse(GameAction gameAction, int mouseCode)
          Maps a GameAction to a specific mouse action.
 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)
           
private  void mouseHelper(int codeNeg, int codePos, int amount)
           
 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)
           
private  void recenterMouse()
          Uses the Robot class to try to postion the mouse in the center of the screen.
 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 setRelativeMouseMode(boolean mode)
          Sets whether realtive mouse mode is on or not.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, 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
See Also:
Constant Field Values

MOUSE_BUTTON_2

public static final int MOUSE_BUTTON_2
See Also:
Constant Field Values

MOUSE_BUTTON_3

public static final int MOUSE_BUTTON_3
See Also:
Constant Field Values

NUM_MOUSE_CODES

private static final int NUM_MOUSE_CODES
See Also:
Constant Field Values

NUM_KEY_CODES

private static final int NUM_KEY_CODES
See Also:
Constant Field Values

keyActions

private GameAction[] keyActions

mouseActions

private GameAction[] mouseActions

mouseLocation

private java.awt.Point mouseLocation

centerLocation

private java.awt.Point centerLocation

comp

private java.awt.Component comp

robot

private java.awt.Robot robot

isRecentering

private boolean isRecentering
Constructor Detail

InputManager

public InputManager(java.awt.Component comp)
Creates a new InputManager that listens to input from the specified component.

Method Detail

setCursor

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


setRelativeMouseMode

public void setRelativeMouseMode(boolean mode)
Sets whether realtive mouse mode is on 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.


isRelativeMouseMode

public boolean isRelativeMouseMode()
Returns whether or not relative mouse mode is on.


mapToKey

public void mapToKey(GameAction gameAction,
                     int keyCode)
Maps a GameAction to a specific key. The key codes are defined in java.awt.KeyEvent. If the key already has a GameAction mapped to it, the new GameAction overwrites it.


mapToMouse

public void mapToMouse(GameAction gameAction,
                       int mouseCode)
Maps a GameAction to a specific mouse action. The mouse codes are defined herer in InputManager (MOUSE_MOVE_LEFT, MOUSE_BUTTON_1, etc). If the mouse action already has a GameAction mapped to it, the new GameAction overwrites it.


clearMap

public void clearMap(GameAction gameAction)
Clears all mapped keys and mouse actions to this GameAction.


clearAllMaps

public void clearAllMaps()
Clears all mapped keys and mouse actions.


getMaps

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


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)
Gets the name of a key code.


getMouseName

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


getMouseX

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


getMouseY

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


recenterMouse

private void recenterMouse()
Uses the Robot class to try to postion the mouse in the center of the screen.

Note that use of the Robot class may not be available on all platforms.


getKeyAction

private GameAction getKeyAction(java.awt.event.KeyEvent e)

getMouseButtonCode

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


getMouseButtonAction

private GameAction getMouseButtonAction(java.awt.event.MouseEvent e)

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

mouseHelper

private void mouseHelper(int codeNeg,
                         int codePos,
                         int amount)