* Prevent ActionListener.onAction( ... , true ) from being called repeatedly when using AWT panels

* Formatting in AwtMouseInput

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9426 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
Sha..rd 13 years ago
parent 4fe9fdf1f1
commit e18d1c261c
  1. 42
      engine/src/desktop/com/jme3/input/awt/AwtKeyInput.java
  2. 90
      engine/src/desktop/com/jme3/input/awt/AwtMouseInput.java

@ -39,6 +39,8 @@ import java.awt.Component;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.BitSet;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
@ -55,6 +57,7 @@ public class AwtKeyInput implements KeyInput, KeyListener {
private final ArrayList<KeyInputEvent> eventQueue = new ArrayList<KeyInputEvent>(); private final ArrayList<KeyInputEvent> eventQueue = new ArrayList<KeyInputEvent>();
private RawInputListener listener; private RawInputListener listener;
private Component component; private Component component;
private BitSet keyStateSet = new BitSet(0xFF);
public AwtKeyInput(){ public AwtKeyInput(){
} }
@ -70,6 +73,7 @@ public class AwtKeyInput implements KeyInput, KeyListener {
if (component != null){ if (component != null){
component.removeKeyListener(this); component.removeKeyListener(this);
eventQueue.clear(); eventQueue.clear();
keyStateSet.clear();
} }
component = comp; component = comp;
component.addKeyListener(this); component.addKeyListener(this);
@ -104,29 +108,35 @@ public class AwtKeyInput implements KeyInput, KeyListener {
public void keyTyped(KeyEvent evt) { public void keyTyped(KeyEvent evt) {
// key code is zero for typed events // key code is zero for typed events
// int code = 0;
// KeyInputEvent keyEvent = new KeyInputEvent(code, evt.getKeyChar(), false, true);
// keyEvent.setTime(evt.getWhen());
// synchronized (eventQueue){
// eventQueue.add(keyEvent);
// }
} }
public void keyPressed(KeyEvent evt) { public void keyPressed(KeyEvent evt) {
int code = convertAwtKey(evt.getKeyCode()); int code = convertAwtKey(evt.getKeyCode());
KeyInputEvent keyEvent = new KeyInputEvent(code, evt.getKeyChar(), true, false);
keyEvent.setTime(evt.getWhen()); // Check if key was already pressed
synchronized (eventQueue){ if (!keyStateSet.get(code)){
eventQueue.add(keyEvent); keyStateSet.set(code);
KeyInputEvent keyEvent = new KeyInputEvent(code, evt.getKeyChar(), true, false);
keyEvent.setTime(evt.getWhen());
synchronized (eventQueue){
eventQueue.add(keyEvent);
}
System.out.println(evt);
} }
} }
public void keyReleased(KeyEvent evt) { public void keyReleased(KeyEvent evt) {
int code = convertAwtKey(evt.getKeyCode()); int code = convertAwtKey(evt.getKeyCode());
KeyInputEvent keyEvent = new KeyInputEvent(code, evt.getKeyChar(), false, false);
keyEvent.setTime(evt.getWhen()); // Check if key was already released
synchronized (eventQueue){ if (keyStateSet.get(code)) {
eventQueue.add(keyEvent); keyStateSet.clear(code);
KeyInputEvent keyEvent = new KeyInputEvent(code, evt.getKeyChar(), false, false);
keyEvent.setTime(evt.getWhen());
synchronized (eventQueue){
eventQueue.add(keyEvent);
}
System.out.println(evt);
} }
} }
@ -365,7 +375,7 @@ public class AwtKeyInput implements KeyInput, KeyListener {
case KEY_RMENU: case KEY_RMENU:
return KeyEvent.VK_ALT; //todo: location right return KeyEvent.VK_ALT; //todo: location right
} }
logger.warning("unsupported key:" + key); logger.log(Level.WARNING, "unsupported key:{0}", key);
return 0x10000 + key; return 0x10000 + key;
} }
@ -595,7 +605,7 @@ public class AwtKeyInput implements KeyInput, KeyListener {
return KEY_RCONTROL; return KEY_RCONTROL;
} }
logger.warning( "unsupported key:" + key ); logger.log( Level.WARNING, "unsupported key:{0}", key);
if ( key >= 0x10000 ) { if ( key >= 0x10000 ) {
return key - 0x10000; return key - 0x10000;
} }

