2020-11-24 01:52:31 +00:00
|
|
|
import L, {Coords, DoneCallback, TileLayerOptions} from 'leaflet';
|
|
|
|
import {DynmapProjection} from "@/leaflet/projection/DynmapProjection";
|
|
|
|
import {Coordinate, DynmapMap} from "@/dynmap";
|
|
|
|
|
|
|
|
export interface DynmapTileLayerOptions extends TileLayerOptions {
|
|
|
|
mapSettings: DynmapMap;
|
|
|
|
errorTileUrl: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DynmapTileLayer extends L.TileLayer {
|
|
|
|
options: DynmapTileLayerOptions;
|
2020-12-01 23:20:38 +00:00
|
|
|
_projection: DynmapProjection;
|
|
|
|
_mapSettings: DynmapMap;
|
|
|
|
_cachedTileUrls: Map<string, string>;
|
|
|
|
_namedTiles: Map<string, DynmapTileElement>;
|
|
|
|
_tileTemplate: DynmapTileElement;
|
|
|
|
_loadQueue: DynmapTileElement[];
|
|
|
|
_loadingTiles: Set<DynmapTileElement>;
|
2020-11-24 01:52:31 +00:00
|
|
|
|
|
|
|
locationToLatLng(location: Coordinate): L.LatLng;
|
|
|
|
|
|
|
|
latLngToLocation(latLng: L.LatLng): Coordinate;
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
export interface DynmapTile {
|
|
|
|
active?: boolean;
|
|
|
|
coords: Coords;
|
|
|
|
current: boolean;
|
|
|
|
el: DynmapTileElement;
|
|
|
|
loaded?: Date;
|
|
|
|
retain?: boolean;
|
|
|
|
complete: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DynmapTileElement extends HTMLImageElement {
|
2020-11-24 01:52:31 +00:00
|
|
|
tileName: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TileInfo {
|
|
|
|
prefix: string;
|
|
|
|
nightday: string;
|
|
|
|
scaledx: number;
|
|
|
|
scaledy: number;
|
|
|
|
zoom: string;
|
|
|
|
zoomprefix: string;
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
fmt: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DynmapTileLayer extends L.TileLayer {
|
2020-12-01 23:20:38 +00:00
|
|
|
|
2020-11-24 01:52:31 +00:00
|
|
|
constructor(options: DynmapTileLayerOptions) {
|
|
|
|
super('', options);
|
2020-12-01 23:20:38 +00:00
|
|
|
L.Util.setOptions(this, options);
|
2020-11-24 01:52:31 +00:00
|
|
|
|
|
|
|
if (options.mapSettings === null) {
|
|
|
|
throw new TypeError("mapSettings missing");
|
|
|
|
}
|
|
|
|
|
|
|
|
this._projection = new DynmapProjection({});
|
|
|
|
this._mapSettings = options.mapSettings;
|
2020-12-01 23:20:38 +00:00
|
|
|
this._cachedTileUrls = Object.seal(new Map());
|
|
|
|
this._namedTiles = Object.seal(new Map());
|
|
|
|
this._loadQueue = [];
|
|
|
|
this._loadingTiles = Object.seal(new Set());
|
|
|
|
|
|
|
|
this._tileTemplate = L.DomUtil.create('img', 'leaflet-tile') as DynmapTileElement;
|
|
|
|
this._tileTemplate.style.width = this._tileTemplate.style.height = this.options.tileSize + 'px';
|
|
|
|
this._tileTemplate.alt = '';
|
|
|
|
this._tileTemplate.tileName = '';
|
|
|
|
this._tileTemplate.setAttribute('role', 'presentation');
|
|
|
|
|
|
|
|
Object.seal(this._tileTemplate);
|
|
|
|
|
|
|
|
if(this.options.crossOrigin || this.options.crossOrigin === '') {
|
|
|
|
this._tileTemplate.crossOrigin = this.options.crossOrigin === true ? '' : this.options.crossOrigin;
|
|
|
|
}
|
2020-11-24 01:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getTileName(coords: Coordinate): string {
|
|
|
|
throw "getTileName not implemented";
|
|
|
|
}
|
|
|
|
|
|
|
|
getTileUrl(coords: Coordinate) {
|
|
|
|
const tileName = this.getTileName(coords);
|
2020-12-01 23:20:38 +00:00
|
|
|
let url = this._cachedTileUrls.get(tileName);
|
2020-11-24 01:52:31 +00:00
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
const path = escape(`${this._mapSettings.world.name}/${tileName}`);
|
|
|
|
url = `${window.config.url.tiles}${path}`;
|
2020-12-01 23:20:38 +00:00
|
|
|
this._cachedTileUrls.set(tileName, url);
|
2020-11-24 01:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateNamedTile(name: string) {
|
2020-12-01 23:20:38 +00:00
|
|
|
const tile = this._namedTiles.get(name);
|
|
|
|
this._cachedTileUrls.delete(name);
|
2020-11-24 01:52:31 +00:00
|
|
|
|
|
|
|
if (tile) {
|
|
|
|
//tile.src = this._cachedTileUrls[name] = this.getTileUrl(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createTile(coords: Coords, done: DoneCallback) {
|
2020-12-01 23:20:38 +00:00
|
|
|
//Clone template image instead of creating a new one
|
|
|
|
const tile = this._tileTemplate.cloneNode(false) as DynmapTileElement;
|
|
|
|
|
|
|
|
tile.tileName = this.getTileName(coords);
|
|
|
|
tile.dataset.src = this.getTileUrl(coords);
|
|
|
|
|
|
|
|
this._namedTiles.set(tile.tileName, tile);
|
|
|
|
this._loadQueue.push(tile);
|
2020-11-24 01:52:31 +00:00
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
//Use addEventListener here
|
|
|
|
tile.onload = () => {
|
|
|
|
this._tileOnLoad(done, tile);
|
|
|
|
this._loadingTiles.delete(tile);
|
|
|
|
this._tickLoadQueue();
|
|
|
|
};
|
|
|
|
tile.onerror = () => {
|
|
|
|
this._tileOnError(done, tile, {name: 'Error', message: 'Error'});
|
|
|
|
this._loadingTiles.delete(tile);
|
|
|
|
this._tickLoadQueue();
|
|
|
|
};
|
2020-11-24 01:52:31 +00:00
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
this._tickLoadQueue();
|
2020-11-24 01:52:31 +00:00
|
|
|
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
_tickLoadQueue() {
|
2020-12-10 02:22:59 +00:00
|
|
|
if (this._loadingTiles.size > 6) {
|
2020-12-01 23:20:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tile = this._loadQueue.shift();
|
|
|
|
|
|
|
|
if (!tile) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._loadingTiles.add(tile);
|
|
|
|
tile.src = tile.dataset.src as string;
|
|
|
|
}
|
|
|
|
|
2020-11-24 01:52:31 +00:00
|
|
|
// stops loading all tiles in the background layer
|
|
|
|
_abortLoading() {
|
|
|
|
let tile;
|
2020-12-01 23:20:38 +00:00
|
|
|
|
2020-11-24 01:52:31 +00:00
|
|
|
for (const i in this._tiles) {
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(this._tiles, i)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
tile = this._tiles[i] as DynmapTile;
|
2020-11-24 01:52:31 +00:00
|
|
|
|
|
|
|
if (tile.coords.z !== this._tileZoom) {
|
2020-12-01 23:20:38 +00:00
|
|
|
if (tile.loaded && tile.el && tile.el.tileName) {
|
|
|
|
this._namedTiles.delete(tile.el.tileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this._loadQueue.includes(tile.el)) {
|
|
|
|
this._loadQueue.splice(this._loadQueue.indexOf(tile.el), 1);
|
2020-11-24 01:52:31 +00:00
|
|
|
}
|
2020-12-01 23:20:38 +00:00
|
|
|
|
|
|
|
this._loadingTiles.delete(tile.el);
|
2020-11-24 01:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super._abortLoading.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
_removeTile(key: string) {
|
2020-12-01 23:20:38 +00:00
|
|
|
const tile = this._tiles[key] as DynmapTile;
|
2020-11-24 01:52:31 +00:00
|
|
|
|
|
|
|
if (!tile) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
const tileName = tile.el.tileName as string;
|
2020-11-24 01:52:31 +00:00
|
|
|
|
|
|
|
if (tileName) {
|
2020-12-01 23:20:38 +00:00
|
|
|
this._namedTiles.delete(tileName);
|
|
|
|
this._cachedTileUrls.delete(tileName);
|
|
|
|
this._loadingTiles.delete(tile.el);
|
|
|
|
|
|
|
|
if(this._loadQueue.includes(tile.el)) {
|
|
|
|
this._loadQueue.splice(this._loadQueue.indexOf(tile.el), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
tile.el.onerror = null;
|
|
|
|
tile.el.onload = null;
|
2020-11-24 01:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
super._removeTile.call(this, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some helper functions.
|
|
|
|
zoomprefix(amount: number) {
|
|
|
|
return 'z'.repeat(amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
getTileInfo(coords: Coordinate): TileInfo {
|
|
|
|
// zoom: max zoomed in = this.options.maxZoom, max zoomed out = 0
|
|
|
|
// izoom: max zoomed in = 0, max zoomed out = this.options.maxZoom
|
|
|
|
// zoomoutlevel: izoom < mapzoomin -> 0, else -> izoom - mapzoomin (which ranges from 0 till mapzoomout)
|
|
|
|
const izoom = this._getZoomForUrl(),
|
|
|
|
zoomoutlevel = Math.max(0, izoom - this._mapSettings.extraZoomLevels),
|
|
|
|
scale = 1 << zoomoutlevel,
|
|
|
|
x = scale * coords.x,
|
|
|
|
y = scale * coords.y;
|
|
|
|
|
|
|
|
return {
|
|
|
|
prefix: this._mapSettings.prefix,
|
|
|
|
nightday: /*(this._mapSettings.nightAndDay && this.options.dynmap.serverday) ? '_day' :*/ '',
|
|
|
|
scaledx: x >> 5,
|
|
|
|
scaledy: y >> 5,
|
|
|
|
zoom: this.zoomprefix(zoomoutlevel),
|
|
|
|
zoomprefix: (zoomoutlevel == 0) ? "" : (this.zoomprefix(zoomoutlevel) + "_"),
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
fmt: this._mapSettings.imageFormat || 'png'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getProjection(): DynmapProjection {
|
|
|
|
return this._projection;
|
|
|
|
}
|
|
|
|
}
|