Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix map dragging state management in DefaultTool #5033

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading