Merge pull request #361 from mikeprimm/master

Fix flat and kzed 'bigworld' support
This commit is contained in:
mikeprimm 2011-08-02 08:48:45 -07:00
commit 883640f88c
3 changed files with 262 additions and 262 deletions

View file

@ -1,260 +1,260 @@
var DynmapProjection = L.Class.extend({ var DynmapProjection = L.Class.extend({
initialize: function(options) { initialize: function(options) {
L.Util.setOptions(this, options); L.Util.setOptions(this, options);
}, },
fromLocationToLatLng: function(location) { fromLocationToLatLng: function(location) {
throw "fromLocationToLatLng not implemented"; throw "fromLocationToLatLng not implemented";
} }
}); });
var DynmapTileLayer = L.TileLayer.extend({ var DynmapTileLayer = L.TileLayer.extend({
_currentzoom: undefined, _currentzoom: undefined,
getProjection: function() { getProjection: function() {
return this.projection; return this.projection;
}, },
onTileUpdated: function(tile, tileName) { onTileUpdated: function(tile, tileName) {
var src = this.dynmap.getTileUrl(tileName); var src = this.dynmap.getTileUrl(tileName);
tile.attr('src', src); tile.attr('src', src);
tile.show(); tile.show();
}, },
getTileName: function(tilePoint, zoom) { getTileName: function(tilePoint, zoom) {
throw "getTileName not implemented"; throw "getTileName not implemented";
}, },
getTileUrl: function(tilePoint, zoom) { getTileUrl: function(tilePoint, zoom) {
var tileName = this.getTileName(tilePoint, zoom); var tileName = this.getTileName(tilePoint, zoom);
var url = this._cachedTileUrls[tileName]; var url = this._cachedTileUrls[tileName];
if (!url) { if (!url) {
this._cachedTileUrls[tileName] = url = this.options.dynmap.getTileUrl(tileName) + '?' + new Date().getUTCMilliseconds(); this._cachedTileUrls[tileName] = url = this.options.dynmap.getTileUrl(tileName) + '?' + new Date().getUTCMilliseconds();
} }
return url; return url;
}, },
updateNamedTile: function(name) { updateNamedTile: function(name) {
var tile = this._namedTiles[name]; var tile = this._namedTiles[name];
delete this._cachedTileUrls[name]; delete this._cachedTileUrls[name];
if (tile) { if (tile) {
this.updateTile(tile); this.updateTile(tile);
} }
}, },
updateTile: function(tile) { updateTile: function(tile) {
this._loadTile(tile, tile.tilePoint, this._map.getZoom()); this._loadTile(tile, tile.tilePoint, this._map.getZoom());
}, },
// We should override this, since Leaflet does modulo on tilePoint by default. (https://github.com/CloudMade/Leaflet/blob/master/src/layer/tile/TileLayer.js#L151) // We should override this, since Leaflet does modulo on tilePoint by default. (https://github.com/CloudMade/Leaflet/blob/master/src/layer/tile/TileLayer.js#L151)
_addTile: function(tilePoint) { _addTile: function(tilePoint) {
var tilePos = this._getTilePos(tilePoint), var tilePos = this._getTilePos(tilePoint),
zoom = this._map.getZoom(), zoom = this._map.getZoom(),
key = tilePoint.x + ':' + tilePoint.y, key = tilePoint.x + ':' + tilePoint.y,
name = this.getTileName(tilePoint, zoom); name = this.getTileName(tilePoint, zoom);
// create tile // create tile
var tile = this._createTile(); var tile = this._createTile();
tile.tileName = name; tile.tileName = name;
tile.tilePoint = tilePoint; tile.tilePoint = tilePoint;
L.DomUtil.setPosition(tile, tilePos); L.DomUtil.setPosition(tile, tilePos);
this._tiles[key] = tile; this._tiles[key] = tile;
this._namedTiles[name] = tile; this._namedTiles[name] = tile;
this._loadTile(tile, tilePoint, zoom); this._loadTile(tile, tilePoint, zoom);
this._container.appendChild(tile); this._container.appendChild(tile);
}, },
_removeOtherTiles: function(bounds) { _removeOtherTiles: function(bounds) {
var kArr, x, y, key; var kArr, x, y, key;
for (key in this._tiles) { for (key in this._tiles) {
if (this._tiles.hasOwnProperty(key)) { if (this._tiles.hasOwnProperty(key)) {
kArr = key.split(':'); kArr = key.split(':');
x = parseInt(kArr[0], 10); x = parseInt(kArr[0], 10);
y = parseInt(kArr[1], 10); y = parseInt(kArr[1], 10);
// remove tile if it's out of bounds // remove tile if it's out of bounds
if (x < bounds.min.x || x > bounds.max.x || y < bounds.min.y || y > bounds.max.y) { if (x < bounds.min.x || x > bounds.max.x || y < bounds.min.y || y > bounds.max.y) {
var tile = this._tiles[key]; var tile = this._tiles[key];
if (tile.parentNode === this._container) { if (tile.parentNode === this._container) {
this._container.removeChild(this._tiles[key]); this._container.removeChild(this._tiles[key]);
} }
delete this._namedTiles[tile.tileName]; delete this._namedTiles[tile.tileName];
delete this._tiles[key]; delete this._tiles[key];
} }
} }
} }
}, },
_updateTileSize: function() { _updateTileSize: function() {
var newzoom = this._map.getZoom(); var newzoom = this._map.getZoom();
if (this._currentzoom !== newzoom) { if (this._currentzoom !== newzoom) {
var newTileSize = this.calculateTileSize(newzoom); var newTileSize = this.calculateTileSize(newzoom);
this._currentzoom = newzoom; this._currentzoom = newzoom;
if (newTileSize !== this.options.tileSize) { if (newTileSize !== this.options.tileSize) {
this.setTileSize(newTileSize); this.setTileSize(newTileSize);
} }
} }
}, },
_reset: function() { _reset: function() {
this._updateTileSize(); this._updateTileSize();
this._tiles = {}; this._tiles = {};
this._namedTiles = {}; this._namedTiles = {};
this._cachedTileUrls = {}; this._cachedTileUrls = {};
this._initContainer(); this._initContainer();
this._container.innerHTML = ''; this._container.innerHTML = '';
}, },
_update: function() { _update: function() {
this._updateTileSize(); this._updateTileSize();
var bounds = this._map.getPixelBounds(), var bounds = this._map.getPixelBounds(),
tileSize = this.options.tileSize; tileSize = this.options.tileSize;
var nwTilePoint = new L.Point( var nwTilePoint = new L.Point(
Math.floor(bounds.min.x / tileSize), Math.floor(bounds.min.x / tileSize),
Math.floor(bounds.min.y / tileSize)), Math.floor(bounds.min.y / tileSize)),
seTilePoint = new L.Point( seTilePoint = new L.Point(
Math.floor(bounds.max.x / tileSize), Math.floor(bounds.max.x / tileSize),
Math.floor(bounds.max.y / tileSize)), Math.floor(bounds.max.y / tileSize)),
tileBounds = new L.Bounds(nwTilePoint, seTilePoint); tileBounds = new L.Bounds(nwTilePoint, seTilePoint);
this._addTilesFromCenterOut(tileBounds); this._addTilesFromCenterOut(tileBounds);
if (this.options.unloadInvisibleTiles) { if (this.options.unloadInvisibleTiles) {
this._removeOtherTiles(tileBounds); this._removeOtherTiles(tileBounds);
} }
}, },
/*calculateTileSize: function(zoom) { /*calculateTileSize: function(zoom) {
return this.options.tileSize; return this.options.tileSize;
},*/ },*/
calculateTileSize: function(zoom) { calculateTileSize: function(zoom) {
// zoomoutlevel: 0 when izoom > mapzoomin, else mapzoomin - izoom (which ranges from 0 till mapzoomin) // zoomoutlevel: 0 when izoom > mapzoomin, else mapzoomin - izoom (which ranges from 0 till mapzoomin)
var izoom = this.options.maxZoom - zoom; var izoom = this.options.maxZoom - zoom;
var zoominlevel = Math.max(0, this.options.mapzoomin - izoom); var zoominlevel = Math.max(0, this.options.mapzoomin - izoom);
return 128 << zoominlevel; return 128 << zoominlevel;
}, },
setTileSize: function(tileSize) { setTileSize: function(tileSize) {
this.options.tileSize = tileSize; this.options.tileSize = tileSize;
this._tiles = {}; this._tiles = {};
this._createTileProto(); this._createTileProto();
}, },
updateTileSize: function(zoom) {}, updateTileSize: function(zoom) {},
// Some helper functions. // Some helper functions.
zoomprefix: function(amount) { zoomprefix: function(amount) {
return 'zzzzzzzzzzzzzzzzzzzzzz'.substr(0, amount); return 'zzzzzzzzzzzzzzzzzzzzzz'.substr(0, amount);
}, },
getTileInfo: function(tilePoint, zoom) { getTileInfo: function(tilePoint, zoom) {
// zoom: max zoomed in = this.options.maxZoom, max zoomed out = 0 // zoom: max zoomed in = this.options.maxZoom, max zoomed out = 0
// izoom: max zoomed in = 0, max zoomed out = this.options.maxZoom // izoom: max zoomed in = 0, max zoomed out = this.options.maxZoom
// zoomoutlevel: izoom < mapzoomin -> 0, else -> izoom - mapzoomin (which ranges from 0 till mapzoomout) // zoomoutlevel: izoom < mapzoomin -> 0, else -> izoom - mapzoomin (which ranges from 0 till mapzoomout)
var izoom = this.options.maxZoom - zoom; var izoom = this.options.maxZoom - zoom;
var zoomoutlevel = Math.max(0, izoom - this.options.mapzoomin); var zoomoutlevel = Math.max(0, izoom - this.options.mapzoomin);
var scale = 1 << zoomoutlevel; var scale = 1 << zoomoutlevel;
var zoomprefix = this.zoomprefix(zoomoutlevel); var x = scale*tilePoint.x;
var x = scale*tilePoint.x; var y = scale*tilePoint.y;
var y = scale*tilePoint.y; return {
return { prefix: this.options.prefix,
prefix: this.options.prefix, nightday: (this.options.nightandday && this.options.dynmap.serverday) ? '_day' : '',
nightday: (this.options.nightandday && this.options.dynmap.serverday) ? '_day' : '', scaledx: x >> 5,
scaledx: x >> 5, scaledy: y >> 5,
scaledy: y >> 5, zoom: this.zoomprefix(zoomoutlevel),
zoom: this.zoomprefix(zoomoutlevel), zoomprefix: (zoomoutlevel==0)?"":(this.zoomprefix(zoomoutlevel)+"_"),
x: x, x: x,
y: y y: y
}; };
} }
}); });
function loadjs(url, completed) { function loadjs(url, completed) {
var script = document.createElement('script'); var script = document.createElement('script');
script.setAttribute('src', url); script.setAttribute('src', url);
script.setAttribute('type', 'text/javascript'); script.setAttribute('type', 'text/javascript');
var isloaded = false; var isloaded = false;
script.onload = function() { script.onload = function() {
if (isloaded) { return; } if (isloaded) { return; }
isloaded = true; isloaded = true;
completed(); completed();
}; };
// Hack for IE, don't know whether this still applies to IE9. // Hack for IE, don't know whether this still applies to IE9.
script.onreadystatechange = function() { script.onreadystatechange = function() {
if (script.readyState == 'loaded' || script.readyState == 'complete') if (script.readyState == 'loaded' || script.readyState == 'complete')
script.onload(); script.onload();
}; };
(document.head || document.getElementsByTagName('head')[0]).appendChild(script); (document.head || document.getElementsByTagName('head')[0]).appendChild(script);
} }
function loadcss(url, completed) { function loadcss(url, completed) {
var script = document.createElement('link'); var script = document.createElement('link');
script.setAttribute('href', url); script.setAttribute('href', url);
script.setAttribute('rel', 'stylesheet'); script.setAttribute('rel', 'stylesheet');
var isloaded = false; var isloaded = false;
if (completed) { if (completed) {
script.onload = function() { script.onload = function() {
if (isloaded) { return; } if (isloaded) { return; }
isloaded = true; isloaded = true;
completed(); completed();
}; };
// Hack for IE, don't know whether this still applies to IE9. // Hack for IE, don't know whether this still applies to IE9.
script.onreadystatechange = function() { script.onreadystatechange = function() {
script.onload(); script.onload();
}; };
} }
(document.head || document.getElementsByTagName('head')[0]).appendChild(script); (document.head || document.getElementsByTagName('head')[0]).appendChild(script);
} }
function splitArgs(s) { function splitArgs(s) {
var r = s.split(' '); var r = s.split(' ');
delete arguments[0]; delete arguments[0];
var obj = {}; var obj = {};
var index = 0; var index = 0;
$.each(arguments, function(argumentIndex, argument) { $.each(arguments, function(argumentIndex, argument) {
if (!argumentIndex) { return; } if (!argumentIndex) { return; }
var value = r[argumentIndex-1]; var value = r[argumentIndex-1];
obj[argument] = value; obj[argument] = value;
}); });
return obj; return obj;
} }
function swtch(value, options, defaultOption) { function swtch(value, options, defaultOption) {
return (options[value] || defaultOption || function(){})(value); return (options[value] || defaultOption || function(){})(value);
} }
(function( $ ){ (function( $ ){
$.fn.scrollHeight = function(height) { $.fn.scrollHeight = function(height) {
return this[0].scrollHeight; return this[0].scrollHeight;
}; };
})($); })($);
function Location(world, x, y, z) { function Location(world, x, y, z) {
this.world = world; this.world = world;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
} }
function namedReplace(str, obj) function namedReplace(str, obj)
{ {
var startIndex = 0; var startIndex = 0;
var result = ''; var result = '';
while(true) { while(true) {
var variableBegin = str.indexOf('{', startIndex); var variableBegin = str.indexOf('{', startIndex);
var variableEnd = str.indexOf('}', variableBegin+1); var variableEnd = str.indexOf('}', variableBegin+1);
if (variableBegin < 0 || variableEnd < 0) { if (variableBegin < 0 || variableEnd < 0) {
result += str.substr(startIndex); result += str.substr(startIndex);
break; break;
} }
if (variableBegin < variableEnd) { if (variableBegin < variableEnd) {
var variableName = str.substring(variableBegin+1, variableEnd); var variableName = str.substring(variableBegin+1, variableEnd);
result += str.substring(startIndex, variableBegin); result += str.substring(startIndex, variableBegin);
result += obj[variableName]; result += obj[variableName];
} else /* found '{}' */ { } else /* found '{}' */ {
result += str.substring(startIndex, variableBegin-1); result += str.substring(startIndex, variableBegin-1);
result += ''; result += '';
} }
startIndex = variableEnd+1; startIndex = variableEnd+1;
} }
return result; return result;
} }

