org.jrabbit.base.input
Class MouseHandler

java.lang.Object
  extended by org.jrabbit.base.input.MouseHandler

public final class MouseHandler
extends java.lang.Object

MouseHandler is a static class that provides the default methods for reacting to player input via the mouse. It is automatically updated in the main game loop. MouseHandler is designed to work with a mouse that has any number of buttons; to learn the number of buttons simply use the getButtonCount() method in Mouse. Additionally, MouseHandler handles mouse movement interpretation. In to a "motion-listener" system, where objects can be notified of mouse movements, MouseHandler also stores VectorPaths that keeps track of the motion of the mouse over time, both in world coordinates and screen coordinates. This path has a maximum length that can be set. NOTE: It's possible that a different input setup will better suit your needs. If this is the case, you should be aware that by default, MouseHandler "eats" all Mouse events. You can turn this activity off, however, and interpret them with your own code.

Author:
Chris Molini

Field Summary
private static boolean activated
          Whether or not MouseHandler should parse Mouse events.
private static java.util.ArrayList<MouseEventListener> eventListeners
          The listeners that will be notified of all LWJGL Mouse events.
private static boolean[] inputClicked
          The stored data about mouse clicks.
private static boolean[] inputDown
          The stored data about held mouse buttons.
private static boolean[] inputReleased
          The stored data about mouse button releases.
private static java.util.ArrayList<MouseListener> listeners
          The listeners that will be notified of wheel and button input events.
private static java.util.ArrayList<MouseMotionListener> motionListeners
          The listeners that will be notified of movement events.
private static int mouseWheelChange
          The amount the mouse wheel hase moved since the last update cycle.
private static int NUM_BUTTONS
          The number of buttons on the mouse that are valid for input.
private static VectorTransform positionCalculator
          The calculator that transforms screen coordinates into game-world coordinates.
private static Vector2f screenLocation
          The location of the mouse, in screen coordinates.
private static VectorPath screenPath
          The mouse motion path, in screen coordinates.
private static Vector2f worldLocation
          The location of the mouse, in world coordinates.
private static VectorPath worldPath
          The mouse motion path, in calculated game-world coordinates.
 
Constructor Summary
MouseHandler()
           
 
Method Summary
static void activate(boolean active)
          Determines whether or not Mouse events should be processed.
static boolean activated()
          Learns if the MouseHandler is active.
static java.util.Collection<MouseEventListener> eventListeners()
          Accesses the list of Mouse Event listeners.
static void hideMouse(boolean hide)
          Use this to make the cursor invisible or not.
static boolean isButtonDown(int button)
          Checks to see if the indicated mouse button is held down.
static java.util.Collection<MouseListener> listeners()
          Accesses the list of Mouse listeners.
static Vector2f location(boolean onscreen)
          Learns the current location of the mouse hotspot, in either screen or game-world coordinates.
static java.util.Collection<MouseMotionListener> motionListeners()
          Accesses the list of Mouse motion listeners.
static VectorPath path(boolean onscreen)
          Learns the stored mouse path, in either screen or game-world coordinates.
static VectorTransform positionCalculator()
          Accesses the VectorTransform used to calculate game-world coordinates from screen coordinates.
static void setCursor(java.lang.String filepath)
          Defines the cursor by the image at the indicated filepath.
static void setCursor(java.lang.String filepath, java.awt.Point hotspot)
          Defines the cursor by the image at the indicated filepath with the indicated hotspot.
static void setPathLength(int length)
          Defines the length of the stored mouse path.
static void setPositionCalculator(VectorTransform calculator)
          Redefines the object that calculates game-world coordinates from screen coordinates.
static void update()
          Advances the update cycle, clears stored input data, and builds the mouse paths and location.
static boolean wasButtonClicked(int button)
          Checks to see if the indicated mouse button was clicked since the last update cycle.
static boolean wasButtonReleased(int button)
          Checks to see if the indicated mouse button was released since the last update cycle.
static int wheelChange()
          Accesses the amount the mouse wheel was rotated since the last update cycle.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

activated

private static boolean activated
Whether or not MouseHandler should parse Mouse events.


NUM_BUTTONS

private static int NUM_BUTTONS
The number of buttons on the mouse that are valid for input.


inputClicked

private static boolean[] inputClicked
The stored data about mouse clicks.


inputReleased

private static boolean[] inputReleased
The stored data about mouse button releases.


inputDown

private static boolean[] inputDown
The stored data about held mouse buttons.


mouseWheelChange

private static int mouseWheelChange
The amount the mouse wheel hase moved since the last update cycle.


screenLocation

private static Vector2f screenLocation
The location of the mouse, in screen coordinates.


worldLocation

private static Vector2f worldLocation
The location of the mouse, in world coordinates.


screenPath

private static VectorPath screenPath
The mouse motion path, in screen coordinates.


worldPath

