diff --git a/src/foundry_imports.js b/src/foundry_imports.js index 3f26dd3..d3e9b8f 100644 --- a/src/foundry_imports.js +++ b/src/foundry_imports.js @@ -160,18 +160,27 @@ export function measure(destination, options={}) { if (isToken && !this.draggedEntity.isVisible) return [] + options.snap = options.snap ?? !disableSnap; + if (options.snap) { destination = getSnapPointForEntity(destination.x, destination.y, this.draggedEntity); } - // Remove waypoints generated by pathfinding - this.waypoints.filter(waypoint => waypoint.isPathfinding).forEach(_ => this.labels.removeChild(this.labels.children.pop())); - this.waypoints = this.waypoints.filter(waypoint => !waypoint.isPathfinding); + this.dragRulerRemovePathfindingWaypoints(); if (isToken && isPathfindingEnabled()) { let path = findPath(getGridPositionFromPixelsObj(this.waypoints[this.waypoints.length - 1]), getGridPositionFromPixelsObj(destination)); if (path) { - path = path.map(point => getCenterFromGridPositionObj(point)); + path = path.map(point => getCenterFromGridPositionObj(point)) + + // If the token is snapped to the grid, the first point of the path is already handled by the ruler + if (path[0].x === this.waypoints[this.waypoints.length - 1].x && path[0].y === this.waypoints[this.waypoints.length - 1].y) + path = path.slice(1); + + // If snapping is enabled, the last point of the path is already handled by the ruler + if (options.snap) + path = path.slice(0, path.length - 1); + for (const point of path) { point.isPathfinding = true; this.labels.addChild(new PreciseText("", CONFIG.canvasTextStyle)); diff --git a/src/ruler.js b/src/ruler.js index d5553c2..da115cb 100644 --- a/src/ruler.js +++ b/src/ruler.js @@ -98,6 +98,7 @@ export function extendRuler() { } dragRulerDeleteWaypoint(event={preventDefault: () => {return}}, options={}) { + this.dragRulerRemovePathfindingWaypoints(); options.snap = options.snap ?? true; if (this.waypoints.filter(w => !w.isPrevious).length > 1) { event.preventDefault(); @@ -114,6 +115,11 @@ export function extendRuler() { } } + dragRulerRemovePathfindingWaypoints() { + this.waypoints.filter(waypoint => waypoint.isPathfinding).forEach(_ => this.labels.removeChild(this.labels.children.pop())); + this.waypoints = this.waypoints.filter(waypoint => !waypoint.isPathfinding); + } + dragRulerAbortDrag(event={preventDefault: () => {return}}) { const token = this.draggedEntity; this._endMeasurement();