OSDN Git Service

2f2d56dcad09e379a99144916b32e9e5568b5d4e
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / input / AbsoluteMouse.java
1 /*
2  * Copyright (c) 2003-2009 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 /*
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
37  */
38
39 package com.jme.input;
40
41 import com.jme.input.action.InputAction;
42 import com.jme.input.action.InputActionEvent;
43
44 /**
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).
50  *
51  * @author Mark Powell
52  * @author Gregg Patton
53  * @version $Id: AbsoluteMouse.java,v 1.26 2007/04/03 14:30:22 nca Exp $
54  */
55 public class AbsoluteMouse extends Mouse {
56
57     private static final long serialVersionUID = 1L;
58
59     private boolean usingDelta = false;
60     private int limitHeight, limitWidth;
61     /**
62      * @return true if mouse position delta are used to compute the absolute position, false if the absolute
63      * mouse coordinates are used directly
64      */
65     public boolean isUsingDelta() {
66         return usingDelta;
67     }
68
69     /**
70      * @param usingDelta true to compute the absolute position from mouse position delta, false to use the absolute
71      * mouse coordinates directly
72      */
73     public void setUsingDelta( boolean usingDelta ) {
74         this.usingDelta = usingDelta;
75     }
76
77     //position
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!
82             } else {
83                 localTranslation.x = evt.getTriggerPosition() * limitWidth * speed - hotSpotOffset.x;
84             }
85
86             if ( localTranslation.x + hotSpotOffset.x < 0 ) {
87                 localTranslation.x = -hotSpotOffset.x;
88             }
89             else if ( localTranslation.x + hotSpotOffset.x > limitWidth ) {
90                 localTranslation.x = width - hotSpotOffset.x;
91             }
92             worldTranslation.x = localTranslation.x;
93             hotSpotLocation.x = localTranslation.x + hotSpotOffset.x;
94         }
95     };
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!
100             } else {
101                 localTranslation.y = evt.getTriggerPosition() * limitHeight * speed - hotSpotOffset.y;
102             }
103
104             if ( localTranslation.y + hotSpotOffset.y < 0 /*- imageHeight*/ ) {
105                 localTranslation.y = 0/* - imageHeight*/ - hotSpotOffset.y;
106             }
107             else if ( localTranslation.y + hotSpotOffset.y > limitHeight ) {
108                 localTranslation.y = height - hotSpotOffset.y;
109             }
110             worldTranslation.y = localTranslation.y;
111             hotSpotLocation.y = localTranslation.y + hotSpotOffset.y;
112         }
113     };
114     private InputHandler registeredInputHandler;
115
116     /**
117      * Constructor instantiates a new <code>AbsoluteMouse</code> object. The
118      * limits of the mouse movements are provided.
119      *
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.
124      */
125     public AbsoluteMouse( String name, int limitWidth, int limitHeight ) {
126         super( name );
127         this.limitWidth = limitWidth;
128         this.limitHeight = limitHeight;
129         getXUpdateAction().setSpeed( 1 );
130         getYUpdateAction().setSpeed( 1 );
131     }
132
133     /**
134      * set the mouse's limit.
135      *
136      * @param limitWidth  the width of the mouse's limit.
137      * @param limitHeight the height of the mouse's limit.
138      */
139     public void setLimit( int limitWidth, int limitHeight ) {
140         this.limitWidth = limitWidth;
141         this.limitHeight = limitHeight;
142     }
143
144     public void setSpeed( float speed ) {
145         getXUpdateAction().setSpeed( speed );
146         getYUpdateAction().setSpeed( speed );
147     }
148
149     /**
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()
155      */
156     public void registerWithInputHandler( InputHandler inputHandler ) {
157         if ( registeredInputHandler != null ) {
158             registeredInputHandler.removeAction( getXUpdateAction() );
159             registeredInputHandler.removeAction( getYUpdateAction() );
160         }
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 );
165         }
166     }
167
168     public InputAction getXUpdateAction() {
169         return xUpdateAction;
170     }
171
172     public InputAction getYUpdateAction() {
173         return yUpdateAction;
174     }
175 }