From b1ed5c363254778ca4e3b4f99f6a9ba40f823836 Mon Sep 17 00:00:00 2001 From: Mark Langen Date: Fri, 14 Apr 2017 17:02:27 -0600 Subject: [PATCH] Fixed deck editor Drag & Drop * Deck editor drag and drop was broken by the window borders change. Still not entirley sure what the root cause is (for some reason SwingUtilities.getDeepestComponentAt won't work correctly with the new setup), but this will fix it by expliticly specifying a deeper root component to start the search from (The MagePane that is active to be specific). --- .../client/cards/CardDraggerGlassPane.java | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/cards/CardDraggerGlassPane.java b/Mage.Client/src/main/java/mage/client/cards/CardDraggerGlassPane.java index ed9ae1f67fb..e79ea3944c4 100644 --- a/Mage.Client/src/main/java/mage/client/cards/CardDraggerGlassPane.java +++ b/Mage.Client/src/main/java/mage/client/cards/CardDraggerGlassPane.java @@ -1,6 +1,7 @@ package mage.client.cards; import mage.cards.MageCard; +import mage.client.MagePane; import mage.client.plugins.impl.Plugins; import mage.view.CardView; @@ -24,9 +25,12 @@ public class CardDraggerGlassPane implements MouseListener, MouseMotionListener private DragCardTarget currentDragTarget; private boolean isDragging; + // This should not be strictly needed, but for some reason I can't figure out getDeepestComponentAt and + // getComponentAt do not seem to work correctly for our setup if called on the root MageFrame. + private MagePane currentEventRootMagePane; + public CardDraggerGlassPane(DragCardSource source) { this.source = source; - } public void beginDrag(Component c, MouseEvent e) { @@ -46,6 +50,17 @@ public class CardDraggerGlassPane implements MouseListener, MouseMotionListener glassPane.setOpaque(false); glassPane.setVisible(true); + // Get root mage pane to handle drag targeting in + Component rootMagePane = c; + while (rootMagePane != null && !(rootMagePane instanceof MagePane)) { + rootMagePane = rootMagePane.getParent(); + } + if (rootMagePane == null) { + throw new RuntimeException("CardDraggerGlassPane::beginDrag not in a MagePane?"); + } else { + currentEventRootMagePane = (MagePane)rootMagePane; + } + // Hook up events c.addMouseListener(this); c.addMouseMotionListener(this); @@ -72,19 +87,19 @@ public class CardDraggerGlassPane implements MouseListener, MouseMotionListener // Update the target currentDragTarget = null; - updateCurrentTarget(SwingUtilities.convertMouseEvent(glassPane, e, currentRoot), false); + updateCurrentTarget(SwingUtilities.convertMouseEvent(glassPane, e, currentEventRootMagePane), false); } // e is relative to currentRoot private void updateCurrentTarget(MouseEvent e, boolean isEnding) { - Component mouseOver = SwingUtilities.getDeepestComponentAt(currentRoot.getContentPane(), e.getX(), e.getY()); + Component mouseOver = SwingUtilities.getDeepestComponentAt(currentEventRootMagePane, e.getX(), e.getY()); while (mouseOver != null) { if (mouseOver instanceof DragCardTarget) { DragCardTarget target = (DragCardTarget)mouseOver; - MouseEvent targetEvent = SwingUtilities.convertMouseEvent(currentRoot, e, mouseOver); + MouseEvent targetEvent = SwingUtilities.convertMouseEvent(currentEventRootMagePane, e, mouseOver); if (target != currentDragTarget) { if (currentDragTarget != null) { - MouseEvent oldTargetEvent = SwingUtilities.convertMouseEvent(currentRoot, e, (Component) currentDragTarget); + MouseEvent oldTargetEvent = SwingUtilities.convertMouseEvent(currentEventRootMagePane, e, (Component) currentDragTarget); currentDragTarget.dragCardExit(oldTargetEvent); } currentDragTarget = target; @@ -101,7 +116,7 @@ public class CardDraggerGlassPane implements MouseListener, MouseMotionListener mouseOver = mouseOver.getParent(); } if (currentDragTarget != null) { - MouseEvent oldTargetEvent = SwingUtilities.convertMouseEvent(currentRoot, e, (Component)currentDragTarget); + MouseEvent oldTargetEvent = SwingUtilities.convertMouseEvent(currentEventRootMagePane, e, (Component)currentDragTarget); currentDragTarget.dragCardExit(oldTargetEvent); } currentDragTarget = null; @@ -124,7 +139,7 @@ public class CardDraggerGlassPane implements MouseListener, MouseMotionListener dragComponent.removeMouseMotionListener(this); // Convert the event into root coords - e = SwingUtilities.convertMouseEvent(dragComponent, e, currentRoot); + e = SwingUtilities.convertMouseEvent(dragComponent, e, currentEventRootMagePane); // Remove the drag card glassPane.remove(dragView); @@ -144,7 +159,7 @@ public class CardDraggerGlassPane implements MouseListener, MouseMotionListener dragView.setLocation(glassE.getX(), glassE.getY()); dragView.repaint(); // Convert the event into root coords and update target - e = SwingUtilities.convertMouseEvent(dragComponent, e, currentRoot); + e = SwingUtilities.convertMouseEvent(dragComponent, e, currentEventRootMagePane); updateCurrentTarget(e, false); }