refactor: improve code formatting and organization in labels and renderers

This commit is contained in:
StempunkDev 2026-02-19 20:07:48 +01:00
parent 6d99d8260d
commit 6fa3f786dc
4 changed files with 48 additions and 56 deletions

View file

@ -79,15 +79,17 @@ class LabelsModule {
}
getBurgLabel(burgId: number): BurgLabelData | undefined {
return pack.labels.find(
(l) => l.type === "burg" && l.burgId === burgId,
) as BurgLabelData | undefined;
return pack.labels.find((l) => l.type === "burg" && l.burgId === burgId) as
| BurgLabelData
| undefined;
}
addStateLabel(
data: Omit<StateLabelData, "i" | "type">,
): StateLabelData {
const label: StateLabelData = { i: this.getNextId(), type: "state", ...data };
addStateLabel(data: Omit<StateLabelData, "i" | "type">): StateLabelData {
const label: StateLabelData = {
i: this.getNextId(),
type: "state",
...data,
};
pack.labels.push(label);
return label;
}
@ -98,10 +100,12 @@ class LabelsModule {
return label;
}
addCustomLabel(
data: Omit<CustomLabelData, "i" | "type">,
): CustomLabelData {
const label: CustomLabelData = { i: this.getNextId(), type: "custom", ...data };
addCustomLabel(data: Omit<CustomLabelData, "i" | "type">): CustomLabelData {
const label: CustomLabelData = {
i: this.getNextId(),
type: "custom",
...data,
};
pack.labels.push(label);
return label;
}
@ -123,8 +127,7 @@ class LabelsModule {
removeByGroup(group: string): void {
pack.labels = pack.labels.filter(
(l) =>
!((l.type === "burg" || l.type === "custom") && l.group === group),
(l) => !((l.type === "burg" || l.type === "custom") && l.group === group),
);
}
@ -145,7 +148,7 @@ class LabelsModule {
clear(): void {
pack.labels = [];
}
/**
* Generate state labels data entries for each state.
* Only stores essential label data; raycast path calculation happens during rendering.
@ -153,52 +156,52 @@ class LabelsModule {
*/
generateStateLabels(list?: number[]): void {
if (TIME) console.time("generateStateLabels");
const { states } = pack;
// Remove existing state labels that need regeneration
if (list) {
list.forEach((stateId) => this.removeStateLabel(stateId));
} else {
this.removeByType("state");
}
// Generate new label entries
for (const state of states) {
if (!state.i || state.removed || state.lock) continue;
if (list && !list.includes(state.i)) continue;
this.addStateLabel({
stateId: state.i,
text: state.name!,
fontSize: 100,
});
}
if (TIME) console.timeEnd("generateStateLabels");
}
/**
* Generate burg labels data from burgs.
* Populates pack.labels with BurgLabelData for each burg.
*/
generateBurgLabels(): void {
if (TIME) console.time("generateBurgLabels");
// Remove existing burg labels
this.removeByType("burg");
// Generate new labels for all active burgs
for (const burg of pack.burgs) {
if (!burg.i || burg.removed) continue;
const group = burg.group || "unmarked";
// Get label group offset attributes if they exist (will be set during rendering)
// For now, use defaults - these will be updated during rendering phase
const dx = 0;
const dy = 0;
this.addBurgLabel({
burgId: burg.i,
group,
@ -209,11 +212,9 @@ class LabelsModule {
dy,
});
}
if (TIME) console.timeEnd("generateBurgLabels");
}
}
window.Labels = new LabelsModule();

View file

@ -1,17 +1,7 @@
import { curveNatural, line, max, select } from "d3";
import {
findClosestCell,
minmax,
rn,
round,
splitInTwo,
} from "../utils";
import type { StateLabelData } from "../modules/labels";
import {
raycast,
findBestRayPair,
ANGLES,
} from "./label-raycast";
import { findClosestCell, minmax, rn, round, splitInTwo } from "../utils";
import { ANGLES, findBestRayPair, raycast } from "./label-raycast";
declare global {
var drawStateLabels: (list?: number[]) => void;
@ -57,8 +47,11 @@ const stateLabelsRenderer = (list?: number[]): void => {
// Get labels to render
const labelsToRender = list
? Labels.getAll()
.filter((l) => l.type === "state" && list.includes((l as StateLabelData).stateId))
.map((l) => l as StateLabelData)
.filter(
(l) =>
l.type === "state" && list.includes((l as StateLabelData).stateId),
)
.map((l) => l as StateLabelData)
: Labels.getByType("state").map((l) => l as StateLabelData);
const letterLength = checkExampleLetterLength();
@ -67,7 +60,10 @@ const stateLabelsRenderer = (list?: number[]): void => {
// restore labels visibility
labels.style("display", layerDisplay);
function drawLabelPath(letterLength: number, labelDataList: StateLabelData[]): void {
function drawLabelPath(
letterLength: number,
labelDataList: StateLabelData[],
): void {
const mode = options.stateLabelsMode || "auto";
const lineGen = line<[number, number]>().curve(curveNatural);
@ -78,8 +74,7 @@ const stateLabelsRenderer = (list?: number[]): void => {
for (const labelData of labelDataList) {
const state = states[labelData.stateId];
if (!state.i || state.removed)
continue;
if (!state.i || state.removed) continue;
// Calculate pathPoints using raycast algorithm (recalculated on each draw)
const offset = getOffsetWidth(state.cells!);
@ -161,16 +156,16 @@ const stateLabelsRenderer = (list?: number[]): void => {
textElement.insertAdjacentHTML("afterbegin", spans.join(""));
const { width, height } = textElement.getBBox();
textElement.setAttribute("href", `#textPath_stateLabel${labelData.stateId}`);
textElement.setAttribute(
"href",
`#textPath_stateLabel${labelData.stateId}`,
);
const stateIds = pack.cells.state;
if (mode === "full" || lines.length === 1) continue;
// check if label fits state boundaries. If no, replace it with short name
const [[x1, y1], [x2, y2]] = [
pathPoints.at(0)!,
pathPoints.at(-1)!,
];
const [[x1, y1], [x2, y2]] = [pathPoints.at(0)!, pathPoints.at(-1)!];
const angleRad = Math.atan2(y2 - y1, x2 - x1);
const isInsideState = checkIfInsideState(

View file

@ -49,11 +49,7 @@ export function raycast({
const stateIds = cells.state;
let ray = { length: 0, x: x0, y: y0 };
for (
let length = LENGTH_START;
length < LENGTH_MAX;
length += LENGTH_STEP
) {
for (let length = LENGTH_START; length < LENGTH_MAX; length += LENGTH_STEP) {
const [x, y] = [x0 + length * dx, y0 + length * dy];
// offset points are perpendicular to the ray
const offset1: [number, number] = [x + -dy * offset, y + dx * offset];

View file

@ -1,11 +1,11 @@
import type { Burg } from "../modules/burgs-generator";
import type { Culture } from "../modules/cultures-generator";
import type { PackedGraphFeature } from "../modules/features";
import type { LabelData } from "../modules/labels";
import type { Province } from "../modules/provinces-generator";
import type { River } from "../modules/river-generator";
import type { Route } from "../modules/routes-generator";
import type { State } from "../modules/states-generator";
import type { LabelData } from "../modules/labels";
import type { Zone } from "../modules/zones-generator";
type TypedArray =