refactor: draw burgs and labels separately

This commit is contained in:
max 2022-08-20 15:28:34 +03:00
parent b0f081b3ba
commit 2c3cdad59d
12 changed files with 84 additions and 67 deletions

View file

@ -630,7 +630,7 @@
<u>L</u>abels <u>L</u>abels
</li> </li>
<li <li
id="toggleIcons" id="toggleBurgs"
data-tip="Burg icons: click to toggle, drag to raise or lower the layer. Ctrl + click to edit layer style" data-tip="Burg icons: click to toggle, drag to raise or lower the layer. Ctrl + click to edit layer style"
data-shortcut="I" data-shortcut="I"
> >

View file

@ -18,7 +18,7 @@ let isLoaded = false;
export function open({id} = {}) { export function open({id} = {}) {
if (customization) return; if (customization) return;
closeDialogs(".stable"); closeDialogs(".stable");
if (!layerIsOn("toggleIcons")) toggleLayer("toggleIcons"); if (!layerIsOn("toggleBurgs")) toggleLayer("toggleBurgs");
if (!layerIsOn("toggleLabels")) toggleLayer("toggleLabels"); if (!layerIsOn("toggleLabels")) toggleLayer("toggleLabels");
const burg = id || d3.event.target.dataset.id; const burg = id || d3.event.target.dataset.id;

View file

@ -22,7 +22,7 @@ let isLoaded = false;
export function open() { export function open() {
if (customization) return; if (customization) return;
closeDialogs("#burgsOverview, .stable"); closeDialogs("#burgsOverview, .stable");
if (!layerIsOn("toggleIcons")) toggleLayer("toggleIcons"); if (!layerIsOn("toggleBurgs")) toggleLayer("toggleBurgs");
if (!layerIsOn("toggleLabels")) toggleLayer("toggleLabels"); if (!layerIsOn("toggleLabels")) toggleLayer("toggleLabels");
const body = document.getElementById("burgsBody"); const body = document.getElementById("burgsBody");

View file

@ -16,7 +16,7 @@ let presets: Dict<string[]> = {};
const defaultPresets = { const defaultPresets = {
political: [ political: [
"toggleBorders", "toggleBorders",
"toggleIcons", "toggleBurgs",
"toggleIce", "toggleIce",
"toggleLabels", "toggleLabels",
"toggleRivers", "toggleRivers",
@ -27,7 +27,7 @@ const defaultPresets = {
cultural: [ cultural: [
"toggleBorders", "toggleBorders",
"toggleCultures", "toggleCultures",
"toggleIcons", "toggleBurgs",
"toggleLabels", "toggleLabels",
"toggleRivers", "toggleRivers",
"toggleRoutes", "toggleRoutes",
@ -35,14 +35,14 @@ const defaultPresets = {
], ],
religions: [ religions: [
"toggleBorders", "toggleBorders",
"toggleIcons", "toggleBurgs",
"toggleLabels", "toggleLabels",
"toggleReligions", "toggleReligions",
"toggleRivers", "toggleRivers",
"toggleRoutes", "toggleRoutes",
"toggleScaleBar" "toggleScaleBar"
], ],
provinces: ["toggleBorders", "toggleIcons", "toggleProvinces", "toggleRivers", "toggleScaleBar"], provinces: ["toggleBorders", "toggleBurgs", "toggleProvinces", "toggleRivers", "toggleScaleBar"],
biomes: ["toggleBiomes", "toggleIce", "toggleRivers", "toggleScaleBar"], biomes: ["toggleBiomes", "toggleIce", "toggleRivers", "toggleScaleBar"],
heightmap: ["toggleHeight", "toggleRivers"], heightmap: ["toggleHeight", "toggleRivers"],
physical: ["toggleCoordinates", "toggleHeight", "toggleIce", "toggleRivers", "toggleScaleBar"], physical: ["toggleCoordinates", "toggleHeight", "toggleIce", "toggleRivers", "toggleScaleBar"],
@ -50,7 +50,7 @@ const defaultPresets = {
"toggleBorders", "toggleBorders",
"toggleHeight", "toggleHeight",
"toggleIce", "toggleIce",
"toggleIcons", "toggleBurgs",
"toggleMarkers", "toggleMarkers",
"toggleRivers", "toggleRivers",
"toggleRoutes", "toggleRoutes",
@ -58,7 +58,7 @@ const defaultPresets = {
], ],
military: [ military: [
"toggleBorders", "toggleBorders",
"toggleIcons", "toggleBurgs",
"toggleLabels", "toggleLabels",
"toggleMilitary", "toggleMilitary",
"toggleRivers", "toggleRivers",
@ -68,7 +68,7 @@ const defaultPresets = {
], ],
emblems: [ emblems: [
"toggleBorders", "toggleBorders",
"toggleIcons", "toggleBurgs",
"toggleIce", "toggleIce",
"toggleEmblems", "toggleEmblems",
"toggleRivers", "toggleRivers",
@ -117,7 +117,7 @@ const layerButtonToElementMap: Dict<string> = {
toggleGrid: "gridOverlay", toggleGrid: "gridOverlay",
toggleHeight: "terrs", toggleHeight: "terrs",
toggleIce: "ice", toggleIce: "ice",
toggleIcons: "icons", toggleBurgs: "icons",
toggleLabels: "labels", toggleLabels: "labels",
toggleMarkers: "markers", toggleMarkers: "markers",
toggleMilitary: "armies", toggleMilitary: "armies",

View file

@ -3,7 +3,6 @@ import {rn} from "utils/numberUtils";
export function drawBurgs() { export function drawBurgs() {
// remove old data // remove old data
burgIcons.selectAll("circle").remove(); burgIcons.selectAll("circle").remove();
burgLabels.selectAll("text").remove();
icons.selectAll("use").remove(); icons.selectAll("use").remove();
const validBurgs = pack.burgs.filter(burg => burg.i && !(burg as IBurg).removed) as IBurg[]; const validBurgs = pack.burgs.filter(burg => burg.i && !(burg as IBurg).removed) as IBurg[];
@ -11,7 +10,7 @@ export function drawBurgs() {
// capitals // capitals
const capitals = validBurgs.filter(burg => burg.capital); const capitals = validBurgs.filter(burg => burg.capital);
const capitalIcons = burgIcons.select("#cities"); const capitalIcons = burgIcons.select("#cities");
const capitalLabels = burgLabels.select("#cities");
const capitalSize = Number(capitalIcons.attr("size")) || 1; const capitalSize = Number(capitalIcons.attr("size")) || 1;
const capitalAnchors = anchors.selectAll("#cities"); const capitalAnchors = anchors.selectAll("#cities");
const caSize = Number(capitalAnchors.attr("size")) || 2; const caSize = Number(capitalAnchors.attr("size")) || 2;
@ -27,18 +26,6 @@ export function drawBurgs() {
.attr("cy", d => d.y) .attr("cy", d => d.y)
.attr("r", capitalSize); .attr("r", capitalSize);
capitalLabels
.selectAll("text")
.data(capitals)
.enter()
.append("text")
.attr("id", d => "burgLabel" + d.i)
.attr("data-id", d => d.i)
.attr("x", d => d.x)
.attr("y", d => d.y)
.attr("dy", `${capitalSize * -1.5}px`)
.text(d => d.name);
capitalAnchors capitalAnchors
.selectAll("use") .selectAll("use")
.data(capitals.filter(c => c.port)) .data(capitals.filter(c => c.port))
@ -54,7 +41,6 @@ export function drawBurgs() {
// towns // towns
const towns = validBurgs.filter(burg => !burg.capital); const towns = validBurgs.filter(burg => !burg.capital);
const townIcons = burgIcons.select("#towns"); const townIcons = burgIcons.select("#towns");
const townLabels = burgLabels.select("#towns");
const townSize = Number(townIcons.attr("size")) || 0.5; const townSize = Number(townIcons.attr("size")) || 0.5;
const townsAnchors = anchors.selectAll("#towns"); const townsAnchors = anchors.selectAll("#towns");
const taSize = Number(townsAnchors.attr("size")) || 1; const taSize = Number(townsAnchors.attr("size")) || 1;
@ -70,18 +56,6 @@ export function drawBurgs() {
.attr("cy", d => d.y) .attr("cy", d => d.y)
.attr("r", townSize); .attr("r", townSize);
townLabels
.selectAll("text")
.data(towns)
.enter()
.append("text")
.attr("id", d => "burgLabel" + d.i)
.attr("data-id", d => d.i)
.attr("x", d => d.x)
.attr("y", d => d.y)
.attr("dy", `${townSize * -1.5}px`)
.text(d => d.name);
townsAnchors townsAnchors
.selectAll("use") .selectAll("use")
.data(towns.filter(c => c.port)) .data(towns.filter(c => c.port))

View file

@ -0,0 +1,47 @@
export function drawLabels() {
drawBurgLabels();
// TODO: draw other labels
window.Zoom.invoke();
}
function drawBurgLabels() {
// remove old data
burgLabels.selectAll("text").remove();
const validBurgs = pack.burgs.filter(burg => burg.i && !(burg as IBurg).removed) as IBurg[];
// capitals
const capitals = validBurgs.filter(burg => burg.capital);
const capitalSize = Number(burgIcons.select("#cities").attr("size")) || 1;
burgLabels
.select("#cities")
.selectAll("text")
.data(capitals)
.enter()
.append("text")
.attr("id", d => "burgLabel" + d.i)
.attr("data-id", d => d.i)
.attr("x", d => d.x)
.attr("y", d => d.y)
.attr("dy", `${capitalSize * -1.5}px`)
.text(d => d.name);
// towns
const towns = validBurgs.filter(burg => !burg.capital);
const townSize = Number(burgIcons.select("#towns").attr("size")) || 0.5;
burgLabels
.select("#towns")
.selectAll("text")
.data(towns)
.enter()
.append("text")
.attr("id", d => "burgLabel" + d.i)
.attr("data-id", d => d.i)
.attr("x", d => d.x)
.attr("y", d => d.y)
.attr("dy", `${townSize * -1.5}px`)
.text(d => d.name);
}

View file

@ -1,4 +1,3 @@
// @ts-nocheck
import {TIME} from "config/logging"; import {TIME} from "config/logging";
import {drawBiomes} from "./drawBiomes"; import {drawBiomes} from "./drawBiomes";
import {drawBorders} from "./drawBorders"; import {drawBorders} from "./drawBorders";
@ -11,6 +10,7 @@ import {drawFeatures} from "./drawFeatures";
import {drawGrid} from "./drawGrid"; import {drawGrid} from "./drawGrid";
import {drawHeightmap} from "./drawHeightmap"; import {drawHeightmap} from "./drawHeightmap";
import {drawIce} from "./drawIce"; import {drawIce} from "./drawIce";
import {drawLabels} from "./drawLabels";
import {drawMarkers} from "./drawMarkers"; import {drawMarkers} from "./drawMarkers";
import {drawPopulation} from "./drawPopulation"; import {drawPopulation} from "./drawPopulation";
import {drawPrecipitation} from "./drawPrecipitation"; import {drawPrecipitation} from "./drawPrecipitation";
@ -34,6 +34,7 @@ const layerRenderersMap = {
grid: drawGrid, grid: drawGrid,
heightmap: drawHeightmap, heightmap: drawHeightmap,
ice: drawIce, ice: drawIce,
labels: drawLabels,
markers: drawMarkers, markers: drawMarkers,
population: drawPopulation, population: drawPopulation,
precipitation: drawPrecipitation, precipitation: drawPrecipitation,
@ -45,7 +46,7 @@ const layerRenderersMap = {
temperature: drawTemperature temperature: drawTemperature
}; };
export function renderLayer(layerName: keyof typeof layerRenderersMap, ...args) { export function renderLayer(layerName: keyof typeof layerRenderersMap, ...args: any[]) {
const renderer = layerRenderersMap[layerName]; const renderer = layerRenderersMap[layerName];
TIME && console.time(renderer.name); TIME && console.time(renderer.name);
renderer(...args); renderer(...args);

View file

@ -21,7 +21,7 @@ const layerTogglesMap = {
toggleGrid, toggleGrid,
toggleHeight, toggleHeight,
toggleIce, toggleIce,
toggleIcons, toggleBurgs,
toggleLabels, toggleLabels,
toggleMarkers, toggleMarkers,
toggleMilitary, toggleMilitary,
@ -417,31 +417,26 @@ function toggleMarkers(event?: MouseEvent) {
function toggleLabels(event?: MouseEvent) { function toggleLabels(event?: MouseEvent) {
if (!layerIsOn("toggleLabels")) { if (!layerIsOn("toggleLabels")) {
turnLayerButtonOn("toggleLabels"); turnLayerButtonOn("toggleLabels");
labels.style("display", null); renderLayer("labels");
window.Zoom.invoke();
if (isCtrlPressed(event)) editStyle("labels"); if (isCtrlPressed(event)) editStyle("labels");
} else { } else {
if (isCtrlPressed(event)) { if (isCtrlPressed(event)) return editStyle("labels");
editStyle("labels"); labels.selectAll("text").remove();
return; // TODO: remove text paths
}
turnLayerButtonOff("toggleLabels"); turnLayerButtonOff("toggleLabels");
labels.style("display", "none");
} }
} }
function toggleIcons(event?: MouseEvent) { function toggleBurgs(event?: MouseEvent) {
if (!layerIsOn("toggleIcons")) { if (!layerIsOn("toggleBurgs")) {
turnLayerButtonOn("toggleIcons"); turnLayerButtonOn("toggleBurgs");
$("#icons").fadeIn(); renderLayer("burgs");
if (isCtrlPressed(event)) editStyle("burgIcons"); if (isCtrlPressed(event)) editStyle("burgIcons");
} else { } else {
if (isCtrlPressed(event)) { if (isCtrlPressed(event)) return editStyle("burgIcons");
editStyle("burgIcons"); burgIcons.selectAll("circle").remove();
return; icons.selectAll("use").remove();
} turnLayerButtonOff("toggleBurgs");
turnLayerButtonOff("toggleIcons");
$("#icons").fadeOut();
} }
} }

View file

@ -473,7 +473,7 @@ async function parseLoadedData(data) {
if (hasChild(prec, "circle")) turnOn("togglePrec"); if (hasChild(prec, "circle")) turnOn("togglePrec");
if (notHidden(emblems) && hasChild(emblems, "use")) turnOn("toggleEmblems"); if (notHidden(emblems) && hasChild(emblems, "use")) turnOn("toggleEmblems");
if (notHidden(labels)) turnOn("toggleLabels"); if (notHidden(labels)) turnOn("toggleLabels");
if (notHidden(icons)) turnOn("toggleIcons"); if (notHidden(icons)) turnOn("toggleBurgs");
if (hasChildren(armies) && notHidden(armies)) turnOn("toggleMilitary"); if (hasChildren(armies) && notHidden(armies)) turnOn("toggleMilitary");
if (hasChildren(markers)) turnOn("toggleMarkers"); if (hasChildren(markers)) turnOn("toggleMarkers");
if (notHidden(ruler)) turnOn("toggleRulers"); if (notHidden(ruler)) turnOn("toggleRulers");

View file

@ -339,7 +339,7 @@ window.ThreeD = (function () {
} }
// icons // icons
if (layerIsOn("toggleIcons")) { if (layerIsOn("toggleBurgs")) {
const geometry = isCity ? city_icon_geometry : town_icon_geometry; const geometry = isCity ? city_icon_geometry : town_icon_geometry;
const material = isCity ? city_icon_material : town_icon_material; const material = isCity ? city_icon_material : town_icon_material;
const iconMesh = new THREE.Mesh(geometry, material); const iconMesh = new THREE.Mesh(geometry, material);

View file

@ -34,7 +34,7 @@ export function generateRoutes(burgs: TBurgs, cells: Pick<IPack["cells"], "c" |
const points: TPoints = featureCapitals.map(burg => [burg.x, burg.y]); const points: TPoints = featureCapitals.map(burg => [burg.x, burg.y]);
const urquhartEdges = calculateUrquhartEdges(points); const urquhartEdges = calculateUrquhartEdges(points);
urquhartEdges.forEach(([fromId, toId]) => { urquhartEdges.forEach(([fromId, toId]) => {
drawLine(points[fromId], points[toId], {stroke: "red", strokeWidth: 0.05}); drawLine(points[fromId], points[toId], {stroke: "red", strokeWidth: 0.03});
const start = featureCapitals[fromId].cell; const start = featureCapitals[fromId].cell;
const exit = featureCapitals[toId].cell; const exit = featureCapitals[toId].cell;
@ -69,7 +69,7 @@ export function generateRoutes(burgs: TBurgs, cells: Pick<IPack["cells"], "c" |
const points: TPoints = featureBurgs.map(burg => [burg.x, burg.y]); const points: TPoints = featureBurgs.map(burg => [burg.x, burg.y]);
const urquhartEdges = calculateUrquhartEdges(points); const urquhartEdges = calculateUrquhartEdges(points);
urquhartEdges.forEach(([fromId, toId]) => { urquhartEdges.forEach(([fromId, toId]) => {
drawLine(points[fromId], points[toId], {strokeWidth: 0.05}); drawLine(points[fromId], points[toId], {strokeWidth: 0.03});
const start = featureBurgs[fromId].cell; const start = featureBurgs[fromId].cell;
const exit = featureBurgs[toId].cell; const exit = featureBurgs[toId].cell;
@ -116,7 +116,7 @@ export function generateRoutes(burgs: TBurgs, cells: Pick<IPack["cells"], "c" |
const heightChangeCost = Math.abs(cells.h[neibCellId] - cells.h[next]) * 10; // routes tend to avoid elevation changes const heightChangeCost = Math.abs(cells.h[neibCellId] - cells.h[next]) * 10; // routes tend to avoid elevation changes
const heightCost = cells.h[neibCellId] > 80 ? cells.h[neibCellId] : 0; // routes tend to avoid mountainous areas const heightCost = cells.h[neibCellId] > 80 ? cells.h[neibCellId] : 0; // routes tend to avoid mountainous areas
const cellCoast = 10 + stateChangeCost + habitedCost + heightChangeCost + heightCost; const cellCoast = 10 + stateChangeCost + habitedCost + heightChangeCost + heightCost;
const totalCost = priority + (cellRoutes[neibCellId] || cells.burg[neibCellId] ? cellCoast / 2 : cellCoast); const totalCost = priority + (cellRoutes[neibCellId] || cells.burg[neibCellId] ? cellCoast / 3 : cellCoast);
if (from[neibCellId] || totalCost >= cost[neibCellId]) continue; if (from[neibCellId] || totalCost >= cost[neibCellId]) continue;
from[neibCellId] = next; from[neibCellId] = next;
@ -174,12 +174,12 @@ function getRouteSegments(pathCells: number[], cellRoutes: Uint8Array) {
// UC: complitely new route // UC: complitely new route
if (pathCells.every(noRoute)) return [pathCells]; if (pathCells.every(noRoute)) return [pathCells];
// UC: all cells already have route
if (pathCells.every(hasRoute)) return [];
// UC: only first and/or last cell have route // UC: only first and/or last cell have route
if (pathCells.slice(1, -1).every(noRoute)) return [pathCells]; if (pathCells.slice(1, -1).every(noRoute)) return [pathCells];
// UC: all cells already have route
if (pathCells.every(hasRoute)) return [];
// UC: discontinuous route // UC: discontinuous route
for (let i = 0; i < pathCells.length; i++) { for (let i = 0; i < pathCells.length; i++) {
const cellId = pathCells[i]; const cellId = pathCells[i];

View file

@ -105,7 +105,7 @@ function handleKeyup(event: KeyboardEvent) {
else if (code === "KeyA") toggleLayer("togglePrec"); else if (code === "KeyA") toggleLayer("togglePrec");
else if (code === "KeyY") toggleLayer("toggleEmblems"); else if (code === "KeyY") toggleLayer("toggleEmblems");
else if (code === "KeyL") toggleLayer("toggleLabels"); else if (code === "KeyL") toggleLayer("toggleLabels");
else if (code === "KeyI") toggleLayer("toggleIcons"); else if (code === "KeyI") toggleLayer("toggleBurgs");
else if (code === "KeyM") toggleLayer("toggleMilitary"); else if (code === "KeyM") toggleLayer("toggleMilitary");
else if (code === "KeyK") toggleLayer("toggleMarkers"); else if (code === "KeyK") toggleLayer("toggleMarkers");
else if (code === "Equal") toggleLayer("toggleRulers"); else if (code === "Equal") toggleLayer("toggleRulers");