2 * Copyright (c) 2003-2009 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.
34 * EDIT: 02/08/2004 - Added update(boolean updateState) to allow for a
35 * WidgetViewport to update an AbstractInputHandler
36 * without polling the mouse. GOP
39 package com.jme.input;
41 import com.jme.input.action.InputAction;
42 import com.jme.input.action.InputActionEvent;
45 * <code>AbsoluteMouse</code> defines a mouse object that maintains a position
46 * within the window. Each call to update adjusts the current position by the
47 * change in position since the previous update. The mouse is forced to be
48 * contained within the values provided during construction (typically these
49 * correspond to the width and height of the window).
52 * @author Gregg Patton
53 * @version $Id: AbsoluteMouse.java,v 1.26 2007/04/03 14:30:22 nca Exp $
55 public class AbsoluteMouse extends Mouse {
57 private static final long serialVersionUID = 1L;
59 private boolean usingDelta = false;
60 private int limitHeight, limitWidth;
62 * @return true if mouse position delta are used to compute the absolute position, false if the absolute
63 * mouse coordinates are used directly
65 public boolean isUsingDelta() {
70 * @param usingDelta true to compute the absolute position from mouse position delta, false to use the absolute
71 * mouse coordinates directly
73 public void setUsingDelta( boolean usingDelta ) {
74 this.usingDelta = usingDelta;
78 private InputAction xUpdateAction = new InputAction() {
79 public void performAction( InputActionEvent evt ) {
80 if ( isUsingDelta() ) {
81 localTranslation.x += evt.getTriggerDelta() * limitWidth * speed; //speed of the action!
83 localTranslation.x = evt.getTriggerPosition() * limitWidth * speed - hotSpotOffset.x;
86 if ( localTranslation.x + hotSpotOffset.x < 0 ) {
87 localTranslation.x = -hotSpotOffset.x;
89 else if ( localTranslation.x + hotSpotOffset.x > limitWidth ) {
90 localTranslation.x = width - hotSpotOffset.x;
92 worldTranslation.x = localTranslation.x;
93 hotSpotLocation.x = localTranslation.x + hotSpotOffset.x;
96 private InputAction yUpdateAction = new InputAction() {
97 public void performAction( InputActionEvent evt ) {
98 if ( isUsingDelta() ) {
99 localTranslation.y += evt.getTriggerDelta() * limitHeight * speed; //speed of the action!
101 localTranslation.y = evt.getTriggerPosition() * limitHeight * speed - hotSpotOffset.y;
104 if ( localTranslation.y + hotSpotOffset.y < 0 /*- imageHeight*/ ) {
105 localTranslation.y = 0/* - imageHeight*/ - hotSpotOffset.y;
107 else if ( localTranslation.y + hotSpotOffset.y > limitHeight ) {
108 localTranslation.y = height - hotSpotOffset.y;
110 worldTranslation.y = localTranslation.y;
111 hotSpotLocation.y = localTranslation.y + hotSpotOffset.y;
114 private InputHandler registeredInputHandler;
117 * Constructor instantiates a new <code>AbsoluteMouse</code> object. The
118 * limits of the mouse movements are provided.
120 * @param name the name of the scene element. This is required for
121 * identification and comparision purposes.
122 * @param limitWidth the width of the mouse's limit.
123 * @param limitHeight the height of the mouse's limit.
125 public AbsoluteMouse( String name, int limitWidth, int limitHeight ) {
127 this.limitWidth = limitWidth;
128 this.limitHeight = limitHeight;
129 getXUpdateAction().setSpeed( 1 );
130 getYUpdateAction().setSpeed( 1 );
134 * set the mouse's limit.
136 * @param limitWidth the width of the mouse's limit.
137 * @param limitHeight the height of the mouse's limit.
139 public void setLimit( int limitWidth, int limitHeight ) {
140 this.limitWidth = limitWidth;
141 this.limitHeight = limitHeight;
144 public void setSpeed( float speed ) {
145 getXUpdateAction().setSpeed( speed );
146 getYUpdateAction().setSpeed( speed );
150 * Registers the xUpdateAction and the yUpdateAction with axis 0 and 1 of the mouse.
151 * Note: you can register the actions with other devices, too, instead of calling this method.
152 * @param inputHandler handler to register with (null to unregister)
153 * @see #getXUpdateAction()
154 * @see #getYUpdateAction()
156 public void registerWithInputHandler( InputHandler inputHandler ) {
157 if ( registeredInputHandler != null ) {
158 registeredInputHandler.removeAction( getXUpdateAction() );
159 registeredInputHandler.removeAction( getYUpdateAction() );
161 registeredInputHandler = inputHandler;
162 if ( inputHandler != null ) {
163 inputHandler.addAction( getXUpdateAction(), InputHandler.DEVICE_MOUSE, InputHandler.BUTTON_NONE, 0, false );
164 inputHandler.addAction( getYUpdateAction(), InputHandler.DEVICE_MOUSE, InputHandler.BUTTON_NONE, 1, false );
168 public InputAction getXUpdateAction() {
169 return xUpdateAction;
172 public InputAction getYUpdateAction() {
173 return yUpdateAction;