Stop patching _highlightMeasurement and import it instead to increase compatibility with other modules

This commit is contained in:
Manuel Vögele
2021-03-09 09:09:50 +01:00
parent 20e8713e30
commit 6e74582089
3 changed files with 45 additions and 26 deletions
+41 -1
View File
@@ -166,7 +166,7 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) {
}
// Highlight grid positions
this._highlightMeasurement(ray, s.startDistance);
highlightMeasurementNative.call(this, ray, s.startDistance);
}
// Draw endpoints
@@ -177,3 +177,43 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) {
// Return the measured segments
return segments;
}
export function highlightMeasurementNative(ray, startDistance) {
const spacer = canvas.scene.data.gridType === CONST.GRID_TYPES.SQUARE ? 1.41 : 1;
const nMax = Math.max(Math.floor(ray.distance / (spacer * Math.min(canvas.grid.w, canvas.grid.h))), 1);
const tMax = Array.fromRange(nMax+1).map(t => t / nMax);
// Track prior position
let prior = null;
// Iterate over ray portions
for ( let [i, t] of tMax.entries() ) {
let {x, y} = ray.project(t);
// Get grid position
let [x0, y0] = (i === 0) ? [null, null] : prior;
let [x1, y1] = canvas.grid.grid.getGridPositionFromPixels(x, y);
if ( x0 === x1 && y0 === y1 ) continue;
// Highlight the grid position
let [xg, yg] = canvas.grid.grid.getPixelsFromGridPosition(x1, y1);
let subDistance = canvas.grid.measureDistances([{ray: new Ray(ray.A, {x: xg, y: yg})}], {gridSpaces: true})[0]
let color = dragRuler.getColorForDistance.call(this, startDistance, subDistance)
canvas.grid.highlightPosition(this.name, {x: xg, y: yg, color: color});
// Skip the first one
prior = [x1, y1];
if ( i === 0 ) continue;
// If the positions are not neighbors, also highlight their halfway point
if ( !canvas.grid.isNeighbor(x0, y0, x1, y1) ) {
let th = tMax[i - 1] + (0.5 / nMax);
let {x, y} = ray.project(th);
let [x1h, y1h] = canvas.grid.grid.getGridPositionFromPixels(x, y);
let [xgh, ygh] = canvas.grid.grid.getPixelsFromGridPosition(x1h, y1h);
subDistance = canvas.grid.measureDistances([{ray: new Ray(ray.A, {x: xg, y: yg})}], {gridSpaces: true})[0]
color = dragRuler.getColorForDistance.call(this, startDistance, subDistance)
canvas.grid.highlightPosition(this.name, {x: xgh, y: ygh, color: color});
}
}
}