refactor: fix ts errors

This commit is contained in:
Azgaar 2022-07-05 22:00:34 +03:00
parent 98ae3292fc
commit 3018d94618
17 changed files with 83 additions and 54 deletions

View file

@ -1,3 +1,4 @@
// @ts-nocheck
import {TIME} from "config/logging";
import {drawBiomes} from "./drawBiomes";
import {drawBorders} from "./drawBorders";

View file

@ -8,6 +8,8 @@ import {editStyle, calculateFriendlyGridSize, shiftCompass} from "modules/ui/sty
import {turnLayerButtonOn, turnLayerButtonOff, layerIsOn} from "./utils";
import {renderLayer} from "./renderers";
import {getInputNumber, getInputValue} from "utils/nodeUtils";
// @ts-expect-error js module
import {editUnits} from "modules/ui/editors";
const layerTogglesMap = {
toggleBiomes,
@ -465,20 +467,14 @@ function toggleScaleBar(event?: MouseEvent) {
if (!layerIsOn("toggleScaleBar")) {
turnLayerButtonOn("toggleScaleBar");
$("#scaleBar").fadeIn();
if (isCtrlPressed(event)) openUnitsEditor();
if (isCtrlPressed(event)) editUnits();
} else {
if (isCtrlPressed(event)) openUnitsEditor();
if (isCtrlPressed(event)) editUnits();
else {
$("#scaleBar").fadeOut();
turnLayerButtonOff("toggleScaleBar");
}
}
async function openUnitsEditor() {
// @ts-ignore untyped module
const {editUnits} = await import("../modules/ui/units-editor.js");
editUnits();
}
}
function toggleZones(event?: MouseEvent) {

View file

@ -14,6 +14,8 @@ import {addGlobalListeners} from "./scripts/listeners";
import {tip} from "./scripts/tooltips";
import {checkForUpdates} from "./scripts/updater";
import {getInputNumber} from "utils/nodeUtils";
// @ts-expect-error js module
import {editUnits} from "modules/ui/editors";
checkForUpdates();
addGlobalListeners();
@ -52,7 +54,7 @@ svgHeight = 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
.on("mousemove", () => tip("Drag to change the position. Click to hide the legend"))
.on("click", () => clearLegend());

View file

@ -11,6 +11,7 @@ import {link} from "utils/linkUtils";
import {minmax, rn} from "utils/numberUtils";
import {regenerateMap} from "scripts/generation";
import {reMarkFeatures} from "modules/markup";
import {editUnits} from "modules/ui/editors";
// add drag to upload logic, pull request from @evyatron
export function addDragToUpload() {
@ -480,7 +481,7 @@ async function parseLoadedData(data) {
})();
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
.on("mousemove", () => tip("Drag to change the position. Click to hide the legend"))
.on("click", () => clearLegend());

View file

@ -1027,20 +1027,25 @@ function refreshAllEditors() {
}
// dynamically loaded editors
async function editStates() {
export async function editStates() {
if (customization) return;
const Editor = await import("../dynamic/editors/states-editor.js?v=12062022");
Editor.open();
}
async function editCultures() {
export async function editCultures() {
if (customization) return;
const Editor = await import("../dynamic/editors/cultures-editor.js?v=1.87.01");
Editor.open();
}
async function editReligions() {
export async function editReligions() {
if (customization) return;
const Editor = await import("../dynamic/editors/religions-editor.js?v=1.87.01");
Editor.open();
}
export async function editUnits() {
const {open} = await import("./units-editor.js");
open();
}

View file

@ -1,16 +1,17 @@
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 {editUnits} from "modules/ui/editors";
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) {
if (customization) return tip("Please exit the customization mode first", false, "warning");

View file

@ -5,7 +5,7 @@ import {findCell} from "utils/graphUtils";
import {tip} from "scripts/tooltips";
import {prompt} from "scripts/prompt";
export function editUnits() {
export function open() {
closeDialogs("#unitsEditor, .stable");
$("#unitsEditor").dialog();

View file

@ -1,5 +1,3 @@
import {ERROR} from "../config/logging";
interface IPromptStringOptions {
default: string;
required?: boolean;

View file

@ -10,7 +10,7 @@ import {rn} from "utils/numberUtils";
export function reGraph() {
TIME && console.time("reGraph");
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;
for (const i of gridCells.i) {

View file

@ -6,5 +6,7 @@ interface Dict<T> {
[key: string]: T;
}
type IntArray = Uint8Array | Uint16Array | Uint32Array;
type RGB = `rgb(${number}, ${number}, ${number})`;
type Hex = `#${string}`;

18
src/types/grid.d.ts vendored
View file

@ -1,7 +1,21 @@
interface IGrid {
spacing: number;
boundary: TPoints;
points: TPoints;
features: IFeature[];
cells: {
h: TypedArray;
prec: number[];
i: IntArray;
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
View file

@ -1,10 +1,20 @@
interface IPack {
vertices: {
p: TPoints;
v: number[][];
c: number[][];
};
cells: {
i: number[];
g: number[];
h: number[];
pop: number[];
burg: number[];
i: IntArray;
p: TPoints;
v: number[][];
c: number[][];
g: IntArray;
h: IntArray;
pop: Float32Array;
burg: IntArray;
area: IntArray;
q: d3.Quadtree<number[]>;
};
states: IState[];
cultures: ICulture[];

View file

@ -15,18 +15,28 @@ const c12: Hex[] = [
"#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) {
const cRB = d3.scaleSequential(d3.interpolateRainbow);
const scheme = colorSchemeMap.default;
const colors = d3.shuffle(
d3
.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;
}
export function getRandomColor() {
return d3.color(d3.scaleSequential(d3.interpolateRainbow)(Math.random())).hex();
export function getRandomColor(): Hex {
const rgb = colorSchemeMap.default(Math.random());
return d3.color(rgb)!.formatHex() as Hex;
}
// 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 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) {
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")) {
const fittedValue = height < 20 ? height - 5 : height;
return scheme(1 - fittedValue / 100);

View file

@ -1,13 +1,11 @@
import {rn} from "./numberUtils";
const {mapCoordinates, graphWidth, graphHeight} = window;
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) {
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) {

View file

@ -148,7 +148,9 @@ export function getPackPolygon(i: number) {
}
// 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);
return found ? found[2] : undefined;
}

View file

@ -2,7 +2,6 @@ import {polygon} from "lineclip";
// clip polygon by graph bbox
export function clipPoly(points: TPoints) {
// @ts-expect-error graphWidth/graphWidth are global variables
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
export function getMiddlePoint(cell1: number, cell2: number) {
// @ts-expect-error pack is global variable
const {cells, vertices} = pack;
const commonVertices = cells.v[cell1].filter((vertex: number) =>