tmpkus / piccolo2d

Automatically exported from code.google.com/p/piccolo2d
0 stars 0 forks source link

PSwing components don't handle events across multiple cameras #26

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a few canvases that point to the same root object
2. Add some PSwing elements to the tree
3. Try clicking on the PSwing element in both canvases

What is the expected output? What do you see instead?
Only one canvas will be able to receive events due to how the
PSwingEventHandler is configured.

Here is the diff for the proposed fix:
Index: extras/edu/umd/cs/piccolox/pswing/PSwingCanvas.java
===================================================================
--- extras/edu/umd/cs/piccolox/pswing/PSwingCanvas.java (revision 24)
+++ extras/edu/umd/cs/piccolox/pswing/PSwingCanvas.java (revision 25)
@@ -22,7 +22,6 @@
  * @author Sam R. Reid
  * @author Lance E. Good
  */
-
 public class PSwingCanvas extends PCanvas {
     public static final String SWING_WRAPPER_KEY = "Swing Wrapper";

@@ -35,7 +34,6 @@
         swingWrapper = new SwingWrapper(this);
         add(swingWrapper);
         initRepaintManager();
-        new PSwingEventHandler(this, getCamera()).setActive(true);
     }

     private void initRepaintManager() {
Index: extras/edu/umd/cs/piccolox/pswing/PSwingEventHandler.java
===================================================================
--- extras/edu/umd/cs/piccolox/pswing/PSwingEventHandler.java   (revision 24)
+++ extras/edu/umd/cs/piccolox/pswing/PSwingEventHandler.java   (revision 25)
@@ -60,7 +60,26 @@
         listenNode = node;
     }