@ -89,15 +89,15 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
centerLocationOnScreen = new Point(); centerLocationOnScreen = new Point();
lastKnownLocation = new Point(); lastKnownLocation = new Point();
try{ try {
robot = new Robot(); robot = new Robot();
}catch (java.awt.AWTException e){ } catch (java.awt.AWTException e) {
logger.log(Level.SEVERE, "Could not create a robot, so the mouse cannot be grabbed! ", e); logger.log(Level.SEVERE, "Could not create a robot, so the mouse cannot be grabbed! ", e);
} }
} }
public void setInputSource(Component comp){ public void setInputSource(Component comp) {
if (component != null){ if (component != null) {
component.removeMouseListener(this); component.removeMouseListener(this);
component.removeMouseMotionListener(this); component.removeMouseMotionListener(this);
component.removeMouseWheelListener(this); component.removeMouseWheelListener(this);
@ -132,7 +132,7 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
return true; return true;
} }
public void setInputListener(RawInputListener listener){ public void setInputListener(RawInputListener listener) {
this.listener = listener; this.listener = listener;
} }
@ -140,9 +140,8 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
return System.nanoTime(); return System.nanoTime();
} }
public void setCursorVisible(boolean visible){ public void setCursorVisible(boolean visible) {
if (this.visible != visible){ if (this.visible != visible) {
lastKnownLocation.x = lastKnownLocation.y = 0; lastKnownLocation.x = lastKnownLocation.y = 0;
this.visible = visible; this.visible = visible;
@ -150,15 +149,16 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {
public void run() { public void run() {
component.setCursor(newVisible ? null : getTransparentCursor()); component.setCursor(newVisible ? null : getTransparentCursor());
if (!newVisible) if (!newVisible) {
recenterMouse(component); recenterMouse(component);
}
} }
}); });
} }
} }
public void update() { public void update() {
if (cursorMoved){ if (cursorMoved) {
int newX = location.x; int newX = location.x;
int newY = location.y; int newY = location.y;
int newWheel = wheelPos; int newWheel = wheelPos;
@ -179,20 +179,20 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
cursorMoved = false; cursorMoved = false;
} }
synchronized (eventQueue){ synchronized (eventQueue) {
eventQueueCopy.clear(); eventQueueCopy.clear();
eventQueueCopy.addAll(eventQueue); eventQueueCopy.addAll(eventQueue);
eventQueue.clear(); eventQueue.clear();
} }
int size = eventQueueCopy.size(); int size = eventQueueCopy.size();
for (int i = 0; i < size; i++){ for (int i = 0; i < size; i++) {
listener.onMouseButtonEvent(eventQueueCopy.get(i)); listener.onMouseButtonEvent(eventQueueCopy.get(i));
} }
} }
private Cursor getTransparentCursor() { private Cursor getTransparentCursor() {
if (transparentCursor == null){ if (transparentCursor == null) {
BufferedImage cursorImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); BufferedImage cursorImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
cursorImage.setRGB(0, 0, 0); cursorImage.setRGB(0, 0, 0);
transparentCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(0, 0), "empty cursor"); transparentCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(0, 0), "empty cursor");
@ -214,69 +214,71 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
return 3; return 3;
} }
public void mouseClicked(MouseEvent arg0) { public void mouseClicked(MouseEvent awtEvt) {
// MouseButtonEvent evt = new MouseButtonEvent(getJMEButtonIndex(arg0), false); // MouseButtonEvent evt = new MouseButtonEvent(getJMEButtonIndex(arg0), false);
// listener.onMouseButtonEvent(evt); // listener.onMouseButtonEvent(evt);
} }
public void mousePressed(MouseEvent arg0) { public void mousePressed(MouseEvent awtEvt) {
MouseButtonEvent evt = new MouseButtonEvent(getJMEButtonIndex(arg0), true, arg0.getX(), arg0.getY()); MouseButtonEvent evt = new MouseButtonEvent(getJMEButtonIndex(awtEvt), true, awtEvt.getX(), awtEvt.getY());
evt.setTime(arg0.getWhen()); evt.setTime(awtEvt.getWhen());
synchronized (eventQueue){ synchronized (eventQueue) {
eventQueue.add(evt); eventQueue.add(evt);
} }
} }
public void mouseReleased(MouseEvent arg0) { public void mouseReleased(MouseEvent awtEvt) {
MouseButtonEvent evt = new MouseButtonEvent(getJMEButtonIndex(arg0), false, arg0.getX(), arg0.getY()); MouseButtonEvent evt = new MouseButtonEvent(getJMEButtonIndex(awtEvt), false, awtEvt.getX(), awtEvt.getY());
evt.setTime(arg0.getWhen()); evt.setTime(awtEvt.getWhen());
synchronized (eventQueue){ synchronized (eventQueue) {
eventQueue.add(evt); eventQueue.add(evt);
} }
} }
public void mouseEntered(MouseEvent arg0) { public void mouseEntered(MouseEvent awtEvt) {
if (!visible) if (!visible) {
recenterMouse(arg0.getComponent()); recenterMouse(awtEvt.getComponent());
}
} }
public void mouseExited(MouseEvent arg0) { public void mouseExited(MouseEvent awtEvt) {
if (!visible) if (!visible) {
recenterMouse(arg0.getComponent()); recenterMouse(awtEvt.getComponent());
}
} }
public void mouseWheelMoved(MouseWheelEvent arg0) { public void mouseWheelMoved(MouseWheelEvent awtEvt) {
int dwheel = arg0.getUnitsToScroll(); int dwheel = awtEvt.getUnitsToScroll();
wheelPos += dwheel * WHEEL_AMP; wheelPos += dwheel * WHEEL_AMP;
cursorMoved = true; cursorMoved = true;
} }
public void mouseDragged(MouseEvent arg0) { public void mouseDragged(MouseEvent awtEvt) {
mouseMoved(arg0); mouseMoved(awtEvt);
} }
public void mouseMoved(MouseEvent arg0) { public void mouseMoved(MouseEvent awtEvt) {
if (isRecentering) { if (isRecentering) {
// MHenze (cylab) Fix Issue 35: // MHenze (cylab) Fix Issue 35:
// As long as the MouseInput is in recentering mode, nothing is done until the mouse is entered in the component // As long as the MouseInput is in recentering mode, nothing is done until the mouse is entered in the component
// by the events generated by the robot. If this happens, the last known location is resetted. // by the events generated by the robot. If this happens, the last known location is resetted.
if ((centerLocation.x == arg0.getX() && centerLocation.y == arg0.getY()) || eventsSinceRecenter++ == 5) { if ((centerLocation.x == awtEvt.getX() && centerLocation.y == awtEvt.getY()) || eventsSinceRecenter++ == 5) {
lastKnownLocation.x = arg0.getX(); lastKnownLocation.x = awtEvt.getX();
lastKnownLocation.y = arg0.getY(); lastKnownLocation.y = awtEvt.getY();
isRecentering = false; isRecentering = false;
} }
} else { } else {
// MHenze (cylab) Fix Issue 35: // MHenze (cylab) Fix Issue 35:
// Compute the delta and absolute coordinates and recenter the mouse if necessary // Compute the delta and absolute coordinates and recenter the mouse if necessary
int dx = arg0.getX() - lastKnownLocation.x; int dx = awtEvt.getX() - lastKnownLocation.x;
int dy = arg0.getY() - lastKnownLocation.y; int dy = awtEvt.getY() - lastKnownLocation.y;
location.x += dx; location.x += dx;
location.y += dy; location.y += dy;
if (!visible) { if (!visible) {
recenterMouse(arg0.getComponent()); recenterMouse(awtEvt.getComponent());
} }
lastKnownLocation.x = arg0.getX(); lastKnownLocation.x = awtEvt.getX();
lastKnownLocation.y = arg0.getY(); lastKnownLocation.y = awtEvt.getY();
cursorMoved = true; cursorMoved = true;
} }
@ -287,16 +289,16 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
if (robot != null) { if (robot != null) {
eventsSinceRecenter = 0; eventsSinceRecenter = 0;
isRecentering = true; isRecentering = true;
centerLocation.setLocation(component.getWidth()/2, component.getHeight()/2); centerLocation.setLocation(component.getWidth() / 2, component.getHeight() / 2);
centerLocationOnScreen.setLocation(centerLocation); centerLocationOnScreen.setLocation(centerLocation);
SwingUtilities.convertPointToScreen(centerLocationOnScreen, component); SwingUtilities.convertPointToScreen(centerLocationOnScreen, component);
robot.mouseMove(centerLocationOnScreen.x, centerLocationOnScreen.y); robot.mouseMove(centerLocationOnScreen.x, centerLocationOnScreen.y);
} }
} }
private int getJMEButtonIndex( MouseEvent arg0 ) { private int getJMEButtonIndex(MouseEvent awtEvt) {
int index; int index;
switch (arg0.getButton()) { switch (awtEvt.getButton()) {
default: default:
case MouseEvent.BUTTON1: //left case MouseEvent.BUTTON1: //left
index = MouseInput.BUTTON_LEFT; index = MouseInput.BUTTON_LEFT;

Loading…
Cancel
Save