diff --git a/CHANGELOG.md b/CHANGELOG.md index 722e611..2d6818a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 1.4.5 +### Bugfixes +- Tiny tokens (0.5x0.5 or smaller) now snap to the coners of a square like they do in vanilla foundry ([#49](https://github.com/manuelVo/foundryvtt-drag-ruler/issues/49)) +- Fixed a bug that could cause a meausred distance to be wrong when disabling token snapping using the shift key +- Fixed a bug where the highlighted path could have gaps when disabbling token snapping using the shift key + + ## 1.4.4 ### Bugfixes - Fix snapping for tokens that are smaller than 1x1 diff --git a/module.json b/module.json index e674931..36bb15c 100644 --- a/module.json +++ b/module.json @@ -2,7 +2,7 @@ "name": "drag-ruler", "title": "Drag Ruler", "description": "When dragging a token displays a ruler showing how far you've moved that token.", - "version": "1.4.4", + "version": "1.4.5", "minimumCoreVersion" : "0.7.9", "compatibleCoreVersion" : "0.7.9", "authors": [ @@ -41,7 +41,7 @@ } ], "url": "https://github.com/manuelVo/foundryvtt-drag-ruler", - "download": "https://github.com/manuelVo/foundryvtt-drag-ruler/archive/v1.4.4.zip", + "download": "https://github.com/manuelVo/foundryvtt-drag-ruler/archive/v1.4.5.zip", "manifest": "https://raw.githubusercontent.com/manuelVo/foundryvtt-drag-ruler/master/module.json", "readme": "https://github.com/manuelVo/foundryvtt-drag-ruler/blob/master/README.md", "changelog": "https://github.com/manuelVo/foundryvtt-drag-ruler/blob/master/CHANGELOG.md", diff --git a/src/foundry_imports.js b/src/foundry_imports.js index a3c5f32..2d5d660 100644 --- a/src/foundry_imports.js +++ b/src/foundry_imports.js @@ -108,9 +108,14 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) { if (snap) destination = getSnapPointForToken(destination.x, destination.y, this.draggedToken) + const terrainRulerAvailable = game.modules.get("terrain-ruler")?.active && canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS; + const waypoints = this.waypoints.concat([destination]); // Move the waypoints to the center of the grid if a size is used that measures from edge to edge const centeredWaypoints = applyTokenSizeOffset(waypoints, this.draggedToken) + // Foundries native ruler requires the waypoints to sit in the dead center of the square to work properly + if (!terrainRulerAvailable) + centeredWaypoints.forEach(w => [w.x, w.y] = canvas.grid.getCenter(w.x, w.y)); const r = this.ruler; this.destination = destination; @@ -133,10 +138,10 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) { centeredSegments.push({ray: centeredRay, label}) } + const shape = getTokenShape(this.draggedToken) // Compute measured distance - const terrainRulerAvailable = game.modules.get("terrain-ruler")?.active && canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS let distances if (terrainRulerAvailable) distances = game.terrainRuler.measureDistances(centeredSegments, {costFunction: (x, y) => getCostFromSpeedProvider(this.draggedToken, getAreaFromPositionAndShape({x, y}, shape), {x, y})}); diff --git a/src/util.js b/src/util.js index bacb6d0..dd284b0 100644 --- a/src/util.js +++ b/src/util.js @@ -7,6 +7,9 @@ export function* zip(it1, it2) { } export function getSnapPointForToken(x, y, token) { + if (canvas.grid.type === CONST.GRID_TYPES.GRIDLESS) { + return new PIXI.Point(x, y); + } if (canvas.grid.isHex && game.modules.get("hex-size-support")?.active && CONFIG.hexSizeSupport.getAltSnappingFlag(token)) { if (token.getFlag("hex-size-support", "borderSize") % 2 === 0) { const snapPoint = CONFIG.hexSizeSupport.findVertexSnapPoint(x, y, token, canvas.grid.grid) @@ -16,9 +19,22 @@ export function getSnapPointForToken(x, y, token) { return new PIXI.Point(...canvas.grid.getCenter(x, y)) } } + // Tiny tokens can snap to the cells corners + if (!canvas.grid.isHex && token.data.width <= 0.5) { + const [topLeftX, topLeftY] = canvas.grid.getTopLeft(x, y); + const offsetX = x - topLeftX; + const offsetY = y - topLeftY; + const subGridWidth = Math.floor(canvas.grid.w / 2); + const subGridHeight = Math.floor(canvas.grid.h / 2); + const subGridPosX = Math.floor(offsetX / subGridWidth); + const subGridPosY = Math.floor(offsetY / subGridHeight); + return new PIXI.Point(topLeftX + (subGridPosX + 0.5) * subGridWidth, topLeftY + (subGridPosY + 0.5) * subGridHeight); + } + // Hex tokens, tokens with odd multipliers (1x1, 3x3, ...) and tokens smaller than 1x1 but bigger than 0.5x0.5 snap to the center of the grid cell if (canvas.grid.isHex || Math.round(token.data.width) % 2 === 1 || token.data.width < 1) { return new PIXI.Point(...canvas.grid.getCenter(x, y)) } + // All remaining tokens (those with even or fractional multipliers on square grids) snap to the intersection points of the grid const [snappedX, snappedY] = canvas.grid.getCenter(x - canvas.grid.w / 2, y - canvas.grid.h / 2) return new PIXI.Point(snappedX + canvas.grid.w / 2, snappedY + canvas.grid.h / 2) }