1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/**
* Copyright (C) 2004 Jeremy Booth (jeremy@newdawnsoftware.com)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer. Redistributions in binary
* form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided
* with the distribution.
* The name of the author may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*/
package net.java.games.input;
import java.awt.AWTEvent;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
/**
* @author Jeremy
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class AWTMouse extends Mouse implements AWTEventListener {
private AWTAxis xMove = new AWTAxis("X", Component.Identifier.Axis.X);
private AWTAxis yMove = new AWTAxis("Y", Component.Identifier.Axis.Y);
private AWTAxis zMove = new AWTAxis("Wheel", Component.Identifier.Axis.SLIDER);
private AWTButton button1 = new AWTButton("Left", Component.Identifier.Button.LEFT);
private AWTButton button2 = new AWTButton("Middle", Component.Identifier.Button.MIDDLE);
private AWTButton button3 = new AWTButton("Right", Component.Identifier.Button.RIGHT);
private Point oldMouseLocation = new Point(0,0);
private Point newMouseLocation = new Point(0,0);
private int scrollAmount = 0;
private boolean button1Value = false;
private boolean button2Value = false;
private boolean button3Value = false;
/**
* @param name
*/
protected AWTMouse(String name) {
super(name);
Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.MOUSE_WHEEL_EVENT_MASK);
this.ball = new AWTMouseBall(xMove, yMove, zMove);
this.buttons = new AWTMouseButtons(new AWTMouseButton(button1), new AWTMouseButton(button2), new AWTMouseButton(button3));
}
/* (non-Javadoc)
* @see net.java.games.input.Controller#poll()
*/
public boolean poll() {
button1.setValue(button1Value);
button2.setValue(button2Value);
button3.setValue(button3Value);
zMove.setValue(scrollAmount);
scrollAmount = 0;
//System.out.println("old mouse location " + oldMouseLocation);
//System.out.println("new mouse location " + newMouseLocation);
yMove.setValue((float)(newMouseLocation.getY() - oldMouseLocation.getY()));
xMove.setValue((float)(newMouseLocation.getX() - oldMouseLocation.getX()));
oldMouseLocation.setLocation(newMouseLocation.getLocation());
//newMouseLocation.setLocation(0,0);
return true;
}
/* (non-Javadoc)
* @see java.awt.event.AWTEventListener#eventDispatched(java.awt.AWTEvent)
*/
public void eventDispatched(AWTEvent event) {
//System.out.println("AWTMouse: From: " + arg0.getSource() + " - " + arg0);
if(event instanceof MouseWheelEvent) {
MouseWheelEvent mwe = (MouseWheelEvent)event;
scrollAmount += mwe.getWheelRotation();
//System.out.println("New scroll amount: " + scrollAmount);
}
if(event instanceof MouseEvent) {
MouseEvent me = (MouseEvent)event;
newMouseLocation.setLocation(me.getPoint());
//System.out.println("Mouse moved to " + newMouseLocation);
if(me.getID() == MouseEvent.MOUSE_PRESSED) {
//System.out.println("Button was pressed");
if(me.getButton() == MouseEvent.BUTTON1) {
//System.out.println("Button 1 was pressed");
button1Value = true;
} else if(me.getButton() == MouseEvent.BUTTON2) {
//System.out.println("Button 2 was pressed");
button2Value = true;
} else if(me.getButton() == MouseEvent.BUTTON3) {
//System.out.println("Button 3 was pressed");
button3Value = true;
}
} else if(me.getID() == MouseEvent.MOUSE_RELEASED) {
//ystem.out.println("Button was released");
if(me.getButton() == MouseEvent.BUTTON1) {
//System.out.println("Button 1 was released");
button1Value = false;
} else if(me.getButton() == MouseEvent.BUTTON2) {
//System.out.println("Button 2 was released");
button2Value = false;
} else if(me.getButton() == MouseEvent.BUTTON3) {
//System.out.println("Button 3 was released");
button3Value = false;
}
} else {
//System.out.println("Mouse event ID " + me.getID() + " (" + me.getClass().getName() + ")");
}
} else {
System.out.println("AWTMouse got an event of type " + event.getClass().getName());
}
}
/** Mouse ball under AWT
*/
private class AWTMouseBall extends Ball {
/** Constructs the new mouse ball
* @param x The x axis
* @param y The y axis
* @param wheel The mouse wheel axis
*/
public AWTMouseBall(Component x, Component y, Component wheel) {
super(AWTMouse.this.getName() + " ball");
this.x = x;
this.y = y;
this.wheel = wheel;
}
}
/** Mouse buttons under AWT
*/
private class AWTMouseButtons extends Buttons {
/** Creates the new mouse's buttons
* @param left Left mouse button
* @param right Right mouse button
* @param middle Middle mouse button
*/
public AWTMouseButtons(Button left, Button right, Button middle) {
super(AWTMouse.this.getName() + " buttons");
this.left = left;
this.right = right;
this.middle = middle;
}
}
/** AWT specific mouse buttons
*/
private class AWTMouseButton extends Mouse.Button {
/** The real Axis
*/
private Component realAxis;
/** Construct an AWT mouse button from the given axis
* @param axis The axis that holds the data
*/
public AWTMouseButton(Component axis) {
super(axis.getName(), (Component.Identifier.Button)axis.getIdentifier());
this.realAxis = axis;
}
/** Returns true f this axis is relative
* @return Always returns false for a mouse button
*/
public boolean isRelative() {
return false;
}
/** Returns the data for this mouse button
* @return Retursn this mouse buttons value
*/
public float getPollData(){
return realAxis.getPollData();
}
}
}
|