diff --git a/src/api.js b/src/api.js index 5e2c0c4..7e58f6f 100644 --- a/src/api.js +++ b/src/api.js @@ -116,6 +116,20 @@ export function getCostFromSpeedProvider(token, area, options) { } } +export function getColorForDistanceAndToken(distance, token, ranges=null) { + if (!ranges) { + ranges = getRangesFromSpeedProvider(token); + } + if (ranges.length === 0) + return this.color; + const currentRange = ranges.reduce((minRange, currentRange) => { + if (distance <= currentRange.range && currentRange.range < minRange.range) + return currentRange; + return minRange; + }, {range: Infinity, color: getUnreachableColorFromSpeedProvider()}); + return currentRange.color; +} + export function getMovedDistanceFromToken(token) { const history = getMovementHistory(token); const segments = Ruler.dragRulerGetRaysFromWaypoints(history, {x: token.x, y: token.y}).map(ray => {return {ray}}); diff --git a/src/compatibility.js b/src/compatibility.js index edc5849..503ac8b 100644 --- a/src/compatibility.js +++ b/src/compatibility.js @@ -1,5 +1,4 @@ import {getCostFromSpeedProvider} from "./api.js"; -import {getColorForDistance} from "./main.js" import {settingsKey} from "./settings.js"; import {getAreaFromPositionAndShape, highlightTokenShape} from "./util.js"; @@ -10,7 +9,7 @@ export function getHexSizeSupportTokenGridCenter(token) { export function highlightMeasurementTerrainRuler(ray, startDistance, tokenShape=[{x: 0, y: 0}], alpha=1) { for (const space of ray.terrainRulerVisitedSpaces.reverse()) { - const color = getColorForDistance.call(this, startDistance, space.distance) + const color = this.dragRulerGetColorForDistance(startDistance + space.distance); highlightTokenShape.call(this, space, tokenShape, color, alpha) } } diff --git a/src/foundry_imports.js b/src/foundry_imports.js index 741bb58..2ddd30b 100644 --- a/src/foundry_imports.js +++ b/src/foundry_imports.js @@ -1,7 +1,6 @@ import {highlightMeasurementTerrainRuler, measureDistances} from "./compatibility.js"; import {getGridPositionFromPixels} from "./foundry_fixes.js"; import {Line} from "./geometry.js"; -import {getColorForDistance} from "./main.js" import {trackRays} from "./movement_tracking.js" import {recalculate} from "./socket.js"; import {applyTokenSizeOffset, getSnapPointForEntity, getSnapPointForToken, getTokenShape, highlightTokenShape, zip} from "./util.js"; @@ -229,7 +228,7 @@ export function measure(destination, options={}) { r.clear(); let rulerColor if (!options.gridSpaces || canvas.grid.type === CONST.GRID_TYPES.GRIDLESS) - rulerColor = getColorForDistance.call(this, totalDistance) + rulerColor = this.dragRulerGetColorForDistance(totalDistance); else rulerColor = this.color for (const [s, cs] of zip(segments.reverse(), centeredSegments.reverse())) { @@ -301,7 +300,7 @@ export function highlightMeasurementNative(ray, startDistance, tokenShape=[{x: 0 // Highlight the grid position let [xg, yg] = canvas.grid.grid.getPixelsFromGridPosition(x1, y1); const subDistance = canvas.grid.measureDistances([{ray: new Ray(ray.A, {x: xg, y: yg})}], {gridSpaces: true})[0] - const color = dragRuler.getColorForDistance.call(this, startDistance, subDistance) + const color = this.dragRulerGetColorForDistance(startDistance + subDistance); const snapPoint = getSnapPointForToken(...canvas.grid.getTopLeft(x, y), this.draggedEntity); const [snapX, snapY] = getGridPositionFromPixels(snapPoint.x + 1, snapPoint.y + 1); @@ -314,7 +313,7 @@ export function highlightMeasurementNative(ray, startDistance, tokenShape=[{x: 0 let [x1h, y1h] = canvas.grid.grid.getGridPositionFromPixels(x, y); let [xgh, ygh] = canvas.grid.grid.getPixelsFromGridPosition(x1h, y1h); const subDistance = canvas.grid.measureDistances([{ray: new Ray(ray.A, {x: xgh, y: ygh})}], {gridSpaces: true})[0] - const color = dragRuler.getColorForDistance.call(this, startDistance, subDistance) + const color = this.dragRulerGetColorForDistance(startDistance + subDistance); const snapPoint = getSnapPointForToken(...canvas.grid.getTopLeft(x, y), this.draggedEntity); const [snapX, snapY] = getGridPositionFromPixels(snapPoint.x + 1, snapPoint.y + 1); highlightTokenShape.call(this, {x: snapX, y: snapY}, tokenShape, color, alpha); diff --git a/src/main.js b/src/main.js index 42b54fb..94503f6 100644 --- a/src/main.js +++ b/src/main.js @@ -1,6 +1,6 @@ "use strict" -import {currentSpeedProvider, getMovedDistanceFromToken, getRangesFromSpeedProvider, getUnreachableColorFromSpeedProvider, initApi, registerModule, registerSystem} from "./api.js" +import {currentSpeedProvider, getColorForDistanceAndToken, getMovedDistanceFromToken, initApi, registerModule, registerSystem} from "./api.js"; import {checkDependencies, getHexSizeSupportTokenGridCenter} from "./compatibility.js"; import {moveEntities, onMouseMove} from "./foundry_imports.js" import {performMigrations} from "./migration.js" @@ -22,7 +22,7 @@ Hooks.once("init", () => { Ruler = DragRulerRuler; window.dragRuler = { - getColorForDistance, + getColorForDistanceAndToken, getMovedDistanceFromToken, registerModule, registerSystem, @@ -265,29 +265,3 @@ function onEntityDragLeftCancel(event) { } return true } - -export function getColorForDistance(startDistance, subDistance=0) { - if (!this.isDragRuler) - return this.color - if (!this.draggedEntity.actor) { - return this.color; - } - // Don't apply colors if the current user doesn't have at least observer permissions - if (this.draggedEntity.actor.permission < 2) { - // If this is a pc and alwaysShowSpeedForPCs is enabled we show the color anyway - if (!(this.draggedEntity.actor.data.type === "character" && game.settings.get(settingsKey, "alwaysShowSpeedForPCs"))) - return this.color - } - const distance = startDistance + subDistance - if (!this.dragRulerRanges) - this.dragRulerRanges = getRangesFromSpeedProvider(this.draggedEntity); - const ranges = this.dragRulerRanges; - if (ranges.length === 0) - return this.color - const currentRange = ranges.reduce((minRange, currentRange) => { - if (distance <= currentRange.range && currentRange.range < minRange.range) - return currentRange - return minRange - }, {range: Infinity, color: getUnreachableColorFromSpeedProvider()}) - return currentRange.color -} diff --git a/src/ruler.js b/src/ruler.js index c7292c7..66fdd01 100644 --- a/src/ruler.js +++ b/src/ruler.js @@ -1,3 +1,4 @@ +import {getColorForDistanceAndToken, getRangesFromSpeedProvider} from "./api.js"; import {cancelScheduledMeasurement, measure} from "./foundry_imports.js" import {getMovementHistory} from "./movement_tracking.js"; import {settingsKey} from "./settings.js"; @@ -152,4 +153,21 @@ export class DragRulerRuler extends Ruler { return ray; }); } + + dragRulerGetColorForDistance(distance) { + if (!this.isDragRuler) + return this.color; + if (!this.draggedEntity.actor) { + return this.color; + } + // Don't apply colors if the current user doesn't have at least observer permissions + if (this.draggedEntity.actor.permission < 2) { + // If this is a pc and alwaysShowSpeedForPCs is enabled we show the color anyway + if (!(this.draggedEntity.actor.data.type === "character" && game.settings.get(settingsKey, "alwaysShowSpeedForPCs"))) + return this.color; + } + if (!this.dragRulerRanges) + this.dragRulerRanges = getRangesFromSpeedProvider(this.draggedEntity); + return getColorForDistanceAndToken(distance, this.draggedEntity, this.dragRulerRanges); + } }