From 85cfb293a154a78537ea9f5d0eb4d4c7fb1467ac Mon Sep 17 00:00:00 2001 From: Kenneth VanderLinde Date: Tue, 5 Nov 2024 03:39:55 -0800 Subject: [PATCH] Fix map dragging state management in DefaultTool When a drag is completed, the drag state is completely cleared so it can't be reused again later, even accidentally. This ensures that we aren't affected by the strange case of a right-button drag event comming in before the right-button press event. --- .../maptool/client/tool/DefaultTool.java | 58 ++++++++++--------- .../maptool/client/tool/PointerTool.java | 2 +- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/tool/DefaultTool.java b/src/main/java/net/rptools/maptool/client/tool/DefaultTool.java index d7d060826d..3f5ce05c10 100644 --- a/src/main/java/net/rptools/maptool/client/tool/DefaultTool.java +++ b/src/main/java/net/rptools/maptool/client/tool/DefaultTool.java @@ -14,10 +14,12 @@ */ package net.rptools.maptool.client.tool; +import java.awt.Point; import java.awt.dnd.DragSource; import java.awt.event.*; import java.util.Map; import java.util.Set; +import javax.annotation.Nullable; import javax.swing.*; import net.rptools.maptool.client.AppPreferences; import net.rptools.maptool.client.AppState; @@ -43,16 +45,22 @@ public abstract class DefaultTool extends Tool private Zone.Layer selectedLayer; private boolean isDraggingMap; - private int dragStartX; - private int dragStartY; + + /** + * The origin point for a map drag, or {@code null} if there is no drag possible. + * + *

Will be non-null when a drag is possible (right button pressed). To check whether the drag + * is actually happening, use {@link #isDraggingMap()}. This field will be non-{@code null} + * whenever {@link #isDraggingMap} is {@code true}, but it is also possible to be non-{@code null} + * even if {@link #isDraggingMap} is {@code false}. + */ + private @Nullable Point mapDragStart; + private int dragThreshold = DragSource.getDragThreshold(); protected int mouseX; protected int mouseY; - // This is to manage overflowing of map move events (keep things snappy) - private int mapDX, mapDY; - // TBD private boolean isTouchScreen = false; @@ -87,10 +95,16 @@ protected void detachFrom(ZoneRenderer renderer) { super.detachFrom(renderer); } - public boolean isDraggingMap() { + protected boolean isDraggingMap() { return isDraggingMap; } + /** Stop dragging the map. */ + protected void cancelMapDrag() { + mapDragStart = null; + isDraggingMap = false; + } + protected void repaintZone() { renderer.repaint(); } @@ -198,24 +212,18 @@ public void mousePressed(MouseEvent e) { * @param y the y coordinate of the drag start */ public void setDragStart(int x, int y) { - dragStartX = x; - dragStartY = y; + mapDragStart = new Point(x, y); } @Override public void mouseReleased(MouseEvent e) { - if (isDraggingMap && isRightMouseButton(e)) { - renderer.maybeForcePlayersView(); - } - // Cleanup - isDraggingMap = false; - } + if (isRightMouseButton(e) && mapDragStart != null) { + if (isDraggingMap) { + renderer.maybeForcePlayersView(); + } - /** - * @param isDraggingMap whether the user drags the map - */ - void setDraggingMap(boolean isDraggingMap) { - this.isDraggingMap = isDraggingMap; + cancelMapDrag(); + } } /* @@ -277,20 +285,18 @@ public void mouseDragged(MouseEvent e) { MapTool.getFrame().getCoordinateStatusBar().clear(); } // MAP MOVEMENT - if (isRightMouseButton(e)) { - - mapDX += mX - dragStartX; - mapDY += mY - dragStartY; + // Sometimes the mousePressed() event can come after the first mouseDragged() event when the + // right button is pressed. So check that we are actually intending to drag the map. + if (isRightMouseButton(e) && mapDragStart != null) { + var mapDX = mX - mapDragStart.x; + var mapDY = mY - mapDragStart.y; if (mapDX * mapDX + mapDY * mapDY > dragThreshold * dragThreshold) { isDraggingMap = true; } setDragStart(mX, mY); - renderer.moveViewBy(mapDX, mapDY); - mapDX = 0; - mapDY = 0; } } diff --git a/src/main/java/net/rptools/maptool/client/tool/PointerTool.java b/src/main/java/net/rptools/maptool/client/tool/PointerTool.java index dab3c32092..26c6263030 100644 --- a/src/main/java/net/rptools/maptool/client/tool/PointerTool.java +++ b/src/main/java/net/rptools/maptool/client/tool/PointerTool.java @@ -495,7 +495,7 @@ public void mouseReleased(MouseEvent e) { // WAYPOINT if (SwingUtilities.isRightMouseButton(e) && tokenDragOp != null) { tokenDragOp.setWaypoint(); - setDraggingMap(false); // We no longer drag the map. Fixes bug #616 + cancelMapDrag(); // We no longer drag the map. Fixes bug #616 return; }