submap: projection moved to options, fix double burg error

This commit is contained in:
Mészáros Gergely 2022-04-08 11:41:29 +02:00
parent 312f640107
commit 711653e115
4 changed files with 93 additions and 55 deletions

View file

@ -9,30 +9,71 @@ function openSubmapOptions() {
title: "Submap options",
resizable: false,
width: fitContent(),
position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}
position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"},
buttons: {
Submap: function () {
$(this).dialog("close");
generateSubmap();
},
Cancel: function () { $(this).dialog("close"); },
}
});
}
function openRemapOptions() {
resetZoom(0);
$("#remapOptionsDialog").dialog({
title: "Resampler options",
resizable: false,
width: fitContent(),
position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"}
position: {my: "right top", at: "right-10 top+10", of: "svg", collision: "fit"},
buttons: {
Resample: function () {
const cellNumId = Number(document.getElementById('submapPointsInput').value);
const cells = cellsDensityConstants[cellNumId];
$(this).dialog("close");
if (!cells) {
console.error('Unknown cell number!');
return;
}
changeCellsDensity(cellNumId);
resampleCurrentMap();
},
Cancel: function () { $(this).dialog("close"); },
},
});
}
const resampleCurrentMap = debounce(async function () {
WARN && console.warn("Resampling current map");
});
/* callbacks */
const generateSubmap = debounce(async function () {
const resampleCurrentMap = debounce(function () {
// Resample the whole map to different cell resolution or shape
WARN && console.warn("Resampling current map");
const options = {
lockMarkers: false,
lockBurgs: false,
depressRivers: false,
addLakesInDepressions: false,
promoteTowns: false,
smoothHeightMap: false,
projection: (x,y) => [x, y],
inverse: (x,y) => [x, y],
}
startResample(options);
}, 1000);
const generateSubmap = debounce(function () {
// Create submap from the current map
// submap limits defined by the current window size (canvas viewport)
WARN && console.warn("Resampling current map");
closeDialogs("#worldConfigurator, #options3d");
const checked = id => Boolean(document.getElementById(id).checked)
// Create projection func from current zoom extents
const [[x0, y0], [x1, y1]] = getViewBoxExtent();
const options = {
lockMarkers: checked("submapLockMarkers"),
lockBurgs: checked("submapLockBurgs"),
@ -41,14 +82,8 @@ const generateSubmap = debounce(async function () {
addLakesInDepressions: checked("submapAddLakeInDepression"),
promoteTowns: checked("submapPromoteTowns"),
smoothHeightMap: scale > 2,
}
// Create projection func from current zoom extents
const [[x0, y0], [x1, y1]] = getViewBoxExtent();
const projection = (x, y, inverse=false) => {
return inverse
? [x * (x1-x0) / graphWidth + x0, y * (y1-y0) / graphHeight + y0]
: [(x-x0) * graphWidth / (x1-x0), (y-y0) * graphHeight / (y1-y0)];
inverse: (x,y) => [x * (x1-x0) / graphWidth + x0, y * (y1-y0) / graphHeight + y0],
projection: (x, y) => [(x-x0) * graphWidth / (x1-x0), (y-y0) * graphHeight / (y1-y0)],
}
// converting map position on the planet
@ -65,7 +100,11 @@ const generateSubmap = debounce(async function () {
distanceScaleInput.value = distanceScaleOutput.value = rn(distanceScale = distanceScaleOutput.value / scale, 2);
populationRateInput.value = populationRateOutput.value = rn(populationRate = populationRateOutput.value / scale, 2);
customization = 0;
startResample(options);
}, 1000);
async function startResample(options) {
undraw();
resetZoom(0);
let oldstate = {
@ -78,7 +117,7 @@ const generateSubmap = debounce(async function () {
try {
const oldScale = scale;
await Submap.resample(oldstate, projection, options);
await Submap.resample(oldstate, options);
if (options.promoteTowns) {
const groupName = 'largetowns';
moveAllBurgsToGroup('towns', groupName);
@ -87,7 +126,7 @@ const generateSubmap = debounce(async function () {
invokeActiveZooming();
}
} catch (error) {
generateSubmapErrorHandler(error, oldstate, projection, options);
showSubmapErrorHandler(error);
}
oldstate = null; // destroy old state to free memory
@ -96,31 +135,21 @@ const generateSubmap = debounce(async function () {
turnButtonOn('toggleMarkers');
if (ThreeD.options.isOn) ThreeD.redraw();
if ($("#worldConfigurator").is(":visible")) editWorld();
}, 1000);
}
function generateSubmapErrorHandler(error, oldstate, projection, options) {
function showSubmapErrorHandler(error) {
ERROR && console.error(error);
clearMainTip();
alertMessage.innerHTML = `An error is occured on map resampling. Please retry.
<br>If error is critical, clear the stored data and try again.
alertMessage.innerHTML = `Map resampling failed :_(.
<br>You may retry after clearing stored data or contact us at discord.
<p id="errorBox">${parseError(error)}</p>`;
$("#alert").dialog({
resizable: false,
title: "Generation error",
width: "32em",
buttons: {
Regenerate: async function () {
try {
await Submap.resample(oldstate, projection, options);
} catch (error) {
generateSubmapErrorHandler(error, oldstate, projection, options);
}
$(this).dialog("close");
},
Ignore: function () {
$(this).dialog("close");
}
Ok: function () { $(this).dialog("close"); }
},
position: {my: "center", at: "center", of: "svg"}
});