From 94fe223529169c4e6ec0649eab2f3bd3934db2d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Thu, 1 Apr 2021 00:07:26 +0200 Subject: [PATCH 1/3] Fix measurement errors when disabling token snapping while the Terrain Ruler module isn't enabled --- CHANGELOG.md | 6 ++++++ src/foundry_imports.js | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 722e611..3f4ac94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## In development +### Bugfixes +- 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/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})}); From 57ed545e30e268496b0dfb484e9f528c7eb2b4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Thu, 1 Apr 2021 22:17:25 +0200 Subject: [PATCH 2/3] Snap tiny tokens (0.5x0.5 or smaller) to the corners of the grid squares like it is done in vanilla foundry (fixes #49) --- CHANGELOG.md | 1 + src/util.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f4ac94..6906a2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## In development ### 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 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) } From fb9a2ee60cd4830ea90144ef9cd08ca74b671e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Thu, 1 Apr 2021 22:35:23 +0200 Subject: [PATCH 3/3] Release v1.4.5 --- CHANGELOG.md | 2 +- module.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6906a2e..2d6818a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## In development +## 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 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",