Pass projection into LiveAtlasMapDefinition instead of creating it internally

This commit is contained in:
James Lyne 2022-02-21 20:27:09 +00:00
parent 6b0317b595
commit 68eccb2b5b
4 changed files with 26 additions and 23 deletions

7
src/index.d.ts vendored
View File

@ -20,7 +20,7 @@ import LiveAtlasMapDefinition from "@/model/LiveAtlasMapDefinition";
import { import {
Coords, Coords,
DoneCallback, FitBoundsOptions, DoneCallback, FitBoundsOptions,
InternalTiles, InternalTiles, LatLng,
PathOptions, PathOptions,
PointTuple, PointTuple,
PolylineOptions PolylineOptions
@ -160,6 +160,11 @@ interface LiveAtlasWorldDefinition {
maps: Set<LiveAtlasMapDefinition>; maps: Set<LiveAtlasMapDefinition>;
} }
interface LiveAtlasProjection {
locationToLatLng(location: Coordinate): LatLng;
latLngToLocation(latLng: LatLng, y: number): Coordinate;
}
interface LiveAtlasWorldState { interface LiveAtlasWorldState {
raining: boolean; raining: boolean;
thundering: boolean; thundering: boolean;

View File

@ -18,22 +18,22 @@
*/ */
import {LatLng} from 'leaflet'; import {LatLng} from 'leaflet';
import {Coordinate} from "@/index"; import {Coordinate, LiveAtlasProjection} from "@/index";
export interface LiveAtlasProjectionOptions { export interface DynmapProjectionOptions {
mapToWorld: [number, number, number, number, number, number, number, number, number], mapToWorld: [number, number, number, number, number, number, number, number, number],
worldToMap: [number, number, number, number, number, number, number, number, number], worldToMap: [number, number, number, number, number, number, number, number, number],
nativeZoomLevels: number, nativeZoomLevels: number,
tileSize: number, tileSize: number,
} }
export class LiveAtlasProjection { export class DynmapProjection implements LiveAtlasProjection {
private readonly mapToWorld: [number, number, number, number, number, number, number, number, number]; private readonly mapToWorld: [number, number, number, number, number, number, number, number, number];
private readonly worldToMap: [number, number, number, number, number, number, number, number, number]; private readonly worldToMap: [number, number, number, number, number, number, number, number, number];
private readonly nativeZoomLevels: number; private readonly nativeZoomLevels: number;
private readonly tileSize: number; private readonly tileSize: number;
constructor(options: LiveAtlasProjectionOptions) { constructor(options: DynmapProjectionOptions) {
this.mapToWorld = options.mapToWorld || [0, 0, 0, 0, 0, 0, 0, 0]; this.mapToWorld = options.mapToWorld || [0, 0, 0, 0, 0, 0, 0, 0];
this.worldToMap = options.worldToMap || [0, 0, 0, 0, 0, 0, 0, 0]; this.worldToMap = options.worldToMap || [0, 0, 0, 0, 0, 0, 0, 0];
this.nativeZoomLevels = options.nativeZoomLevels || 1; this.nativeZoomLevels = options.nativeZoomLevels || 1;

View File

@ -14,9 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
import {Coordinate, LiveAtlasWorldDefinition} from "@/index"; import {Coordinate, LiveAtlasProjection, LiveAtlasWorldDefinition} from "@/index";
import {LatLng} from "leaflet"; import {LatLng} from "leaflet";
import {LiveAtlasProjection} from "@/model/LiveAtlasProjection";
import {ImageFormat} from "dynmap"; import {ImageFormat} from "dynmap";
export interface LiveAtlasMapDefinitionOptions { export interface LiveAtlasMapDefinitionOptions {
@ -32,8 +31,7 @@ export interface LiveAtlasMapDefinitionOptions {
imageFormat: ImageFormat; imageFormat: ImageFormat;
tileSize: number; tileSize: number;
prefix?: string; prefix?: string;
mapToWorld?: [number, number, number, number, number, number, number, number, number]; projection?: LiveAtlasProjection;
worldToMap?: [number, number, number, number, number, number, number, number, number];
nativeZoomLevels: number; nativeZoomLevels: number;
extraZoomLevels: number; extraZoomLevels: number;
tileUpdateInterval?: number; tileUpdateInterval?: number;
@ -52,7 +50,7 @@ export default class LiveAtlasMapDefinition {
readonly imageFormat: ImageFormat; readonly imageFormat: ImageFormat;
readonly tileSize: number; readonly tileSize: number;
readonly prefix: string; readonly prefix: string;
private readonly projection?: Readonly<LiveAtlasProjection>; readonly projection?: LiveAtlasProjection;
readonly nativeZoomLevels: number; readonly nativeZoomLevels: number;
readonly extraZoomLevels: number; readonly extraZoomLevels: number;
readonly scale: number; readonly scale: number;
@ -73,20 +71,12 @@ export default class LiveAtlasMapDefinition {
this.imageFormat = options.imageFormat; this.imageFormat = options.imageFormat;
this.tileSize = options.tileSize; this.tileSize = options.tileSize;
this.prefix = options.prefix || ''; this.prefix = options.prefix || '';
this.projection = options.projection || undefined;
this.nativeZoomLevels = options.nativeZoomLevels || 1; this.nativeZoomLevels = options.nativeZoomLevels || 1;
this.extraZoomLevels = options.extraZoomLevels || 0; this.extraZoomLevels = options.extraZoomLevels || 0;
this.scale = (1 / Math.pow(2, this.nativeZoomLevels)); this.scale = (1 / Math.pow(2, this.nativeZoomLevels));
this.tileUpdateInterval = options.tileUpdateInterval || undefined; this.tileUpdateInterval = options.tileUpdateInterval || undefined;
if(options.mapToWorld || options.worldToMap) {
this.projection = new LiveAtlasProjection({
mapToWorld: options.mapToWorld || [0, 0, 0, 0, 0, 0, 0, 0, 0],
worldToMap: options.worldToMap || [0, 0, 0, 0, 0, 0, 0, 0, 0],
nativeZoomLevels: this.nativeZoomLevels,
tileSize: this.tileSize
});
}
} }
locationToLatLng(location: Coordinate): LatLng { locationToLatLng(location: Coordinate): LatLng {

View File

@ -52,6 +52,7 @@ import {
} from "dynmap"; } from "dynmap";
import {PointTuple} from "leaflet"; import {PointTuple} from "leaflet";
import {LiveAtlasMarkerType} from "@/util/markers"; import {LiveAtlasMarkerType} from "@/util/markers";
import {DynmapProjection} from "@/leaflet/projection/DynmapProjection";
export function buildServerConfig(response: Options): LiveAtlasServerConfig { export function buildServerConfig(response: Options): LiveAtlasServerConfig {
let title = 'Dynmap'; let title = 'Dynmap';
@ -126,6 +127,9 @@ export function buildWorlds(response: Configuration): Array<LiveAtlasWorldDefini
return; return;
} }
const tileSize = 128 << (map.tilescale || 0),
nativeZoomLevels = map.mapzoomout || 1;
// Maps with append_to_world set are added both the original and target world's map set // Maps with append_to_world set are added both the original and target world's map set
// The world property is always the original world, an additional appendedWorld property contains the target world // The world property is always the original world, an additional appendedWorld property contains the target world
const mapDef = Object.freeze(new LiveAtlasMapDefinition({ const mapDef = Object.freeze(new LiveAtlasMapDefinition({
@ -136,14 +140,18 @@ export function buildWorlds(response: Configuration): Array<LiveAtlasWorldDefini
backgroundNight: map.backgroundnight || '#000000', backgroundNight: map.backgroundnight || '#000000',
icon: (map.icon || undefined) as string | undefined, icon: (map.icon || undefined) as string | undefined,
imageFormat: map['image-format'] || 'png', imageFormat: map['image-format'] || 'png',
tileSize: 128 << (map.tilescale || 0), tileSize,
name: map.name || '(Unnamed map)', name: map.name || '(Unnamed map)',
nightAndDay: map.nightandday || false, nightAndDay: map.nightandday || false,
prefix: map.prefix || '', prefix: map.prefix || '',
displayName: map.title || '', displayName: map.title || '',
mapToWorld: map.maptoworld || undefined, projection: new DynmapProjection({
worldToMap: map.worldtomap || undefined, mapToWorld: map.maptoworld || undefined,
nativeZoomLevels: map.mapzoomout || 1, worldToMap: map.worldtomap || undefined,
nativeZoomLevels,
tileSize,
}),
nativeZoomLevels,
extraZoomLevels: map.mapzoomin || 0 extraZoomLevels: map.mapzoomin || 0
})) as LiveAtlasMapDefinition; })) as LiveAtlasMapDefinition;