+    public void setCanvas(PSwingCanvas canvas) {
+        this.canvas = canvas;
+    }
+
     /**
+     * Changes the node that this SwingEventHandler is listening to
+     *
+     * @param listenNode PNode to listen for events on
+     */
+    public void setListenNode(PNode listenNode) {
+        if (active) {
+            setActive(false);
+            this.listenNode = listenNode;
+            setActive(true);
+        } else {
+            this.listenNode = listenNode;
+        }
+    }
+
+    /**
      * Constructs a new PSwingEventHandler for the given canvas.
      */
     public PSwingEventHandler( PSwingCanvas canvas ) {
@@ -72,14 +91,13 @@
      *
      * @param active
      */
-    void setActive( boolean active ) {
+    public void setActive(boolean active) {
         if( this.active && !active ) {
             if( listenNode != null ) {
                 this.active = false;
                 listenNode.removeInputEventListener( this );
             }
-        }
-        else if( !this.active && active ) {
+        } else if (!this.active && active) {
             if( listenNode != null ) {
                 this.active = true;
                 listenNode.addInputEventListener( this );
@@ -120,8 +138,7 @@
                     Point p = comp.getLocation();
                     if( comp instanceof Container ) {
                         comp = findShowingComponentAt( comp, x -
(int)p.getX(), y - (int)p.getY() );
-                    }
-                    else {
+                    } else {
                         comp = comp.getComponentAt( x - (int)p.getX(), y -
(int)p.getY() );
                     }
                     if( comp != null && comp.isShowing() ) {
@@ -185,11 +202,9 @@
                 if( comp != null && pSwingMouseEvent.getID() ==
MouseEvent.MOUSE_PRESSED ) {
                     if( SwingUtilities.isLeftMouseButton( pSwingMouseEvent
) ) {
                         leftButtonData.setState( swing, pickedNode, comp,
offX, offY );
-                    }
-                    else if( SwingUtilities.isMiddleMouseButton(
pSwingMouseEvent ) ) {
+                    } else if
(SwingUtilities.isMiddleMouseButton(pSwingMouseEvent)) {
                         middleButtonData.setState( swing, pickedNode,
comp, offX, offY );
-                    }
-                    else if( SwingUtilities.isRightMouseButton(
pSwingMouseEvent ) ) {
+                    } else if
(SwingUtilities.isRightMouseButton(pSwingMouseEvent)) {
                         rightButtonData.setState( swing, pickedNode, comp,
offX, offY );
                     }
                 }
@@ -208,14 +223,12 @@
             }

             // MIDDLE MOUSE BUTTON
-            if( SwingUtilities.isMiddleMouseButton( pSwingMouseEvent ) &&
middleButtonData.getFocusedComponent() != null )
-            {
+            if (SwingUtilities.isMiddleMouseButton(pSwingMouseEvent) &&
middleButtonData.getFocusedComponent() != null) {
                 handleButton( pSwingMouseEvent, aEvent, middleButtonData );
             }

             // RIGHT MOUSE BUTTON
-            if( SwingUtilities.isRightMouseButton( pSwingMouseEvent ) &&
rightButtonData.getFocusedComponent() != null )
-            {
+            if (SwingUtilities.isRightMouseButton(pSwingMouseEvent) &&
rightButtonData.getFocusedComponent() != null) {
                 handleButton( pSwingMouseEvent, aEvent, rightButtonData );
             }
         }
@@ -265,8 +278,7 @@
                 e2 = PSwingMouseEvent.createMouseEvent( e_temp.getID(),
e_temp, aEvent );
                 comp.dispatchEvent( e2 );
             }
-        }
-        else {
+        } else {
             // This means mouseEntered
             if( comp != null ) {
                 MouseEvent e_temp = createEnterEvent( comp,
pSwingMouseEvent, offX, offY );
@@ -328,11 +340,9 @@

             PSwingMouseEvent e2 = PSwingMouseEvent.createMouseEvent(
e_temp.getID(), e_temp, aEvent );
             dispatchEvent( buttonData.getFocusedComponent(), e2 );
-        }
-        else {
+        } else {
             dispatchEvent( buttonData.getFocusedComponent(), e1 );
         }
-        //buttonData.getPSwing().repaint();  //Experiment with
SliderExample (from Martin) suggests this line is unnecessary, and a
serious problem in performance.
         e1.consume();
         if( e1.getID() == MouseEvent.MOUSE_RELEASED ) {
             buttonData.mouseReleased();
Index: extras/edu/umd/cs/piccolox/pswing/PSwing.java
===================================================================
--- extras/edu/umd/cs/piccolox/pswing/PSwing.java   (revision 24)
+++ extras/edu/umd/cs/piccolox/pswing/PSwing.java   (revision 25)
@@ -200,6 +200,8 @@
         }
     };

+    private PSwingEventHandler swingEventHandler;
+
     /**
      * Constructs a new visual component wrapper for the Swing component.
      *
@@ -217,6 +219,8 @@
         });
         listenForCanvas(this);
         reshape();
+        swingEventHandler = new PSwingEventHandler(canvas, this);
+        swingEventHandler.setActive(true);
     }

     /**
@@ -569,6 +573,7 @@
             canvas = newCanvas;
             if (newCanvas != null) {
                 canvas.addPSwing(this);
+                swingEventHandler.setCanvas(canvas);
                 reshape();
                 repaint();
             }

Original issue reported on code.google.com by steveonjava on 24 Jun 2008 at 9:53

GoogleCodeExporter commented 9 years ago

Original comment by steveonjava on 25 Jun 2008 at 6:35

GoogleCodeExporter commented 9 years ago

Original comment by steveonjava on 2 Jul 2008 at 12:28

GoogleCodeExporter commented 9 years ago

Original comment by steveonjava on 3 Jul 2008 at 5:05

GoogleCodeExporter commented 9 years ago
Deferring to Milestone-2.0 per email from Steve.

Original comment by heue...@gmail.com on 27 Oct 2009 at 1:03

GoogleCodeExporter commented 9 years ago
Reverting back to New status

Original comment by heue...@gmail.com on 31 Aug 2012 at 8:19

GoogleCodeExporter commented 9 years ago

Original comment by heue...@gmail.com on 31 Aug 2012 at 8:20

GoogleCodeExporter commented 9 years ago

Original comment by heue...@gmail.com on 31 Aug 2012 at 8:22

GoogleCodeExporter commented 9 years ago

Original comment by heue...@gmail.com on 26 Nov 2013 at 11:22