View file

@ -21,7 +21,7 @@ var FlatMapType = DynmapTileLayer.extend({
}, },
getTileName: function(tilePoint, zoom) { getTileName: function(tilePoint, zoom) {
return namedReplace(this.options.bigmap return namedReplace(this.options.bigmap
? '{prefix}{nightday}_128/{scaledx}_{scaledy}/{zoom}_{x}_{y}.png' ? '{prefix}{nightday}_128/{scaledx}_{scaledy}/{zoomprefix}{x}_{y}.png'
: '{zoom}{prefix}{nightday}_128_{x}_{y}.png' : '{zoom}{prefix}{nightday}_128_{x}_{y}.png'
, this.getTileInfo(tilePoint, zoom)); , this.getTileInfo(tilePoint, zoom));
} }

View file

@ -28,7 +28,7 @@ var KzedMapType = DynmapTileLayer.extend({
getTileName: function(tilePoint, zoom) { getTileName: function(tilePoint, zoom) {
var info = this.getTileInfo(tilePoint, zoom); var info = this.getTileInfo(tilePoint, zoom);
return namedReplace(this.options.bigmap return namedReplace(this.options.bigmap
? '{prefix}{nightday}/{scaledx}_{scaledy}/{zoom}_{x}_{y}.png' ? '{prefix}{nightday}/{scaledx}_{scaledy}/{zoomprefix}{x}_{y}.png'
: '{zoom}{prefix}{nightday}_{x}_{y}.png' : '{zoom}{prefix}{nightday}_{x}_{y}.png'
, this.getTileInfo(tilePoint, zoom)); , this.getTileInfo(tilePoint, zoom));
}, },
@ -37,7 +37,6 @@ var KzedMapType = DynmapTileLayer.extend({
var izoom = this.options.maxZoom - zoom; var izoom = this.options.maxZoom - zoom;
var zoomoutlevel = Math.max(0, izoom - this.options.mapzoomin); var zoomoutlevel = Math.max(0, izoom - this.options.mapzoomin);
var scale = 1 << zoomoutlevel; var scale = 1 << zoomoutlevel;
var zoomprefix = this.zoomprefix(zoomoutlevel);
var x = -scale*tilePoint.x*128; var x = -scale*tilePoint.x*128;
var y = scale*tilePoint.y*128; var y = scale*tilePoint.y*128;
return { return {
@ -46,6 +45,7 @@ var KzedMapType = DynmapTileLayer.extend({
scaledx: x >> 12, scaledx: x >> 12,
scaledy: y >> 12, scaledy: y >> 12,
zoom: this.zoomprefix(zoomoutlevel), zoom: this.zoomprefix(zoomoutlevel),
zoomprefix: (zoomoutlevel==0)?"":(this.zoomprefix(zoomoutlevel)+"_"),
x: x, x: x,
y: y y: y
}; };