From 6fa3f786dc384301a3acfefea629a9d0cc1ea7e2 Mon Sep 17 00:00:00 2001 From: StempunkDev Date: Thu, 19 Feb 2026 20:07:48 +0100 Subject: [PATCH] refactor: improve code formatting and organization in labels and renderers --- src/modules/labels.ts | 57 +++++++++++++++--------------- src/renderers/draw-state-labels.ts | 39 +++++++++----------- src/renderers/label-raycast.ts | 6 +--- src/types/PackedGraph.ts | 2 +- 4 files changed, 48 insertions(+), 56 deletions(-) diff --git a/src/modules/labels.ts b/src/modules/labels.ts index 4b89d6c9..a607ee72 100644 --- a/src/modules/labels.ts +++ b/src/modules/labels.ts @@ -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 { - const label: StateLabelData = { i: this.getNextId(), type: "state", ...data }; + addStateLabel(data: Omit): 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 { - const label: CustomLabelData = { i: this.getNextId(), type: "custom", ...data }; + addCustomLabel(data: Omit): 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(); diff --git a/src/renderers/draw-state-labels.ts b/src/renderers/draw-state-labels.ts index 307423bd..7015abae 100644 --- a/src/renderers/draw-state-labels.ts +++ b/src/renderers/draw-state-labels.ts @@ -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( diff --git a/src/renderers/label-raycast.ts b/src/renderers/label-raycast.ts index 342135cf..41b39414 100644 --- a/src/renderers/label-raycast.ts +++ b/src/renderers/label-raycast.ts @@ -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]; diff --git a/src/types/PackedGraph.ts b/src/types/PackedGraph.ts index f54d8157..8c1fed10 100644 --- a/src/types/PackedGraph.ts +++ b/src/types/PackedGraph.ts @@ -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 =