MinedMap/viewer/MinedMap.js

333 lines
7.1 KiB
JavaScript
Raw Normal View History

// bsearch-based array element check
function contains(array, elem) {
let min = 0, max = array.length;
while (min < max) {
const i = min + Math.floor((max-min)/2);
const cur = array[i];
if (cur === elem)
return true;
else if (cur < elem)
min = i + 1;
else
max = i;
}
return false;
}
2024-01-03 04:05:41 +01:00
const MinedMapLayer = L.TileLayer.extend({
initialize: function (mipmaps, layer) {
2024-01-03 04:05:41 +01:00
L.TileLayer.prototype.initialize.call(this, '', {
detectRetina: true,
tileSize: 512,
zoomReverse: true,
minZoom: -(mipmaps.length-1),
maxZoom: 0,
attribution: 'Generated by <a href="https://github.com/neocturne/MinedMap">MinedMap</a>',
});
this.options.maxNativeZoom = this.options.maxZoom;
this.options.maxZoom = undefined;
this.mipmaps = mipmaps;
this.layer = layer;
2015-02-02 09:08:36 +01:00
},
createTile: function (coords, done) {
2024-01-03 04:05:41 +01:00
const tile = L.TileLayer.prototype.createTile.call(this, coords, done);
2024-01-03 04:05:41 +01:00
if (coords.z - this.options.zoomOffset >= 0)
L.DomUtil.addClass(tile, 'overzoomed');
2015-02-02 09:08:36 +01:00
return tile;
},
2024-01-03 04:05:41 +01:00
getTileUrl: function (coords) {
let z = -coords.z + this.options.zoomOffset;
if (z < 0)
z = 0;
2015-02-02 09:08:36 +01:00
2024-01-03 04:05:41 +01:00
const mipmap = this.mipmaps[z];
2020-06-20 14:04:59 +02:00
2024-01-03 04:05:41 +01:00
if (coords.x < mipmap.bounds.minX || coords.x > mipmap.bounds.maxX ||
coords.y < mipmap.bounds.minZ || coords.y > mipmap.bounds.maxZ ||
!contains(mipmap.regions[coords.y] || [], coords.x))
return L.Util.emptyImageUrl;
2020-06-20 14:04:59 +02:00
2024-01-03 04:05:41 +01:00
return 'data/'+this.layer+'/'+z+'/r.'+coords.x+'.'+coords.y+'.png';
2020-06-20 14:04:59 +02:00
},
2015-02-02 09:08:36 +01:00
});
const CoordControl = L.Control.extend({
2015-02-04 07:22:49 +01:00
initialize: function () {
this.options.position = 'bottomleft';
},
onAdd: function (map) {
this._container = L.DomUtil.create('div', 'leaflet-control-attribution');
return this._container;
},
update: function (x, z) {
if (!this._map) { return; }
this._container.innerHTML = 'X: ' + x + '&nbsp;&nbsp;&nbsp;Z: ' + z;
}
});
const parseHash = function () {
const args = {};
2015-02-04 17:00:53 +01:00
if (window.location.hash) {
const parts = window.location.hash.substring(1).split('&');
2015-02-04 17:00:53 +01:00
for (const part of parts) {
const key_value = part.split('=');
const key = key_value[0], value = key_value.slice(1).join('=');
2015-02-04 17:00:53 +01:00
args[key] = value;
}
}
return args;
}
const colors = {
black: '#000000',
dark_blue: '#0000AA',
dark_green: '#00AA00',
dark_aqua: '#00AAAA',
dark_red: '#AA0000',
dark_purple: '#AA00AA',
gold: '#FFAA00',
gray: '#AAAAAA',
dark_gray: '#555555',
blue: '#5555FF',
green: '#55FF55',
aqua: '#55FFFF',
red: '#FF5555',
light_purple: '#FF55FF',
yellow: '#FFFF55',
white: '#FFFFFF',
};
function formatSignLine(line) {
const el = document.createElement('span');
el.style.whiteSpace = 'pre';
el.style.fontFamily = 'sans';
for (const span of line) {
const child = document.createElement('span');
child.textContent = span.text;
const color = colors[span.color ?? 'black'] || colors['black'];
if (span.bold)
child.style.fontWeight = 'bold';
if (span.italic)
child.style.fontStyle = 'italic';
child.style.textDecoration = '';
if (span.underlined)
child.style.textDecoration += ' underline';
if (span.strikethrough)
child.style.textDecoration += ' line-through';
child.style.color = color;
if (span.obfuscated) {
child.style.backgroundColor = color;
child.className = 'obfuscated';
}
el.appendChild(child);
}
return el;
}
function loadSigns(signLayer) {
const xhr = new XMLHttpRequest();
xhr.onload = function () {
const res = JSON.parse(this.responseText);
const groups = {};
// Group signs by x,z coordinates
for (const sign of res.signs) {
const key = `${sign.x},${sign.z}`;
const group = groups[key] ??= [];
group[sign.y] = sign;
}
for (const [key, group] of Object.entries(groups)) {
const el = document.createElement('span');
const [x, z] = key.split(',').map((i) => +i);
group.forEach((sign) => {
if (sign.front_text) {
for (const line of sign.front_text) {
el.appendChild(formatSignLine(line));
el.appendChild(document.createElement('br'));
}
el.appendChild(document.createElement('hr'));
}
if (sign.back_text) {
for (let line of sign.back_text) {
el.appendChild(formatSignLine(line));
el.appendChild(document.createElement('br'));
}
el.appendChild(document.createElement('hr'));
}
});
const lastChild = el.lastChild;
if (lastChild)
lastChild.remove();
L.marker([-z-0.5, x+0.5]).addTo(signLayer).bindPopup(el);
}
}
xhr.open('GET', 'data/entities.json', true);
xhr.send();
}
2015-02-04 17:00:53 +01:00
2015-02-02 09:08:36 +01:00
window.createMap = function () {
const xhr = new XMLHttpRequest();
2015-02-02 09:08:36 +01:00
xhr.onload = function () {
const res = JSON.parse(this.responseText),
mipmaps = res.mipmaps,
2015-02-02 18:39:44 +01:00
spawn = res.spawn;
2015-02-02 09:08:36 +01:00
let x, z, zoom, light, signs;
2015-02-04 22:01:14 +01:00
const updateParams = function () {
const args = parseHash();
2015-02-04 22:01:14 +01:00
zoom = parseInt(args['zoom']);
x = parseFloat(args['x']);
z = parseFloat(args['z']);
light = parseInt(args['light']);
signs = parseInt(args['signs'] ?? '1');
2015-02-04 22:01:14 +01:00
if (isNaN(zoom))
zoom = 0;
if (isNaN(x))
x = spawn.x;
if (isNaN(z))
z = spawn.z;
};
updateParams();
2015-02-04 17:00:53 +01:00
const map = L.map('map', {
2015-02-04 17:00:53 +01:00
center: [-z, x],
zoom: zoom,
minZoom: -(mipmaps.length-1),
2015-02-02 14:18:04 +01:00
maxZoom: 3,
2015-02-02 09:08:36 +01:00
crs: L.CRS.Simple,
maxBounds: [
[-512*(mipmaps[0].bounds.maxZ+1), 512*mipmaps[0].bounds.minX],
[-512*mipmaps[0].bounds.minZ, 512*(mipmaps[0].bounds.maxX+1)],
2015-02-02 09:08:36 +01:00
],
});
const mapLayer = new MinedMapLayer(mipmaps, 'map');
const lightLayer = new MinedMapLayer(mipmaps, 'light');
const signLayer = L.layerGroup();
loadSigns(signLayer);
2015-02-03 13:17:36 +01:00
mapLayer.addTo(map);
2015-02-04 17:00:53 +01:00
if (light)
map.addLayer(lightLayer);
if (signs)
map.addLayer(signLayer);
2015-02-04 17:00:53 +01:00
const overlayMaps = {
2015-02-03 13:17:36 +01:00
"Illumination": lightLayer,
"Signs": signLayer,
2015-02-03 13:17:36 +01:00
};
L.control.layers({}, overlayMaps).addTo(map);
2015-02-04 07:22:49 +01:00
const coordControl = new CoordControl();
2015-02-04 07:22:49 +01:00
coordControl.addTo(map);
map.on('mousemove', function(e) {
coordControl.update(Math.round(e.latlng.lng), Math.round(-e.latlng.lat));
});
2015-02-04 17:00:53 +01:00
const makeHash = function () {
let ret = '#x='+x+'&z='+z;
2015-02-04 22:01:14 +01:00
if (zoom != 0)
ret += '&zoom='+zoom;
2015-02-04 17:00:53 +01:00
if (map.hasLayer(lightLayer))
2015-02-04 22:01:14 +01:00
ret += '&light=1';
if (!map.hasLayer(signLayer))
ret += '&signs=0';
2015-02-04 17:00:53 +01:00
return ret;
};
const updateHash = function () {
2015-02-04 17:00:53 +01:00
window.location.hash = makeHash();
};
const refreshHash = function (ev) {
if (ev.type === 'layeradd' || ev.type === 'layerremove') {
if (ev.layer !== lightLayer && ev.layer !== signLayer)
return;
}
2015-02-04 22:01:14 +01:00
zoom = map.getZoom();
center = map.getCenter();
x = Math.round(center.lng);
z = Math.round(-center.lat);
updateHash();
}
2015-02-04 17:19:56 +01:00
updateHash();
2015-02-04 22:01:14 +01:00
map.on('moveend', refreshHash);
map.on('zoomend', refreshHash);
map.on('layeradd', refreshHash);
map.on('layerremove', refreshHash);
2015-02-04 17:00:53 +01:00
window.onhashchange = function () {
if (window.location.hash === makeHash())
2015-02-04 17:00:53 +01:00
return;
2015-02-04 22:01:14 +01:00
updateParams();
2015-02-04 17:00:53 +01:00
map.setView([-z, x], zoom);
if (light)
map.addLayer(lightLayer);
else
map.removeLayer(lightLayer);
if (signs)
map.addLayer(signLayer);
else
map.removeLayer(signLayer);
2015-02-04 22:01:14 +01:00
updateHash();
2015-02-04 17:00:53 +01:00
};
2015-02-02 09:08:36 +01:00
};
xhr.open('GET', 'data/info.json', true);
xhr.send();
}