feat: burg group editor - apply changes

This commit is contained in:
Azgaar 2024-10-09 00:33:22 +02:00
parent b6708bf698
commit b700bf0630
8 changed files with 102 additions and 29 deletions

View file

@ -211,6 +211,7 @@ function editBurgGroups() {
el.previousElementSibling.value = JSON.stringify(values);
el.innerHTML = Object.keys(values).length ? "some" : "any";
$(this).dialog("close");
},
Cancel: function () {
@ -305,6 +306,14 @@ function editBurgGroups() {
return group;
});
// put burgs to new groups
const validBurgs = pack.burgs.filter(b => b.i && !b.removed);
const populations = validBurgs.map(b => b.population).sort((a, b) => a - b);
validBurgs.forEach(burg => Burgs.defineGroup(burg, populations));
if (layerIsOn("toggleBurgIcons")) drawBurgIcons();
if (layerIsOn("toggleLabels")) drawBurgLabels();
$("#burgGroupsEditor").dialog("close");
}
}

View file

@ -240,13 +240,10 @@ function togglePort(burg) {
.attr("height", size);
}
// TODO: rework this function to use the new data structure
function getBurgLink(burg) {
if (burg.link) return burg.link;
const population = burg.population * populationRate * urbanization;
if (population >= options.villageMaxPopulation || burg.citadel || burg.walls || burg.temple || burg.shanty)
return createMfcgLink(burg);
if (burg.citadel || burg.walls || burg.temple || burg.shanty) return createMfcgLink(burg);
return createVillageGeneratorLink(burg);
}

View file

@ -71,13 +71,22 @@ async function fetchSystemPreset(preset) {
}
}
function applyStyle(style) {
for (const selector in style) {
function applyStyle(styleJSON) {
for (const selector in styleJSON) {
if (selector.startsWith("#burgLabels")) {
const group = selector.replace("#burgLabels > g#", "");
style.burgLabels[group] = styleJSON[selector];
}
if (selector.startsWith("#burgIcons")) {
const group = selector.replace("#burgIcons > g#", "");
style.burgIcons[group] = styleJSON[selector];
}
const el = document.querySelector(selector);
if (!el) continue;
for (const attribute in style[selector]) {
const value = style[selector][attribute];
for (const attribute in styleJSON[selector]) {
const value = styleJSON[selector][attribute];
if (value === "null" || value === null) {
el.removeAttribute(attribute);
@ -342,8 +351,18 @@ function addStylePreset() {
"stroke-dasharray",
"stroke-linecap"
];
const burgLabelsAttributes = [
"opacity",
"fill",
"text-shadow",
"letter-spacing",
"data-size",
"font-size",
"font-family"
];
options.burgs.groups.forEach(({name}) => {
attributes[`#burgIcons > g.${name}`] = burgIconsAttributes;
attributes[`#burgLabels > g#${name}`] = burgLabelsAttributes;
});
for (const selector in attributes) {