2021-02-06 08:29:24 +00:00
|
|
|
import Vector2 from "../helpers/Vector2";
|
2021-02-07 00:16:36 +00:00
|
|
|
import {
|
|
|
|
getCellLocation,
|
|
|
|
getNearestCellCoordinates,
|
|
|
|
getCellCorners,
|
|
|
|
} from "../helpers/grid";
|
2021-02-06 08:29:24 +00:00
|
|
|
|
2021-02-07 05:42:29 +00:00
|
|
|
import useSetting from "./useSetting";
|
|
|
|
|
2021-03-12 00:02:58 +00:00
|
|
|
import {
|
|
|
|
useGrid,
|
|
|
|
useGridOffset,
|
|
|
|
useGridCellPixelSize,
|
|
|
|
useGridCellPixelOffset,
|
|
|
|
} from "../contexts/GridContext";
|
2021-02-06 08:29:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a function that when called will snap a node to the current grid
|
2021-02-07 05:42:29 +00:00
|
|
|
* @param {number=} snappingSensitivity 1 = Always snap, 0 = never snap if undefined the default user setting will be used
|
2021-02-06 08:29:24 +00:00
|
|
|
*/
|
2021-02-07 05:42:29 +00:00
|
|
|
function useGridSnapping(snappingSensitivity) {
|
|
|
|
const [defaultSnappingSensitivity] = useSetting(
|
|
|
|
"map.gridSnappingSensitivity"
|
|
|
|
);
|
|
|
|
snappingSensitivity = snappingSensitivity || defaultSnappingSensitivity;
|
|
|
|
|
2021-03-12 00:02:58 +00:00
|
|
|
const grid = useGrid();
|
|
|
|
const gridOffset = useGridOffset();
|
|
|
|
const gridCellPixelSize = useGridCellPixelSize();
|
|
|
|
const gridCellPixelOffset = useGridCellPixelOffset();
|
2021-02-06 08:29:24 +00:00
|
|
|
|
|
|
|
/**
|
2021-02-09 03:13:08 +00:00
|
|
|
* @param {Vector2} node The node to snap
|
2021-02-06 08:29:24 +00:00
|
|
|
*/
|
2021-02-09 03:13:08 +00:00
|
|
|
function snapPositionToGrid(position) {
|
2021-02-07 00:16:36 +00:00
|
|
|
// Account for grid offset
|
2021-02-11 08:57:34 +00:00
|
|
|
let offsetPosition = Vector2.subtract(
|
|
|
|
Vector2.subtract(position, gridOffset),
|
|
|
|
gridCellPixelOffset
|
|
|
|
);
|
2021-02-07 00:16:36 +00:00
|
|
|
const nearsetCell = getNearestCellCoordinates(
|
|
|
|
grid,
|
|
|
|
offsetPosition.x,
|
|
|
|
offsetPosition.y,
|
|
|
|
gridCellPixelSize
|
|
|
|
);
|
|
|
|
const cellPosition = getCellLocation(
|
|
|
|
grid,
|
|
|
|
nearsetCell.x,
|
|
|
|
nearsetCell.y,
|
|
|
|
gridCellPixelSize
|
2021-02-06 08:29:24 +00:00
|
|
|
);
|
2021-02-07 00:16:36 +00:00
|
|
|
const cellCorners = getCellCorners(
|
|
|
|
grid,
|
|
|
|
cellPosition.x,
|
|
|
|
cellPosition.y,
|
|
|
|
gridCellPixelSize
|
2021-02-06 08:29:24 +00:00
|
|
|
);
|
|
|
|
|
2021-02-07 00:16:36 +00:00
|
|
|
const snapPoints = [cellPosition, ...cellCorners];
|
|
|
|
|
|
|
|
for (let snapPoint of snapPoints) {
|
|
|
|
const distanceToSnapPoint = Vector2.distance(offsetPosition, snapPoint);
|
|
|
|
if (
|
|
|
|
distanceToSnapPoint <
|
2021-02-07 05:42:29 +00:00
|
|
|
Vector2.min(gridCellPixelSize) * snappingSensitivity
|
2021-02-07 00:16:36 +00:00
|
|
|
) {
|
|
|
|
// Reverse grid offset
|
2021-02-11 08:57:34 +00:00
|
|
|
let offsetSnapPoint = Vector2.add(
|
|
|
|
Vector2.add(snapPoint, gridOffset),
|
|
|
|
gridCellPixelOffset
|
|
|
|
);
|
2021-02-09 03:13:08 +00:00
|
|
|
return offsetSnapPoint;
|
2021-02-07 00:16:36 +00:00
|
|
|
}
|
2021-02-06 08:29:24 +00:00
|
|
|
}
|
2021-02-09 03:13:08 +00:00
|
|
|
|
|
|
|
return position;
|
2021-02-06 08:29:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 03:13:08 +00:00
|
|
|
return snapPositionToGrid;
|
2021-02-06 08:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default useGridSnapping;
|