Skip to content

Commit

Permalink
Merge pull request #5033 from kwvanderlinde/bugfix/5032-jumpy-map-drag
Browse files Browse the repository at this point in the history
Fix map dragging state management in DefaultTool
  • Loading branch information
cwisniew authored Nov 14, 2024
2 parents 1872708 + 85cfb29 commit cb93818
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
58 changes: 32 additions & 26 deletions src/main/java/net/rptools/maptool/client/tool/DefaultTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
* <p>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;

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
}

/*
Expand Down Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit cb93818

Please sign in to comment.