Zone filter (#728)

* Adding zone type dropdown to index.html

* added zone type filter to zones-editor.js

* zoneTypes dialog

add and delete types, dialog

* filtering types

* correct filter button and prompt

The filter button now works on click with an added zonesEditorAddLines(); on function toggleFilterTable().

If the table is empty, a message appears on a line giving advice.

* Amount of types and fixes

Fixed some spacing in zoneTypes dialog, and made a count for the amount of zones per type.

* Minor changes for zone types

* dialog for types on a button

New button for the zone Types dialog.

* typo in index.html

* update on click add type

Co-authored-by: Evolvedexperiment <evolvedexperiment@gmail.com>
Co-authored-by: Azgaar <maxganiev@yandex.ru>
This commit is contained in:
Ángel Montero Lamas 2022-02-06 21:09:30 +01:00 committed by Peter
parent 3e353e98cc
commit 61d4d52589
2 changed files with 86 additions and 0 deletions

View file

@ -158,6 +158,7 @@ let customization = 0;
let biomesData = applyDefaultBiomesSystem();
let nameBases = Names.getNameBases(); // cultures-related data
const zoneTypes = ["Invasion", "Rebels", "Proselytism", "Crusade", "Disease", "Disaster"];
let color = d3.scaleSequential(d3.interpolateSpectral); // default color scheme
const lineGen = d3.line().curve(d3.curveBasis); // d3 line generator with default curve interpolation
@ -2056,6 +2057,14 @@ function addZones(number = 1) {
TIME && console.timeEnd("addZones");
}
// Update zone types
function updateZoneType(zoneId, newType) {
const zone = document.getElementById(zoneId);
if (zone) {
zone.dataset.type = newType;
}
}
// show map stats on generation complete
function showStatistics() {
const heightmap = byId("templateInput").value;

View file

@ -30,6 +30,8 @@ function editZones() {
document.getElementById("zonesManuallyApply").addEventListener("click", applyZonesManualAssignent);
document.getElementById("zonesManuallyCancel").addEventListener("click", cancelZonesManualAssignent);
document.getElementById("zonesAdd").addEventListener("click", addZonesLayer);
document.getElementById("zonesEditTypes").addEventListener("click", addZonesDialog);
document.getElementById("zonesNewTypeButton").addEventListener("click", addZonesType);
document.getElementById("zonesExport").addEventListener("click", downloadZonesData);
document.getElementById("zonesRemove").addEventListener("click", toggleEraseMode);
@ -53,6 +55,14 @@ function editZones() {
else if (el.classList.contains("zoneType")) zone.attr("data-type", el.value);
});
function refreshZonesEditor() {
updateSVG();
zonesEditorAddLines();
}
function updateSVG() {
const value = document.getElementById("zonesFilterType").value;
// update type filter with a list of used types
function updateFilters() {
const zones = Array.from(document.querySelectorAll("#zones > g"));
@ -82,6 +92,7 @@ function editZones() {
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 zoneTypeList = getZoneTypesList(this.id, this.dataset.type);
const populationTip = `Total population: ${si(population)}; Rural population: ${si(rural)}; Urban population: ${si(urban)}. Click to change`;
const inactive = zoneEl.style.display === "none";
const focused = defs.select("#fog #focus" + zoneEl.id).size();
@ -97,6 +108,7 @@ function editZones() {
<div data-tip="Zone area" class="biomeArea hide">${si(area) + unit}</div>
<span data-tip="${populationTip}" class="icon-male hide"></span>
<div data-tip="${populationTip}" class="culturePopulation hide">${si(population)}</div>
${zoneTypeList}
<span data-tip="Drag to raise or lower the zone" class="icon-resize-vertical hide"></span>
<span data-tip="Toggle zone focus" class="icon-pin ${focused ? "" : " inactive"} hide ${c.length ? "" : " placeholder"}"></span>
<span data-tip="Toggle zone visibility" class="icon-eye ${inactive ? " inactive" : ""} hide ${c.length ? "" : " placeholder"}"></span>
@ -364,6 +376,11 @@ function editZones() {
}
}
function toggleFilterTable() {
this.classList.toggle("pressed");
zonesEditorAddLines();
}
function addZonesLayer() {
const id = getNextId("zone");
const description = "Unknown zone";
@ -470,6 +487,66 @@ function editZones() {
}
}
function zonesTypesAddLines() {
const zoneTypeListBody = document.getElementById("zonesTypesBodySection");
let lines = "";
zoneTypes.forEach(function(z, i) {
let count=0; // Amount of zones per type
zones.selectAll("g").each(function() { if (this.dataset.type === z) count++; });
lines += `<div class="states"><span class="religionDeity">${z}</span><span class="statePopulation">${count}</span>`;
if (i > 5) {
let id="removeZoneType" + i;
lines += `<span data-tip="Remove zone type" class="icon-trash-empty" id="${id}"></span>`;
}
lines += '</div>';
});
zoneTypeListBody.innerHTML = lines;
zonesTypesFooterNumber.innerHTML = zoneTypes.length;
for (let i=0; i<zoneTypes.length; i++) {
let d = document.getElementById("removeZoneType" + i);
if (d) {
d.addEventListener("click", function() { removeZoneType(zoneTypes[i]); });
}
}
}
function addZonesDialog() {
$("#zonesTypes").dialog({
title: "Add zones and types",
width: fitContent(),
position: {my: "center", at: "center", of: "svg"},
});
zonesTypesAddLines();
}
function addZonesType() {
let zoneType = zonesNewTypeInput.value;
if (!zoneTypes.includes(zoneType)) { zoneTypes.push(zoneType); }
zonesNewTypeInput.value = "";
zonesTypesAddLines();
zonesTypesFooterNumber.innerHTML = zoneTypes.length;
zonesEditorAddLines();
}
function removeZoneType(zoneType) {
zones.selectAll("g").each(function () {
if (this.dataset.type === zoneType) {
this.dataset.type = zoneTypes[0];
}
});
for (let i=0; i<zoneTypes.length; i++) {
if (zoneTypes[i] === zoneType) {
zoneTypes.splice(i, 1);
zonesTypesAddLines();
break;
}
}
}
function zoneRemove(zone) {
zones.select("#" + zone).remove();
unfog("focusZone" + zone);