From 1f2c70a6122eb8a1bc03825a925feebc887c6497 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Sat, 19 Jun 2021 09:36:00 +1000 Subject: [PATCH] Change measure tool to use full snapping to grid center and add exact euclidean measurements --- src/components/map/MapMeasure.js | 5 ++++- src/helpers/grid.js | 9 +++------ src/hooks/useGridSnapping.js | 13 ++++++++++--- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/components/map/MapMeasure.js b/src/components/map/MapMeasure.js index f9318c9..5e5acc6 100644 --- a/src/components/map/MapMeasure.js +++ b/src/components/map/MapMeasure.js @@ -44,7 +44,10 @@ function MapMeasure({ map, active }) { const gridScale = parseGridScale(active && grid.measurement.scale); - const snapPositionToGrid = useGridSnapping(); + const snapPositionToGrid = useGridSnapping( + grid.measurement.type === "euclidean" ? 0 : 1, + false + ); useEffect(() => { if (!active) { diff --git a/src/helpers/grid.js b/src/helpers/grid.js index b7612c8..57ecd2a 100644 --- a/src/helpers/grid.js +++ b/src/helpers/grid.js @@ -289,10 +289,7 @@ export function gridDistance(grid, a, b, cellSize) { const bCoord = getNearestCellCoordinates(grid, b.x, b.y, cellSize); if (grid.type === "square") { if (grid.measurement.type === "chebyshev") { - return Math.max( - Math.abs(aCoord.x - bCoord.x), - Math.abs(aCoord.y - bCoord.y) - ); + return Vector2.max(Vector2.abs(Vector2.subtract(aCoord, bCoord))); } else if (grid.measurement.type === "alternating") { // Alternating diagonal distance like D&D 3.5 and Pathfinder const delta = Vector2.abs(Vector2.subtract(aCoord, bCoord)); @@ -300,7 +297,7 @@ export function gridDistance(grid, a, b, cellSize) { const min = Vector2.min(delta); return max - min + Math.floor(1.5 * min); } else if (grid.measurement.type === "euclidean") { - return Vector2.distance(aCoord, bCoord); + return Vector2.length(Vector2.divide(Vector2.subtract(a, b), cellSize)); } else if (grid.measurement.type === "manhattan") { return Math.abs(aCoord.x - bCoord.x) + Math.abs(aCoord.y - bCoord.y); } @@ -316,7 +313,7 @@ export function gridDistance(grid, a, b, cellSize) { 2 ); } else if (grid.measurement.type === "euclidean") { - return Vector2.distance(aCoord, bCoord); + return Vector2.length(Vector2.divide(Vector2.subtract(a, b), cellSize)); } } } diff --git a/src/hooks/useGridSnapping.js b/src/hooks/useGridSnapping.js index 9c1a85c..aa52040 100644 --- a/src/hooks/useGridSnapping.js +++ b/src/hooks/useGridSnapping.js @@ -17,12 +17,16 @@ import { /** * Returns a function that when called will snap a node to the current grid * @param {number=} snappingSensitivity 1 = Always snap, 0 = never snap if undefined the default user setting will be used + * @param {boolean=} useCorners Snap to grid cell corners */ -function useGridSnapping(snappingSensitivity) { +function useGridSnapping(snappingSensitivity, useCorners = true) { const [defaultSnappingSensitivity] = useSetting( "map.gridSnappingSensitivity" ); - snappingSensitivity = snappingSensitivity || defaultSnappingSensitivity; + snappingSensitivity = + snappingSensitivity === undefined + ? defaultSnappingSensitivity + : snappingSensitivity; const grid = useGrid(); const gridOffset = useGridOffset(); @@ -57,7 +61,10 @@ function useGridSnapping(snappingSensitivity) { gridCellPixelSize ); - const snapPoints = [cellPosition, ...cellCorners]; + const snapPoints = [cellPosition]; + if (useCorners) { + snapPoints.push(...cellCorners); + } for (let snapPoint of snapPoints) { const distanceToSnapPoint = Vector2.distance(offsetPosition, snapPoint);