mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-22 12:01:23 +01:00
refactor(#902): lock state - keep label
This commit is contained in:
parent
aa29f69bbf
commit
b7f88b519d
2 changed files with 48 additions and 65 deletions
|
|
@ -602,40 +602,15 @@ window.BurgsAndStates = (function () {
|
||||||
const displayed = layerIsOn("toggleLabels");
|
const displayed = layerIsOn("toggleLabels");
|
||||||
if (!displayed) toggleLabels();
|
if (!displayed) toggleLabels();
|
||||||
|
|
||||||
if (!list) {
|
// remove state labels to be redrawn
|
||||||
// remove all labels and textpaths
|
for (const state of pack.states) {
|
||||||
g.selectAll("text")
|
if (!state.i || state.removed || state.lock) continue;
|
||||||
.filter((_, i) => {
|
if (list && !list.includes(state.i)) continue;
|
||||||
const id = g.select(`:nth-child(${i + 1})`).node()?.id;
|
|
||||||
if (!id) return true;
|
|
||||||
|
|
||||||
return !pack.states.some(s => s.lock && `${s.old_i}` === id.substring(10));
|
byId(`stateLabel${state.i}`)?.remove();
|
||||||
})
|
byId(`textPath_stateLabel6${state.i}`)?.remove();
|
||||||
.remove();
|
|
||||||
t.selectAll("path[id*='stateLabel']")
|
|
||||||
.filter((_, i) => {
|
|
||||||
const id = t.select(`:nth-child(${i + 1})`).node()?.id;
|
|
||||||
if (!id) return true;
|
|
||||||
|
|
||||||
return !pack.states.some(s => s.lock && `${s.old_i}` === id.substring(19));
|
|
||||||
})
|
|
||||||
.remove();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pack.states.forEach(s => {
|
|
||||||
if (!s.lock) return;
|
|
||||||
|
|
||||||
// For locked states, get the name and update its index to keep it in place
|
|
||||||
const g = labels.select("#states");
|
|
||||||
const t = defs.select("#textPaths");
|
|
||||||
|
|
||||||
const labelNode = g.select(`#stateLabel${s.old_i}`);
|
|
||||||
const textNode = t.select(`#textPath_stateLabel${s.old_i}`);
|
|
||||||
|
|
||||||
labelNode.attr("id", `stateLabel${s.i}`).select("textPath").attr("xlink:href", `#textPath_stateLabel${s.i}`);
|
|
||||||
textNode.attr("id", `textPath_stateLabel${s.i}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
const example = g.append("text").attr("x", 0).attr("x", 0).text("Average");
|
const example = g.append("text").attr("x", 0).attr("x", 0).text("Average");
|
||||||
const letterLength = example.node().getComputedTextLength() / 7; // average length of 1 letter
|
const letterLength = example.node().getComputedTextLength() / 7; // average length of 1 letter
|
||||||
|
|
||||||
|
|
@ -644,11 +619,6 @@ window.BurgsAndStates = (function () {
|
||||||
const state = states[p[0]];
|
const state = states[p[0]];
|
||||||
const {name, fullName} = state;
|
const {name, fullName} = state;
|
||||||
|
|
||||||
if (list) {
|
|
||||||
t.select("#textPath_stateLabel" + id).remove();
|
|
||||||
g.select("#stateLabel" + id).remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
const path = p[1].length > 1 ? round(lineGen(p[1])) : `M${p[1][0][0] - 50},${p[1][0][1]}h${100}`;
|
const path = p[1].length > 1 ? round(lineGen(p[1])) : `M${p[1][0][0] - 50},${p[1][0][1]}h${100}`;
|
||||||
const textPath = t
|
const textPath = t
|
||||||
.append("path")
|
.append("path")
|
||||||
|
|
|
||||||
|
|
@ -152,6 +152,7 @@ function regenerateStates() {
|
||||||
else drawStates();
|
else drawStates();
|
||||||
if (!layerIsOn("toggleBorders")) toggleBorders();
|
if (!layerIsOn("toggleBorders")) toggleBorders();
|
||||||
else drawBorders();
|
else drawBorders();
|
||||||
|
|
||||||
BurgsAndStates.drawStateLabels();
|
BurgsAndStates.drawStateLabels();
|
||||||
Military.generate();
|
Military.generate();
|
||||||
if (layerIsOn("toggleEmblems")) drawEmblems(); // redrawEmblems
|
if (layerIsOn("toggleEmblems")) drawEmblems(); // redrawEmblems
|
||||||
|
|
@ -167,6 +168,7 @@ function recreateStates() {
|
||||||
|
|
||||||
const statesCount = +regionsOutput.value;
|
const statesCount = +regionsOutput.value;
|
||||||
const validBurgs = pack.burgs.filter(b => b.i && !b.removed);
|
const validBurgs = pack.burgs.filter(b => b.i && !b.removed);
|
||||||
|
|
||||||
if (!validBurgs.length)
|
if (!validBurgs.length)
|
||||||
return tip("There are no any burgs to generate states. Please create burgs first", false, "error");
|
return tip("There are no any burgs to generate states. Please create burgs first", false, "error");
|
||||||
if (validBurgs.length < statesCount)
|
if (validBurgs.length < statesCount)
|
||||||
|
|
@ -189,30 +191,27 @@ function recreateStates() {
|
||||||
burg.capital = 0;
|
burg.capital = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove emblems
|
// remove labels and emblems for non-locked states
|
||||||
document.querySelectorAll("[id^=stateCOA]").forEach(el => el.remove());
|
for (const state of pack.states) {
|
||||||
document.querySelectorAll("[id^=provinceCOA]").forEach(el => el.remove());
|
if (!state.i || state.removed || state.lock) continue;
|
||||||
emblems.selectAll("use").remove();
|
|
||||||
|
// remove state labels
|
||||||
|
byId(`stateLabel${state.i}`)?.remove();
|
||||||
|
byId(`textPath_stateLabel${state.i}`)?.remove();
|
||||||
|
|
||||||
|
// remove state emblems
|
||||||
|
byId(`stateCOA${state.i}`)?.remove();
|
||||||
|
document.querySelector(`#stateEmblems > use[data-i="${state.i}"]`)?.remove();
|
||||||
|
|
||||||
|
// remove province emblems
|
||||||
|
for (const provinceId of state.provinces) {
|
||||||
|
byId(`provinceCOA${provinceId}`)?.remove();
|
||||||
|
document.querySelector(`#provinceEmblems > use[data-i="${provinceId}"]`)?.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unfog();
|
unfog();
|
||||||
|
|
||||||
if (!statesCount) {
|
|
||||||
tip(`Cannot generate zero states. Please check the <i>States Number</i> option`, false, "warn");
|
|
||||||
pack.states = pack.states.slice(0, 1); // remove all except of neutrals
|
|
||||||
pack.states[0].diplomacy = []; // clear diplomacy
|
|
||||||
pack.provinces = [0]; // remove all provinces
|
|
||||||
pack.cells.state = new Uint16Array(pack.cells.i.length); // reset cells data
|
|
||||||
|
|
||||||
borders.selectAll("path").remove(); // remove borders
|
|
||||||
regions.selectAll("path").remove(); // remove states fill
|
|
||||||
labels.select("#states").selectAll("text"); // remove state labels
|
|
||||||
defs.select("#textPaths").selectAll("path[id*='stateLabel']").remove(); // remove state labels paths
|
|
||||||
|
|
||||||
if (document.getElementById("burgsOverviewRefresh").offsetParent) burgsOverviewRefresh.click();
|
|
||||||
if (document.getElementById("statesEditorRefresh").offsetParent) statesEditorRefresh.click();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// burg local ids sorted by a bit randomized population. Also ignore burgs of a locked state
|
// burg local ids sorted by a bit randomized population. Also ignore burgs of a locked state
|
||||||
const sortedBurgs = validBurgs
|
const sortedBurgs = validBurgs
|
||||||
.filter(b => !lockedStatesIds.includes(b.state))
|
.filter(b => !lockedStatesIds.includes(b.state))
|
||||||
|
|
@ -229,26 +228,38 @@ function recreateStates() {
|
||||||
const newStates = [{i: 0, name: pack.states[0].name}];
|
const newStates = [{i: 0, name: pack.states[0].name}];
|
||||||
|
|
||||||
// restore locked states
|
// restore locked states
|
||||||
lockedStates.forEach(s => {
|
lockedStates.forEach(state => {
|
||||||
const newId = newStates.length;
|
const newId = newStates.length;
|
||||||
|
const {x, y} = validBurgs[state.capital];
|
||||||
|
capitalsTree.add([x, y]);
|
||||||
|
|
||||||
s.provinces.forEach(id => {
|
// update label id reference
|
||||||
|
labels
|
||||||
|
.select("#states")
|
||||||
|
.select(`#stateLabel${state.i}`)
|
||||||
|
.attr("id", `stateLabel${newId}`)
|
||||||
|
.select("textPath")
|
||||||
|
.attr("xlink:href", `#textPath_stateLabel${newId}`);
|
||||||
|
defs.select("#textPaths").select(`#textPath_stateLabel${state.i}`).attr("id", `textPath_stateLabel${newId}`);
|
||||||
|
|
||||||
|
// update emblem id reference
|
||||||
|
byId(`stateCOA${state.i}`)?.setAttribute("id", `stateCOA${newId}`);
|
||||||
|
document.querySelector(`#stateEmblems > use[data-i="${state.i}"]`)?.setAttribute("data-i", newId);
|
||||||
|
|
||||||
|
state.provinces.forEach(id => {
|
||||||
if (!pack.provinces[id] || !pack.provinces[id].removed) return;
|
if (!pack.provinces[id] || !pack.provinces[id].removed) return;
|
||||||
pack.provinces[id].state = newId;
|
pack.provinces[id].state = newId;
|
||||||
// pack.provinces[id].should_restore = true;
|
// pack.provinces[id].should_restore = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
const {x, y} = validBurgs[s.capital];
|
state.i = newId;
|
||||||
capitalsTree.add([x, y]);
|
newStates.push(state);
|
||||||
|
|
||||||
s.i = newId;
|
|
||||||
newStates.push(s);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const i of pack.cells.i) {
|
for (const i of pack.cells.i) {
|
||||||
const stateId = pack.cells.state[i];
|
const stateId = pack.cells.state[i];
|
||||||
const lockedStateIndex = lockedStatesIds.indexOf(stateId) + 1;
|
const lockedStateIndex = lockedStatesIds.indexOf(stateId) + 1;
|
||||||
// lockedStateIndex is an index of locked state of 0 if state is not locked
|
// lockedStateIndex is an index of locked state or 0 if state is not locked
|
||||||
pack.cells.state[i] = lockedStateIndex;
|
pack.cells.state[i] = lockedStateIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -291,6 +302,8 @@ function recreateStates() {
|
||||||
newStates.push({i, name, type, capital: capital.i, center: capital.cell, culture, expansionism, coa});
|
newStates.push({i, name, type, capital: capital.i, center: capital.cell, culture, expansionism, coa});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!statesCount) tip(`<i>States Number</i> option is set to zero. No counties are generated`, false, "warn");
|
||||||
|
|
||||||
pack.states = newStates;
|
pack.states = newStates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue