mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 17:51:24 +01:00
refactor: fix ts errors
This commit is contained in:
parent
98ae3292fc
commit
3018d94618
17 changed files with 83 additions and 54 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
// @ts-nocheck
|
||||||
import {TIME} from "config/logging";
|
import {TIME} from "config/logging";
|
||||||
import {drawBiomes} from "./drawBiomes";
|
import {drawBiomes} from "./drawBiomes";
|
||||||
import {drawBorders} from "./drawBorders";
|
import {drawBorders} from "./drawBorders";
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ import {editStyle, calculateFriendlyGridSize, shiftCompass} from "modules/ui/sty
|
||||||
import {turnLayerButtonOn, turnLayerButtonOff, layerIsOn} from "./utils";
|
import {turnLayerButtonOn, turnLayerButtonOff, layerIsOn} from "./utils";
|
||||||
import {renderLayer} from "./renderers";
|
import {renderLayer} from "./renderers";
|
||||||
import {getInputNumber, getInputValue} from "utils/nodeUtils";
|
import {getInputNumber, getInputValue} from "utils/nodeUtils";
|
||||||
|
// @ts-expect-error js module
|
||||||
|
import {editUnits} from "modules/ui/editors";
|
||||||
|
|
||||||
const layerTogglesMap = {
|
const layerTogglesMap = {
|
||||||
toggleBiomes,
|
toggleBiomes,
|
||||||
|
|
@ -465,20 +467,14 @@ function toggleScaleBar(event?: MouseEvent) {
|
||||||
if (!layerIsOn("toggleScaleBar")) {
|
if (!layerIsOn("toggleScaleBar")) {
|
||||||
turnLayerButtonOn("toggleScaleBar");
|
turnLayerButtonOn("toggleScaleBar");
|
||||||
$("#scaleBar").fadeIn();
|
$("#scaleBar").fadeIn();
|
||||||
if (isCtrlPressed(event)) openUnitsEditor();
|
if (isCtrlPressed(event)) editUnits();
|
||||||
} else {
|
} else {
|
||||||
if (isCtrlPressed(event)) openUnitsEditor();
|
if (isCtrlPressed(event)) editUnits();
|
||||||
else {
|
else {
|
||||||
$("#scaleBar").fadeOut();
|
$("#scaleBar").fadeOut();
|
||||||
turnLayerButtonOff("toggleScaleBar");
|
turnLayerButtonOff("toggleScaleBar");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function openUnitsEditor() {
|
|
||||||
// @ts-ignore untyped module
|
|
||||||
const {editUnits} = await import("../modules/ui/units-editor.js");
|
|
||||||
editUnits();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleZones(event?: MouseEvent) {
|
function toggleZones(event?: MouseEvent) {
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ import {addGlobalListeners} from "./scripts/listeners";
|
||||||
import {tip} from "./scripts/tooltips";
|
import {tip} from "./scripts/tooltips";
|
||||||
import {checkForUpdates} from "./scripts/updater";
|
import {checkForUpdates} from "./scripts/updater";
|
||||||
import {getInputNumber} from "utils/nodeUtils";
|
import {getInputNumber} from "utils/nodeUtils";
|
||||||
|
// @ts-expect-error js module
|
||||||
|
import {editUnits} from "modules/ui/editors";
|
||||||
|
|
||||||
checkForUpdates();
|
checkForUpdates();
|
||||||
addGlobalListeners();
|
addGlobalListeners();
|
||||||
|
|
@ -52,7 +54,7 @@ svgHeight = graphHeight;
|
||||||
|
|
||||||
defineSvg(graphWidth, graphHeight);
|
defineSvg(graphWidth, graphHeight);
|
||||||
|
|
||||||
scaleBar.on("mousemove", () => tip("Click to open Units Editor")).on("click", () => editUnits());
|
scaleBar.on("mousemove", () => tip("Click to open Units Editor")).on("click", editUnits);
|
||||||
legend
|
legend
|
||||||
.on("mousemove", () => tip("Drag to change the position. Click to hide the legend"))
|
.on("mousemove", () => tip("Drag to change the position. Click to hide the legend"))
|
||||||
.on("click", () => clearLegend());
|
.on("click", () => clearLegend());
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import {link} from "utils/linkUtils";
|
||||||
import {minmax, rn} from "utils/numberUtils";
|
import {minmax, rn} from "utils/numberUtils";
|
||||||
import {regenerateMap} from "scripts/generation";
|
import {regenerateMap} from "scripts/generation";
|
||||||
import {reMarkFeatures} from "modules/markup";
|
import {reMarkFeatures} from "modules/markup";
|
||||||
|
import {editUnits} from "modules/ui/editors";
|
||||||
|
|
||||||
// add drag to upload logic, pull request from @evyatron
|
// add drag to upload logic, pull request from @evyatron
|
||||||
export function addDragToUpload() {
|
export function addDragToUpload() {
|
||||||
|
|
@ -480,7 +481,7 @@ async function parseLoadedData(data) {
|
||||||
})();
|
})();
|
||||||
|
|
||||||
void (function restoreEvents() {
|
void (function restoreEvents() {
|
||||||
scaleBar.on("mousemove", () => tip("Click to open Units Editor")).on("click", () => editUnits());
|
scaleBar.on("mousemove", () => tip("Click to open Units Editor")).on("click", editUnits);
|
||||||
legend
|
legend
|
||||||
.on("mousemove", () => tip("Drag to change the position. Click to hide the legend"))
|
.on("mousemove", () => tip("Drag to change the position. Click to hide the legend"))
|
||||||
.on("click", () => clearLegend());
|
.on("click", () => clearLegend());
|
||||||
|
|
|
||||||
|
|
@ -1027,20 +1027,25 @@ function refreshAllEditors() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// dynamically loaded editors
|
// dynamically loaded editors
|
||||||
async function editStates() {
|
export async function editStates() {
|
||||||
if (customization) return;
|
if (customization) return;
|
||||||
const Editor = await import("../dynamic/editors/states-editor.js?v=12062022");
|
const Editor = await import("../dynamic/editors/states-editor.js?v=12062022");
|
||||||
Editor.open();
|
Editor.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function editCultures() {
|
export async function editCultures() {
|
||||||
if (customization) return;
|
if (customization) return;
|
||||||
const Editor = await import("../dynamic/editors/cultures-editor.js?v=1.87.01");
|
const Editor = await import("../dynamic/editors/cultures-editor.js?v=1.87.01");
|
||||||
Editor.open();
|
Editor.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function editReligions() {
|
export async function editReligions() {
|
||||||
if (customization) return;
|
if (customization) return;
|
||||||
const Editor = await import("../dynamic/editors/religions-editor.js?v=1.87.01");
|
const Editor = await import("../dynamic/editors/religions-editor.js?v=1.87.01");
|
||||||
Editor.open();
|
Editor.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function editUnits() {
|
||||||
|
const {open} = await import("./units-editor.js");
|
||||||
|
open();
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,17 @@
|
||||||
import * as d3 from "d3";
|
import * as d3 from "d3";
|
||||||
|
|
||||||
import {restoreDefaultEvents} from "scripts/events";
|
|
||||||
import {findCell} from "utils/graphUtils";
|
|
||||||
import {last} from "utils/arrayUtils";
|
|
||||||
import {tip, clearMainTip} from "scripts/tooltips";
|
|
||||||
import {rn} from "utils/numberUtils";
|
|
||||||
import {isCtrlPressed} from "utils/keyboardUtils";
|
|
||||||
import {prompt} from "scripts/prompt";
|
|
||||||
import {getNextId} from "utils/nodeUtils";
|
|
||||||
import {P, generateSeed} from "utils/probabilityUtils";
|
|
||||||
import {turnLayerButtonOn} from "layers";
|
import {turnLayerButtonOn} from "layers";
|
||||||
|
import {editUnits} from "modules/ui/editors";
|
||||||
import {aleaPRNG} from "scripts/aleaPRNG";
|
import {aleaPRNG} from "scripts/aleaPRNG";
|
||||||
|
import {restoreDefaultEvents} from "scripts/events";
|
||||||
|
import {prompt} from "scripts/prompt";
|
||||||
|
import {clearMainTip, tip} from "scripts/tooltips";
|
||||||
|
import {last} from "utils/arrayUtils";
|
||||||
|
import {findCell} from "utils/graphUtils";
|
||||||
|
import {isCtrlPressed} from "utils/keyboardUtils";
|
||||||
|
import {getNextId} from "utils/nodeUtils";
|
||||||
|
import {rn} from "utils/numberUtils";
|
||||||
|
import {generateSeed, P} from "utils/probabilityUtils";
|
||||||
|
|
||||||
toolsContent.addEventListener("click", function (event) {
|
toolsContent.addEventListener("click", function (event) {
|
||||||
if (customization) return tip("Please exit the customization mode first", false, "warning");
|
if (customization) return tip("Please exit the customization mode first", false, "warning");
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import {findCell} from "utils/graphUtils";
|
||||||
import {tip} from "scripts/tooltips";
|
import {tip} from "scripts/tooltips";
|
||||||
import {prompt} from "scripts/prompt";
|
import {prompt} from "scripts/prompt";
|
||||||
|
|
||||||
export function editUnits() {
|
export function open() {
|
||||||
closeDialogs("#unitsEditor, .stable");
|
closeDialogs("#unitsEditor, .stable");
|
||||||
$("#unitsEditor").dialog();
|
$("#unitsEditor").dialog();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
import {ERROR} from "../config/logging";
|
|
||||||
|
|
||||||
interface IPromptStringOptions {
|
interface IPromptStringOptions {
|
||||||
default: string;
|
default: string;
|
||||||
required?: boolean;
|
required?: boolean;
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import {rn} from "utils/numberUtils";
|
||||||
export function reGraph() {
|
export function reGraph() {
|
||||||
TIME && console.time("reGraph");
|
TIME && console.time("reGraph");
|
||||||
const {cells: gridCells, points, features} = grid;
|
const {cells: gridCells, points, features} = grid;
|
||||||
const newCells: {p: number[][]; g: number[]; h: number[]} = {p: [], g: [], h: []}; // store new data
|
const newCells: {p: TPoints; g: number[]; h: number[]} = {p: [], g: [], h: []}; // store new data
|
||||||
const spacing2 = grid.spacing ** 2;
|
const spacing2 = grid.spacing ** 2;
|
||||||
|
|
||||||
for (const i of gridCells.i) {
|
for (const i of gridCells.i) {
|
||||||
|
|
|
||||||
2
src/types/common.d.ts
vendored
2
src/types/common.d.ts
vendored
|
|
@ -6,5 +6,7 @@ interface Dict<T> {
|
||||||
[key: string]: T;
|
[key: string]: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type IntArray = Uint8Array | Uint16Array | Uint32Array;
|
||||||
|
|
||||||
type RGB = `rgb(${number}, ${number}, ${number})`;
|
type RGB = `rgb(${number}, ${number}, ${number})`;
|
||||||
type Hex = `#${string}`;
|
type Hex = `#${string}`;
|
||||||
|
|
|
||||||
18
src/types/grid.d.ts
vendored
18
src/types/grid.d.ts
vendored
|
|
@ -1,7 +1,21 @@
|
||||||
interface IGrid {
|
interface IGrid {
|
||||||
|
spacing: number;
|
||||||
|
boundary: TPoints;
|
||||||
points: TPoints;
|
points: TPoints;
|
||||||
|
features: IFeature[];
|
||||||
cells: {
|
cells: {
|
||||||
h: TypedArray;
|
i: IntArray;
|
||||||
prec: number[];
|
b: IntArray;
|
||||||
|
c: number[][];
|
||||||
|
h: IntArray;
|
||||||
|
t: IntArray;
|
||||||
|
f: IntArray;
|
||||||
|
prec: IntArray;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
interface IFeature {
|
||||||
|
i: number;
|
||||||
|
land: boolean;
|
||||||
|
border: boolean;
|
||||||
|
type: "ocean" | "lake" | "island";
|
||||||
|
}
|
||||||
|
|
|
||||||
20
src/types/pack.d.ts
vendored
20
src/types/pack.d.ts
vendored
|
|
@ -1,10 +1,20 @@
|
||||||
interface IPack {
|
interface IPack {
|
||||||
|
vertices: {
|
||||||
|
p: TPoints;
|
||||||
|
v: number[][];
|
||||||
|
c: number[][];
|
||||||
|
};
|
||||||
cells: {
|
cells: {
|
||||||
i: number[];
|
i: IntArray;
|
||||||
g: number[];
|
p: TPoints;
|
||||||
h: number[];
|
v: number[][];
|
||||||
pop: number[];
|
c: number[][];
|
||||||
burg: number[];
|
g: IntArray;
|
||||||
|
h: IntArray;
|
||||||
|
pop: Float32Array;
|
||||||
|
burg: IntArray;
|
||||||
|
area: IntArray;
|
||||||
|
q: d3.Quadtree<number[]>;
|
||||||
};
|
};
|
||||||
states: IState[];
|
states: IState[];
|
||||||
cultures: ICulture[];
|
cultures: ICulture[];
|
||||||
|
|
|
||||||
|
|
@ -15,18 +15,28 @@ const c12: Hex[] = [
|
||||||
"#eb8de7"
|
"#eb8de7"
|
||||||
];
|
];
|
||||||
|
|
||||||
|
type ColorScheme = (n: number) => string;
|
||||||
|
const colorSchemeMap: Dict<ColorScheme> = {
|
||||||
|
default: d3.scaleSequential(d3.interpolateRainbow),
|
||||||
|
bright: d3.scaleSequential(d3.interpolateSpectral),
|
||||||
|
light: d3.scaleSequential(d3.interpolateRdYlGn),
|
||||||
|
green: d3.scaleSequential(d3.interpolateGreens),
|
||||||
|
monochrome: d3.scaleSequential(d3.interpolateGreys)
|
||||||
|
};
|
||||||
|
|
||||||
export function getColors(number: number) {
|
export function getColors(number: number) {
|
||||||
const cRB = d3.scaleSequential(d3.interpolateRainbow);
|
const scheme = colorSchemeMap.default;
|
||||||
const colors = d3.shuffle(
|
const colors = d3.shuffle(
|
||||||
d3
|
d3
|
||||||
.range(number)
|
.range(number)
|
||||||
.map((index: number) => (index < 12 ? c12[index] : d3.color(cRB((index - 12) / (number - 12))).hex()))
|
.map((index: number) => (index < 12 ? c12[index] : d3.color(scheme((index - 12) / (number - 12)))!.formatHex()))
|
||||||
);
|
);
|
||||||
return colors;
|
return colors;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRandomColor() {
|
export function getRandomColor(): Hex {
|
||||||
return d3.color(d3.scaleSequential(d3.interpolateRainbow)(Math.random())).hex();
|
const rgb = colorSchemeMap.default(Math.random());
|
||||||
|
return d3.color(rgb)!.formatHex() as Hex;
|
||||||
}
|
}
|
||||||
|
|
||||||
// mix a color with a random color
|
// mix a color with a random color
|
||||||
|
|
@ -36,22 +46,13 @@ export function getMixedColor(hexColor: string, mixation = 0.2, bright = 0.3) {
|
||||||
const color2 = getRandomColor();
|
const color2 = getRandomColor();
|
||||||
const mixedColor = d3.interpolate(color1, color2)(mixation);
|
const mixedColor = d3.interpolate(color1, color2)(mixation);
|
||||||
|
|
||||||
return d3.color(mixedColor).brighter(bright).hex();
|
return d3.color(mixedColor)!.brighter(bright).hex();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getColorScheme(schemeName: string) {
|
export function getColorScheme(schemeName: string) {
|
||||||
return colorSchemeMap[schemeName] || colorSchemeMap.default;
|
return colorSchemeMap[schemeName] || colorSchemeMap.default;
|
||||||
}
|
}
|
||||||
|
|
||||||
type ColorScheme = (n: number) => RGB;
|
|
||||||
const colorSchemeMap: Dict<ColorScheme> = {
|
|
||||||
default: d3.scaleSequential(d3.interpolateSpectral),
|
|
||||||
bright: d3.scaleSequential(d3.interpolateSpectral),
|
|
||||||
light: d3.scaleSequential(d3.interpolateRdYlGn),
|
|
||||||
green: d3.scaleSequential(d3.interpolateGreens),
|
|
||||||
monochrome: d3.scaleSequential(d3.interpolateGreys)
|
|
||||||
};
|
|
||||||
|
|
||||||
export function getHeightColor(height: number, scheme = getColorScheme("default")) {
|
export function getHeightColor(height: number, scheme = getColorScheme("default")) {
|
||||||
const fittedValue = height < 20 ? height - 5 : height;
|
const fittedValue = height < 20 ? height - 5 : height;
|
||||||
return scheme(1 - fittedValue / 100);
|
return scheme(1 - fittedValue / 100);
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
import {rn} from "./numberUtils";
|
import {rn} from "./numberUtils";
|
||||||
|
|
||||||
const {mapCoordinates, graphWidth, graphHeight} = window;
|
|
||||||
|
|
||||||
function getLongitude(x: number, decimals = 2) {
|
function getLongitude(x: number, decimals = 2) {
|
||||||
return rn(mapCoordinates.lonW + (x / graphWidth) * mapCoordinates.lonT, decimals);
|
return rn(window.mapCoordinates.lonW + (x / graphWidth) * window.mapCoordinates.lonT, decimals);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLatitude(y: number, decimals = 2) {
|
function getLatitude(y: number, decimals = 2) {
|
||||||
return rn(mapCoordinates.latN - (y / graphHeight) * mapCoordinates.latT, decimals);
|
return rn(window.mapCoordinates.latN - (y / graphHeight) * window.mapCoordinates.latT, decimals);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCoordinates(x: number, y: number, decimals = 2) {
|
export function getCoordinates(x: number, y: number, decimals = 2) {
|
||||||
|
|
|
||||||
|
|
@ -148,7 +148,9 @@ export function getPackPolygon(i: number) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// return closest cell index
|
// return closest cell index
|
||||||
export function findCell(x: number, y: number, radius = Infinity) {
|
export function findCell(x: number, y: number): number;
|
||||||
|
export function findCell(x: number, y: number, radius: number): number | undefined;
|
||||||
|
export function findCell(x: number, y: number, radius = Infinity): number | undefined {
|
||||||
const found = pack.cells.q.find(x, y, radius);
|
const found = pack.cells.q.find(x, y, radius);
|
||||||
return found ? found[2] : undefined;
|
return found ? found[2] : undefined;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ import {polygon} from "lineclip";
|
||||||
|
|
||||||
// clip polygon by graph bbox
|
// clip polygon by graph bbox
|
||||||
export function clipPoly(points: TPoints) {
|
export function clipPoly(points: TPoints) {
|
||||||
// @ts-expect-error graphWidth/graphWidth are global variables
|
|
||||||
return polygon(points, [0, 0, graphWidth, graphWidth]);
|
return polygon(points, [0, 0, graphWidth, graphWidth]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,7 +38,6 @@ export function getSegmentId(points: TPoints, point: TPoint, step = 10) {
|
||||||
|
|
||||||
// return center point of common edge of 2 pack cells
|
// return center point of common edge of 2 pack cells
|
||||||
export function getMiddlePoint(cell1: number, cell2: number) {
|
export function getMiddlePoint(cell1: number, cell2: number) {
|
||||||
// @ts-expect-error pack is global variable
|
|
||||||
const {cells, vertices} = pack;
|
const {cells, vertices} = pack;
|
||||||
|
|
||||||
const commonVertices = cells.v[cell1].filter((vertex: number) =>
|
const commonVertices = cells.v[cell1].filter((vertex: number) =>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue