aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/awt/src/net/java/games/input/AWTMouse.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/awt/src/net/java/games/input/AWTMouse.java')
-rw-r--r--plugins/awt/src/net/java/games/input/AWTMouse.java321
1 files changed, 173 insertions, 148 deletions
diff --git a/plugins/awt/src/net/java/games/input/AWTMouse.java b/plugins/awt/src/net/java/games/input/AWTMouse.java
index ed76ed6..37c8eee 100644
--- a/plugins/awt/src/net/java/games/input/AWTMouse.java
+++ b/plugins/awt/src/net/java/games/input/AWTMouse.java
@@ -33,165 +33,190 @@ import java.awt.event.AWTEventListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
+import java.util.List;
+import java.util.ArrayList;
+
+import java.io.IOException;
+
/**
* @author Jeremy
- *
- * TODO To change the template for this generated type comment go to
- * Window - Preferences - Java - Code Style - Code Templates
+ * @author elias
*/
-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);
+final class AWTMouse extends Mouse implements AWTEventListener {
+ private final static int EVENT_X = 1;
+ private final static int EVENT_Y = 2;
+ private final static int EVENT_BUTTON = 4;
+
+ private final List awt_events = new ArrayList();
+ private final List processed_awt_events = new ArrayList();
+
+ private int event_state = EVENT_X;
+
+ protected AWTMouse() {
+ super("AWTMouse", createComponents(), new Controller[]{}, new Rumbler[]{});
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;
- }
+ private final static Component[] createComponents() {
+ return new Component[]{new Axis(Component.Identifier.Axis.X),
+ new Axis(Component.Identifier.Axis.Y),
+ new Axis(Component.Identifier.Axis.Z),
+ new Button(Component.Identifier.Button.LEFT),
+ new Button(Component.Identifier.Button.MIDDLE),
+ new Button(Component.Identifier.Button.RIGHT)};
+ }
- /* (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) {
+ private final void processButtons(int button_enum, float value) {
+ Button button = getButton(button_enum);
+ if (button != null)
+ button.setValue(value);
+ }
+
+ private final Button getButton(int button_enum) {
+ switch (button_enum) {
+ case MouseEvent.BUTTON1:
+ return (Button)getLeft();
+ case MouseEvent.BUTTON2:
+ return (Button)getMiddle();
+ case MouseEvent.BUTTON3:
+ return (Button)getRight();
+ case MouseEvent.NOBUTTON:
+ default:
+ // Unknown button
+ return null;
+ }
+ }
+
+ private final void processEvent(AWTEvent event) throws IOException {
+ if (event instanceof MouseWheelEvent) {
MouseWheelEvent mwe = (MouseWheelEvent)event;
- scrollAmount += mwe.getWheelRotation();
- //System.out.println("New scroll amount: " + scrollAmount);
- }
- if(event instanceof MouseEvent) {
+ Axis wheel = (Axis)getWheel();
+ wheel.setValue(wheel.poll() + mwe.getWheelRotation());
+ } else 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());
- }
- }
+ Axis x = (Axis)getX();
+ Axis y = (Axis)getY();
+ x.setValue(me.getX());
+ y.setValue(me.getY());
+ switch (me.getID()) {
+ case MouseEvent.MOUSE_PRESSED:
+ processButtons(me.getButton(), 1f);
+ break;
+ case MouseEvent.MOUSE_RELEASED:
+ processButtons(me.getButton(), 0f);
+ break;
+ default:
+ break;
+ }
+ }
+ }
- /** 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;
+ public final synchronized void pollDevice() throws IOException {
+ Axis wheel = (Axis)getWheel();
+ wheel.setValue(0);
+ for (int i = 0; i < awt_events.size(); i++) {
+ AWTEvent event = (AWTEvent)awt_events.get(i);
+ processEvent(event);
+ processed_awt_events.add(event);
}
- }
+ awt_events.clear();
+ }
- /** 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;
- }
- }
+ protected final synchronized boolean getNextDeviceEvent(Event event) throws IOException {
+ while (true) {
+ if (processed_awt_events.isEmpty())
+ return false;
+ AWTEvent awt_event = (AWTEvent)processed_awt_events.get(0);
+ if (awt_event instanceof MouseWheelEvent) {
+ MouseWheelEvent awt_wheel_event = (MouseWheelEvent)awt_event;
+ long nanos = awt_wheel_event.getWhen()*1000000L;
+ event.set(getWheel(), awt_wheel_event.getWheelRotation(), nanos);
+ processed_awt_events.remove(0);
+ } else if (awt_event instanceof MouseEvent) {
+ MouseEvent mouse_event = (MouseEvent)awt_event;
+ long nanos = mouse_event.getWhen()*1000000L;
+ switch (event_state) {
+ case EVENT_X:
+ event_state = EVENT_Y;
+ event.set(getX(), mouse_event.getX(), nanos);
+ return true;
+ case EVENT_Y:
+ event_state = EVENT_BUTTON;
+ event.set(getY(), mouse_event.getY(), nanos);
+ return true;
+ case EVENT_BUTTON:
+ processed_awt_events.remove(0);
+ event_state = EVENT_X;
+ Button button = getButton(mouse_event.getButton());
+ if (button != null) {
+ switch (mouse_event.getID()) {
+ case MouseEvent.MOUSE_PRESSED:
+ event.set(button, 1f, nanos);
+ return true;
+ case MouseEvent.MOUSE_RELEASED:
+ event.set(button, 0f, nanos);
+ return true;
+ default:
+ break;
+ }
+ }
+ break;
+ default:
+ throw new RuntimeException("Unknown event state: " + event_state);
+ }
+ }
+ }
+ }
- /** 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();
- }
- }
+ public final synchronized void eventDispatched(AWTEvent event) {
+ awt_events.add(event);
+ }
+
+ final static class Axis extends AbstractComponent {
+ private float value;
+
+ public Axis(Component.Identifier.Axis axis_id) {
+ super(axis_id.getName(), axis_id);
+ }
+
+ public final boolean isRelative() {
+ return false;
+ }
+
+ public final boolean isAnalog() {
+ return true;
+ }
+
+ protected final void setValue(float value) {
+ this.value = value;
+ }
+
+ protected final float poll() throws IOException {
+ return value;
+ }
+ }
+
+ final static class Button extends AbstractComponent {
+ private float value;
+
+ public Button(Component.Identifier.Button button_id) {
+ super(button_id.getName(), button_id);
+ }
+
+ protected final void setValue(float value) {
+ this.value = value;
+ }
+
+ protected final float poll() throws IOException {
+ return value;
+ }
+
+ public final boolean isAnalog() {
+ return false;
+ }
+
+ public final boolean isRelative() {
+ return false;
+ }
+ }
}