Add support for the Toggle Snap To Grid module (#97)

This commit is contained in:
Michael Clavell
2021-07-30 04:16:27 -04:00
committed by GitHub
parent 93f0bf6369
commit e1265ad6fb
7 changed files with 72 additions and 33 deletions
+16 -8
View File
@@ -148,22 +148,30 @@ function scheduleMeasurement(destination, event) {
}
// This is a modified version of Ruler.measure form foundry 0.7.9
export function measure(destination, {gridSpaces=true, snap=false} = {}) {
export function measure(destination, options={}) {
const isToken = this.draggedEntity instanceof Token;
if (isToken && !this.draggedEntity.isVisible)
return []
if (snap) {
if (options.snap) {
destination = getSnapPointForEntity(destination.x, destination.y, this.draggedEntity);
}
const terrainRulerAvailable = isToken && game.modules.get("terrain-ruler")?.active && (!game.modules.get("TerrainLayer")?.active || canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS);
if(options.gridSpaces === undefined) {
options.gridSpaces = canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS;
}
if(options.ignoreGrid === undefined) {
options.ignoreGrid = false;
}
options.enableTerrainRuler = isToken && game.modules.get("terrain-ruler")?.active && (!game.modules.get("TerrainLayer")?.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 = isToken ? applyTokenSizeOffset(waypoints, this.draggedEntity) : duplicate(waypoints);
// Foundries native ruler requires the waypoints to sit in the dead center of the square to work properly
if (!terrainRulerAvailable)
if (!options.enableTerrainRuler && !options.ignoreGrid)
centeredWaypoints.forEach(w => [w.x, w.y] = canvas.grid.getCenter(w.x, w.y));
const r = this.ruler;
@@ -197,7 +205,7 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) {
const shape = isToken ? getTokenShape(this.draggedEntity) : null;
// Compute measured distance
const distances = measureDistances(centeredSegments, this.draggedEntity, shape, {gridSpaces});
const distances = measureDistances(centeredSegments, this.draggedEntity, shape, options);
let totalDistance = 0;
for (let [i, d] of distances.entries()) {
@@ -216,7 +224,7 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) {
// Draw measured path
r.clear();
let rulerColor
if (canvas.grid.type === CONST.GRID_TYPES.GRIDLESS)
if (!options.gridSpaces || canvas.grid.type === CONST.GRID_TYPES.GRIDLESS)
rulerColor = getColorForDistance.call(this, totalDistance)
else
rulerColor = this.color
@@ -252,8 +260,8 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) {
}
// Highlight grid positions
if (isToken && canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS) {
if (terrainRulerAvailable)
if (isToken && canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS && options.gridSpaces) {
if (options.enableTerrainRuler)
highlightMeasurementTerrainRuler.call(this, cs.ray, cs.startDistance, shape, opacityMultiplier)
else
highlightMeasurementNative.call(this, cs.ray, cs.startDistance, shape, opacityMultiplier);