diff --git a/web/js/dynmaputils.js b/web/js/dynmaputils.js index e09b9419..9b628133 100644 --- a/web/js/dynmaputils.js +++ b/web/js/dynmaputils.js @@ -182,3 +182,27 @@ function Location(world, x, y, z) { this.y = y; this.z = z; } + +function namedReplace(str, obj) +{ + var startIndex = 0; + var result = ''; + while(true) { + var variableBegin = str.indexOf('{', startIndex); + var variableEnd = str.indexOf('}', variableBegin+1); + if (variableBegin < 0 || variableEnd < 0) { + result += str.substr(startIndex); + break; + } + if (variableBegin < variableEnd) { + var variableName = str.substring(variableBegin+1, variableEnd-1); + result += str.substring(startIndex, variableBegin-1); + result += obj[variableName]; + } else /* found '{}' */ { + result += str.substring(startIndex, variableBegin-1); + result += ''; + } + startIndex = variableEnd+1; + } + return result; +} \ No newline at end of file diff --git a/web/js/map.js b/web/js/map.js index 39e821a3..e3ad9c90 100644 --- a/web/js/map.js +++ b/web/js/map.js @@ -19,63 +19,6 @@ componentconstructors['testcomponent'] = function(dynmap, configuration) { $(dynmap).bind('playerupdated', function() { console.log('playerupdated'); }); }; -function loadjs(url, completed) { - var script = document.createElement('script'); - script.setAttribute('src', url); - script.setAttribute('type', 'text/javascript'); - var isloaded = false; - script.onload = function() { - if (isloaded) { return; } - isloaded = true; - completed(); - }; - - // Hack for IE, don't know whether this still applies to IE9. - script.onreadystatechange = function() { - script.onload(); - }; - (document.head || document.getElementsByTagName('head')[0]).appendChild(script); -} - -function splitArgs(s) { - var r = s.split(' '); - delete arguments[0]; - var obj = {}; - var index = 0; - $.each(arguments, function(argumentIndex, argument) { - if (!argumentIndex) { return; } - var value = r[argumentIndex-1]; - obj[argument] = value; - }); - return obj; -} - -function swtch(value, options, defaultOption) { - return (options[value] || defaultOption || function(){})(value); -} -(function( $ ){ - $.fn.scrollHeight = function(height) { - return this[0].scrollHeight; - }; -})($); - -function DynMapType() { } -DynMapType.prototype = { - onTileUpdated: function(tile, tileName) { - var src = this.dynmap.getTileUrl(tileName); - tile.attr('src', src); - tile.show(); - }, - updateTileSize: function(zoom) {} -}; - -function Location(world, x, y, z) { - this.world = world; - this.x = x; - this.y = y; - this.z = z; -} - function DynMap(options) { var me = this; me.options = options; diff --git a/web/js/regions.js b/web/js/regions.js index 3fd8a76c..7a890dd9 100644 --- a/web/js/regions.js +++ b/web/js/regions.js @@ -1,36 +1,33 @@ -// Author: nidefawl. contact me at bukkit.org or irc.esper.net #nide - -var regionCfg; -var regionPolygons = {} ; -var regionInfoWindow = new google.maps.InfoWindow(); var regionConstructors = {}; -function makeRegionPolygonCube(map, name, region) -{ - new regionConstructors['polygon'](map, name, region); -} -function regionInfo(event, name, region) -{ - new regionConstructors['info'](event, name, region); -} -componentconstructors['regions'] = function(dynmap, configuration) -{ + +componentconstructors['regions'] = function(dynmap, configuration) { regionCfg = configuration; - - loadjs('js/regions_' + regionCfg.name + '.js', function() - { - var world_info = dynmap.map.mapTypeId.split('.'); - new regionConstructors['update'](world_info[0]); - - $(dynmap).bind('mapchanged', function() { - var world_info = dynmap.map.mapTypeId.split('.'); - new regionConstructors['update'](world_info[0]); - }); + var regionType = regionCfg.name; + loadjs('js/regions_' + regionType + '.js', function() { + var regionsLayer = undefined; + function undraw() { + if (regionsLayer) { + dynmap.map.removeLayer(regionsLayer); + regionsLayer = undefined; + } + } + function redraw() { + undraw(); + var worldName = dynmap.world && dynmap.world.name; + if (worldName) { + regionConstructors[regionType](dynmap, worldName, function(regionLayers) { + var newRegionsLayer = new L.LayerGroup(); + $.each(regionLayers, function(name, layer) { + console.log(name, layer); + newRegionsLayer.addLayer(layer); + }); + regionsLayer = newRegionsLayer; + dynmap.map.addLayer(newRegionsLayer); + }); + } + } + $(dynmap).bind('mapchanged', redraw); + $(dynmap).bind('mapchanging', undraw); + redraw(); }); -} - -function arrayReplace(replace, by, str) -{ - for (var i=0; i'; - }); - - name = ''+name+''; - owners['players'] = ''+owners['players']+''; - owners['groups'] = ''+owners['groups']+''; - members['players'] = ''+members['players']+''; - members['groups'] = ''+members['groups']+''; - var region_parent = (region.parent) ? ''+region.parent+'' : ''; - flags = ''+flags+''; - var region_priority = ''+region.priority+''; - - var replace = ['%regionname%','%playerowners%','%groupowners%','%playermembers%','%groupmembers%','%parent%','%flags%','%priority%']; - var by = [name,owners['players'],owners['groups'],members['players'], members['groups'],region_parent,flags,region_priority]; - - var contentString = arrayReplace(replace, by, regionCfg.infowindow) - - regionInfoWindow.setContent(contentString); - regionInfoWindow.setPosition(event.latLng); - - regionInfoWindow.open(dynmap.map); + return []; + if(region.max.y <= region.min.y) + region.min.y = region.max.y - 1; + return createPolygonSurfaces(latlng, region.max.x, region.min.x, region.max.y, region.min.y, region.max.z, region.min.z); } -regionConstructors['update'] = function(map) -{ - if(regionInfoWindow) - regionInfoWindow.close(); - $.each(regionPolygons, function(index, region) - { - region.setMap(null); - }); - regionPolygons = {}; +function createPopupContent(name, region) { + + return $('
') + .append($('').addClass('regionname').text(name)) + .append(region.owners.players && $('').addClass('playerowners').text(region.owners.players.concat())) + .append(region.owners.groups && $('').addClass('groupowners').text(region.owners.groups.concat())) + .append(region.members.players && $('').addClass('playermembers').text(region.members.players.concat())) + .append(region.members.groups && $('').addClass('groupmembers').text(region.members.groups.concat())) + .append(region.parent && $('').addClass('regionparent').text(region.parent)) + .append(region.flags && function() { + var regionflags = $('').addClass('regionflags'); + $.each(region.flags, function(name, value) { + regionflags.append($('').addClass('regionflag').text(name + ': ' + value)); + }); + return regionflags; + }()) + .append($('').addClass('regionpriority').text(region.priority)) + [0]; +}; + +regionConstructors['WorldGuard'] = function(dynmap, worldName, result) { + var latlng = function(x, y, z) { + var l; + if (typeof x === 'Object' && !y && !z) { + l = x; + } else { + l = new Location(undefined, x,y,z); + } + return dynmap.getProjection().fromLocationToLatLng(l); + }; regionFile = regionCfg.filename.substr(0, regionCfg.filename.lastIndexOf('.')); - regionFile += '_'+map+'.json'; - - $.getJSON('standalone/'+regionFile, function(data) - { - var regionnames = ''; - var count = 0; - $.each(data, function(name, residence) - { - count += 1; - regionnames += ", "+name; - makeRegionPolygonCube(dynmap.map, name, residence); + regionFile += '_'+worldName+'.json'; + $.getJSON('standalone/'+regionFile, function(data) { + var regionLayers = {}; + $.each(data, function(name, region) { + var polygons = createPolygonsFromWorldGuardRegion(latlng, name, region); + var regionLayer = new L.FeatureGroup(polygons); + + regionLayer.bindPopup(createPopupContent(name, region)); + + regionLayers[name] = regionLayer; }); + result(regionLayers); + }); -} \ No newline at end of file +}; \ No newline at end of file