* 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. 26
      engine/src/desktop/com/jme3/input/awt/AwtKeyInput.java
  2. 62
      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,30 +108,36 @@ 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());
// Check if key was already pressed
if (!keyStateSet.get(code)){
keyStateSet.set(code);
KeyInputEvent keyEvent = new KeyInputEvent(code, evt.getKeyChar(), true, false); KeyInputEvent keyEvent = new KeyInputEvent(code, evt.getKeyChar(), true, false);
keyEvent.setTime(evt.getWhen()); keyEvent.setTime(evt.getWhen());
synchronized (eventQueue){ synchronized (eventQueue){
eventQueue.add(keyEvent); 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());
// Check if key was already released
if (keyStateSet.get(code)) {
keyStateSet.clear(code);
KeyInputEvent keyEvent = new KeyInputEvent(code, evt.getKeyChar(), false, false); KeyInputEvent keyEvent = new KeyInputEvent(code, evt.getKeyChar(), false, false);
keyEvent.setTime(evt.getWhen()); keyEvent.setTime(evt.getWhen());
synchronized (eventQueue){ synchronized (eventQueue){
eventQueue.add(keyEvent); 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;
} }

@ -142,7 +142,6 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
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,9 +149,10 @@ 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);
} }
}
}); });
} }
} }
@ -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;
} }
@ -294,9 +296,9 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
} }
} }
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