From 97f2b213c4022fa78406d81e4d72bd10c18abd97 Mon Sep 17 00:00:00 2001 From: Azgaar Date: Sat, 25 Jun 2022 17:22:30 +0300 Subject: [PATCH] refactor(es modules): continue migration --- .DS_Store | Bin 0 -> 6148 bytes index.html | 86 ++++++++-------- modules/dynamic/auto-update.js | 2 +- modules/dynamic/editors/cultures-editor.js | 2 + modules/dynamic/editors/religions-editor.js | 2 + modules/dynamic/editors/states-editor.js | 2 + modules/dynamic/heightmap-selection.js | 2 + modules/dynamic/overview/charts-overview.js | 1 + modules/heightmap-generator.js | 2 +- modules/io/export.js | 30 ++++-- modules/io/load.js | 3 +- modules/markers-generator.js | 1 + modules/relief-icons.js | 64 +++++++++++- modules/submap.js | 6 +- modules/ui/biomes-editor.js | 5 +- modules/ui/burg-editor.js | 5 +- modules/ui/burgs-overview.js | 5 +- modules/ui/coastline-editor.js | 5 +- modules/ui/diplomacy-editor.js | 5 +- modules/ui/editors.js | 3 +- modules/ui/elevation-profile.js | 6 +- modules/ui/general.js | 4 +- modules/ui/heightmap-editor.js | 4 +- modules/ui/ice-editor.js | 5 +- modules/ui/labels-editor.js | 5 +- modules/ui/lakes-editor.js | 5 +- modules/ui/layers.js | 1 + modules/ui/markers-editor.js | 20 +++- modules/ui/provinces-editor.js | 5 +- modules/ui/regiment-editor.js | 5 +- modules/ui/regiments-overview.js | 5 +- modules/ui/relief-editor.js | 5 +- modules/ui/rivers-creator.js | 5 +- modules/ui/rivers-editor.js | 5 +- modules/ui/tools.js | 3 +- modules/ui/units-editor.js | 5 +- modules/ui/zones-editor.js | 4 +- src/main.ts | 21 ++-- {modules/ui => src/modules}/measurers.js | 17 ++-- src/types/global.d.ts | 8 ++ src/utils/{graphUtils.js => graphUtils.ts} | 104 +------------------- src/utils/index.ts | 1 - 42 files changed, 252 insertions(+), 222 deletions(-) create mode 100644 .DS_Store rename {modules/ui => src/modules}/measurers.js (98%) rename src/utils/{graphUtils.js => graphUtils.ts} (73%) delete mode 100644 src/utils/index.ts diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f07b95e63e0ce35eb5d2f8551be3603cf8a80c3b GIT binary patch literal 6148 zcmZQzU|@7AO)+F(5MW?n;9!8zOq>i@0Z1N%F(jFwA|RR(Y(_joE<-*;3PUMF4peTG z8r&fOiaQ>LJaDX~G9braDnoHnd2vBfPJR*t1H+D_f}G6a5(9&4j7-cdtZeKY>>S)2 zvB4Sn<-sM1C8fnqiAB*MUPyj^P7;irm=uL4>U^E1vA;1iw1wi$`D+2?r{y#+3C^;GeqaiRXLx7RR zCD_FYTq$GsAE>Sc)u#ziX;AGCs*V{!^)x~ZTop4x1{5Wr!l3FNq!mPit71k5NKHQ4 P5P*fyC_Nei^bY|5%)C!_ literal 0 HcmV?d00001 diff --git a/index.html b/index.html index c7887d63..9c174912 100644 --- a/index.html +++ b/index.html @@ -7795,13 +7795,12 @@ - + - @@ -7813,53 +7812,54 @@ - + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - - + + + + + - + +f diff --git a/modules/dynamic/auto-update.js b/modules/dynamic/auto-update.js index fdb758de..70e5bcd8 100644 --- a/modules/dynamic/auto-update.js +++ b/modules/dynamic/auto-update.js @@ -1,4 +1,4 @@ -"use strict"; +import {findCell} from "/src/utils/graphUtils"; // update old .map version to the current one export function resolveVersionConflicts(version) { diff --git a/modules/dynamic/editors/cultures-editor.js b/modules/dynamic/editors/cultures-editor.js index b5e329ce..2fe11502 100644 --- a/modules/dynamic/editors/cultures-editor.js +++ b/modules/dynamic/editors/cultures-editor.js @@ -1,3 +1,5 @@ +import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils"; + const $body = insertEditorHtml(); addListeners(); diff --git a/modules/dynamic/editors/religions-editor.js b/modules/dynamic/editors/religions-editor.js index a76aec30..cd926a7a 100644 --- a/modules/dynamic/editors/religions-editor.js +++ b/modules/dynamic/editors/religions-editor.js @@ -1,3 +1,5 @@ +import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils"; + const $body = insertEditorHtml(); addListeners(); diff --git a/modules/dynamic/editors/states-editor.js b/modules/dynamic/editors/states-editor.js index 5299be21..fa870d30 100644 --- a/modules/dynamic/editors/states-editor.js +++ b/modules/dynamic/editors/states-editor.js @@ -1,3 +1,5 @@ +import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils"; + const $body = insertEditorHtml(); addListeners(); diff --git a/modules/dynamic/heightmap-selection.js b/modules/dynamic/heightmap-selection.js index 0f44f74f..99fdc8a7 100644 --- a/modules/dynamic/heightmap-selection.js +++ b/modules/dynamic/heightmap-selection.js @@ -1,3 +1,5 @@ +import {shouldRegenerateGrid, generateGrid} from "/src/utils/graphUtils"; + const initialSeed = generateSeed(); let graph = getGraph(grid); diff --git a/modules/dynamic/overview/charts-overview.js b/modules/dynamic/overview/charts-overview.js index 4d5e14fb..a40aa931 100644 --- a/modules/dynamic/overview/charts-overview.js +++ b/modules/dynamic/overview/charts-overview.js @@ -1,4 +1,5 @@ import {rollups} from "../../../utils/functionUtils.js"; +import {isWater} from "/src/utils/graphUtils"; const entitiesMap = { states: { diff --git a/modules/heightmap-generator.js b/modules/heightmap-generator.js index 1e8c2567..efbf59d0 100644 --- a/modules/heightmap-generator.js +++ b/modules/heightmap-generator.js @@ -1,5 +1,5 @@ import {TIME} from "/src/config/logging"; -import {createTypedArray} from "/src/utils"; +import {createTypedArray} from "/src/utils/arrayUtils"; import {findGridCell} from "/src/utils/graphUtils"; window.HeightmapGenerator = (function () { diff --git a/modules/io/export.js b/modules/io/export.js index fded29db..597894ef 100644 --- a/modules/io/export.js +++ b/modules/io/export.js @@ -1,5 +1,4 @@ -"use strict"; -// Functions to export map to image or data files +import {getGridPolygon} from "/src/utils/graphUtils"; // download map as SVG async function saveSVG() { @@ -10,7 +9,12 @@ async function saveSVG() { link.href = url; link.click(); - tip(`${link.download} is saved. Open "Downloads" screen (crtl + J) to check. You can set image scale in options`, true, "success", 5000); + tip( + `${link.download} is saved. Open "Downloads" screen (crtl + J) to check. You can set image scale in options`, + true, + "success", + 5000 + ); TIME && console.timeEnd("saveSVG"); } @@ -36,7 +40,12 @@ async function savePNG() { window.setTimeout(function () { canvas.remove(); window.URL.revokeObjectURL(link.href); - tip(`${link.download} is saved. Open "Downloads" screen (crtl + J) to check. You can set image scale in options`, true, "success", 5000); + tip( + `${link.download} is saved. Open "Downloads" screen (crtl + J) to check. You can set image scale in options`, + true, + "success", + 5000 + ); }, 1000); }); }; @@ -140,7 +149,15 @@ async function saveTiles() { // parse map svg to object url async function getMapURL(type, options = {}) { - const {debug = false, globe = false, noLabels = false, noWater = false, noScaleBar = false, noIce = false, fullMap = false} = options; + const { + debug = false, + globe = false, + noLabels = false, + noWater = false, + noScaleBar = false, + noIce = false, + fullMap = false + } = options; if (fullMap) drawScaleBar(1); @@ -315,7 +332,8 @@ async function getMapURL(type, options = {}) { clone.remove(); - const serialized = `` + new XMLSerializer().serializeToString(cloneEl); + const serialized = + `` + new XMLSerializer().serializeToString(cloneEl); const blob = new Blob([serialized], {type: "image/svg+xml;charset=utf-8"}); const url = window.URL.createObjectURL(blob); window.setTimeout(() => window.URL.revokeObjectURL(url), 5000); diff --git a/modules/io/load.js b/modules/io/load.js index 946df93f..debc27a8 100644 --- a/modules/io/load.js +++ b/modules/io/load.js @@ -1,5 +1,4 @@ -"use strict"; -// Functions to load and parse .map files +import {calculateVoronoi, findCell} from "/src/utils/graphUtils"; function quickLoad() { ldb.get("lastMap", blob => { diff --git a/modules/markers-generator.js b/modules/markers-generator.js index d7a12ca1..04d8e187 100644 --- a/modules/markers-generator.js +++ b/modules/markers-generator.js @@ -1,4 +1,5 @@ import {TIME} from "/src/config/logging"; +import {getFriendlyHeight} from "./ui/general"; window.Markers = (function () { let config = []; diff --git a/modules/relief-icons.js b/modules/relief-icons.js index 498886df..cd3934fc 100644 --- a/modules/relief-icons.js +++ b/modules/relief-icons.js @@ -1,4 +1,4 @@ -"use strict"; +import {getPackPolygon} from "/src/utils/graphUtils"; window.ReliefIcons = (function () { const ReliefIcons = function () { @@ -124,5 +124,67 @@ window.ReliefIcons = (function () { return "#relief-" + getOldIcon(type) + "-1"; // simple } + // mbostock's poissonDiscSampler + function* poissonDiscSampler(x0, y0, x1, y1, r, k = 3) { + if (!(x1 >= x0) || !(y1 >= y0) || !(r > 0)) throw new Error(); + + const width = x1 - x0; + const height = y1 - y0; + const r2 = r * r; + const r2_3 = 3 * r2; + const cellSize = r * Math.SQRT1_2; + const gridWidth = Math.ceil(width / cellSize); + const gridHeight = Math.ceil(height / cellSize); + const grid = new Array(gridWidth * gridHeight); + const queue = []; + + function far(x, y) { + const i = (x / cellSize) | 0; + const j = (y / cellSize) | 0; + const i0 = Math.max(i - 2, 0); + const j0 = Math.max(j - 2, 0); + const i1 = Math.min(i + 3, gridWidth); + const j1 = Math.min(j + 3, gridHeight); + for (let j = j0; j < j1; ++j) { + const o = j * gridWidth; + for (let i = i0; i < i1; ++i) { + const s = grid[o + i]; + if (s) { + const dx = s[0] - x; + const dy = s[1] - y; + if (dx * dx + dy * dy < r2) return false; + } + } + } + return true; + } + + function sample(x, y) { + queue.push((grid[gridWidth * ((y / cellSize) | 0) + ((x / cellSize) | 0)] = [x, y])); + return [x + x0, y + y0]; + } + + yield sample(width / 2, height / 2); + + pick: while (queue.length) { + const i = (Math.random() * queue.length) | 0; + const parent = queue[i]; + + for (let j = 0; j < k; ++j) { + const a = 2 * Math.PI * Math.random(); + const r = Math.sqrt(Math.random() * r2_3 + r2); + const x = parent[0] + r * Math.cos(a); + const y = parent[1] + r * Math.sin(a); + if (0 <= x && x < width && 0 <= y && y < height && far(x, y)) { + yield sample(x, y); + continue pick; + } + } + + const r = queue.pop(); + if (i < queue.length) queue[i] = r; + } + } + return ReliefIcons; })(); diff --git a/modules/submap.js b/modules/submap.js index 85408487..6f7d3d2b 100644 --- a/modules/submap.js +++ b/modules/submap.js @@ -1,8 +1,4 @@ -"use strict"; -/* -Cell resampler module used by submapper and resampler (transform) -main function: resample(options); -*/ +import {findCell} from "/src/utils/graphUtils"; window.Submap = (function () { const isWater = (pack, id) => pack.cells.h[id] < 20; diff --git a/modules/ui/biomes-editor.js b/modules/ui/biomes-editor.js index b3752e9e..4fdce51a 100644 --- a/modules/ui/biomes-editor.js +++ b/modules/ui/biomes-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editBiomes() { +import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils"; + +export function editBiomes() { if (customization) return; closeDialogs("#biomesEditor, .stable"); if (!layerIsOn("toggleBiomes")) toggleBiomes(); diff --git a/modules/ui/burg-editor.js b/modules/ui/burg-editor.js index 4e0a10e0..7fe4680e 100644 --- a/modules/ui/burg-editor.js +++ b/modules/ui/burg-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editBurg(id) { +import {findCell} from "/src/utils/graphUtils"; + +export function editBurg(id) { if (customization) return; closeDialogs(".stable"); if (!layerIsOn("toggleIcons")) toggleIcons(); diff --git a/modules/ui/burgs-overview.js b/modules/ui/burgs-overview.js index 5a1f02cf..c34f0d49 100644 --- a/modules/ui/burgs-overview.js +++ b/modules/ui/burgs-overview.js @@ -1,5 +1,6 @@ -"use strict"; -function overviewBurgs() { +import {findCell} from "/src/utils/graphUtils"; + +export function overviewBurgs() { if (customization) return; closeDialogs("#burgsOverview, .stable"); if (!layerIsOn("toggleIcons")) toggleIcons(); diff --git a/modules/ui/coastline-editor.js b/modules/ui/coastline-editor.js index 424456db..d14a7bfb 100644 --- a/modules/ui/coastline-editor.js +++ b/modules/ui/coastline-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editCoastline(node = d3.event.target) { +import {getPackPolygon} from "/src/utils/graphUtils"; + +export function editCoastline(node = d3.event.target) { if (customization) return; closeDialogs(".stable"); if (layerIsOn("toggleCells")) toggleCells(); diff --git a/modules/ui/diplomacy-editor.js b/modules/ui/diplomacy-editor.js index 2fbe2529..02de7675 100644 --- a/modules/ui/diplomacy-editor.js +++ b/modules/ui/diplomacy-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editDiplomacy() { +import {findCell} from "/src/utils/graphUtils"; + +export function editDiplomacy() { if (customization) return; if (pack.states.filter(s => s.i && !s.removed).length < 2) return tip("There should be at least 2 states to edit the diplomacy", false, "error"); diff --git a/modules/ui/editors.js b/modules/ui/editors.js index 8512474e..1059be61 100644 --- a/modules/ui/editors.js +++ b/modules/ui/editors.js @@ -1,5 +1,4 @@ -// module stub to store common functions for ui editors -"use strict"; +import {findCell} from "/src/utils/graphUtils"; // restore default viewbox events function restoreDefaultEvents() { diff --git a/modules/ui/elevation-profile.js b/modules/ui/elevation-profile.js index 1419c02e..6793ce46 100644 --- a/modules/ui/elevation-profile.js +++ b/modules/ui/elevation-profile.js @@ -1,6 +1,6 @@ -"use strict"; +import {findCell} from "/src/utils/graphUtils"; -function showEPForRoute(node) { +export function showEPForRoute(node) { const points = []; debug .select("#controlPoints") @@ -14,7 +14,7 @@ function showEPForRoute(node) { showElevationProfile(points, routeLen, false); } -function showEPForRiver(node) { +export function showEPForRiver(node) { const points = []; debug .select("#controlPoints") diff --git a/modules/ui/general.js b/modules/ui/general.js index 059de567..0fbff4e5 100644 --- a/modules/ui/general.js +++ b/modules/ui/general.js @@ -1,4 +1,4 @@ -import {findCell} from "/src/utils/graphUtils"; +import {findCell, findGridCell} from "/src/utils/graphUtils"; import {MOBILE} from "/src/constants"; // fit full-screen map if window is resized @@ -305,7 +305,7 @@ function getDepth(f, p) { } // get user-friendly (real-world) height value from map data -function getFriendlyHeight([x, y]) { +export function getFriendlyHeight([x, y]) { const packH = pack.cells.h[findCell(x, y)]; const gridH = grid.cells.h[findGridCell(x, y, grid)]; const h = packH < 20 ? gridH : packH; diff --git a/modules/ui/heightmap-editor.js b/modules/ui/heightmap-editor.js index 4e30d3b1..4a5e7260 100644 --- a/modules/ui/heightmap-editor.js +++ b/modules/ui/heightmap-editor.js @@ -1,6 +1,6 @@ -"use strict"; +import {findGridCell, findGridAll, findCell, getPackPolygon, getGridPolygon} from "/src/utils/graphUtils"; -function editHeightmap(options) { +export function editHeightmap(options) { const {mode, tool} = options || {}; restartHistory(); viewbox.insert("g", "#terrs").attr("id", "heights"); diff --git a/modules/ui/ice-editor.js b/modules/ui/ice-editor.js index b0d2a986..6ae9a3eb 100644 --- a/modules/ui/ice-editor.js +++ b/modules/ui/ice-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editIce() { +import {findGridCell, getGridPolygon} from "/src/utils/graphUtils"; + +export function editIce() { if (customization) return; closeDialogs(".stable"); if (!layerIsOn("toggleIce")) toggleIce(); diff --git a/modules/ui/labels-editor.js b/modules/ui/labels-editor.js index 9995faf8..5143c459 100644 --- a/modules/ui/labels-editor.js +++ b/modules/ui/labels-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editLabel() { +import {findCell} from "/src/utils/graphUtils"; + +export function editLabel() { if (customization) return; closeDialogs(); if (!layerIsOn("toggleLabels")) toggleLabels(); diff --git a/modules/ui/lakes-editor.js b/modules/ui/lakes-editor.js index 2fcab524..961e4111 100644 --- a/modules/ui/lakes-editor.js +++ b/modules/ui/lakes-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editLake() { +import {getPackPolygon} from "/src/utils/graphUtils"; + +export function editLake() { if (customization) return; closeDialogs(".stable"); if (layerIsOn("toggleCells")) toggleCells(); diff --git a/modules/ui/layers.js b/modules/ui/layers.js index 54e11d96..1d0d90b9 100644 --- a/modules/ui/layers.js +++ b/modules/ui/layers.js @@ -1,5 +1,6 @@ import {TIME} from "/src/config/logging"; import {invokeActiveZooming} from "../activeZooming"; +import {getGridPolygon} from "/src/utils/graphUtils"; let presets = {}; // global object restoreCustomPresets(); // run on-load diff --git a/modules/ui/markers-editor.js b/modules/ui/markers-editor.js index 26f035fa..0a7bab3e 100644 --- a/modules/ui/markers-editor.js +++ b/modules/ui/markers-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editMarker(markerI) { +import {findCell} from "/src/utils/graphUtils"; + +export function editMarker(markerI) { if (customization) return; closeDialogs(".stable"); @@ -97,7 +98,18 @@ function editMarker(markerI) { } function updateInputs() { - const {icon, type = "", size = 30, dx = 50, dy = 50, px = 12, stroke = "#000000", fill = "#ffffff", pin = "bubble", lock} = marker; + const { + icon, + type = "", + size = 30, + dx = 50, + dy = 50, + px = 12, + stroke = "#000000", + fill = "#ffffff", + pin = "bubble", + lock + } = marker; markerType.value = type; markerIcon.value = icon; @@ -241,7 +253,7 @@ function editMarker(markerI) { } function deleteMarker() { - Markers.deleteMarker(marker.i) + Markers.deleteMarker(marker.i); element.remove(); $("#markerEditor").dialog("close"); if (document.getElementById("markersOverviewRefresh").offsetParent) markersOverviewRefresh.click(); diff --git a/modules/ui/provinces-editor.js b/modules/ui/provinces-editor.js index 6a55f032..79b0ac35 100644 --- a/modules/ui/provinces-editor.js +++ b/modules/ui/provinces-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editProvinces() { +import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils"; + +export function editProvinces() { if (customization) return; closeDialogs("#provincesEditor, .stable"); if (!layerIsOn("toggleProvinces")) toggleProvinces(); diff --git a/modules/ui/regiment-editor.js b/modules/ui/regiment-editor.js index 7043c7ae..8c0b089b 100644 --- a/modules/ui/regiment-editor.js +++ b/modules/ui/regiment-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editRegiment(selector) { +import {findCell} from "/src/utils/graphUtils"; + +export function editRegiment(selector) { if (customization) return; closeDialogs(".stable"); if (!layerIsOn("toggleMilitary")) toggleMilitary(); diff --git a/modules/ui/regiments-overview.js b/modules/ui/regiments-overview.js index 9e334437..1c2d7a7e 100644 --- a/modules/ui/regiments-overview.js +++ b/modules/ui/regiments-overview.js @@ -1,5 +1,6 @@ -"use strict"; -function overviewRegiments(state) { +import {findCell} from "/src/utils/graphUtils"; + +export function overviewRegiments(state) { if (customization) return; closeDialogs(".stable"); if (!layerIsOn("toggleMilitary")) toggleMilitary(); diff --git a/modules/ui/relief-editor.js b/modules/ui/relief-editor.js index 268e2714..402ac8dc 100644 --- a/modules/ui/relief-editor.js +++ b/modules/ui/relief-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editReliefIcon() { +import {findCell} from "/src/utils/graphUtils"; + +export function editReliefIcon() { if (customization) return; closeDialogs(".stable"); if (!layerIsOn("toggleRelief")) toggleRelief(); diff --git a/modules/ui/rivers-creator.js b/modules/ui/rivers-creator.js index 70b8ae06..630b5c45 100644 --- a/modules/ui/rivers-creator.js +++ b/modules/ui/rivers-creator.js @@ -1,5 +1,6 @@ -"use strict"; -function createRiver() { +import {getPackPolygon, findCell} from "/src/utils/graphUtils"; + +export function createRiver() { if (customization) return; closeDialogs(); if (!layerIsOn("toggleRivers")) toggleRivers(); diff --git a/modules/ui/rivers-editor.js b/modules/ui/rivers-editor.js index d492f466..de60f291 100644 --- a/modules/ui/rivers-editor.js +++ b/modules/ui/rivers-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editRiver(id) { +import {findCell, getPackPolygon} from "/src/utils/graphUtils"; + +export function editRiver(id) { if (customization) return; if (elSelected && id === elSelected.attr("id")) return; closeDialogs(".stable"); diff --git a/modules/ui/tools.js b/modules/ui/tools.js index 0c0b9130..bd48d307 100644 --- a/modules/ui/tools.js +++ b/modules/ui/tools.js @@ -1,4 +1,5 @@ -"use strict"; +import {findCell} from "/src/utils/graphUtils"; + // module to control the Tools options (click to edit, to re-geenerate, tp add) toolsContent.addEventListener("click", function (event) { diff --git a/modules/ui/units-editor.js b/modules/ui/units-editor.js index 3f5feacf..0f3b541c 100644 --- a/modules/ui/units-editor.js +++ b/modules/ui/units-editor.js @@ -1,5 +1,6 @@ -"use strict"; -function editUnits() { +import {findCell} from "/src/utils/graphUtils"; + +export function editUnits() { closeDialogs("#unitsEditor, .stable"); $("#unitsEditor").dialog(); diff --git a/modules/ui/zones-editor.js b/modules/ui/zones-editor.js index 8752598c..1facd14b 100644 --- a/modules/ui/zones-editor.js +++ b/modules/ui/zones-editor.js @@ -1,6 +1,6 @@ -"use strict"; +import {findAll, findCell, getPackPolygon} from "/src/utils/graphUtils"; -function editZones() { +export function editZones() { closeDialogs(); if (!layerIsOn("toggleZones")) toggleZones(); const body = document.getElementById("zonesBodySection"); diff --git a/src/main.ts b/src/main.ts index 8fe716a4..665b0d97 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,14 +3,22 @@ import {PRODUCTION, UINT16_MAX} from "./constants"; import {INFO, TIME, WARN, ERROR} from "./config/logging"; -import {createTypedArray} from "./utils"; -import {shouldRegenerateGrid, generateGrid, calculateVoronoi, getPackPolygon, isLand} from "./utils/graphUtils"; +import { + shouldRegenerateGrid, + generateGrid, + calculateVoronoi, + getPackPolygon, + isLand, + findCell +} from "./utils/graphUtils"; +import {createTypedArray} from "./utils/arrayUtils"; import {drawRivers, drawStates, drawBorders} from "../modules/ui/layers"; import {invokeActiveZooming} from "../modules/activeZooming"; import {applyStoredOptions, applyMapSize, randomizeOptions} from "../modules/ui/options"; import {locked} from "../modules/ui/general"; +import {Rulers, Ruler, drawScaleBar} from "./modules/measurers"; -globalThis.fmg = { +window.fmg = { modules: {} }; @@ -931,10 +939,9 @@ function drawCoastline() { TIME && console.time("drawCoastline"); reMarkFeatures(); - const cells = pack.cells, - vertices = pack.vertices, - n = cells.i.length, - features = pack.features; + const {cells, vertices, features} = pack; + const n = cells.i.length; + const used = new Uint8Array(features.length); // store connected features const largestLand = d3.scan( features.map(f => (f.land ? f.cells : 0)), diff --git a/modules/ui/measurers.js b/src/modules/measurers.js similarity index 98% rename from modules/ui/measurers.js rename to src/modules/measurers.js index 54a6d9f8..15456485 100644 --- a/modules/ui/measurers.js +++ b/src/modules/measurers.js @@ -1,4 +1,6 @@ -class Rulers { +import {findCell} from "/src/utils/graphUtils"; + +export class Rulers { constructor() { this.data = []; } @@ -113,7 +115,7 @@ class Measurer { } } -class Ruler extends Measurer { +export class Ruler extends Measurer { constructor(points) { super(points); } @@ -275,7 +277,7 @@ class Ruler extends Measurer { } } -class Opisometer extends Measurer { +export class Opisometer extends Measurer { constructor(points) { super(points); } @@ -365,7 +367,7 @@ class Opisometer extends Measurer { } } -class RouteOpisometer extends Measurer { +export class RouteOpisometer extends Measurer { constructor(points) { super(points); if (pack.cells) { @@ -495,7 +497,7 @@ class RouteOpisometer extends Measurer { } } -class Planimeter extends Measurer { +export class Planimeter extends Measurer { constructor(points) { super(points); } @@ -533,8 +535,7 @@ class Planimeter extends Measurer { } } -// Scale bar -function drawScaleBar(scaleLevel) { +export function drawScaleBar(scaleLevel) { if (scaleBar.style("display") === "none") return; // no need to re-draw hidden element scaleBar.selectAll("*").remove(); // fully redraw every time @@ -621,7 +622,7 @@ function drawScaleBar(scaleLevel) { } // fit ScaleBar to canvas size -function fitScaleBar() { +export function fitScaleBar() { if (!scaleBar.select("rect").size() || scaleBar.style("display") === "none") return; const px = isNaN(+barPosX.value) ? 0.99 : barPosX.value / 100; const py = isNaN(+barPosY.value) ? 0.99 : barPosY.value / 100; diff --git a/src/types/global.d.ts b/src/types/global.d.ts index a2d9e3fa..102e7b3e 100644 --- a/src/types/global.d.ts +++ b/src/types/global.d.ts @@ -4,4 +4,12 @@ interface Navigator { }; } +interface Window { + fmg: { + modules: { + [key: string]: boolean; + }; + }; +} + type UnknownObject = {[key: string]: unknown}; diff --git a/src/utils/graphUtils.js b/src/utils/graphUtils.ts similarity index 73% rename from src/utils/graphUtils.js rename to src/utils/graphUtils.ts index 64936b26..2510357a 100644 --- a/src/utils/graphUtils.js +++ b/src/utils/graphUtils.ts @@ -1,5 +1,5 @@ import {TIME} from "../config/logging"; -import {createTypedArray} from "."; +import {createTypedArray} from "./arrayUtils"; // check if new grid graph should be generated or we can use the existing one export function shouldRegenerateGrid(grid) { @@ -103,7 +103,7 @@ export function findGridCell(x, y, grid) { } // return array of cell indexes in radius on a regular square grid -function findGridAll(x, y, radius) { +export function findGridAll(x, y, radius) { const c = grid.cells.c; let r = Math.floor(radius / grid.spacing); let found = [findGridCell(x, y, grid)]; @@ -128,11 +128,6 @@ function findGridAll(x, y, radius) { return found; } -// return closest pack points quadtree datum -function find(x, y, radius = Infinity) { - return pack.cells.q.find(x, y, radius); -} - // return array of cell indexes in radius export function findAll(x, y, radius) { const found = pack.cells.q.findAll(x, y, radius); @@ -151,72 +146,10 @@ export function findCell(x, y, radius = Infinity) { } // get polygon points for initial cells knowing cell id -function getGridPolygon(i) { +export function getGridPolygon(i) { return grid.cells.v[i].map(v => grid.vertices.p[v]); } -// mbostock's poissonDiscSampler -function* poissonDiscSampler(x0, y0, x1, y1, r, k = 3) { - if (!(x1 >= x0) || !(y1 >= y0) || !(r > 0)) throw new Error(); - - const width = x1 - x0; - const height = y1 - y0; - const r2 = r * r; - const r2_3 = 3 * r2; - const cellSize = r * Math.SQRT1_2; - const gridWidth = Math.ceil(width / cellSize); - const gridHeight = Math.ceil(height / cellSize); - const grid = new Array(gridWidth * gridHeight); - const queue = []; - - function far(x, y) { - const i = (x / cellSize) | 0; - const j = (y / cellSize) | 0; - const i0 = Math.max(i - 2, 0); - const j0 = Math.max(j - 2, 0); - const i1 = Math.min(i + 3, gridWidth); - const j1 = Math.min(j + 3, gridHeight); - for (let j = j0; j < j1; ++j) { - const o = j * gridWidth; - for (let i = i0; i < i1; ++i) { - const s = grid[o + i]; - if (s) { - const dx = s[0] - x; - const dy = s[1] - y; - if (dx * dx + dy * dy < r2) return false; - } - } - } - return true; - } - - function sample(x, y) { - queue.push((grid[gridWidth * ((y / cellSize) | 0) + ((x / cellSize) | 0)] = [x, y])); - return [x + x0, y + y0]; - } - - yield sample(width / 2, height / 2); - - pick: while (queue.length) { - const i = (Math.random() * queue.length) | 0; - const parent = queue[i]; - - for (let j = 0; j < k; ++j) { - const a = 2 * Math.PI * Math.random(); - const r = Math.sqrt(Math.random() * r2_3 + r2); - const x = parent[0] + r * Math.cos(a); - const y = parent[1] + r * Math.sin(a); - if (0 <= x && x < width && 0 <= y && y < height && far(x, y)) { - yield sample(x, y); - continue pick; - } - } - - const r = queue.pop(); - if (i < queue.length) queue[i] = r; - } -} - // filter land cells export function isLand(i) { return pack.cells.h[i] >= 20; @@ -308,34 +241,3 @@ void (function addFindAll() { } }; })(); - -// helper function non-used for the generation -function drawCellsValue(data) { - debug.selectAll("text").remove(); - debug - .selectAll("text") - .data(data) - .enter() - .append("text") - .attr("x", (d, i) => pack.cells.p[i][0]) - .attr("y", (d, i) => pack.cells.p[i][1]) - .text(d => d); -} - -// helper function non-used for the generation -function drawPolygons(data) { - const max = d3.max(data), - min = d3.min(data), - scheme = getColorScheme(terrs.attr("scheme")); - data = data.map(d => 1 - normalize(d, min, max)); - - debug.selectAll("polygon").remove(); - debug - .selectAll("polygon") - .data(data) - .enter() - .append("polygon") - .attr("points", (d, i) => getPackPolygon(i)) - .attr("fill", d => scheme(d)) - .attr("stroke", d => scheme(d)); -} diff --git a/src/utils/index.ts b/src/utils/index.ts deleted file mode 100644 index c627e1db..00000000 --- a/src/utils/index.ts +++ /dev/null @@ -1 +0,0 @@ -export {last, unique, createTypedArray} from "./arrayUtils";