Change measure tool to use full snapping to grid center and add exact euclidean measurements

This commit is contained in:
Mitchell McCaffrey 2021-06-19 09:36:00 +10:00
parent bfc9c4fba3
commit 1f2c70a612
3 changed files with 17 additions and 10 deletions

View File

@ -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) {

View File

@ -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));
}
}
}

View File

@ -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);