private static VectorPath worldPath
The mouse motion path, in calculated game-world coordinates.


positionCalculator

private static VectorTransform positionCalculator
The calculator that transforms screen coordinates into game-world coordinates. The initial value, shown here, simply copies the vector. It should be noted, however, that the default jRabbit setup redefines this upon initialization.


listeners

private static java.util.ArrayList<MouseListener> listeners
The listeners that will be notified of wheel and button input events.


motionListeners

private static java.util.ArrayList<MouseMotionListener> motionListeners
The listeners that will be notified of movement events.


eventListeners

private static java.util.ArrayList<MouseEventListener> eventListeners
The listeners that will be notified of all LWJGL Mouse events.

Constructor Detail

MouseHandler

public MouseHandler()
Method Detail

activate

public static void activate(boolean active)
Determines whether or not Mouse events should be processed. Use this to turn off MouseHandler calculations if you want to use a different input system for handling Keyboard events.

Parameters:
active - Whether or not the MouseHandler should process Mouse events.

activated

public static boolean activated()
Learns if the MouseHandler is active.

Returns:
True if Mouse events are being processed, false if not.

listeners

public static java.util.Collection<MouseListener> listeners()
Accesses the list of Mouse listeners. NOTE: This is the means that should be used to add and remove new MouseListeners.

Returns:
All of the objects currently listening for input changes.

motionListeners

public static java.util.Collection<MouseMotionListener> motionListeners()
Accesses the list of Mouse motion listeners. NOTE: This is the means that should be used to add and remove new MouseMotionListeners.

Returns:
All of the objects currently listening for mouse movement.

eventListeners

public static java.util.Collection<MouseEventListener> eventListeners()
Accesses the list of Mouse Event listeners. NOTE: This is the means that should be used to add and remove new MouseEventListener.

Returns:
All of the objects currently listening for LWJGL Mouse events.

isButtonDown

public static boolean isButtonDown(int button)
Checks to see if the indicated mouse button is held down.

Parameters:
button - The button number to check. Main button is 0, right is 1, etc. in decreasing order of prominence.
Returns:
True if the button is currently pressed, false if not.

wasButtonClicked

public static boolean wasButtonClicked(int button)
Checks to see if the indicated mouse button was clicked since the last update cycle.

Parameters:
button - The button number to check. Main button is 0, right is 1, etc. in decreasing order of prominence.
Returns:
True if the button was clicked, false if not.

wasButtonReleased

public static boolean wasButtonReleased(int button)
Checks to see if the indicated mouse button was released since the last update cycle.

Parameters:
button - The button number to check. Main button is 0, right is 1, etc. in decreasing order of prominence.
Returns:
True if the button was released, false if not.

wheelChange

public static int wheelChange()
Accesses the amount the mouse wheel was rotated since the last update cycle.

Returns:
The amount the wheel changed. The magnitude may very from mouse to mouse.

location

public static Vector2f location(boolean onscreen)
Learns the current location of the mouse hotspot, in either screen or game-world coordinates.

Parameters:
onscreen - True indicates the coordinates should be for the screen, false indicates they should be in-game.
Returns:
The location of the mouse.

path

public static VectorPath path(boolean onscreen)
Learns the stored mouse path, in either screen or game-world coordinates.

Parameters:
onscreen - True indicates the coordinates should be for the screen, false indicates they should be in-game.
Returns:
The stored path of the mouse.

setPathLength

public static void setPathLength(int length)
Defines the length of the stored mouse path. If the indicated length is less than the current length of the path, the path is cropped to the size desired. However, if the indicated length is longer than the path, the path is cropped to fit.

Parameters:
length - The number of locations to store as a consecutive path.

positionCalculator

public static VectorTransform positionCalculator()
Accesses the VectorTransform used to calculate game-world coordinates from screen coordinates.

Returns:
The position calculator.

setPositionCalculator

public static void setPositionCalculator(VectorTransform calculator)
Redefines the object that calculates game-world coordinates from screen coordinates.

Parameters:
calculator - The new VectorTransform to use.

hideMouse

public static void hideMouse(boolean hide)
Use this to make the cursor invisible or not. NOTE: The in no way affects the calculations of mouse coordinates and events, it simply changes how the mouse is shown.

Parameters:
hide - Whether or not to hide the mouse.

setCursor

public static void setCursor(java.lang.String filepath)
Defines the cursor by the image at the indicated filepath. The hotspot is located at the top-left of the mouse image.

Parameters:
filepath - The location of the image to use.

setCursor

public static void setCursor(java.lang.String filepath,
                             java.awt.Point hotspot)
Defines the cursor by the image at the indicated filepath with the indicated hotspot.

Parameters:
filepath - The location of the image to use.
hotspot - The point on the image to use as the hotspot.

update

public static void update()
Advances the update cycle, clears stored input data, and builds the mouse paths and location. If the MouseHandler is activated, it also consumes all Mouse events and updates button info.