diff --git a/CHANGELOG.md b/CHANGELOG.md index 1eac13f..2335404 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### New features - Drag Ruler can now measure difficult terrain on gridless maps (if the Terrain Ruler module is installed and enabled) +- Improved the positioning of the labels around the ruler. The labels should now never overlap with the waypoint. ### Bugfixes - Fixed a bug that sometimes measured diagonals incorrectly with the 5/10/5 grid rule diff --git a/src/foundry_imports.js b/src/foundry_imports.js index 75759b4..6141fc4 100644 --- a/src/foundry_imports.js +++ b/src/foundry_imports.js @@ -1,5 +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"; @@ -200,7 +201,21 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) { label.text = text; label.alpha = last ? 1.0 : 0.5; label.visible = true; - let labelPosition = cs.ray.project((cs.ray.distance + 50) / cs.ray.distance); + let labelPosition = {x: s.ray.x0, y: s.ray.y0}; + labelPosition.x -= label.width / 2; + labelPosition.y -= label.height / 2; + const rayLine = Line.fromPoints(s.ray.A, s.ray.B); + const rayLabelXHitY = rayLine.calcY(labelPosition.x); + let innerDistance; + // If ray hits top or bottom side of label + if (rayLine.isVertical || rayLabelXHitY < labelPosition.y || rayLabelXHitY > labelPosition.y + label.height) + innerDistance = Math.abs((label.height / 2) / Math.sin(s.ray.angle)); + // If ray hits left or right side of label + else + innerDistance = Math.abs((label.width / 2) / Math.cos(s.ray.angle)); + labelPosition = s.ray.project((s.ray.distance + 50 + innerDistance) / s.ray.distance); + labelPosition.x -= label.width / 2; + labelPosition.y -= label.height / 2; label.position.set(labelPosition.x, labelPosition.y); } diff --git a/src/geometry.js b/src/geometry.js new file mode 100644 index 0000000..f5ee5ef --- /dev/null +++ b/src/geometry.js @@ -0,0 +1,21 @@ +export class Line { + constructor(m, b) { + this.m = m; + this.b = b; + } + + static fromPoints(p1, p2) { + // Bring line into y=mx+b form + const m = (p1.y - p2.y) / (p1.x - p2.x); + const b = p1.y - m * p1.x; + return new Line(m, b); + } + + get isVertical() { + return !isFinite(this.m); + } + + calcY(x) { + return this.m * x + this.b; + } +}