// UI module to control the style "use strict"; // add available filters to lists { const filters = Array.from(byId("filters").querySelectorAll("filter")); const emptyOption = ''; const options = filters.map(filter => { const id = filter.getAttribute("id"); const name = filter.getAttribute("name"); return ``; }); const allOptions = emptyOption + options.join(""); byId("styleFilterInput").innerHTML = allOptions; byId("styleStatesBodyFilter").innerHTML = allOptions; byId("styleScaleBarBackgroundFilter").innerHTML = allOptions; } // store some style inputs as options styleElements.on("change", function (ev) { if (ev.target.dataset.stored) lock(ev.target.dataset.stored); }); // select element to be edited function editStyle(element, group) { showOptions(); styleTab.click(); styleElementSelect.value = element; if (group) styleGroupSelect.options.add(new Option(group, group, true, true)); selectStyleElement(); styleElementSelect.classList.add("glow"); if (group) styleGroupSelect.classList.add("glow"); setTimeout(() => { styleElementSelect.classList.remove("glow"); if (group) styleGroupSelect.classList.remove("glow"); }, 1500); } // Color schemes const heightmapColorSchemes = { bright: d3.scaleSequential(d3.interpolateSpectral), light: d3.scaleSequential(d3.interpolateRdYlGn), natural: d3.scaleSequential(d3.interpolateRgbBasis(["white", "#EEEECC", "tan", "green", "teal"])), green: d3.scaleSequential(d3.interpolateGreens), olive: d3.scaleSequential(d3.interpolateRgbBasis(["#ffffff", "#cea48d", "#d5b085", "#0c2c19", "#151320"])), livid: d3.scaleSequential(d3.interpolateRgbBasis(["#BBBBDD", "#2A3440", "#17343B", "#0A1E24"])), monochrome: d3.scaleSequential(d3.interpolateGreys) }; // add default color schemes to the list of options byId("styleHeightmapScheme").innerHTML = Object.keys(heightmapColorSchemes) .map(scheme => ``) .join(""); function addCustomColorScheme(scheme) { const stops = scheme.split(","); heightmapColorSchemes[scheme] = d3.scaleSequential(d3.interpolateRgbBasis(stops)); byId("styleHeightmapScheme").options.add(new Option(scheme, scheme, false, true)); } function getColorScheme(scheme = "bright") { if (!(scheme in heightmapColorSchemes)) { const colors = scheme.split(","); heightmapColorSchemes[scheme] = d3.scaleSequential(d3.interpolateRgbBasis(colors)); } return heightmapColorSchemes[scheme]; } function getColor(value, scheme = getColorScheme("bright")) { return scheme(1 - (value < 20 ? value - 5 : value) / 100); } // Toggle style sections on element select styleElementSelect.on("change", selectStyleElement); function selectStyleElement() { const styleElement = styleElementSelect.value; let el = d3.select("#" + styleElement); styleElements.querySelectorAll("tbody").forEach(e => (e.style.display = "none")); // hide all sections // show alert line if layer is not visible const isLayerOff = styleElement !== "ocean" && (el.style("display") === "none" || !el.selectAll("*").size()); styleIsOff.style.display = isLayerOff ? "block" : "none"; // active group element if (["routes", "labels", "coastline", "lakes", "anchors", "burgIcons", "borders", "terrs"].includes(styleElement)) { const group = styleGroupSelect.value; const defaultGroupSelector = styleElement === "terrs" ? "#landHeights" : "g"; el = group && el.select("#" + group).size() ? el.select("#" + group) : el.select(defaultGroupSelector); } // opacity if (!["landmass", "ocean", "regions", "legend"].includes(styleElement)) { styleOpacity.style.display = "block"; styleOpacityInput.value = el.attr("opacity") || 1; } // filter if (!["landmass", "legend", "regions", "scaleBar"].includes(styleElement)) { styleFilter.style.display = "block"; styleFilterInput.value = el.attr("filter") || ""; } // fill if (["rivers", "lakes", "landmass", "prec", "ice", "fogging", "scaleBar", "vignette"].includes(styleElement)) { styleFill.style.display = "block"; styleFillInput.value = styleFillOutput.value = el.attr("fill"); } // stroke color and width if ( [ "armies", "biomes", "borders", "cells", "coastline", "coordinates", "cults", "gridOverlay", "ice", "icons", "lakes", "prec", "relig", "routes", "zones" ].includes(styleElement) ) { styleStroke.style.display = "block"; styleStrokeInput.value = styleStrokeOutput.value = el.attr("stroke"); styleStrokeWidth.style.display = "block"; styleStrokeWidthInput.value = el.attr("stroke-width") || 0; } // stroke dash if ( [ "borders", "cells", "coordinates", "gridOverlay", "legend", "population", "routes", "temperature", "zones" ].includes(styleElement) ) { styleStrokeDash.style.display = "block"; styleStrokeDasharrayInput.value = el.attr("stroke-dasharray") || ""; styleStrokeLinecapInput.value = el.attr("stroke-linecap") || "inherit"; } // clipping if ( [ "biomes", "cells", "compass", "coordinates", "gridOverlay", "population", "prec", "routes", "temperature", "terrain", "texture", "zones" ].includes(styleElement) ) { styleClipping.style.display = "block"; styleClippingInput.value = el.attr("mask") || ""; } // show specific sections if (styleElement === "texture") { styleTexture.style.display = "block"; styleTextureShiftX.value = el.attr("data-x") || 0; styleTextureShiftY.value = el.attr("data-y") || 0; updateTextureSelectValue(el.attr("data-href")); } if (styleElement === "terrs") { styleHeightmap.style.display = "block"; styleHeightmapRenderOceanOption.style.display = el.attr("id") === "oceanHeights" ? "block" : "none"; styleHeightmapRenderOcean.checked = +el.attr("data-render"); styleHeightmapScheme.value = el.attr("scheme"); styleHeightmapTerracing.value = el.attr("terracing"); styleHeightmapSkip.value = el.attr("skip"); styleHeightmapSimplification.value = el.attr("relax"); styleHeightmapCurve.value = el.attr("curve"); } if (styleElement === "markers") { styleMarkers.style.display = "block"; styleRescaleMarkers.checked = +markers.attr("rescale"); } if (styleElement === "gridOverlay") { styleGrid.style.display = "block"; styleGridType.value = el.attr("type"); styleGridScale.value = el.attr("scale") || 1; styleGridShiftX.value = el.attr("dx") || 0; styleGridShiftY.value = el.attr("dy") || 0; calculateFriendlyGridSize(); } if (styleElement === "compass") { styleCompass.style.display = "block"; const tr = parseTransform(compass.select("use").attr("transform")); styleCompassShiftX.value = tr[0]; styleCompassShiftY.value = tr[1]; styleCompassSizeInput.value = tr[2]; } if (styleElement === "terrain") { styleRelief.style.display = "block"; styleReliefSize.value = terrain.attr("size") || 1; styleReliefDensity.value = terrain.attr("density") || 0.4; styleReliefSet.value = terrain.attr("set"); } if (styleElement === "population") { stylePopulation.style.display = "block"; stylePopulationRuralStrokeInput.value = stylePopulationRuralStrokeOutput.value = population .select("#rural") .attr("stroke"); stylePopulationUrbanStrokeInput.value = stylePopulationUrbanStrokeOutput.value = population .select("#urban") .attr("stroke"); styleStrokeWidth.style.display = "block"; styleStrokeWidthInput.value = el.attr("stroke-width") || 0; } if (styleElement === "regions") { styleStates.style.display = "block"; styleStatesBodyOpacity.value = statesBody.attr("opacity") || 1; styleStatesBodyFilter.value = statesBody.attr("filter") || ""; styleStatesHaloWidth.value = statesHalo.attr("data-width") || 10; styleStatesHaloOpacity.value = statesHalo.attr("opacity") || 1; styleStatesHaloBlur.value = parseFloat(statesHalo.attr("filter")?.match(/blur\(([^)]+)\)/)?.[1]) || 0; } if (styleElement === "provs") { styleFill.style.display = "block"; styleSize.style.display = "block"; styleFillInput.value = styleFillOutput.value = el.attr("fill") || "#111111"; styleFont.style.display = "block"; styleSelectFont.value = el.attr("font-family"); styleFontSize.value = el.attr("font-size"); } if (styleElement === "labels") { styleFill.style.display = "block"; styleStroke.style.display = "block"; styleStrokeWidth.style.display = "block"; styleLetterSpacing.style.display = "block"; styleShadow.style.display = "block"; styleSize.style.display = "block"; styleVisibility.style.display = "block"; styleFillInput.value = styleFillOutput.value = el.attr("fill") || "#3e3e4b"; styleStrokeInput.value = styleStrokeOutput.value = el.attr("stroke") || "#3a3a3a"; styleStrokeWidthInput.value = el.attr("stroke-width") || 0; styleLetterSpacingInput.value = el.attr("letter-spacing") || 0; styleShadowInput.value = el.style("text-shadow") || "white 0 0 4px"; styleFont.style.display = "block"; styleSelectFont.value = el.attr("font-family"); styleFontSize.value = el.attr("data-size"); if (el.node().parentNode.id === "burgLabels") { styleFontShift.style.display = "block"; styleFontShiftY.value = el.attr("data-dy") || 0; } } if (styleElement == "burgIcons") { styleBurgIcons.style.display = "block"; styleBurgIconsIcon.value = el.attr("data-icon"); styleBurgIconsIconSize.value = el.attr("font-size"); styleBurgIconsStrokeLinejoin.value = el.attr("stroke-linejoin"); styleBurgIconsFillOpacity.value = el.attr("fill-opacity"); styleFill.style.display = "block"; styleStroke.style.display = "block"; styleStrokeWidth.style.display = "block"; styleStrokeDash.style.display = "block"; styleFillInput.value = styleFillOutput.value = el.attr("fill") || "#ffffff"; styleStrokeInput.value = styleStrokeOutput.value = el.attr("stroke") || "#3e3e4b"; styleStrokeWidthInput.value = el.attr("stroke-width") || 0.24; styleStrokeDasharrayInput.value = el.attr("stroke-dasharray") || ""; styleStrokeLinecapInput.value = el.attr("stroke-linecap") || "inherit"; } if (styleElement == "anchors") { styleFill.style.display = "block"; styleStroke.style.display = "block"; styleStrokeWidth.style.display = "block"; styleFillInput.value = styleFillOutput.value = el.attr("fill") || "#ffffff"; styleStrokeInput.value = styleStrokeOutput.value = el.attr("stroke") || "#3e3e4b"; styleStrokeWidthInput.value = el.attr("stroke-width") || 0.24; } if (styleElement === "legend") { styleStroke.style.display = "block"; styleStrokeWidth.style.display = "block"; styleSize.style.display = "block"; styleLegend.style.display = "block"; styleLegendColItems.value = el.attr("data-columns"); const legendBox = el.select("#legendBox"); styleLegendBack.value = styleLegendBackOutput.value = legendBox.size() ? legendBox.attr("fill") : "#ffffff"; styleLegendOpacity.value = legendBox.size() ? legendBox.attr("fill-opacity") : 1; styleStrokeInput.value = styleStrokeOutput.value = el.attr("stroke") || "#111111"; styleStrokeWidthInput.value = el.attr("stroke-width") || 0.5; styleFont.style.display = "block"; styleSelectFont.value = el.attr("font-family"); styleFontSize.value = el.attr("data-size"); } if (styleElement === "ocean") { styleOcean.style.display = "block"; styleOceanFill.value = styleOceanFillOutput.value = oceanLayers.select("#oceanBase").attr("fill"); styleOceanPattern.value = byId("oceanicPattern")?.getAttribute("href"); styleOceanPatternOpacity.value = byId("oceanicPattern").getAttribute("opacity") || 1; outlineLayers.value = oceanLayers.attr("layers"); } if (styleElement === "temperature") { styleStrokeWidth.style.display = "block"; styleTemperature.style.display = "block"; styleStrokeWidthInput.value = el.attr("stroke-width") || ""; styleTemperatureFillOpacityInput.value = el.attr("fill-opacity") || 0.1; styleTemperatureFillInput.value = styleTemperatureFillOutput.value = el.attr("fill") || "#000"; styleTemperatureFontSizeInput.value = el.attr("font-size") || "8px"; } if (styleElement === "coordinates") { styleSize.style.display = "block"; styleFontSize.value = el.attr("data-size"); } if (styleElement === "armies") { styleArmies.style.display = "block"; styleArmiesFillOpacity.value = el.attr("fill-opacity"); styleArmiesSize.value = el.attr("box-size"); } if (styleElement === "emblems") { styleEmblems.style.display = "block"; styleStrokeWidth.style.display = "block"; styleStrokeWidthInput.value = el.attr("stroke-width") || 1; emblemsStateSizeInput.value = emblems.select("#stateEmblems").attr("data-size") || 1; emblemsProvinceSizeInput.value = emblems.select("#provinceEmblems").attr("data-size") || 1; emblemsBurgSizeInput.value = emblems.select("#burgEmblems").attr("data-size") || 1; } // update group options styleGroupSelect.options.length = 0; // remove all options if (["routes", "labels", "coastline", "lakes", "anchors", "burgIcons", "borders", "terrs"].includes(styleElement)) { const groups = byId(styleElement).querySelectorAll("g"); groups.forEach(el => { if (el.id === "burgLabels") return; const option = new Option(`${el.id} (${el.childElementCount})`, el.id, false, false); styleGroupSelect.options.add(option); }); styleGroupSelect.value = el.attr("id"); styleGroup.style.display = "block"; } else { styleGroupSelect.options.add(new Option(styleElement, styleElement, false, true)); styleGroup.style.display = "none"; } if (styleElement === "coastline" && styleGroupSelect.value === "sea_island") { styleCoastline.style.display = "block"; const auto = (styleCoastlineAuto.checked = coastline.select("#sea_island").attr("auto-filter")); if (auto) styleFilter.style.display = "none"; } if (styleElement === "scaleBar") { styleScaleBar.style.display = "block"; styleScaleBarSize.value = el.attr("data-bar-size"); styleScaleBarFontSize.value = el.attr("font-size"); styleScaleBarPositionX.value = el.attr("data-x") || "99"; styleScaleBarPositionY.value = el.attr("data-y") || "99"; styleScaleBarLabel.value = el.attr("data-label") || ""; const scaleBarBack = el.select("#scaleBarBack"); if (scaleBarBack.size()) { styleScaleBarBackgroundOpacity.value = scaleBarBack.attr("opacity"); styleScaleBarBackgroundFill.value = styleScaleBarBackgroundFillOutput.value = scaleBarBack.attr("fill"); styleScaleBarBackgroundStroke.value = styleScaleBarBackgroundStrokeOutput.value = scaleBarBack.attr("stroke"); styleScaleBarBackgroundStrokeWidth.value = scaleBarBack.attr("stroke-width"); styleScaleBarBackgroundFilter.value = scaleBarBack.attr("filter"); styleScaleBarBackgroundPaddingTop.value = scaleBarBack.attr("data-top"); styleScaleBarBackgroundPaddingRight.value = scaleBarBack.attr("data-right"); styleScaleBarBackgroundPaddingBottom.value = scaleBarBack.attr("data-bottom"); styleScaleBarBackgroundPaddingLeft.value = scaleBarBack.attr("data-left"); } } if (styleElement === "vignette") { styleVignette.style.display = "block"; const maskRect = byId("vignette-rect"); if (maskRect) { const digit = str => str.replace(/[^\d.]/g, ""); styleVignetteX.value = digit(maskRect.getAttribute("x")); styleVignetteY.value = digit(maskRect.getAttribute("y")); styleVignetteWidth.value = digit(maskRect.getAttribute("width")); styleVignetteHeight.value = digit(maskRect.getAttribute("height")); styleVignetteRx.value = digit(maskRect.getAttribute("rx")); styleVignetteRy.value = digit(maskRect.getAttribute("ry")); styleVignetteBlur.value = digit(maskRect.getAttribute("filter")); } } } // Handle style inputs change styleGroupSelect.on("change", selectStyleElement); function getEl() { const el = styleElementSelect.value; const g = styleGroupSelect.value; if (g === el || g === "") return svg.select("#" + el); else return svg.select("#" + el).select("#" + g); } styleFillInput.on("input", function () { styleFillOutput.value = this.value; getEl().attr("fill", this.value); }); styleStrokeInput.on("input", function () { styleStrokeOutput.value = this.value; getEl().attr("stroke", this.value); if (styleElementSelect.value === "gridOverlay" && layerIsOn("toggleGrid")) drawGrid(); }); styleStrokeWidthInput.on("input", e => { getEl().attr("stroke-width", e.target.value); if (styleElementSelect.value === "gridOverlay" && layerIsOn("toggleGrid")) drawGrid(); }); styleLetterSpacingInput.on("input", e => { getEl().attr("letter-spacing", e.target.value); }); styleStrokeDasharrayInput.on("input", function () { getEl().attr("stroke-dasharray", this.value); if (styleElementSelect.value === "gridOverlay" && layerIsOn("toggleGrid")) drawGrid(); }); styleStrokeLinecapInput.on("change", function () { getEl().attr("stroke-linecap", this.value); if (styleElementSelect.value === "gridOverlay" && layerIsOn("toggleGrid")) drawGrid(); }); styleOpacityInput.on("input", e => { getEl().attr("opacity", e.target.value); }); styleFilterInput.on("change", function () { if (styleGroupSelect.value === "ocean") return oceanLayers.attr("filter", this.value); getEl().attr("filter", this.value); }); styleTextureInput.on("change", function () { changeTexture(this.value); }); function changeTexture(href) { texture.attr("data-href", href); texture.select("image").attr("href", href); } function updateTextureSelectValue(href) { const isAdded = Array.from(styleTextureInput.options).some(option => option.value === href); if (isAdded) { styleTextureInput.value = href; } else { const name = href.split("/").pop().slice(0, 20); styleTextureInput.add(new Option(name, href, false, true)); } } styleTextureShiftX.on("input", function () { texture.attr("data-x", this.value); texture .select("image") .attr("x", this.value) .attr("width", graphWidth - this.valueAsNumber); }); styleTextureShiftY.on("input", function () { texture.attr("data-y", this.value); texture .select("image") .attr("y", this.value) .attr("height", graphHeight - this.valueAsNumber); }); styleClippingInput.on("change", function () { getEl().attr("mask", this.value); }); styleGridType.on("change", function () { getEl().attr("type", this.value); if (layerIsOn("toggleGrid")) drawGrid(); calculateFriendlyGridSize(); }); styleGridScale.on("input", function () { getEl().attr("scale", this.value); if (layerIsOn("toggleGrid")) drawGrid(); calculateFriendlyGridSize(); }); function calculateFriendlyGridSize() { const size = styleGridScale.value * 25; const friendly = `${rn(size * distanceScale, 2)} ${distanceUnitInput.value}`; styleGridSizeFriendly.value = friendly; } styleGridShiftX.on("input", function () { getEl().attr("dx", this.value); if (layerIsOn("toggleGrid")) drawGrid(); }); styleGridShiftY.on("input", function () { getEl().attr("dy", this.value); if (layerIsOn("toggleGrid")) drawGrid(); }); styleRescaleMarkers.on("change", function () { markers.attr("rescale", +this.checked); invokeActiveZooming(); }); styleCoastlineAuto.on("change", function () { coastline.select("#sea_island").attr("auto-filter", +this.checked); styleFilter.style.display = this.checked ? "none" : "block"; invokeActiveZooming(); }); styleOceanFill.on("input", function () { oceanLayers.select("rect").attr("fill", this.value); styleOceanFillOutput.value = this.value; }); styleOceanPattern.on("change", function () { byId("oceanicPattern")?.setAttribute("href", this.value); }); styleOceanPatternOpacity.on("input", e => { byId("oceanicPattern").setAttribute("opacity", e.target.value); }); outlineLayers.on("change", function () { oceanLayers.selectAll("path").remove(); oceanLayers.attr("layers", this.value); OceanLayers(); }); styleHeightmapScheme.on("change", function () { getEl().attr("scheme", this.value); drawHeightmap(); }); openCreateHeightmapSchemeButton.on("click", function () { // start with current scheme const scheme = getEl().attr("scheme"); this.dataset.stops = scheme.startsWith("#") ? scheme : (() => [0, 0.25, 0.5, 0.75, 1].map(heightmapColorSchemes[scheme]).map(toHEX).join(","))(); // render dialog base structure alertMessage.innerHTML = /* html */ `