Browse Source

* 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

Sha..rd 13 years ago
parent
commit
e18d1c261c

+ 27 - 17
engine/src/desktop/com/jme3/input/awt/AwtKeyInput.java

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

+ 52 - 50
engine/src/desktop/com/jme3/input/awt/AwtMouseInput.java

@@ -89,21 +89,21 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
         centerLocationOnScreen = new Point();
         lastKnownLocation = new Point();
 
-        try{
+        try {
             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);
         }
     }
     
-    public void setInputSource(Component comp){
-        if (component != null){
+    public void setInputSource(Component comp) {
+        if (component != null) {
             component.removeMouseListener(this);
             component.removeMouseMotionListener(this);
             component.removeMouseWheelListener(this);
-            
+
             eventQueue.clear();
-            
+
             wheelPos = 0;
             isRecentering = false;
             eventsSinceRecenter = 0;
@@ -132,7 +132,7 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
         return true;
     }
 
-    public void setInputListener(RawInputListener listener){
+    public void setInputListener(RawInputListener listener) {
         this.listener = listener;
     }
 
@@ -140,25 +140,25 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
         return System.nanoTime();
     }
 
-    public void setCursorVisible(boolean visible){
-        if (this.visible != visible){
-            
+    public void setCursorVisible(boolean visible) {
+        if (this.visible != visible) {
             lastKnownLocation.x = lastKnownLocation.y = 0;
-            
+
             this.visible = visible;
             final boolean newVisible = visible;
             SwingUtilities.invokeLater(new Runnable() {
                 public void run() {
                     component.setCursor(newVisible ? null : getTransparentCursor());
-                    if (!newVisible)
+                    if (!newVisible) {
                         recenterMouse(component);
+                    }
                 }
             });
         }
     }
 
     public void update() {
-        if (cursorMoved){
+        if (cursorMoved) {
             int newX = location.x;
             int newY = location.y;
             int newWheel = wheelPos;
@@ -175,24 +175,24 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
             lastEventX = newX;
             lastEventY = newY;
             lastEventWheel = newWheel;
-            
+
             cursorMoved = false;
         }
 
-        synchronized (eventQueue){
+        synchronized (eventQueue) {
             eventQueueCopy.clear();
             eventQueueCopy.addAll(eventQueue);
             eventQueue.clear();
         }
-        
+
         int size = eventQueueCopy.size();
-        for (int i = 0; i < size; i++){
+        for (int i = 0; i < size; i++) {
             listener.onMouseButtonEvent(eventQueueCopy.get(i));
         }
     }
 
     private Cursor getTransparentCursor() {
-        if (transparentCursor == null){
+        if (transparentCursor == null) {
             BufferedImage cursorImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
             cursorImage.setRGB(0, 0, 0);
             transparentCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(0, 0), "empty cursor");
@@ -214,70 +214,72 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
         return 3;
     }
 
-    public void mouseClicked(MouseEvent arg0) {
+    public void mouseClicked(MouseEvent awtEvt) {
 //        MouseButtonEvent evt = new MouseButtonEvent(getJMEButtonIndex(arg0), false);
 //        listener.onMouseButtonEvent(evt);
     }
 
-    public void mousePressed(MouseEvent arg0) {
-        MouseButtonEvent evt = new MouseButtonEvent(getJMEButtonIndex(arg0), true, arg0.getX(), arg0.getY());
-        evt.setTime(arg0.getWhen());
-        synchronized (eventQueue){
+    public void mousePressed(MouseEvent awtEvt) {
+        MouseButtonEvent evt = new MouseButtonEvent(getJMEButtonIndex(awtEvt), true, awtEvt.getX(), awtEvt.getY());
+        evt.setTime(awtEvt.getWhen());
+        synchronized (eventQueue) {
             eventQueue.add(evt);
         }
     }
 
-    public void mouseReleased(MouseEvent arg0) {
-        MouseButtonEvent evt = new MouseButtonEvent(getJMEButtonIndex(arg0), false, arg0.getX(), arg0.getY());
-        evt.setTime(arg0.getWhen());
-        synchronized (eventQueue){
+    public void mouseReleased(MouseEvent awtEvt) {
+        MouseButtonEvent evt = new MouseButtonEvent(getJMEButtonIndex(awtEvt), false, awtEvt.getX(), awtEvt.getY());
+        evt.setTime(awtEvt.getWhen());
+        synchronized (eventQueue) {
             eventQueue.add(evt);
         }
     }
 
-    public void mouseEntered(MouseEvent arg0) {
-        if (!visible)
-            recenterMouse(arg0.getComponent());
+    public void mouseEntered(MouseEvent awtEvt) {
+        if (!visible) {
+            recenterMouse(awtEvt.getComponent());
+        }
     }
 
-    public void mouseExited(MouseEvent arg0) {
-        if (!visible)
-            recenterMouse(arg0.getComponent());
+    public void mouseExited(MouseEvent awtEvt) {
+        if (!visible) {
+            recenterMouse(awtEvt.getComponent());
+        }
     }
 
-    public void mouseWheelMoved(MouseWheelEvent arg0) {
-        int dwheel = arg0.getUnitsToScroll();
+    public void mouseWheelMoved(MouseWheelEvent awtEvt) {
+        int dwheel = awtEvt.getUnitsToScroll();
         wheelPos += dwheel * WHEEL_AMP;
         cursorMoved = true;
     }
 
-    public void mouseDragged(MouseEvent arg0) {
-        mouseMoved(arg0);
+    public void mouseDragged(MouseEvent awtEvt) {
+        mouseMoved(awtEvt);
     }
 
-    public void mouseMoved(MouseEvent arg0) {
+    public void mouseMoved(MouseEvent awtEvt) {
         if (isRecentering) {
             // 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
             // 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) {
-                lastKnownLocation.x = arg0.getX();
-                lastKnownLocation.y = arg0.getY();
+            if ((centerLocation.x == awtEvt.getX() && centerLocation.y == awtEvt.getY()) || eventsSinceRecenter++ == 5) {
+                lastKnownLocation.x = awtEvt.getX();
+                lastKnownLocation.y = awtEvt.getY();
                 isRecentering = false;
             }
         } else {
             // MHenze (cylab) Fix Issue 35:
             // Compute the delta and absolute coordinates and recenter the mouse if necessary
-            int dx = arg0.getX() - lastKnownLocation.x;
-            int dy = arg0.getY() - lastKnownLocation.y;
+            int dx = awtEvt.getX() - lastKnownLocation.x;
+            int dy = awtEvt.getY() - lastKnownLocation.y;
             location.x += dx;
             location.y += dy;
             if (!visible) {
-                recenterMouse(arg0.getComponent());
+                recenterMouse(awtEvt.getComponent());
             }
-            lastKnownLocation.x = arg0.getX();
-            lastKnownLocation.y = arg0.getY();
-            
+            lastKnownLocation.x = awtEvt.getX();
+            lastKnownLocation.y = awtEvt.getY();
+
             cursorMoved = true;
         }
     }
@@ -287,16 +289,16 @@ public class AwtMouseInput implements MouseInput, MouseListener, MouseWheelListe
         if (robot != null) {
             eventsSinceRecenter = 0;
             isRecentering = true;
-            centerLocation.setLocation(component.getWidth()/2, component.getHeight()/2);
+            centerLocation.setLocation(component.getWidth() / 2, component.getHeight() / 2);
             centerLocationOnScreen.setLocation(centerLocation);
             SwingUtilities.convertPointToScreen(centerLocationOnScreen, component);
             robot.mouseMove(centerLocationOnScreen.x, centerLocationOnScreen.y);
         }
     }
 
-    private int getJMEButtonIndex( MouseEvent arg0 ) {
+    private int getJMEButtonIndex(MouseEvent awtEvt) {
         int index;
-        switch (arg0.getButton()) {
+        switch (awtEvt.getButton()) {
             default:
             case MouseEvent.BUTTON1: //left
                 index = MouseInput.BUTTON_LEFT;