modify ruler and token ruler to show distance ranges (#40)

This commit is contained in:
IrkTheImp 2025-05-23 18:19:37 -05:00 committed by GitHub
parent 504e2ca0f2
commit 2d832e74d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 47 additions and 6 deletions

View file

@ -9,6 +9,7 @@ import { registerDHPSettings } from './module/applications/settings.mjs';
import DhpChatLog from './module/ui/chatLog.mjs';
import DhpPlayers from './module/ui/players.mjs';
import DhpRuler from './module/ui/ruler.mjs';
import DhpTokenRuler from './module/ui/tokenRuler.mjs';
globalThis.SYSTEM = SYSTEM;
@ -83,6 +84,7 @@ Hooks.once('init', () => {
CONFIG.ui.combat = DhpCombatTracker;
CONFIG.ui.chat = DhpChatLog;
CONFIG.ui.players = DhpPlayers;
CONFIG.Token.rulerClass = DhpTokenRuler;
game.socket.on(`system.${SYSTEM.id}`, handleSocketEvent);

View file

@ -204,7 +204,7 @@ export const registerDHPSettings = () => {
config: false,
type: Object,
default: {
enabled: false,
enabled: true,
melee: 5,
veryClose: 15,
close: 30,

View file

@ -1,12 +1,17 @@
export default class DhpRuler extends foundry.canvas.interaction.Ruler {
_getSegmentLabel(segment, totalDistance) {
_getWaypointLabelContext(waypoint, state) {
const context = super._getWaypointLabelContext(waypoint, state);
if (!context) return;
const range = game.settings.get(SYSTEM.id, SYSTEM.SETTINGS.gameSettings.General.RangeMeasurement);
if (!range.enabled) return super._getSegmentLabel(segment, totalDistance);
const segmentDistance = Math.round(segment.distance * 100) / 100;
const totalDistanceValue = Math.round(totalDistance * 100) / 100;
if (range.enabled) {
const distance = this.#getRangeLabel(waypoint.measurement.distance.toNearest(0.01), range);
context.cost = { total: distance, units: null };
context.distance = { total: distance, units: null };
}
return `${this.#getRangeLabel(segmentDistance, range)} [${this.#getRangeLabel(totalDistanceValue, range)}]`;
return context;
}
#getRangeLabel(distance, settings) {

34
module/ui/tokenRuler.mjs Normal file
View file

@ -0,0 +1,34 @@
export default class DhpTokenRuler extends foundry.canvas.placeables.tokens.TokenRuler {
_getWaypointLabelContext(waypoint, state) {
const context = super._getWaypointLabelContext(waypoint, state);
if (!context) return;
const range = game.settings.get(SYSTEM.id, SYSTEM.SETTINGS.gameSettings.General.RangeMeasurement);
if (range.enabled) {
const distance = this.#getRangeLabel(waypoint.measurement.distance.toNearest(0.01), range);
context.cost = { total: distance, units: null };
context.distance = { total: distance, units: null };
}
return context;
}
#getRangeLabel(distance, settings) {
if (distance <= settings.melee) {
return game.i18n.localize('DAGGERHEART.Range.Melee.Name');
}
if (distance <= settings.veryClose) {
return game.i18n.localize('DAGGERHEART.Range.VeryClose.Name');
}
if (distance <= settings.close) {
return game.i18n.localize('DAGGERHEART.Range.Close.Name');
}
if (distance <= settings.far) {
return game.i18n.localize('DAGGERHEART.Range.Far.Name');
}
if (distance <= settings.veryFar) {
return game.i18n.localize('DAGGERHEART.Range.VeryFar.Name');
}
}
}