+ data-population=${population} data-type=${r.type} data-form=${r.form} data-deity="${r.deity ? r.deity : ""}" data-expansionism=${r.expansionism}>
${getTypeOptions(r.type)}
-
+
${si(area) + unit}
@@ -127,7 +133,10 @@ function editReligions() {
body.querySelectorAll("div > div.culturePopulation").forEach(el => el.addEventListener("click", changePopulation));
body.querySelectorAll("div > span.icon-trash-empty").forEach(el => el.addEventListener("click", religionRemove));
- if (body.dataset.type === "percentage") {body.dataset.type = "absolute"; togglePercentageMode();}
+ if (body.dataset.type === "percentage") {
+ body.dataset.type = "absolute";
+ togglePercentageMode();
+ }
applySorting(religionsHeader);
$("#religionsEditor").dialog({width: fitContent()});
}
@@ -135,7 +144,7 @@ function editReligions() {
function getTypeOptions(type) {
let options = "";
const types = ["Folk", "Organized", "Cult", "Heresy"];
- types.forEach(t => options += `
${t} `);
+ types.forEach(t => (options += `
${t} `));
return options;
}
@@ -143,14 +152,14 @@ function editReligions() {
const religion = +event.target.dataset.id;
const info = document.getElementById("religionInfo");
if (info) {
- d3.select("#hierarchy").select("g[data-id='"+religion+"'] > path").classed("selected", 1);
+ d3.select("#hierarchy")
+ .select("g[data-id='" + religion + "'] > path")
+ .classed("selected", 1);
const r = pack.religions[religion];
- const type = r.name.includes(r.type) ? ""
- : r.type === "Folk" || r.type === "Organized"
- ? ". " + r.type + " religion" : ". " + r.type;
+ const type = r.name.includes(r.type) ? "" : r.type === "Folk" || r.type === "Organized" ? ". " + r.type + " religion" : ". " + r.type;
const form = r.form === r.type || r.name.includes(r.form) ? "" : ". " + r.form;
- const rural = r.rural * populationRate.value;
- const urban = r.urban * populationRate.value * urbanization.value;
+ const rural = r.rural * populationRate;
+ const urban = r.urban * populationRate * urbanization;
const population = rural + urban > 0 ? ". " + si(rn(rural + urban)) + " believers" : ". Extinct";
info.innerHTML = `${r.name}${type}${form}${population}`;
tip("Drag to change parent, drag to itself to move to the top level. Hold CTRL and click to change abbreviation");
@@ -161,24 +170,46 @@ function editReligions() {
if (!layerIsOn("toggleReligions")) return;
if (customization) return;
- relig.select("#religion"+religion).raise().transition(animate).attr("stroke-width", 2.5).attr("stroke", "#c13119");
- debug.select("#religionsCenter"+religion).raise().transition(animate).attr("r", 8).attr("stroke-width", 2).attr("stroke", "#c13119");
+ relig
+ .select("#religion" + religion)
+ .raise()
+ .transition(animate)
+ .attr("stroke-width", 2.5)
+ .attr("stroke", "#c13119");
+ debug
+ .select("#religionsCenter" + religion)
+ .raise()
+ .transition(animate)
+ .attr("r", 8)
+ .attr("stroke-width", 2)
+ .attr("stroke", "#c13119");
}
function religionHighlightOff(event) {
const religion = +event.target.dataset.id;
const info = document.getElementById("religionInfo");
if (info) {
- d3.select("#hierarchy").select("g[data-id='"+religion+"'] > path").classed("selected", 0);
+ d3.select("#hierarchy")
+ .select("g[data-id='" + religion + "'] > path")
+ .classed("selected", 0);
info.innerHTML = "";
tip("");
}
- const el = body.querySelector(`div[data-id='${religion}']`)
+ const el = body.querySelector(`div[data-id='${religion}']`);
if (el) el.classList.remove("active");
- relig.select("#religion"+religion).transition().attr("stroke-width", null).attr("stroke", null);
- debug.select("#religionsCenter"+religion).transition().attr("r", 4).attr("stroke-width", 1.2).attr("stroke", null);
+ relig
+ .select("#religion" + religion)
+ .transition()
+ .attr("stroke-width", null)
+ .attr("stroke", null);
+ debug
+ .select("#religionsCenter" + religion)
+ .transition()
+ .attr("r", 4)
+ .attr("stroke-width", 1.2)
+ .attr("stroke", null);
}
function religionChangeColor() {
@@ -186,12 +217,12 @@ function editReligions() {
const currentFill = el.getAttribute("fill");
const religion = +el.parentNode.parentNode.dataset.id;
- const callback = function(fill) {
+ const callback = function (fill) {
el.setAttribute("fill", fill);
pack.religions[religion].color = fill;
- relig.select("#religion"+religion).attr("fill", fill);
- debug.select("#religionsCenter"+religion).attr("fill", fill);
- }
+ relig.select("#religion" + religion).attr("fill", fill);
+ debug.select("#religionsCenter" + religion).attr("fill", fill);
+ };
openPicker(currentFill, callback);
}
@@ -200,7 +231,10 @@ function editReligions() {
const religion = +this.parentNode.dataset.id;
this.parentNode.dataset.name = this.value;
pack.religions[religion].name = this.value;
- pack.religions[religion].code = abbreviate(this.value, pack.religions.map(c => c.code));
+ pack.religions[religion].code = abbreviate(
+ this.value,
+ pack.religions.map(c => c.code)
+ );
}
function religionChangeType() {
@@ -233,9 +267,12 @@ function editReligions() {
function changePopulation() {
const religion = +this.parentNode.dataset.id;
const r = pack.religions[religion];
- if (!r.cells) {tip("Religion does not have any cells, cannot change population", false, "error"); return;}
- const rural = rn(r.rural * populationRate.value);
- const urban = rn(r.urban * populationRate.value * urbanization.value);
+ if (!r.cells) {
+ tip("Religion does not have any cells, cannot change population", false, "error");
+ return;
+ }
+ const rural = rn(r.rural * populationRate);
+ const urban = rn(r.urban * populationRate * urbanization);
const total = rural + urban;
const l = n => Number(n).toLocaleString();
const burgs = pack.burgs.filter(b => !b.removed && pack.cells.religion[b.cell] === religion);
@@ -243,52 +280,60 @@ function editReligions() {
alertMessage.innerHTML = `
Please note all population of religion territory is considered
believers of this religion. It means believers number change will directly change population
Rural:
- Urban:
+ Urban:
Total believers: ${l(total)} ⇒ ${l(total)} (100 %)
`;
- const update = function() {
+ const update = function () {
const totalNew = ruralPop.valueAsNumber + urbanPop.valueAsNumber;
if (isNaN(totalNew)) return;
totalPop.innerHTML = l(totalNew);
- totalPopPerc.innerHTML = rn(totalNew / total * 100);
- }
+ totalPopPerc.innerHTML = rn((totalNew / total) * 100);
+ };
ruralPop.oninput = () => update();
urbanPop.oninput = () => update();
$("#alert").dialog({
- resizable: false, title: "Change believers number", width: "24em", buttons: {
- Apply: function() {applyPopulationChange(); $(this).dialog("close");},
- Cancel: function() {$(this).dialog("close");}
- }, position: {my: "center", at: "center", of: "svg"}
+ resizable: false,
+ title: "Change believers number",
+ width: "24em",
+ buttons: {
+ Apply: function () {
+ applyPopulationChange();
+ $(this).dialog("close");
+ },
+ Cancel: function () {
+ $(this).dialog("close");
+ }
+ },
+ position: {my: "center", at: "center", of: "svg"}
});
function applyPopulationChange() {
const ruralChange = ruralPop.value / rural;
if (isFinite(ruralChange) && ruralChange !== 1) {
const cells = pack.cells.i.filter(i => pack.cells.religion[i] === religion);
- cells.forEach(i => pack.cells.pop[i] *= ruralChange);
+ cells.forEach(i => (pack.cells.pop[i] *= ruralChange));
}
if (!isFinite(ruralChange) && +ruralPop.value > 0) {
- const points = ruralPop.value / populationRate.value;
+ const points = ruralPop.value / populationRate;
const cells = pack.cells.i.filter(i => pack.cells.religion[i] === religion);
const pop = rn(points / cells.length);
- cells.forEach(i => pack.cells.pop[i] = pop);
+ cells.forEach(i => (pack.cells.pop[i] = pop));
}
const urbanChange = urbanPop.value / urban;
if (isFinite(urbanChange) && urbanChange !== 1) {
- burgs.forEach(b => b.population = rn(b.population * urbanChange, 4));
+ burgs.forEach(b => (b.population = rn(b.population * urbanChange, 4)));
}
if (!isFinite(urbanChange) && +urbanPop.value > 0) {
- const points = urbanPop.value / populationRate.value / urbanization.value;
+ const points = urbanPop.value / populationRate / urbanization;
const population = rn(points / burgs.length, 4);
- burgs.forEach(b => b.population = population);
+ burgs.forEach(b => (b.population = population));
}
refreshReligionsEditor();
}
-
}
function religionRemove() {
@@ -296,43 +341,59 @@ function editReligions() {
const religion = +this.parentNode.dataset.id;
alertMessage.innerHTML = "Are you sure you want to remove the religion?
This action cannot be reverted";
- $("#alert").dialog({resizable: false, title: "Remove religion",
+ $("#alert").dialog({
+ resizable: false,
+ title: "Remove religion",
buttons: {
- Remove: function() {
- relig.select("#religion"+religion).remove();
- relig.select("#religion-gap"+religion).remove();
- debug.select("#religionsCenter"+religion).remove();
+ Remove: function () {
+ relig.select("#religion" + religion).remove();
+ relig.select("#religion-gap" + religion).remove();
+ debug.select("#religionsCenter" + religion).remove();
- pack.cells.religion.forEach((r, i) => {if(r === religion) pack.cells.religion[i] = 0;});
+ pack.cells.religion.forEach((r, i) => {
+ if (r === religion) pack.cells.religion[i] = 0;
+ });
pack.religions[religion].removed = true;
const origin = pack.religions[religion].origin;
- pack.religions.forEach(r => {if(r.origin === religion) r.origin = origin;});
-
+ pack.religions.forEach(r => {
+ if (r.origin === religion) r.origin = origin;
+ });
+
refreshReligionsEditor();
$(this).dialog("close");
},
- Cancel: function() {$(this).dialog("close");}
+ Cancel: function () {
+ $(this).dialog("close");
+ }
}
});
}
function drawReligionCenters() {
debug.select("#religionCenters").remove();
- const religionCenters = debug.append("g").attr("id", "religionCenters")
- .attr("stroke-width", 1.2).attr("stroke", "#444444").style("cursor", "move");
+ const religionCenters = debug.append("g").attr("id", "religionCenters").attr("stroke-width", 1.2).attr("stroke", "#444444").style("cursor", "move");
const data = pack.religions.filter(r => r.i && r.center && r.cells && !r.removed);
- religionCenters.selectAll("circle").data(data).enter().append("circle")
- .attr("id", d => "religionsCenter"+d.i).attr("data-id", d => d.i)
- .attr("r", 4).attr("fill", d => d.color)
- .attr("cx", d => pack.cells.p[d.center][0]).attr("cy", d => pack.cells.p[d.center][1])
+ religionCenters
+ .selectAll("circle")
+ .data(data)
+ .enter()
+ .append("circle")
+ .attr("id", d => "religionsCenter" + d.i)
+ .attr("data-id", d => d.i)
+ .attr("r", 4)
+ .attr("fill", d => d.color)
+ .attr("cx", d => pack.cells.p[d.center][0])
+ .attr("cy", d => pack.cells.p[d.center][1])
.on("mouseenter", d => {
- tip(d.name+ ". Drag to move the religion center", true);
+ tip(d.name + ". Drag to move the religion center", true);
religionHighlightOn(event);
- }).on("mouseleave", d => {
- tip('', true);
+ })
+ .on("mouseleave", d => {
+ tip("", true);
religionHighlightOff(event);
- }).call(d3.drag().on("start", religionCenterDrag));
+ })
+ .call(d3.drag().on("start", religionCenterDrag));
}
function religionCenterDrag() {
@@ -347,8 +408,14 @@ function editReligions() {
}
function toggleLegend() {
- if (legend.selectAll("*").size()) {clearLegend(); return;}; // hide legend
- const data = pack.religions.filter(r => r.i && !r.removed && r.area).sort((a, b) => b.area - a.area).map(r => [r.i, r.color, r.name]);
+ if (legend.selectAll("*").size()) {
+ clearLegend();
+ return;
+ } // hide legend
+ const data = pack.religions
+ .filter(r => r.i && !r.removed && r.area)
+ .sort((a, b) => b.area - a.area)
+ .map(r => [r.i, r.color, r.name]);
drawLegend("Religions", data);
}
@@ -358,9 +425,9 @@ function editReligions() {
const totalArea = +religionsFooterArea.dataset.area;
const totalPopulation = +religionsFooterPopulation.dataset.population;
- body.querySelectorAll(":scope > div").forEach(function(el) {
- el.querySelector(".biomeArea").innerHTML = rn(+el.dataset.area / totalArea * 100) + "%";
- el.querySelector(".culturePopulation").innerHTML = rn(+el.dataset.population / totalPopulation * 100) + "%";
+ body.querySelectorAll(":scope > div").forEach(function (el) {
+ el.querySelector(".biomeArea").innerHTML = rn((+el.dataset.area / totalArea) * 100) + "%";
+ el.querySelector(".culturePopulation").innerHTML = rn((+el.dataset.population / totalPopulation) * 100) + "%";
});
} else {
body.dataset.type = "absolute";
@@ -372,11 +439,18 @@ function editReligions() {
// build hierarchy tree
pack.religions[0].origin = null;
const religions = pack.religions.filter(r => !r.removed);
- if (religions.length < 3) {tip("Not enough religions to show hierarchy", false, "error"); return;}
- const root = d3.stratify().id(d => d.i).parentId(d => d.origin)(religions);
+ if (religions.length < 3) {
+ tip("Not enough religions to show hierarchy", false, "error");
+ return;
+ }
+ const root = d3
+ .stratify()
+ .id(d => d.i)
+ .parentId(d => d.origin)(religions);
const treeWidth = root.leaves().length;
const treeHeight = root.height;
- const width = treeWidth * 40, height = treeHeight * 60;
+ const width = treeWidth * 40,
+ height = treeHeight * 60;
const margin = {top: 10, right: 10, bottom: -5, left: 10};
const w = width - margin.left - margin.right;
@@ -385,8 +459,7 @@ function editReligions() {
// prepare svg
alertMessage.innerHTML = "
";
- const svg = d3.select("#alertMessage").insert("svg", "#religionInfo").attr("id", "hierarchy")
- .attr("width", width).attr("height", height).style("text-anchor", "middle");
+ const svg = d3.select("#alertMessage").insert("svg", "#religionInfo").attr("id", "hierarchy").attr("width", width).attr("height", height).style("text-anchor", "middle");
const graph = svg.append("g").attr("transform", `translate(10, -45)`);
const links = graph.append("g").attr("fill", "none").attr("stroke", "#aaaaaa");
const nodes = graph.append("g");
@@ -394,45 +467,70 @@ function editReligions() {
renderTree();
function renderTree() {
treeLayout(root);
- links.selectAll('path').data(root.links()).enter()
- .append('path').attr("d", d => {return "M" + d.source.x + "," + d.source.y
- + "C" + d.source.x + "," + (d.source.y * 3 + d.target.y) / 4
- + " " + d.target.x + "," + (d.source.y * 2 + d.target.y) / 3
- + " " + d.target.x + "," + d.target.y;});
+ links
+ .selectAll("path")
+ .data(root.links())
+ .enter()
+ .append("path")
+ .attr("d", d => {
+ return "M" + d.source.x + "," + d.source.y + "C" + d.source.x + "," + (d.source.y * 3 + d.target.y) / 4 + " " + d.target.x + "," + (d.source.y * 2 + d.target.y) / 3 + " " + d.target.x + "," + d.target.y;
+ });
- const node = nodes.selectAll('g').data(root.descendants()).enter()
- .append('g').attr("data-id", d => d.data.i).attr("stroke", "#333333")
+ const node = nodes
+ .selectAll("g")
+ .data(root.descendants())
+ .enter()
+ .append("g")
+ .attr("data-id", d => d.data.i)
+ .attr("stroke", "#333333")
.attr("transform", d => `translate(${d.x}, ${d.y})`)
.on("mouseenter", () => religionHighlightOn(event))
.on("mouseleave", () => religionHighlightOff(event))
.call(d3.drag().on("start", d => dragToReorigin(d)));
- node.append("path").attr("d", d => {
- if (d.data.type === "Folk") return "M11.3,0A11.3,11.3,0,1,1,-11.3,0A11.3,11.3,0,1,1,11.3,0"; else // circle
- if (d.data.type === "Heresy") return "M0,-14L14,0L0,14L-14,0Z"; else // diamond
- if (d.data.type === "Cult") return "M-6.5,-11.26l13,0l6.5,11.26l-6.5,11.26l-13,0l-6.5,-11.26Z"; else // hex
- if (!d.data.i) return "M5,0A5,5,0,1,1,-5,0A5,5,0,1,1,5,0"; else // small circle
- return "M-11,-11h22v22h-22Z"; // square
- }).attr("fill", d => d.data.i ? d.data.color : "#ffffff")
- .attr("stroke-dasharray", d => d.data.cells ? "null" : "1");
+ node
+ .append("path")
+ .attr("d", d => {
+ if (d.data.type === "Folk") return "M11.3,0A11.3,11.3,0,1,1,-11.3,0A11.3,11.3,0,1,1,11.3,0";
+ // circle
+ else if (d.data.type === "Heresy") return "M0,-14L14,0L0,14L-14,0Z";
+ // diamond
+ else if (d.data.type === "Cult") return "M-6.5,-11.26l13,0l6.5,11.26l-6.5,11.26l-13,0l-6.5,-11.26Z";
+ // hex
+ else if (!d.data.i) return "M5,0A5,5,0,1,1,-5,0A5,5,0,1,1,5,0";
+ // small circle
+ else return "M-11,-11h22v22h-22Z"; // square
+ })
+ .attr("fill", d => (d.data.i ? d.data.color : "#ffffff"))
+ .attr("stroke-dasharray", d => (d.data.cells ? "null" : "1"));
- node.append("text").attr("dy", ".35em").text(d => d.data.i ? d.data.code : '');
+ node
+ .append("text")
+ .attr("dy", ".35em")
+ .text(d => (d.data.i ? d.data.code : ""));
}
$("#alert").dialog({
- title: "Religions tree", width: fitContent(), resizable: false,
- position: {my: "left center", at: "left+10 center", of: "svg"}, buttons: {},
- close: () => {alertMessage.innerHTML = "";}
+ title: "Religions tree",
+ width: fitContent(),
+ resizable: false,
+ position: {my: "left center", at: "left+10 center", of: "svg"},
+ buttons: {},
+ close: () => {
+ alertMessage.innerHTML = "";
+ }
});
function dragToReorigin(d) {
- if (isCtrlClick(d3.event.sourceEvent)) {changeCode(d); return;}
+ if (isCtrlClick(d3.event.sourceEvent)) {
+ changeCode(d);
+ return;
+ }
- const originLine = graph.append("path")
- .attr("class", "dragLine").attr("d", `M${d.x},${d.y}L${d.x},${d.y}`);
+ const originLine = graph.append("path").attr("class", "dragLine").attr("d", `M${d.x},${d.y}L${d.x},${d.y}`);
d3.event.on("drag", () => {
- originLine.attr("d", `M${d.x},${d.y}L${d3.event.x},${d3.event.y}`)
+ originLine.attr("d", `M${d.x},${d.y}L${d3.event.x},${d3.event.y}`);
});
d3.event.on("end", () => {
@@ -446,14 +544,17 @@ function editReligions() {
if (newOrigin == religion) newOrigin = 0; // move to top
if (newOrigin && d.descendants().some(node => node.id == newOrigin)) return; // cannot be a child of its own child
pack.religions[religion].origin = d.data.origin = newOrigin; // change data
- showHierarchy() // update hierarchy
+ showHierarchy(); // update hierarchy
});
}
function changeCode(d) {
- prompt(`Please provide an abbreviation for ${d.data.name}`, {default:d.data.code}, v => {
+ prompt(`Please provide an abbreviation for ${d.data.name}`, {default: d.data.code}, v => {
pack.religions[d.data.i].code = v;
- nodes.select("g[data-id='"+d.data.i+"']").select("text").text(v);
+ nodes
+ .select("g[data-id='" + d.data.i + "']")
+ .select("text")
+ .text(v);
});
}
}
@@ -467,20 +568,17 @@ function editReligions() {
if (!layerIsOn("toggleReligions")) toggleReligions();
customization = 7;
relig.append("g").attr("id", "temp");
- document.querySelectorAll("#religionsBottom > button").forEach(el => el.style.display = "none");
+ document.querySelectorAll("#religionsBottom > button").forEach(el => (el.style.display = "none"));
document.getElementById("religionsManuallyButtons").style.display = "inline-block";
debug.select("#religionCenters").style("display", "none");
religionsEditor.querySelectorAll(".hide").forEach(el => el.classList.add("hidden"));
religionsFooter.style.display = "none";
- body.querySelectorAll("div > input, select, span, svg").forEach(e => e.style.pointerEvents = "none");
+ body.querySelectorAll("div > input, select, span, svg").forEach(e => (e.style.pointerEvents = "none"));
$("#religionsEditor").dialog({position: {my: "right top", at: "right-10 top+10", of: "svg"}});
tip("Click on religion to select, drag the circle to change religion", true);
- viewbox.style("cursor", "crosshair")
- .on("click", selectReligionOnMapClick)
- .call(d3.drag().on("start", dragReligionBrush))
- .on("touchmove mousemove", moveReligionBrush);
+ viewbox.style("cursor", "crosshair").on("click", selectReligionOnMapClick).call(d3.drag().on("start", dragReligionBrush)).on("touchmove mousemove", moveReligionBrush);
body.querySelector("div").classList.add("selected");
}
@@ -496,13 +594,13 @@ function editReligions() {
const i = findCell(point[0], point[1]);
if (pack.cells.h[i] < 20) return;
- const assigned = relig.select("#temp").select("polygon[data-cell='"+i+"']");
+ const assigned = relig.select("#temp").select("polygon[data-cell='" + i + "']");
const religion = assigned.size() ? +assigned.attr("data-religion") : pack.cells.religion[i];
body.querySelector("div.selected").classList.remove("selected");
- body.querySelector("div[data-id='"+religion+"']").classList.add("selected");
+ body.querySelector("div[data-id='" + religion + "']").classList.add("selected");
}
-
+
function dragReligionBrush() {
const r = +religionsManuallyBrushNumber.value;
@@ -510,7 +608,7 @@ function editReligions() {
if (!d3.event.dx && !d3.event.dy) return;
const p = d3.mouse(this);
moveCircle(p[0], p[1], r);
-
+
const found = r > 5 ? findAll(p[0], p[1], r) : [findCell(p[0], p[1], r)];
const selection = found.filter(isLand);
if (selection) changeReligionForSelection(selection);
@@ -524,8 +622,8 @@ function editReligions() {
const r = +selected.dataset.id; // religionNew
const color = pack.religions[r].color || "#ffffff";
- selection.forEach(function(i) {
- const exists = temp.select("polygon[data-cell='"+i+"']");
+ selection.forEach(function (i) {
+ const exists = temp.select("polygon[data-cell='" + i + "']");
const religionOld = exists.size() ? +exists.attr("data-religion") : pack.cells.religion[i];
if (r === religionOld) return;
@@ -544,7 +642,7 @@ function editReligions() {
function applyReligionsManualAssignent() {
const changed = relig.select("#temp").selectAll("polygon");
- changed.each(function() {
+ changed.each(function () {
const i = +this.dataset.cell;
const r = +this.dataset.religion;
pack.cells.religion[i] = r;
@@ -557,18 +655,18 @@ function editReligions() {
}
exitReligionsManualAssignment();
}
-
+
function exitReligionsManualAssignment(close) {
customization = 0;
relig.select("#temp").remove();
removeCircle();
- document.querySelectorAll("#religionsBottom > button").forEach(el => el.style.display = "inline-block");
+ document.querySelectorAll("#religionsBottom > button").forEach(el => (el.style.display = "inline-block"));
document.getElementById("religionsManuallyButtons").style.display = "none";
religionsEditor.querySelectorAll(".hide").forEach(el => el.classList.remove("hidden"));
religionsFooter.style.display = "block";
- body.querySelectorAll("div > input, select, span, svg").forEach(e => e.style.pointerEvents = "all");
- if(!close) $("#religionsEditor").dialog({position: {my: "right top", at: "right-10 top+10", of: "svg"}});
+ body.querySelectorAll("div > input, select, span, svg").forEach(e => (e.style.pointerEvents = "all"));
+ if (!close) $("#religionsEditor").dialog({position: {my: "right top", at: "right-10 top+10", of: "svg"}});
debug.select("#religionCenters").style("display", null);
restoreDefaultEvents();
@@ -578,28 +676,37 @@ function editReligions() {
}
function enterAddReligionMode() {
- if (this.classList.contains("pressed")) {exitAddReligionMode(); return;};
+ if (this.classList.contains("pressed")) {
+ exitAddReligionMode();
+ return;
+ }
customization = 8;
this.classList.add("pressed");
tip("Click on the map to add a new religion", true);
viewbox.style("cursor", "crosshair").on("click", addReligion);
- body.querySelectorAll("div > input, select, span, svg").forEach(e => e.style.pointerEvents = "none");
+ body.querySelectorAll("div > input, select, span, svg").forEach(e => (e.style.pointerEvents = "none"));
}
function exitAddReligionMode() {
customization = 0;
restoreDefaultEvents();
clearMainTip();
- body.querySelectorAll("div > input, select, span, svg").forEach(e => e.style.pointerEvents = "all");
+ body.querySelectorAll("div > input, select, span, svg").forEach(e => (e.style.pointerEvents = "all"));
if (religionsAdd.classList.contains("pressed")) religionsAdd.classList.remove("pressed");
}
function addReligion() {
const point = d3.mouse(this);
const center = findCell(point[0], point[1]);
- if (pack.cells.h[center] < 20) {tip("You cannot place religion center into the water. Please click on a land cell", false, "error"); return;}
+ if (pack.cells.h[center] < 20) {
+ tip("You cannot place religion center into the water. Please click on a land cell", false, "error");
+ return;
+ }
const occupied = pack.religions.some(r => !r.removed && r.center === center);
- if (occupied) {tip("This cell is already a religion center. Please select a different cell", false, "error"); return;}
+ if (occupied) {
+ tip("This cell is already a religion center. Please select a different cell", false, "error");
+ return;
+ }
if (d3.event.shiftKey === false) exitAddReligionMode();
Religions.add(center);
@@ -611,9 +718,9 @@ function editReligions() {
function downloadReligionsData() {
const unit = areaUnit.value === "square" ? distanceUnitInput.value + "2" : areaUnit.value;
- let data = "Id,Religion,Color,Type,Form,Deity,Area "+unit+",Believers\n"; // headers
+ let data = "Id,Religion,Color,Type,Form,Deity,Area " + unit + ",Believers\n"; // headers
- body.querySelectorAll(":scope > div").forEach(function(el) {
+ body.querySelectorAll(":scope > div").forEach(function (el) {
data += el.dataset.id + ",";
data += el.dataset.name + ",";
data += el.dataset.color + ",";
@@ -627,11 +734,10 @@ function editReligions() {
const name = getFileName("Religions") + ".csv";
downloadFile(data, name);
}
-
+
function closeReligionsEditor() {
debug.select("#religionCenters").remove();
exitReligionsManualAssignment("close");
exitAddReligionMode();
}
-
-}
\ No newline at end of file
+}
diff --git a/modules/ui/states-editor.js b/modules/ui/states-editor.js
index ffa86812..441b551e 100644
--- a/modules/ui/states-editor.js
+++ b/modules/ui/states-editor.js
@@ -15,7 +15,10 @@ function editStates() {
modules.editStates = true;
$("#statesEditor").dialog({
- title: "States Editor", resizable: false, width: fitContent(), close: closeStatesEditor,
+ title: "States Editor",
+ resizable: false,
+ width: fitContent(),
+ close: closeStatesEditor,
position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}
});
@@ -37,26 +40,35 @@ function editStates() {
document.getElementById("statesAdd").addEventListener("click", enterAddStateMode);
document.getElementById("statesExport").addEventListener("click", downloadStatesData);
- body.addEventListener("click", function(ev) {
- const el = ev.target, cl = el.classList, line = el.parentNode, state = +line.dataset.id;
- if (cl.contains("fillRect")) stateChangeFill(el); else
- if (cl.contains("name")) editStateName(state); else
- if (cl.contains("coaIcon")) editEmblem("state", "stateCOA"+state, pack.states[state]); else
- if (cl.contains("icon-star-empty")) stateCapitalZoomIn(state); else
- if (cl.contains("culturePopulation")) changePopulation(state); else
- if (cl.contains("icon-pin")) toggleFog(state, cl); else
- if (cl.contains("icon-trash-empty")) stateRemovePrompt(state);
+ body.addEventListener("click", function (ev) {
+ const el = ev.target,
+ cl = el.classList,
+ line = el.parentNode,
+ state = +line.dataset.id;
+ if (cl.contains("fillRect")) stateChangeFill(el);
+ else if (cl.contains("name")) editStateName(state);
+ else if (cl.contains("coaIcon")) editEmblem("state", "stateCOA" + state, pack.states[state]);
+ else if (cl.contains("icon-star-empty")) stateCapitalZoomIn(state);
+ else if (cl.contains("culturePopulation")) changePopulation(state);
+ else if (cl.contains("icon-pin")) toggleFog(state, cl);
+ else if (cl.contains("icon-trash-empty")) stateRemovePrompt(state);
});
- body.addEventListener("input", function(ev) {
- const el = ev.target, cl = el.classList, line = el.parentNode, state = +line.dataset.id;
- if (cl.contains("stateCapital")) stateChangeCapitalName(state, line, el.value); else
- if (cl.contains("cultureType")) stateChangeType(state, line, el.value); else
- if (cl.contains("statePower")) stateChangeExpansionism(state, line, el.value);
+ body.addEventListener("input", function (ev) {
+ const el = ev.target,
+ cl = el.classList,
+ line = el.parentNode,
+ state = +line.dataset.id;
+ if (cl.contains("stateCapital")) stateChangeCapitalName(state, line, el.value);
+ else if (cl.contains("cultureType")) stateChangeType(state, line, el.value);
+ else if (cl.contains("statePower")) stateChangeExpansionism(state, line, el.value);
});
- body.addEventListener("change", function(ev) {
- const el = ev.target, cl = el.classList, line = el.parentNode, state = +line.dataset.id;
+ body.addEventListener("change", function (ev) {
+ const el = ev.target,
+ cl = el.classList,
+ line = el.parentNode,
+ state = +line.dataset.id;
if (cl.contains("stateCulture")) stateChangeCulture(state, line, el.value);
});
@@ -69,19 +81,22 @@ function editStates() {
function statesEditorAddLines() {
const unit = areaUnit.value === "square" ? " " + distanceUnitInput.value + "²" : " " + areaUnit.value;
const hidden = statesRegenerateButtons.style.display === "block" ? "" : "hidden"; // show/hide regenerate columns
- let lines = "", totalArea = 0, totalPopulation = 0, totalBurgs = 0;
+ let lines = "",
+ totalArea = 0,
+ totalPopulation = 0,
+ totalBurgs = 0;
for (const s of pack.states) {
if (s.removed) continue;
- const area = s.area * (distanceScaleInput.value ** 2);
- const rural = s.rural * populationRate.value;
- const urban = s.urban * populationRate.value * urbanization.value;
+ const area = s.area * distanceScaleInput.value ** 2;
+ const rural = s.rural * populationRate;
+ const urban = s.urban * populationRate * urbanization;
const population = rn(rural + urban);
const populationTip = `Total population: ${si(population)}; Rural population: ${si(rural)}; Urban population: ${si(urban)}. Click to change`;
totalArea += area;
totalPopulation += population;
totalBurgs += s.burgs;
- const focused = defs.select("#fog #focusState"+s.i).size();
+ const focused = defs.select("#fog #focusState" + s.i).size();
if (!s.i) {
// Neutral line
@@ -110,7 +125,7 @@ function editStates() {
}
const capital = pack.burgs[s.capital].name;
- COArenderer.trigger("stateCOA"+s.i, s.coa);
+ COArenderer.trigger("stateCOA" + s.i, s.coa);
lines += `
@@ -131,7 +146,7 @@ function editStates() {
${s.cells}
-
+
`;
}
@@ -152,21 +167,28 @@ function editStates() {
el.addEventListener("mouseleave", ev => stateHighlightOff(ev));
});
- if (body.dataset.type === "percentage") {body.dataset.type = "absolute"; togglePercentageMode();}
+ if (body.dataset.type === "percentage") {
+ body.dataset.type = "absolute";
+ togglePercentageMode();
+ }
applySorting(statesHeader);
$("#statesEditor").dialog({width: fitContent()});
}
-
+
function getCultureOptions(culture) {
let options = "";
- pack.cultures.forEach(c => {if (!c.removed) { options += `
${c.name} ` }});
+ pack.cultures.forEach(c => {
+ if (!c.removed) {
+ options += `
${c.name} `;
+ }
+ });
return options;
}
function getTypeOptions(type) {
let options = "";
const types = ["Generic", "River", "Lake", "Naval", "Nomadic", "Hunting", "Highland"];
- types.forEach(t => options += `
${t} `);
+ types.forEach(t => (options += `
${t} `));
return options;
}
@@ -176,19 +198,23 @@ function editStates() {
const state = +event.target.dataset.id;
if (customization || !state) return;
- const d = regions.select("#state"+state).attr("d");
+ const d = regions.select("#state" + state).attr("d");
- const path = debug.append("path").attr("class", "highlight").attr("d", d)
- .attr("fill", "none").attr("stroke", "red").attr("stroke-width", 1).attr("opacity", 1)
- .attr("filter", "url(#blur1)");
+ const path = debug.append("path").attr("class", "highlight").attr("d", d).attr("fill", "none").attr("stroke", "red").attr("stroke-width", 1).attr("opacity", 1).attr("filter", "url(#blur1)");
- const l = path.node().getTotalLength(), dur = (l + 5000) / 2;
+ const l = path.node().getTotalLength(),
+ dur = (l + 5000) / 2;
const i = d3.interpolateString("0," + l, l + "," + l);
- path.transition().duration(dur).attrTween("stroke-dasharray", function() {return t => i(t)});
+ path
+ .transition()
+ .duration(dur)
+ .attrTween("stroke-dasharray", function () {
+ return t => i(t);
+ });
}
function stateHighlightOff() {
- debug.selectAll(".highlight").each(function() {
+ debug.selectAll(".highlight").each(function () {
d3.select(this).transition().duration(1000).attr("opacity", 0).remove();
});
}
@@ -197,20 +223,23 @@ function editStates() {
const currentFill = el.getAttribute("fill");
const state = +el.parentNode.parentNode.dataset.id;
- const callback = function(fill) {
+ const callback = function (fill) {
el.setAttribute("fill", fill);
pack.states[state].color = fill;
- statesBody.select("#state"+state).attr("fill", fill);
- statesBody.select("#state-gap"+state).attr("stroke", fill);
+ statesBody.select("#state" + state).attr("fill", fill);
+ statesBody.select("#state-gap" + state).attr("stroke", fill);
const halo = d3.color(fill) ? d3.color(fill).darker().hex() : "#666666";
- statesHalo.select("#state-border"+state).attr("stroke", halo);
+ statesHalo.select("#state-border" + state).attr("stroke", halo);
// recolor regiments
const solidColor = fill[0] === "#" ? fill : "#999";
const darkerColor = d3.color(solidColor).darker().hex();
- armies.select("#army"+state).attr("fill", solidColor);
- armies.select("#army"+state).selectAll("g > rect:nth-of-type(2)").attr("fill", darkerColor);
- }
+ armies.select("#army" + state).attr("fill", solidColor);
+ armies
+ .select("#army" + state)
+ .selectAll("g > rect:nth-of-type(2)")
+ .attr("fill", darkerColor);
+ };
openPicker(currentFill, callback);
}
@@ -231,10 +260,18 @@ function editStates() {
document.getElementById("stateNameEditorFull").value = s.fullName || "";
$("#stateNameEditor").dialog({
- resizable: false, title: "Change state name", buttons: {
- Apply: function() {applyNameChange(s); $(this).dialog("close");},
- Cancel: function() {$(this).dialog("close");}
- }, position: {my: "center", at: "center", of: "svg"}
+ resizable: false,
+ title: "Change state name",
+ buttons: {
+ Apply: function () {
+ applyNameChange(s);
+ $(this).dialog("close");
+ },
+ Cancel: function () {
+ $(this).dialog("close");
+ }
+ },
+ position: {my: "center", at: "center", of: "svg"}
});
if (modules.editStateName) return;
@@ -255,7 +292,7 @@ function editStates() {
}
function regenerateShortNameRandom() {
- const base = rand(nameBases.length-1);
+ const base = rand(nameBases.length - 1);
const name = Names.getState(Names.getBase(base), undefined, base);
document.getElementById("stateNameEditorShort").value = name;
}
@@ -278,8 +315,8 @@ function editStates() {
if (!form) return short;
if (!short && form) return "The " + form;
const tick = +stateNameEditorFullRegenerate.dataset.tick;
- stateNameEditorFullRegenerate.dataset.tick = tick+1;
- return tick%2 ? getAdjective(short) + " " + form : form + " of " + short;
+ stateNameEditorFullRegenerate.dataset.tick = tick + 1;
+ return tick % 2 ? getAdjective(short) + " " + form : form + " of " + short;
}
}
@@ -312,73 +349,85 @@ function editStates() {
const capital = pack.states[state].capital;
if (!capital) return;
pack.burgs[capital].name = value;
- document.querySelector("#burgLabel"+capital).textContent = value;
+ document.querySelector("#burgLabel" + capital).textContent = value;
}
function changePopulation(state) {
const s = pack.states[state];
- if (!s.cells) {tip("State does not have any cells, cannot change population", false, "error"); return;}
- const rural = rn(s.rural * populationRate.value);
- const urban = rn(s.urban * populationRate.value * urbanization.value);
+ if (!s.cells) {
+ tip("State does not have any cells, cannot change population", false, "error");
+ return;
+ }
+ const rural = rn(s.rural * populationRate);
+ const urban = rn(s.urban * populationRate * urbanization);
const total = rural + urban;
const l = n => Number(n).toLocaleString();
alertMessage.innerHTML = `
Rural:
- Urban:
+ Urban:
Total population: ${l(total)} ⇒ ${l(total)} (100 %)
`;
- const update = function() {
+ const update = function () {
const totalNew = ruralPop.valueAsNumber + urbanPop.valueAsNumber;
if (isNaN(totalNew)) return;
totalPop.innerHTML = l(totalNew);
- totalPopPerc.innerHTML = rn(totalNew / total * 100);
- }
+ totalPopPerc.innerHTML = rn((totalNew / total) * 100);
+ };
ruralPop.oninput = () => update();
urbanPop.oninput = () => update();
$("#alert").dialog({
- resizable: false, title: "Change state population", width: "24em", buttons: {
- Apply: function() {applyPopulationChange(); $(this).dialog("close");},
- Cancel: function() {$(this).dialog("close");}
- }, position: {my: "center", at: "center", of: "svg"}
+ resizable: false,
+ title: "Change state population",
+ width: "24em",
+ buttons: {
+ Apply: function () {
+ applyPopulationChange();
+ $(this).dialog("close");
+ },
+ Cancel: function () {
+ $(this).dialog("close");
+ }
+ },
+ position: {my: "center", at: "center", of: "svg"}
});
function applyPopulationChange() {
const ruralChange = ruralPop.value / rural;
if (isFinite(ruralChange) && ruralChange !== 1) {
const cells = pack.cells.i.filter(i => pack.cells.state[i] === state);
- cells.forEach(i => pack.cells.pop[i] *= ruralChange);
+ cells.forEach(i => (pack.cells.pop[i] *= ruralChange));
}
if (!isFinite(ruralChange) && +ruralPop.value > 0) {
- const points = ruralPop.value / populationRate.value;
+ const points = ruralPop.value / populationRate;
const cells = pack.cells.i.filter(i => pack.cells.state[i] === state);
const pop = points / cells.length;
- cells.forEach(i => pack.cells.pop[i] = pop);
+ cells.forEach(i => (pack.cells.pop[i] = pop));
}
const urbanChange = urbanPop.value / urban;
if (isFinite(urbanChange) && urbanChange !== 1) {
const burgs = pack.burgs.filter(b => !b.removed && b.state === state);
- burgs.forEach(b => b.population = rn(b.population * urbanChange, 4));
+ burgs.forEach(b => (b.population = rn(b.population * urbanChange, 4)));
}
if (!isFinite(urbanChange) && +urbanPop.value > 0) {
- const points = urbanPop.value / populationRate.value / urbanization.value;
+ const points = urbanPop.value / populationRate / urbanization;
const burgs = pack.burgs.filter(b => !b.removed && b.state === state);
const population = rn(points / burgs.length, 4);
- burgs.forEach(b => b.population = population);
+ burgs.forEach(b => (b.population = population));
}
refreshStatesEditor();
}
-
}
function stateCapitalZoomIn(state) {
const capital = pack.states[state].capital;
const l = burgLabels.select("[data-id='" + capital + "']");
- const x = +l.attr("x"), y = +l.attr("y");
+ const x = +l.attr("x"),
+ y = +l.attr("y");
zoomTo(x, y, 8, 2000);
}
@@ -392,13 +441,14 @@ function editStates() {
}
function stateChangeExpansionism(state, line, value) {
- line.dataset.expansionism = pack.states[state].expansionism = value;
+ line.dataset.expansionism = pack.states[state].expansionism = value;
recalculateStates();
}
function toggleFog(state, cl) {
if (customization) return;
- const path = statesBody.select("#state"+state).attr("d"), id = "focusState"+state;
+ const path = statesBody.select("#state" + state).attr("d"),
+ id = "focusState" + state;
cl.contains("inactive") ? fog(id, path) : unfog(id);
cl.toggle("inactive");
}
@@ -407,27 +457,35 @@ function editStates() {
if (customization) return;
alertMessage.innerHTML = "Are you sure you want to remove the state?
This action cannot be reverted";
- $("#alert").dialog({resizable: false, title: "Remove state",
+ $("#alert").dialog({
+ resizable: false,
+ title: "Remove state",
buttons: {
- Remove: function() {
+ Remove: function () {
$(this).dialog("close");
stateRemove(state);
},
- Cancel: function() {$(this).dialog("close");}
+ Cancel: function () {
+ $(this).dialog("close");
+ }
}
});
}
function stateRemove(state) {
- statesBody.select("#state"+state).remove();
- statesBody.select("#state-gap"+state).remove();
- statesHalo.select("#state-border"+state).remove();
- labels.select("#stateLabel"+state).remove();
- defs.select("#textPath_stateLabel"+state).remove();
+ statesBody.select("#state" + state).remove();
+ statesBody.select("#state-gap" + state).remove();
+ statesHalo.select("#state-border" + state).remove();
+ labels.select("#stateLabel" + state).remove();
+ defs.select("#textPath_stateLabel" + state).remove();
- unfog("focusState"+state);
- pack.burgs.forEach(b => {if(b.state === state) b.state = 0;});
- pack.cells.state.forEach((s, i) => {if(s === state) pack.cells.state[i] = 0;});
+ unfog("focusState" + state);
+ pack.burgs.forEach(b => {
+ if (b.state === state) b.state = 0;
+ });
+ pack.cells.state.forEach((s, i) => {
+ if (s === state) pack.cells.state[i] = 0;
+ });
// remove emblem
const coaId = "stateCOA" + state;
@@ -437,13 +495,15 @@ function editStates() {
// remove provinces
pack.states[state].provinces.forEach(p => {
pack.provinces[p] = {i: p, removed: true};
- pack.cells.province.forEach((pr, i) => {if(pr === p) pack.cells.province[i] = 0;});
+ pack.cells.province.forEach((pr, i) => {
+ if (pr === p) pack.cells.province[i] = 0;
+ });
const coaId = "provinceCOA" + p;
if (document.getElementById(coaId)) document.getElementById(coaId).remove();
emblems.select(`#provinceEmblems > use[data-i='${p}']`).remove();
const g = provs.select("#provincesBody");
- g.select("#province"+p).remove();
- g.select("#province-gap"+p).remove();
+ g.select("#province" + p).remove();
+ g.select("#province-gap" + p).remove();
});
// remove military
@@ -452,7 +512,7 @@ function editStates() {
const index = notes.findIndex(n => n.id === id);
if (index != -1) notes.splice(index, 1);
});
- armies.select("g#army"+state).remove();
+ armies.select("g#army" + state).remove();
const capital = pack.states[state].capital;
pack.burgs[capital].capital = 0;
@@ -462,15 +522,23 @@ function editStates() {
pack.states[state] = {i: state, removed: true};
debug.selectAll(".highlight").remove();
- if (!layerIsOn("toggleStates")) toggleStates(); else drawStates();
- if (!layerIsOn("toggleBorders")) toggleBorders(); else drawBorders();
+ if (!layerIsOn("toggleStates")) toggleStates();
+ else drawStates();
+ if (!layerIsOn("toggleBorders")) toggleBorders();
+ else drawBorders();
if (layerIsOn("toggleProvinces")) drawProvinces();
refreshStatesEditor();
}
function toggleLegend() {
- if (legend.selectAll("*").size()) {clearLegend(); return;}; // hide legend
- const data = pack.states.filter(s => s.i && !s.removed && s.cells).sort((a, b) => b.area - a.area).map(s => [s.i, s.color, s.name]);
+ if (legend.selectAll("*").size()) {
+ clearLegend();
+ return;
+ } // hide legend
+ const data = pack.states
+ .filter(s => s.i && !s.removed && s.cells)
+ .sort((a, b) => b.area - a.area)
+ .map(s => [s.i, s.color, s.name]);
drawLegend("States", data);
}
@@ -482,11 +550,11 @@ function editStates() {
const totalArea = +statesFooterArea.dataset.area;
const totalPopulation = +statesFooterPopulation.dataset.population;
- body.querySelectorAll(":scope > div").forEach(function(el) {
- el.querySelector(".stateCells").innerHTML = rn(+el.dataset.cells / totalCells * 100) + "%";
- el.querySelector(".stateBurgs").innerHTML = rn(+el.dataset.burgs / totalBurgs * 100) + "%";
- el.querySelector(".biomeArea").innerHTML = rn(+el.dataset.area / totalArea * 100) + "%";
- el.querySelector(".culturePopulation").innerHTML = rn(+el.dataset.population / totalPopulation * 100) + "%";
+ body.querySelectorAll(":scope > div").forEach(function (el) {
+ el.querySelector(".stateCells").innerHTML = rn((+el.dataset.cells / totalCells) * 100) + "%";
+ el.querySelector(".stateBurgs").innerHTML = rn((+el.dataset.burgs / totalBurgs) * 100) + "%";
+ el.querySelector(".biomeArea").innerHTML = rn((+el.dataset.area / totalArea) * 100) + "%";
+ el.querySelector(".culturePopulation").innerHTML = rn((+el.dataset.population / totalPopulation) * 100) + "%";
});
} else {
body.dataset.type = "absolute";
@@ -497,10 +565,15 @@ function editStates() {
function showStatesChart() {
// build hierarchy tree
const data = pack.states.filter(s => !s.removed);
- const root = d3.stratify().id(d => d.i).parentId(d => d.i ? 0 : null)(data)
- .sum(d => d.area).sort((a, b) => b.value - a.value);
+ const root = d3
+ .stratify()
+ .id(d => d.i)
+ .parentId(d => (d.i ? 0 : null))(data)
+ .sum(d => d.area)
+ .sort((a, b) => b.value - a.value);
- const width = 150 + 200 * uiSizeOutput.value, height = 150 + 200 * uiSizeOutput.value;
+ const width = 150 + 200 * uiSizeOutput.value,
+ height = 150 + 200 * uiSizeOutput.value;
const margin = {top: 0, right: -50, bottom: 0, left: -50};
const w = width - margin.left - margin.right;
const h = height - margin.top - margin.bottom;
@@ -515,46 +588,51 @@ function editStates() {
Burgs number
`;
alertMessage.innerHTML += `
`;
- const svg = d3.select("#alertMessage").insert("svg", "#statesInfo").attr("id", "statesTree")
- .attr("width", width).attr("height", height).style("font-family", "Almendra SC")
- .attr("text-anchor", "middle").attr("dominant-baseline", "central");
+ const svg = d3.select("#alertMessage").insert("svg", "#statesInfo").attr("id", "statesTree").attr("width", width).attr("height", height).style("font-family", "Almendra SC").attr("text-anchor", "middle").attr("dominant-baseline", "central");
const graph = svg.append("g").attr("transform", `translate(-50, 0)`);
document.getElementById("statesTreeType").addEventListener("change", updateChart);
treeLayout(root);
- const node = graph.selectAll("g").data(root.leaves()).enter()
- .append("g").attr("transform", d => `translate(${d.x},${d.y})`)
+ const node = graph
+ .selectAll("g")
+ .data(root.leaves())
+ .enter()
+ .append("g")
+ .attr("transform", d => `translate(${d.x},${d.y})`)
.attr("data-id", d => d.data.i)
.on("mouseenter", d => showInfo(event, d))
.on("mouseleave", d => hideInfo(event, d));
- node.append("circle").attr("fill", d => d.data.color).attr("r", d => d.r);
+ node
+ .append("circle")
+ .attr("fill", d => d.data.color)
+ .attr("r", d => d.r);
const exp = /(?=[A-Z][^A-Z])/g;
const lp = n => d3.max(n.split(exp).map(p => p.length)) + 1; // longest name part + 1
- node.append("text")
- .style("font-size", d => rn(d.r ** .97 * 4 / lp(d.data.name), 2) + "px")
- .selectAll("tspan").data(d => d.data.name.split(exp))
- .join("tspan").attr("x", 0).text(d => d)
- .attr("dy", (d, i, n) => `${i ? 1 : (n.length-1) / -2}em`);
+ node
+ .append("text")
+ .style("font-size", d => rn((d.r ** 0.97 * 4) / lp(d.data.name), 2) + "px")
+ .selectAll("tspan")
+ .data(d => d.data.name.split(exp))
+ .join("tspan")
+ .attr("x", 0)
+ .text(d => d)
+ .attr("dy", (d, i, n) => `${i ? 1 : (n.length - 1) / -2}em`);
function showInfo(ev, d) {
d3.select(ev.target).select("circle").classed("selected", 1);
const state = d.data.fullName;
const unit = areaUnit.value === "square" ? " " + distanceUnitInput.value + "²" : " " + areaUnit.value;
- const area = d.data.area * (distanceScaleInput.value ** 2) + unit;
- const rural = rn(d.data.rural * populationRate.value);
- const urban = rn(d.data.urban * populationRate.value * urbanization.value);
+ const area = d.data.area * distanceScaleInput.value ** 2 + unit;
+ const rural = rn(d.data.rural * populationRate);
+ const urban = rn(d.data.urban * populationRate * urbanization);
const option = statesTreeType.value;
- const value = option === "area" ? "Area: " + area
- : option === "rural" ? "Rural population: " + si(rural)
- : option === "urban" ? "Urban population: " + si(urban)
- : option === "burgs" ? "Burgs number: " + d.data.burgs
- : "Population: " + si(rural + urban);
+ const value = option === "area" ? "Area: " + area : option === "rural" ? "Rural population: " + si(rural) : option === "urban" ? "Urban population: " + si(urban) : option === "burgs" ? "Burgs number: " + d.data.burgs : "Population: " + si(rural + urban);
statesInfo.innerHTML = `${state}. ${value}`;
stateHighlightOn(ev);
@@ -568,30 +646,40 @@ function editStates() {
}
function updateChart() {
- const value = this.value === "area" ? d => d.area
- : this.value === "rural" ? d => d.rural
- : this.value === "urban" ? d => d.urban
- : this.value === "burgs" ? d => d.burgs
- : d => d.rural + d.urban;
+ const value = this.value === "area" ? d => d.area : this.value === "rural" ? d => d.rural : this.value === "urban" ? d => d.urban : this.value === "burgs" ? d => d.burgs : d => d.rural + d.urban;
root.sum(value);
node.data(treeLayout(root).leaves());
- node.transition().duration(1500).attr("transform", d => `translate(${d.x},${d.y})`)
- node.select("circle").transition().duration(1500).attr("r", d => d.r);
- node.select("text").transition().duration(1500)
- .style("font-size", d => rn(d.r ** .97 * 4 / lp(d.data.name), 2) + "px");
+ node
+ .transition()
+ .duration(1500)
+ .attr("transform", d => `translate(${d.x},${d.y})`);
+ node
+ .select("circle")
+ .transition()
+ .duration(1500)
+ .attr("r", d => d.r);
+ node
+ .select("text")
+ .transition()
+ .duration(1500)
+ .style("font-size", d => rn((d.r ** 0.97 * 4) / lp(d.data.name), 2) + "px");
}
$("#alert").dialog({
- title: "States bubble chart", width: fitContent(),
- position: {my: "left bottom", at: "left+10 bottom-10", of: "svg"}, buttons: {},
- close: () => {alertMessage.innerHTML = "";}
+ title: "States bubble chart",
+ width: fitContent(),
+ position: {my: "left bottom", at: "left+10 bottom-10", of: "svg"},
+ buttons: {},
+ close: () => {
+ alertMessage.innerHTML = "";
+ }
});
}
function openRegenerationMenu() {
- statesBottom.querySelectorAll(":scope > button").forEach(el => el.style.display = "none");
+ statesBottom.querySelectorAll(":scope > button").forEach(el => (el.style.display = "none"));
statesRegenerateButtons.style.display = "block";
statesEditor.querySelectorAll(".show").forEach(el => el.classList.remove("hidden"));
@@ -603,8 +691,10 @@ function editStates() {
BurgsAndStates.expandStates();
BurgsAndStates.generateProvinces();
- if (!layerIsOn("toggleStates")) toggleStates(); else drawStates();
- if (!layerIsOn("toggleBorders")) toggleBorders(); else drawBorders();
+ if (!layerIsOn("toggleStates")) toggleStates();
+ else drawStates();
+ if (!layerIsOn("toggleBorders")) toggleBorders();
+ else drawBorders();
if (layerIsOn("toggleProvinces")) drawProvinces();
if (adjustLabels.checked) BurgsAndStates.drawStateLabels();
refreshStatesEditor();
@@ -615,13 +705,13 @@ function editStates() {
if (!s.i || s.removed) return;
const expansionism = rn(Math.random() * 4 + 1, 1);
s.expansionism = expansionism;
- body.querySelector("div.states[data-id='"+s.i+"'] > input.statePower").value = expansionism;
+ body.querySelector("div.states[data-id='" + s.i + "'] > input.statePower").value = expansionism;
});
recalculateStates(true, true);
}
function exitRegenerationMenu() {
- statesBottom.querySelectorAll(":scope > button").forEach(el => el.style.display = "inline-block");
+ statesBottom.querySelectorAll(":scope > button").forEach(el => (el.style.display = "inline-block"));
statesRegenerateButtons.style.display = "none";
statesEditor.querySelectorAll(".show").forEach(el => el.classList.add("hidden"));
$("#statesEditor").dialog({position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}});
@@ -631,20 +721,17 @@ function editStates() {
if (!layerIsOn("toggleStates")) toggleStates();
customization = 2;
statesBody.append("g").attr("id", "temp");
- document.querySelectorAll("#statesBottom > button").forEach(el => el.style.display = "none");
+ document.querySelectorAll("#statesBottom > button").forEach(el => (el.style.display = "none"));
document.getElementById("statesManuallyButtons").style.display = "inline-block";
document.getElementById("statesHalo").style.display = "none";
statesEditor.querySelectorAll(".hide").forEach(el => el.classList.add("hidden"));
statesFooter.style.display = "none";
- body.querySelectorAll("div > input, select, span, svg").forEach(e => e.style.pointerEvents = "none");
+ body.querySelectorAll("div > input, select, span, svg").forEach(e => (e.style.pointerEvents = "none"));
$("#statesEditor").dialog({position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}});
tip("Click on state to select, drag the circle to change state", true);
- viewbox.style("cursor", "crosshair")
- .on("click", selectStateOnMapClick)
- .call(d3.drag().on("start", dragStateBrush))
- .on("touchmove mousemove", moveStateBrush);
+ viewbox.style("cursor", "crosshair").on("click", selectStateOnMapClick).call(d3.drag().on("start", dragStateBrush)).on("touchmove mousemove", moveStateBrush);
body.querySelector("div").classList.add("selected");
}
@@ -661,11 +748,11 @@ function editStates() {
const i = findCell(point[0], point[1]);
if (pack.cells.h[i] < 20) return;
- const assigned = statesBody.select("#temp").select("polygon[data-cell='"+i+"']");
+ const assigned = statesBody.select("#temp").select("polygon[data-cell='" + i + "']");
const state = assigned.size() ? +assigned.attr("data-state") : pack.cells.state[i];
body.querySelector("div.selected").classList.remove("selected");
- body.querySelector("div[data-id='"+state+"']").classList.add("selected");
+ body.querySelector("div[data-id='" + state + "']").classList.add("selected");
}
function dragStateBrush() {
@@ -690,8 +777,8 @@ function editStates() {
const stateNew = +selected.dataset.id;
const color = pack.states[stateNew].color || "#ffffff";
- selection.forEach(function(i) {
- const exists = temp.select("polygon[data-cell='"+i+"']");
+ selection.forEach(function (i) {
+ const exists = temp.select("polygon[data-cell='" + i + "']");
const stateOld = exists.size() ? +exists.attr("data-state") : pack.cells.state[i];
if (stateNew === stateOld) return;
if (i === pack.states[stateOld].center) return;
@@ -710,31 +797,40 @@ function editStates() {
}
function applyStatesManualAssignent() {
- const cells = pack.cells, affectedStates = [], affectedProvinces = [];
+ const cells = pack.cells,
+ affectedStates = [],
+ affectedProvinces = [];
- statesBody.select("#temp").selectAll("polygon").each(function() {
- const i = +this.dataset.cell;
- const c = +this.dataset.state;
- affectedStates.push(cells.state[i], c);
- affectedProvinces.push(cells.province[i]);
- cells.state[i] = c;
- if (cells.burg[i]) pack.burgs[cells.burg[i]].state = c;
- });
+ statesBody
+ .select("#temp")
+ .selectAll("polygon")
+ .each(function () {
+ const i = +this.dataset.cell;
+ const c = +this.dataset.state;
+ affectedStates.push(cells.state[i], c);
+ affectedProvinces.push(cells.province[i]);
+ cells.state[i] = c;
+ if (cells.burg[i]) pack.burgs[cells.burg[i]].state = c;
+ });
if (affectedStates.length) {
refreshStatesEditor();
- if (!layerIsOn("toggleStates")) toggleStates(); else drawStates();
+ if (!layerIsOn("toggleStates")) toggleStates();
+ else drawStates();
if (adjustLabels.checked) BurgsAndStates.drawStateLabels([...new Set(affectedStates)]);
adjustProvinces([...new Set(affectedProvinces)]);
- if (!layerIsOn("toggleBorders")) toggleBorders(); else drawBorders();
+ if (!layerIsOn("toggleBorders")) toggleBorders();
+ else drawBorders();
if (layerIsOn("toggleProvinces")) drawProvinces();
}
exitStatesManualAssignment();
}
function adjustProvinces(affectedProvinces) {
- const cells = pack.cells, provinces = pack.provinces, states = pack.states;
- const form = {"Zone":1, "Area":1, "Territory":2, "Province":1};
+ const cells = pack.cells,
+ provinces = pack.provinces,
+ states = pack.states;
+ const form = {Zone: 1, Area: 1, Territory: 2, Province: 1};
affectedProvinces.forEach(p => {
// do nothing if neutral lands are captured
@@ -757,7 +853,7 @@ function editStates() {
const part = states[owner].provinces.find(n => name.includes(provinces[n].name));
if (part) {
provinces[p].removed = true;
- provCells.filter(i => cells.state[i] === owner).forEach(i => cells.province[i] = part);
+ provCells.filter(i => cells.state[i] === owner).forEach(i => (cells.province[i] = part));
} else {
provinces[p].state = owner;
states[owner].provinces.push(p);
@@ -765,43 +861,49 @@ function editStates() {
}
} else {
provinces[p].removed = true;
- provCells.filter(i => !cells.state[i]).forEach(i => cells.province[i] = 0);
+ provCells.filter(i => !cells.state[i]).forEach(i => (cells.province[i] = 0));
}
// create new provinces for non-main part
- provStates.filter(s => s && s !== owner).forEach(s => createProvince(p, s, provCells.filter(i => cells.state[i] === s)));
+ provStates
+ .filter(s => s && s !== owner)
+ .forEach(s =>
+ createProvince(
+ p,
+ s,
+ provCells.filter(i => cells.state[i] === s)
+ )
+ );
});
function createProvince(initProv, state, provCells) {
const province = provinces.length;
- provCells.forEach(i => cells.province[i] = province);
+ provCells.forEach(i => (cells.province[i] = province));
const burgCell = provCells.find(i => cells.burg[i]);
const center = burgCell ? burgCell : provCells[0];
const burg = burgCell ? cells.burg[burgCell] : 0;
- const name = burgCell && P(.7) ? getAdjective(pack.burgs[burg].name)
- : getAdjective(states[state].name) + " " + provinces[initProv].name.split(" ").slice(-1)[0];
+ const name = burgCell && P(0.7) ? getAdjective(pack.burgs[burg].name) : getAdjective(states[state].name) + " " + provinces[initProv].name.split(" ").slice(-1)[0];
const formName = name.split(" ").length > 1 ? provinces[initProv].formName : rw(form);
const fullName = name + " " + formName;
const color = getMixedColor(states[state].color);
- provinces.push({i:province, state, center, burg, name, formName, fullName, color});
+ provinces.push({i: province, state, center, burg, name, formName, fullName, color});
}
-
}
-
+
function exitStatesManualAssignment(close) {
customization = 0;
statesBody.select("#temp").remove();
removeCircle();
- document.querySelectorAll("#statesBottom > button").forEach(el => el.style.display = "inline-block");
+ document.querySelectorAll("#statesBottom > button").forEach(el => (el.style.display = "inline-block"));
document.getElementById("statesManuallyButtons").style.display = "none";
document.getElementById("statesHalo").style.display = "block";
statesEditor.querySelectorAll(".hide:not(.show)").forEach(el => el.classList.remove("hidden"));
statesFooter.style.display = "block";
- body.querySelectorAll("div > input, select, span, svg").forEach(e => e.style.pointerEvents = "all");
- if(!close) $("#statesEditor").dialog({position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}});
+ body.querySelectorAll("div > input, select, span, svg").forEach(e => (e.style.pointerEvents = "all"));
+ if (!close) $("#statesEditor").dialog({position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}});
restoreDefaultEvents();
clearMainTip();
@@ -810,21 +912,32 @@ function editStates() {
}
function enterAddStateMode() {
- if (this.classList.contains("pressed")) {exitAddStateMode(); return;};
+ if (this.classList.contains("pressed")) {
+ exitAddStateMode();
+ return;
+ }
customization = 3;
this.classList.add("pressed");
tip("Click on the map to create a new capital or promote an existing burg", true);
viewbox.style("cursor", "crosshair").on("click", addState);
- body.querySelectorAll("div > input, select, span, svg").forEach(e => e.style.pointerEvents = "none");
+ body.querySelectorAll("div > input, select, span, svg").forEach(e => (e.style.pointerEvents = "none"));
}
function addState() {
- const states = pack.states, burgs = pack.burgs, cells = pack.cells;
+ const states = pack.states,
+ burgs = pack.burgs,
+ cells = pack.cells;
const point = d3.mouse(this);
const center = findCell(point[0], point[1]);
- if (cells.h[center] < 20) {tip("You cannot place state into the water. Please click on a land cell", false, "error"); return;}
+ if (cells.h[center] < 20) {
+ tip("You cannot place state into the water. Please click on a land cell", false, "error");
+ return;
+ }
let burg = cells.burg[center];
- if (burg && burgs[burg].capital) {tip("Existing capital cannot be selected as a new state capital! Select other cell", false, "error"); return;}
+ if (burg && burgs[burg].capital) {
+ tip("Existing capital cannot be selected as a new state capital! Select other cell", false, "error");
+ return;
+ }
if (!burg) burg = addBurg(point); // add new burg
const oldState = cells.state[center];
@@ -838,14 +951,14 @@ function editStates() {
if (d3.event.shiftKey === false) exitAddStateMode();
const culture = cells.culture[center];
- const basename = center%5 === 0 ? burgs[burg].name : Names.getCulture(culture);
+ const basename = center % 5 === 0 ? burgs[burg].name : Names.getCulture(culture);
const name = Names.getState(basename, culture);
const color = getRandomColor();
const pole = cells.p[center];
// generate emblem
const cultureType = pack.cultures[culture].type;
- const coa = COA.generate(burgs[burg].coa, .4, null, cultureType);
+ const coa = COA.generate(burgs[burg].coa, 0.4, null, cultureType);
coa.shield = COA.getShield(culture, null);
// update diplomacy and reverse relations
@@ -857,7 +970,8 @@ function editStates() {
}
let relations = states[oldState].diplomacy[s.i]; // relations between Nth state and old overlord
- if (s.i === oldState) relations = "Enemy"; // new state is Enemy to its old overlord
+ if (s.i === oldState) relations = "Enemy";
+ // new state is Enemy to its old overlord
else if (relations === "Ally") relations = "Suspicion";
else if (relations === "Friendly") relations = "Suspicion";
else if (relations === "Suspicion") relations = "Neutral";
@@ -874,21 +988,34 @@ function editStates() {
cells.state[center] = newState;
cells.province[center] = 0;
- states.push({i:newState, name, diplomacy, provinces:[], color, expansionism:.5, capital:burg, type:"Generic", center, culture, military:[], alert:1, coa, pole});
+ states.push({i: newState, name, diplomacy, provinces: [], color, expansionism: 0.5, capital: burg, type: "Generic", center, culture, military: [], alert: 1, coa, pole});
BurgsAndStates.collectStatistics();
BurgsAndStates.defineStateForms([newState]);
adjustProvinces([cells.province[center]]);
if (layerIsOn("toggleProvinces")) toggleProvinces();
- if (!layerIsOn("toggleStates")) toggleStates(); else drawStates();
- if (!layerIsOn("toggleBorders")) toggleBorders(); else drawBorders();
+ if (!layerIsOn("toggleStates")) toggleStates();
+ else drawStates();
+ if (!layerIsOn("toggleBorders")) toggleBorders();
+ else drawBorders();
// add label
- defs.select("#textPaths").append("path").attr("d", `M${pole[0]-50},${pole[1]+6}h${100}`).attr("id", "textPath_stateLabel"+newState);
- labels.select("#states")
- .append("text").attr("id", "stateLabel"+newState)
- .append("textPath").attr("xlink:href", "#textPath_stateLabel"+newState).attr("startOffset", "50%").attr("font-size", "50%")
- .append("tspan").attr("x", name.length * -3).text(name);
+ defs
+ .select("#textPaths")
+ .append("path")
+ .attr("d", `M${pole[0] - 50},${pole[1] + 6}h${100}`)
+ .attr("id", "textPath_stateLabel" + newState);
+ labels
+ .select("#states")
+ .append("text")
+ .attr("id", "stateLabel" + newState)
+ .append("textPath")
+ .attr("xlink:href", "#textPath_stateLabel" + newState)
+ .attr("startOffset", "50%")
+ .attr("font-size", "50%")
+ .append("tspan")
+ .attr("x", name.length * -3)
+ .text(name);
COArenderer.add("state", newState, coa, states[newState].pole[0], states[newState].pole[1]);
statesEditorAddLines();
@@ -898,15 +1025,15 @@ function editStates() {
customization = 0;
restoreDefaultEvents();
clearMainTip();
- body.querySelectorAll("div > input, select, span, svg").forEach(e => e.style.pointerEvents = "all");
+ body.querySelectorAll("div > input, select, span, svg").forEach(e => (e.style.pointerEvents = "all"));
if (statesAdd.classList.contains("pressed")) statesAdd.classList.remove("pressed");
}
function downloadStatesData() {
const unit = areaUnit.value === "square" ? distanceUnitInput.value + "2" : areaUnit.value;
- let data = "Id,State,Form,Color,Capital,Culture,Type,Expansionism,Cells,Burgs,Area "+unit+",Total Population,Rural Population,Urban Population\n"; // headers
+ let data = "Id,State,Form,Color,Capital,Culture,Type,Expansionism,Cells,Burgs,Area " + unit + ",Total Population,Rural Population,Urban Population\n"; // headers
- body.querySelectorAll(":scope > div").forEach(function(el) {
+ body.querySelectorAll(":scope > div").forEach(function (el) {
const key = parseInt(el.dataset.id);
data += el.dataset.id + ",";
data += el.dataset.name + ",";
@@ -920,8 +1047,8 @@ function editStates() {
data += el.dataset.burgs + ",";
data += el.dataset.area + ",";
data += el.dataset.population + ",";
- data += `${Math.round(pack.states[key].rural*populationRate.value)},`;
- data += `${Math.round(pack.states[key].urban*populationRate.value * urbanization.value)}\n`;
+ data += `${Math.round(pack.states[key].rural * populationRate)},`;
+ data += `${Math.round(pack.states[key].urban * populationRate * urbanization)}\n`;
});
const name = getFileName("States") + ".csv";
diff --git a/modules/ui/units-editor.js b/modules/ui/units-editor.js
index fe12ecf9..54571191 100644
--- a/modules/ui/units-editor.js
+++ b/modules/ui/units-editor.js
@@ -28,9 +28,9 @@ function editUnits() {
document.getElementById("barBackColor").addEventListener("input", changeScaleBarColor);
document.getElementById("populationRateOutput").addEventListener("input", changePopulationRate);
- document.getElementById("populationRate").addEventListener("change", changePopulationRate);
+ document.getElementById("populationRateInput").addEventListener("change", changePopulationRate);
document.getElementById("urbanizationOutput").addEventListener("input", changeUrbanizationRate);
- document.getElementById("urbanization").addEventListener("change", changeUrbanizationRate);
+ document.getElementById("urbanizationInput").addEventListener("change", changeUrbanizationRate);
document.getElementById("addLinearRuler").addEventListener("click", addRuler);
document.getElementById("addOpisometer").addEventListener("click", toggleOpisometerMode);
@@ -86,13 +86,11 @@ function editUnits() {
}
function changePopulationRate() {
- document.getElementById("populationRateOutput").value = this.value;
- document.getElementById("populationRate").value = this.value;
+ populationRate = +this.value;
}
function changeUrbanizationRate() {
- document.getElementById("urbanizationOutput").value = this.value;
- document.getElementById("urbanization").value = this.value;
+ urbanization = +this.value;
}
function restoreDefaultUnits() {
@@ -135,8 +133,8 @@ function editUnits() {
drawScaleBar();
// population
- populationRateOutput.value = populationRate.value = 1000;
- urbanizationOutput.value = urbanization.value = 1;
+ populationRate = populationRateOutput.value = populationRateInput.value = 1000;
+ urbanization = urbanizationOutput.value = urbanizationInput.value = 1;
localStorage.removeItem("populationRate");
localStorage.removeItem("urbanization");
}
diff --git a/modules/ui/zones-editor.js b/modules/ui/zones-editor.js
index 7e751b7c..a27b1f0f 100644
--- a/modules/ui/zones-editor.js
+++ b/modules/ui/zones-editor.js
@@ -9,7 +9,10 @@ function editZones() {
modules.editZones = true;
$("#zonesEditor").dialog({
- title: "Zones Editor", resizable: false, width: fitContent(), close: () => exitZonesManualAssignment("close"),
+ title: "Zones Editor",
+ resizable: false,
+ width: fitContent(),
+ close: () => exitZonesManualAssignment("close"),
position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}
});
@@ -25,19 +28,37 @@ function editZones() {
document.getElementById("zonesExport").addEventListener("click", downloadZonesData);
document.getElementById("zonesRemove").addEventListener("click", toggleEraseMode);
- body.addEventListener("click", function(ev) {
- const el = ev.target, cl = el.classList, zone = el.parentNode.dataset.id;
- if (cl.contains("culturePopulation")) {changePopulation(zone); return;}
- if (cl.contains("icon-trash-empty")) {zoneRemove(zone); return;}
- if (cl.contains("icon-eye")) {toggleVisibility(el); return;}
- if (cl.contains("icon-pin")) {toggleFog(zone, cl); return;}
- if (cl.contains("fillRect")) {changeFill(el); return;}
+ body.addEventListener("click", function (ev) {
+ const el = ev.target,
+ cl = el.classList,
+ zone = el.parentNode.dataset.id;
+ if (cl.contains("culturePopulation")) {
+ changePopulation(zone);
+ return;
+ }
+ if (cl.contains("icon-trash-empty")) {
+ zoneRemove(zone);
+ return;
+ }
+ if (cl.contains("icon-eye")) {
+ toggleVisibility(el);
+ return;
+ }
+ if (cl.contains("icon-pin")) {
+ toggleFog(zone, cl);
+ return;
+ }
+ if (cl.contains("fillRect")) {
+ changeFill(el);
+ return;
+ }
if (customization) selectZone(el);
});
- body.addEventListener("input", function(ev) {
- const el = ev.target, zone = el.parentNode.dataset.id;
- if (el.classList.contains("religionName")) zones.select("#"+zone).attr("data-description", el.value);
+ body.addEventListener("input", function (ev) {
+ const el = ev.target,
+ zone = el.parentNode.dataset.id;
+ if (el.classList.contains("religionName")) zones.select("#" + zone).attr("data-description", el.value);
});
// add line for each zone
@@ -45,17 +66,17 @@ function editZones() {
const unit = areaUnit.value === "square" ? " " + distanceUnitInput.value + "²" : " " + areaUnit.value;
let lines = "";
- zones.selectAll("g").each(function() {
+ zones.selectAll("g").each(function () {
const c = this.dataset.cells ? this.dataset.cells.split(",").map(c => +c) : [];
const description = this.dataset.description;
const fill = this.getAttribute("fill");
- const area = d3.sum(c.map(i => pack.cells.area[i])) * (distanceScaleInput.value ** 2);
- const rural = d3.sum(c.map(i => pack.cells.pop[i])) * populationRate.value;
- const urban = d3.sum(c.map(i => pack.cells.burg[i]).map(b => pack.burgs[b].population)) * populationRate.value * urbanization.value;
+ const area = d3.sum(c.map(i => pack.cells.area[i])) * distanceScaleInput.value ** 2;
+ const rural = d3.sum(c.map(i => pack.cells.pop[i])) * populationRate;
+ const urban = d3.sum(c.map(i => pack.cells.burg[i]).map(b => pack.burgs[b].population)) * populationRate * urbanization;
const population = rural + urban;
const populationTip = `Total population: ${si(population)}; Rural population: ${si(rural)}; Urban population: ${si(urban)}. Click to change`;
const inactive = this.style.display === "none";
- const focused = defs.select("#fog #focus"+this.id).size();
+ const focused = defs.select("#fog #focus" + this.id).size();
lines += `
@@ -67,8 +88,8 @@ function editZones() {
${si(population)}
-
-
+
+
`;
});
@@ -76,8 +97,8 @@ function editZones() {
body.innerHTML = lines;
// update footer
- const totalArea = zonesFooterArea.dataset.area = graphWidth * graphHeight * (distanceScaleInput.value ** 2);
- const totalPop = (d3.sum(pack.cells.pop) + d3.sum(pack.burgs.filter(b => !b.removed).map(b => b.population)) * urbanization.value) * populationRate.value;
+ const totalArea = (zonesFooterArea.dataset.area = graphWidth * graphHeight * distanceScaleInput.value ** 2);
+ const totalPop = (d3.sum(pack.cells.pop) + d3.sum(pack.burgs.filter(b => !b.removed).map(b => b.population)) * urbanization) * populationRate;
zonesFooterPopulation.dataset.population = totalPop;
zonesFooterNumber.innerHTML = zones.selectAll("g").size();
zonesFooterCells.innerHTML = pack.cells.i.length;
@@ -88,48 +109,53 @@ function editZones() {
body.querySelectorAll("div.states").forEach(el => el.addEventListener("mouseenter", ev => zoneHighlightOn(ev)));
body.querySelectorAll("div.states").forEach(el => el.addEventListener("mouseleave", ev => zoneHighlightOff(ev)));
- if (body.dataset.type === "percentage") {body.dataset.type = "absolute"; togglePercentageMode();}
+ if (body.dataset.type === "percentage") {
+ body.dataset.type = "absolute";
+ togglePercentageMode();
+ }
$("#zonesEditor").dialog({width: fitContent()});
}
function zoneHighlightOn(event) {
const zone = event.target.dataset.id;
- zones.select("#"+zone).style("outline", "1px solid red");
+ zones.select("#" + zone).style("outline", "1px solid red");
}
function zoneHighlightOff(event) {
const zone = event.target.dataset.id;
- zones.select("#"+zone).style("outline", null);
+ zones.select("#" + zone).style("outline", null);
}
$(body).sortable({items: "div.states", handle: ".icon-resize-vertical", containment: "parent", axis: "y", update: movezone});
function movezone(ev, ui) {
- const zone = $("#"+ui.item.attr("data-id"));
- const prev = $("#"+ui.item.prev().attr("data-id"));
- if (prev) {zone.insertAfter(prev); return;}
- const next = $("#"+ui.item.next().attr("data-id"));
+ const zone = $("#" + ui.item.attr("data-id"));
+ const prev = $("#" + ui.item.prev().attr("data-id"));
+ if (prev) {
+ zone.insertAfter(prev);
+ return;
+ }
+ const next = $("#" + ui.item.next().attr("data-id"));
if (next) zone.insertBefore(next);
}
function enterZonesManualAssignent() {
if (!layerIsOn("toggleZones")) toggleZones();
customization = 10;
- document.querySelectorAll("#zonesBottom > button").forEach(el => el.style.display = "none");
+ document.querySelectorAll("#zonesBottom > button").forEach(el => (el.style.display = "none"));
document.getElementById("zonesManuallyButtons").style.display = "inline-block";
zonesEditor.querySelectorAll(".hide").forEach(el => el.classList.add("hidden"));
zonesFooter.style.display = "none";
- body.querySelectorAll("div > input, select, svg").forEach(e => e.style.pointerEvents = "none");
+ body.querySelectorAll("div > input, select, svg").forEach(e => (e.style.pointerEvents = "none"));
$("#zonesEditor").dialog({position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}});
tip("Click to select a zone, drag to paint a zone", true);
- viewbox.style("cursor", "crosshair")
- .on("click", selectZoneOnMapClick)
- .call(d3.drag().on("start", dragZoneBrush))
- .on("touchmove mousemove", moveZoneBrush);
+ viewbox.style("cursor", "crosshair").on("click", selectZoneOnMapClick).call(d3.drag().on("start", dragZoneBrush)).on("touchmove mousemove", moveZoneBrush);
body.querySelector("div").classList.add("selected");
- zones.selectAll("g").each(function() {this.setAttribute("data-init", this.getAttribute("data-cells"));});
+ zones.selectAll("g").each(function () {
+ this.setAttribute("data-init", this.getAttribute("data-cells"));
+ });
}
function selectZone(el) {
@@ -154,9 +180,9 @@ function editZones() {
const selection = r > 5 ? findAll(p[0], p[1], r) : [findCell(p[0], p[1], r)];
if (!selection) return;
-
+
const selected = body.querySelector("div.selected");
- const zone = zones.select("#"+selected.dataset.id);
+ const zone = zones.select("#" + selected.dataset.id);
const base = zone.attr("id") + "_"; // id generic part
const dataCells = zone.attr("data-cells");
let cells = dataCells ? dataCells.split(",").map(i => +i) : [];
@@ -175,7 +201,10 @@ function editZones() {
selection.forEach(i => {
if (cells.includes(i)) return;
cells.push(i);
- zone.append("polygon").attr("points", getPackPolygon(i)).attr("id", base + i);
+ zone
+ .append("polygon")
+ .attr("points", getPackPolygon(i))
+ .attr("id", base + i);
});
}
@@ -191,10 +220,10 @@ function editZones() {
}
function applyZonesManualAssignent() {
- zones.selectAll("g").each(function() {
+ zones.selectAll("g").each(function () {
if (this.dataset.cells) return;
// all zone cells are removed
- unfog("focusZone"+this.id);
+ unfog("focusZone" + this.id);
this.style.display = "block";
});
@@ -204,15 +233,20 @@ function editZones() {
// restore initial zone cells
function cancelZonesManualAssignent() {
- zones.selectAll("g").each(function() {
+ zones.selectAll("g").each(function () {
const zone = d3.select(this);
const dataCells = zone.attr("data-init");
const cells = dataCells ? dataCells.split(",").map(i => +i) : [];
zone.attr("data-cells", cells);
zone.selectAll("*").remove();
const base = zone.attr("id") + "_"; // id generic part
- zone.selectAll("*").data(cells).enter().append("polygon")
- .attr("points", d => getPackPolygon(d)).attr("id", d => base + d);
+ zone
+ .selectAll("*")
+ .data(cells)
+ .enter()
+ .append("polygon")
+ .attr("points", d => getPackPolygon(d))
+ .attr("id", d => base + d);
});
exitZonesManualAssignment();
@@ -221,56 +255,68 @@ function editZones() {
function exitZonesManualAssignment(close) {
customization = 0;
removeCircle();
- document.querySelectorAll("#zonesBottom > button").forEach(el => el.style.display = "inline-block");
+ document.querySelectorAll("#zonesBottom > button").forEach(el => (el.style.display = "inline-block"));
document.getElementById("zonesManuallyButtons").style.display = "none";
zonesEditor.querySelectorAll(".hide:not(.show)").forEach(el => el.classList.remove("hidden"));
zonesFooter.style.display = "block";
- body.querySelectorAll("div > input, select, svg").forEach(e => e.style.pointerEvents = "all");
- if(!close) $("#zonesEditor").dialog({position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}});
+ body.querySelectorAll("div > input, select, svg").forEach(e => (e.style.pointerEvents = "all"));
+ if (!close) $("#zonesEditor").dialog({position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}});
restoreDefaultEvents();
clearMainTip();
- zones.selectAll("g").each(function() {this.removeAttribute("data-init");});
+ zones.selectAll("g").each(function () {
+ this.removeAttribute("data-init");
+ });
const selected = body.querySelector("div.selected");
if (selected) selected.classList.remove("selected");
}
function changeFill(el) {
const fill = el.getAttribute("fill");
- const callback = function(fill) {
+ const callback = function (fill) {
el.setAttribute("fill", fill);
document.getElementById(el.parentNode.parentNode.dataset.id).setAttribute("fill", fill);
- }
+ };
openPicker(fill, callback);
}
function toggleVisibility(el) {
- const zone = zones.select("#"+el.parentNode.dataset.id);
+ const zone = zones.select("#" + el.parentNode.dataset.id);
const inactive = zone.style("display") === "none";
inactive ? zone.style("display", "block") : zone.style("display", "none");
el.classList.toggle("inactive");
}
function toggleFog(z, cl) {
- const dataCells = zones.select("#"+z).attr("data-cells");
+ const dataCells = zones.select("#" + z).attr("data-cells");
if (!dataCells) return;
- const path = "M" + dataCells.split(",").map(c => getPackPolygon(+c)).join("M") + "Z", id = "focusZone"+z;
+ const path =
+ "M" +
+ dataCells
+ .split(",")
+ .map(c => getPackPolygon(+c))
+ .join("M") +
+ "Z",
+ id = "focusZone" + z;
cl.contains("inactive") ? fog(id, path) : unfog(id);
cl.toggle("inactive");
}
function toggleLegend() {
- if (legend.selectAll("*").size()) {clearLegend(); return;}; // hide legend
+ if (legend.selectAll("*").size()) {
+ clearLegend();
+ return;
+ } // hide legend
const data = [];
- zones.selectAll("g").each(function() {
+ zones.selectAll("g").each(function () {
const id = this.dataset.id;
const description = this.dataset.description;
const fill = this.getAttribute("fill");
- data.push([id, fill, description])
+ data.push([id, fill, description]);
});
drawLegend("Zones", data);
@@ -283,12 +329,11 @@ function editZones() {
const totalArea = +zonesFooterArea.dataset.area;
const totalPopulation = +zonesFooterPopulation.dataset.population;
- body.querySelectorAll(":scope > div").forEach(function(el) {
- el.querySelector(".stateCells").innerHTML = rn(+el.dataset.cells / totalCells * 100, 2) + "%";
- el.querySelector(".biomeArea").innerHTML = rn(+el.dataset.area / totalArea * 100, 2) + "%";
- el.querySelector(".culturePopulation").innerHTML = rn(+el.dataset.population / totalPopulation * 100, 2) + "%";
+ body.querySelectorAll(":scope > div").forEach(function (el) {
+ el.querySelector(".stateCells").innerHTML = rn((+el.dataset.cells / totalCells) * 100, 2) + "%";
+ el.querySelector(".biomeArea").innerHTML = rn((+el.dataset.area / totalArea) * 100, 2) + "%";
+ el.querySelector(".culturePopulation").innerHTML = rn((+el.dataset.population / totalPopulation) * 100, 2) + "%";
});
-
} else {
body.dataset.type = "absolute";
zonesEditorAddLines();
@@ -298,7 +343,7 @@ function editZones() {
function addZonesLayer() {
const id = getNextId("zone");
const description = "Unknown zone";
- const fill = "url(#hatch" + id.slice(4)%14 + ")";
+ const fill = "url(#hatch" + (id.slice(4) % 14) + ")";
zones.append("g").attr("id", id).attr("data-description", description).attr("data-cells", "").attr("fill", fill);
const unit = areaUnit.value === "square" ? " " + distanceUnitInput.value + "²" : " " + areaUnit.value;
@@ -323,9 +368,9 @@ function editZones() {
function downloadZonesData() {
const unit = areaUnit.value === "square" ? distanceUnitInput.value + "2" : areaUnit.value;
- let data = "Id,Fill,Description,Cells,Area "+unit+",Population\n"; // headers
+ let data = "Id,Fill,Description,Cells,Area " + unit + ",Population\n"; // headers
- body.querySelectorAll(":scope > div").forEach(function(el) {
+ body.querySelectorAll(":scope > div").forEach(function (el) {
data += el.dataset.id + ",";
data += el.dataset.fill + ",";
data += el.dataset.description + ",";
@@ -343,68 +388,83 @@ function editZones() {
}
function changePopulation(zone) {
- const dataCells = zones.select("#"+zone).attr("data-cells");
- const cells = dataCells ? dataCells.split(",").map(i => +i).filter(i => pack.cells.h[i] >= 20) : [];
- if (!cells.length) {tip("Zone does not have any land cells, cannot change population", false, "error"); return;}
+ const dataCells = zones.select("#" + zone).attr("data-cells");
+ const cells = dataCells
+ ? dataCells
+ .split(",")
+ .map(i => +i)
+ .filter(i => pack.cells.h[i] >= 20)
+ : [];
+ if (!cells.length) {
+ tip("Zone does not have any land cells, cannot change population", false, "error");
+ return;
+ }
const burgs = pack.burgs.filter(b => !b.removed && cells.includes(b.cell));
- const rural = rn(d3.sum(cells.map(i => pack.cells.pop[i])) * populationRate.value);
- const urban = rn(d3.sum(cells.map(i => pack.cells.burg[i]).map(b => pack.burgs[b].population)) * populationRate.value * urbanization.value);
+ const rural = rn(d3.sum(cells.map(i => pack.cells.pop[i])) * populationRate);
+ const urban = rn(d3.sum(cells.map(i => pack.cells.burg[i]).map(b => pack.burgs[b].population)) * populationRate * urbanization);
const total = rural + urban;
const l = n => Number(n).toLocaleString();
alertMessage.innerHTML = `
Rural:
- Urban:
+ Urban:
Total population: ${l(total)} ⇒ ${l(total)} (100 %)
`;
- const update = function() {
+ const update = function () {
const totalNew = ruralPop.valueAsNumber + urbanPop.valueAsNumber;
if (isNaN(totalNew)) return;
totalPop.innerHTML = l(totalNew);
- totalPopPerc.innerHTML = rn(totalNew / total * 100);
- }
+ totalPopPerc.innerHTML = rn((totalNew / total) * 100);
+ };
ruralPop.oninput = () => update();
urbanPop.oninput = () => update();
$("#alert").dialog({
- resizable: false, title: "Change zone population", width: "24em", buttons: {
- Apply: function() {applyPopulationChange(); $(this).dialog("close");},
- Cancel: function() {$(this).dialog("close");}
- }, position: {my: "center", at: "center", of: "svg"}
+ resizable: false,
+ title: "Change zone population",
+ width: "24em",
+ buttons: {
+ Apply: function () {
+ applyPopulationChange();
+ $(this).dialog("close");
+ },
+ Cancel: function () {
+ $(this).dialog("close");
+ }
+ },
+ position: {my: "center", at: "center", of: "svg"}
});
function applyPopulationChange() {
const ruralChange = ruralPop.value / rural;
if (isFinite(ruralChange) && ruralChange !== 1) {
- cells.forEach(i => pack.cells.pop[i] *= ruralChange);
+ cells.forEach(i => (pack.cells.pop[i] *= ruralChange));
}
if (!isFinite(ruralChange) && +ruralPop.value > 0) {
- const points = ruralPop.value / populationRate.value;
+ const points = ruralPop.value / populationRate;
const pop = rn(points / cells.length);
- cells.forEach(i => pack.cells.pop[i] = pop);
+ cells.forEach(i => (pack.cells.pop[i] = pop));
}
const urbanChange = urbanPop.value / urban;
if (isFinite(urbanChange) && urbanChange !== 1) {
- burgs.forEach(b => b.population = rn(b.population * urbanChange, 4));
+ burgs.forEach(b => (b.population = rn(b.population * urbanChange, 4)));
}
if (!isFinite(urbanChange) && +urbanPop.value > 0) {
- const points = urbanPop.value / populationRate.value / urbanization.value;
+ const points = urbanPop.value / populationRate / urbanization;
const population = rn(points / burgs.length, 4);
- burgs.forEach(b => b.population = population);
+ burgs.forEach(b => (b.population = population));
}
zonesEditorAddLines();
}
-
}
function zoneRemove(zone) {
- zones.select("#"+zone).remove();
- unfog("focusZone"+zone);
+ zones.select("#" + zone).remove();
+ unfog("focusZone" + zone);
zonesEditorAddLines();
}
-
}