Skip to content

Commit

Permalink
Merge pull request #5098 from kwvanderlinde/fixup/5089-selection-set-…
Browse files Browse the repository at this point in the history
…robustness

Simplify SelectionSet null checks case work
  • Loading branch information
cwisniew authored Dec 11, 2024
2 parents 618652f + fdc7b56 commit f8d0672
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public class RenderPathWorker extends SwingWorker<Void, Void> {
// private static final Logger log = LogManager.getLogger(RenderPathWorker.class);

ZoneRenderer zoneRenderer;
ZoneWalker walker;
@Nonnull ZoneWalker walker;
CellPoint startPoint, endPoint;
private final boolean restrictMovement;
private final Set<TerrainModifierOperation> terrainModifiersIgnored;
private final @Nonnull Token keyToken;

public RenderPathWorker(
ZoneWalker walker,
@Nonnull ZoneWalker walker,
CellPoint endPoint,
boolean restrictMovement,
Set<TerrainModifierOperation> terrainModifiersIgnored,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.ui.zone.RenderPathWorker;
import net.rptools.maptool.client.walker.ZoneWalker;
Expand All @@ -31,12 +32,13 @@
public class SelectionSet {

private final ZoneRenderer renderer;
private final Grid grid;
private final Logger log = LogManager.getLogger(SelectionSet.class);

private final Set<GUID> selectionSet = new HashSet<GUID>();
private final GUID keyToken;
private final String playerId;
private ZoneWalker walker;
private @Nullable ZoneWalker walker;
private final Token token;

private Path<ZonePoint> gridlessPath;
Expand Down Expand Up @@ -69,12 +71,13 @@ public SelectionSet(
startPoint = new ZonePoint(anchorPoint);
currentPoint = new ZonePoint(anchorPoint);

if (token.isSnapToGrid() && renderer.zone.getGrid().getCapabilities().isSnapToGridSupported()) {
if (renderer.zone.getGrid().getCapabilities().isPathingSupported()) {
CellPoint tokenPoint = renderer.zone.getGrid().convert(currentPoint);
grid = renderer.getZone().getGrid();
if (token.isSnapToGrid() && grid.getCapabilities().isSnapToGridSupported()) {
if (grid.getCapabilities().isPathingSupported()) {
CellPoint tokenPoint = grid.convert(currentPoint);

walker = renderer.zone.getGrid().createZoneWalker();
walker.setFootprint(token.getFootprint(renderer.zone.getGrid()));
walker = grid.createZoneWalker();
walker.setFootprint(token.getFootprint(grid));
walker.setWaypoints(tokenPoint, tokenPoint);
}
} else {
Expand Down Expand Up @@ -114,29 +117,32 @@ public boolean contains(Token token) {

/** Aborts the movement for this selection. */
public void cancel() {
walker.close();
if (walker != null) {
walker.close();

renderPathTask.cancel(true);
if (renderPathTask != null) {
renderPathTask.cancel(true);
}
}
}

// This is called when movement is committed/done. It'll let the last thread either finish or
// timeout
public void renderFinalPath() {
walker.close();

if (renderer.zone.getGrid().getCapabilities().isPathingSupported()
&& token.isSnapToGrid()
&& renderPathTask != null) {

log.trace("Waiting on Path Rendering... ");
while (true) {
try {
renderPathTask.get();
break;
} catch (InterruptedException e) {
} catch (ExecutionException e) {
log.error("Error while waiting for task to finish", e);
break;
if (walker != null) {
walker.close();

if (renderPathTask != null) {
log.trace("Waiting on Path Rendering... ");
while (true) {
try {
renderPathTask.get();
break;
} catch (InterruptedException e) {
} catch (ExecutionException e) {
log.error("Error while waiting for task to finish", e);
break;
}
}
}
}
Expand All @@ -146,8 +152,8 @@ public void update(ZonePoint newAnchorPosition) {
currentPoint.x = newAnchorPosition.x;
currentPoint.y = newAnchorPosition.y;

if (renderer.zone.getGrid().getCapabilities().isPathingSupported() && token.isSnapToGrid()) {
CellPoint point = renderer.zone.getGrid().convert(currentPoint);
if (walker != null) {
CellPoint point = grid.convert(currentPoint);
// walker.replaceLastWaypoint(point, restrictMovement); // OLD WAY

// New way threaded, off the swing UI thread...
Expand All @@ -174,8 +180,8 @@ public void update(ZonePoint newAnchorPosition) {
* @param location The point where the waypoint is toggled.
*/
public void toggleWaypoint(ZonePoint location) {
if (walker != null && token.isSnapToGrid() && renderer.getZone().getGrid() != null) {
walker.toggleWaypoint(renderer.getZone().getGrid().convert(location));
if (walker != null) {
walker.toggleWaypoint(grid.convert(location));
} else {
gridlessPath.appendWaypoint(location);
}
Expand All @@ -189,17 +195,14 @@ public void toggleWaypoint(ZonePoint location) {
*/
public ZonePoint getLastWaypoint() {
ZonePoint zp;
if (walker != null && token.isSnapToGrid() && renderer.getZone().getGrid() != null) {
if (walker != null) {
CellPoint cp = walker.getLastPoint();

if (cp == null) {
// log.info("cellpoint is null! FIXME! You have Walker class updating outside of
// thread..."); // Why not save last waypoint to this class?
cp = renderer.zone.getGrid().convert(token.getDragAnchor(renderer.zone));
// log.info("So I set it to: " + cp);
cp = grid.convert(token.getDragAnchor(renderer.zone));
}

zp = renderer.getZone().getGrid().convert(cp);
zp = grid.convert(cp);
} else {
// Gridless path will never be empty if set.
zp = gridlessPath.getWayPointList().getLast();
Expand Down

0 comments on commit f8d0672

Please sign in to comment.