From 4d2e4b77151c32ab7c54b3d8dfe31d53059fafe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Fri, 21 May 2021 14:41:33 +0200 Subject: [PATCH] Calculate the correct snappig for measured templates when placing a waypoint --- CHANGELOG.md | 1 + src/foundry_imports.js | 7 ++----- src/ruler.js | 7 ++++--- src/util.js | 8 ++++++++ 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf72ad5..95c57cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## In development ### Bugfixes +- Fixed a bug that prevented waypoints for measurement templates from snapping to any other point than a grid cell corner (or grid cell center on hex) - Fixed a bug that could cause the ruler to not end up at the token's center (especially if the token is being moved very quickly and then stopped abruptly) diff --git a/src/foundry_imports.js b/src/foundry_imports.js index 0bfcf27..5a33b4a 100644 --- a/src/foundry_imports.js +++ b/src/foundry_imports.js @@ -4,7 +4,7 @@ import {Line} from "./geometry.js"; import {getColorForDistance} from "./main.js" import {trackRays} from "./movement_tracking.js" import {recalculate} from "./socket.js"; -import {applyTokenSizeOffset, getSnapPointForMeasuredTemplate, getSnapPointForToken, getTokenShape, highlightTokenShape, zip} from "./util.js"; +import {applyTokenSizeOffset, getSnapPointForEntity, getSnapPointForToken, getTokenShape, highlightTokenShape, zip} from "./util.js"; // This is a modified version of Ruler.moveToken from foundry 0.7.9 export async function moveEntities(draggedEntity, selectedEntities) { @@ -144,10 +144,7 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) { return [] if (snap) { - if (isToken) - destination = getSnapPointForToken(destination.x, destination.y, this.draggedEntity); - else - destination = getSnapPointForMeasuredTemplate(destination.x, destination.y); + destination = getSnapPointForEntity(destination.x, destination.y, this.draggedEntity); } const terrainRulerAvailable = isToken && game.modules.get("terrain-ruler")?.active && (!game.modules.get("TerrainLayer")?.active || canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS); diff --git a/src/ruler.js b/src/ruler.js index 1f75f02..2a8b751 100644 --- a/src/ruler.js +++ b/src/ruler.js @@ -1,7 +1,7 @@ import {measure} from "./foundry_imports.js" import {getMovementHistory} from "./movement_tracking.js"; import {settingsKey} from "./settings.js"; -import {getSnapPointForToken} from "./util.js"; +import {getSnapPointForEntity} from "./util.js"; export class DragRulerRuler extends Ruler { // Functions below are overridden versions of functions in Ruler @@ -65,8 +65,9 @@ export class DragRulerRuler extends Ruler { // The functions below aren't present in the orignal Ruler class and are added by Drag Ruler dragRulerAddWaypoint(point, snap=true) { - if (snap) - point = getSnapPointForToken(point.x, point.y, this.draggedEntity); + if (snap) { + point = getSnapPointForEntity(point.x, point.y, this.draggedEntity); + } this.waypoints.push(new PIXI.Point(point.x, point.y)); this.labels.addChild(new PreciseText("", CONFIG.canvasTextStyle)); } diff --git a/src/util.js b/src/util.js index fd61907..0d78478 100644 --- a/src/util.js +++ b/src/util.js @@ -62,6 +62,14 @@ export function getSnapPointForMeasuredTemplate(x, y) { return new PIXI.Point(snappedX, snappedY); } +export function getSnapPointForEntity(x, y, entity) { + const isToken = entity instanceof Token; + if (isToken) + return getSnapPointForToken(x, y, entity); + else + return getSnapPointForMeasuredTemplate(x, y); +} + export function highlightTokenShape(position, shape, color, alpha) { const layer = canvas.grid.highlightLayers[this.name]; if ( !layer )