2011-09-07 18:22:44 +02:00
|
|
|
|
|
|
|
var dynmapmarkersets = {};
|
|
|
|
|
|
|
|
componentconstructors['markers'] = function(dynmap, configuration) {
|
|
|
|
var me = this;
|
|
|
|
|
|
|
|
function removeAllMarkers() {
|
|
|
|
$.each(dynmapmarkersets, function(setname, set) {
|
|
|
|
$.each(set.markers, function(mname, marker) {
|
|
|
|
set.layergroup.removeLayer(marker.our_marker);
|
|
|
|
});
|
|
|
|
set.markers = {};
|
2011-11-21 05:37:02 +01:00
|
|
|
$.each(set.areas, function(aname, area) {
|
|
|
|
set.layergroup.removeLayer(area.our_area);
|
|
|
|
});
|
|
|
|
set.areas = {};
|
2011-09-07 18:22:44 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadmarkers(world) {
|
|
|
|
removeAllMarkers();
|
|
|
|
$.getJSON(dynmap.options.tileUrl+'_markers_/marker_'+world+'.json', function(data) {
|
|
|
|
var ts = data.timestamp;
|
|
|
|
$.each(data.sets, function(name, markerset) {
|
|
|
|
var ms = dynmapmarkersets[name];
|
|
|
|
if(!ms) {
|
2011-10-22 23:02:41 +02:00
|
|
|
ms = { id: name, label: markerset.label, hide: markerset.hide, layerprio: markerset.layerprio, markers: {}, areas: {} } ;
|
2011-09-07 18:22:44 +02:00
|
|
|
createMarkerSet(ms, ts);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(ms.label != markerset.label) {
|
|
|
|
ms.label = markerset.label;
|
2011-09-14 07:00:35 +02:00
|
|
|
//dynmap.layercontrol.removeLayer(ms.layergroup);
|
|
|
|
//dynmap.layercontrol.addOverlay(ms.layergroup, ms.label);
|
|
|
|
dynmap.addToLayerSelector(ms.layergroup, ms.label, ms.layerprio || 0);
|
2011-09-07 18:22:44 +02:00
|
|
|
}
|
|
|
|
ms.markers = {};
|
2011-10-22 23:02:41 +02:00
|
|
|
ms.areas = {};
|
2011-09-09 05:02:32 +02:00
|
|
|
ms.hide = markerset.hide;
|
2011-09-07 18:22:44 +02:00
|
|
|
ms.timestamp = ts;
|
|
|
|
}
|
|
|
|
dynmapmarkersets[name] = ms;
|
|
|
|
$.each(markerset.markers, function(mname, marker) {
|
2011-09-30 05:26:31 +02:00
|
|
|
ms.markers[mname] = { label: marker.label, markup: marker.markup, x: marker.x, y: marker.y, z:marker.z,
|
2011-10-13 02:21:28 +02:00
|
|
|
icon: marker.icon, desc: marker.desc };
|
2011-09-07 18:22:44 +02:00
|
|
|
createMarker(ms, ms.markers[mname], ts);
|
|
|
|
});
|
2011-10-22 23:02:41 +02:00
|
|
|
$.each(markerset.areas, function(aname, area) {
|
|
|
|
ms.areas[aname] = { label: area.label, markup: area.markup, desc: area.desc, x: area.x, z: area.z,
|
|
|
|
ytop: area.ytop, ybottom: area.ybottom, color: area.color, weight: area.weight, opacity: area.opacity,
|
|
|
|
fillcolor: area.fillcolor, fillopacity: area.fillopacity };
|
|
|
|
createArea(ms, ms.areas[aname], ts);
|
|
|
|
});
|
2011-09-07 18:22:44 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPosition(marker) {
|
|
|
|
return dynmap.getProjection().fromLocationToLatLng({ x: marker.x, y: marker.y, z: marker.z });
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMarker(set, marker, ts) {
|
|
|
|
var markerPosition = getPosition(marker);
|
|
|
|
marker.our_marker = new L.CustomMarker(markerPosition, { elementCreator: function() {
|
|
|
|
var div = document.createElement('div');
|
|
|
|
|
|
|
|
var markerPosition = getPosition(marker);
|
|
|
|
marker.our_marker.setLatLng(markerPosition);
|
|
|
|
|
|
|
|
$(div)
|
|
|
|
.addClass('Marker')
|
|
|
|
.addClass('mapMarker')
|
2011-09-30 05:26:31 +02:00
|
|
|
.append($('<img/>').addClass('markerIcon16x16').attr({ src: dynmap.options.tileUrl+'_markers_/'+marker.icon+'.png' }));
|
|
|
|
if(marker.markup) {
|
|
|
|
$(div).append($('<span/>')
|
|
|
|
.addClass(configuration.showlabel?'markerName-show':'markerName')
|
2011-10-02 02:19:52 +02:00
|
|
|
.addClass('markerName_' + set.id)
|
2011-09-30 05:26:31 +02:00
|
|
|
.append(marker.label));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$(div).append($('<span/>')
|
2011-09-07 18:22:44 +02:00
|
|
|
.addClass(configuration.showlabel?'markerName-show':'markerName')
|
2011-10-02 02:19:52 +02:00
|
|
|
.addClass('markerName_' + set.id)
|
2011-09-07 18:22:44 +02:00
|
|
|
.text(marker.label));
|
|
|
|
return div;
|
|
|
|
}});
|
|
|
|
marker.timestamp = ts;
|
2011-10-13 02:21:28 +02:00
|
|
|
if(marker.desc) {
|
|
|
|
var popup = document.createElement('div');
|
|
|
|
$(popup).addClass('MarkerPopup').append(marker.desc);
|
|
|
|
marker.our_marker.bindPopup(popup, {});
|
|
|
|
}
|
2011-09-07 18:22:44 +02:00
|
|
|
set.layergroup.addLayer(marker.our_marker);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMarkerSet(set, ts) {
|
|
|
|
set.layergroup = new L.LayerGroup();
|
|
|
|
set.timestamp = ts;
|
2011-09-09 05:02:32 +02:00
|
|
|
if(!set.hide)
|
2011-09-07 18:22:44 +02:00
|
|
|
dynmap.map.addLayer(set.layergroup);
|
2011-09-14 07:00:35 +02:00
|
|
|
// dynmap.layercontrol.addOverlay(set.layergroup, set.label);
|
|
|
|
dynmap.addToLayerSelector(set.layergroup, set.label, set.layerprio || 0);
|
|
|
|
|
2011-09-07 18:22:44 +02:00
|
|
|
}
|
2011-10-22 05:51:31 +02:00
|
|
|
|
2011-10-22 23:02:41 +02:00
|
|
|
function createArea(set, area, ts) {
|
|
|
|
var style = { color: area.color, opacity: area.opacity, weight: area.weight, fillOpacity: area.fillopacity, fillColor: area.fillcolor };
|
|
|
|
|
|
|
|
if(area.x.length == 2) { /* Only 2 points */
|
|
|
|
if(area.ytop == area.ybottom) {
|
|
|
|
area.our_area = create2DBoxLayer(area.x[0], area.x[1], area.ytop, area.ybottom, area.z[0], area.z[1], style);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
area.our_area = create3DBoxLayer(area.x[0], area.x[1], area.ytop, area.ybottom, area.z[0], area.z[1], style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(area.ytop == area.ybottom) {
|
|
|
|
area.our_area = create2DOutlineLayer(area.x, area.ytop, area.ybottom, area.z, style);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
area.our_area = create3DOutlineLayer(area.x, area.ytop, area.ybottom, area.z, style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
area.timestamp = ts;
|
|
|
|
var popup = document.createElement('div');
|
|
|
|
if(area.desc) {
|
|
|
|
$(popup).addClass('AreaPopup').append(area.desc);
|
|
|
|
}
|
|
|
|
else if(area.markup) {
|
|
|
|
$(popup).addClass('AreaPopup').append(area.label);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$(popup).text(area.label);
|
|
|
|
}
|
|
|
|
area.our_area.bindPopup(popup, {});
|
|
|
|
|
|
|
|
set.layergroup.addLayer(area.our_area);
|
|
|
|
}
|
|
|
|
|
2011-10-22 05:51:31 +02:00
|
|
|
// Helper functions
|
|
|
|
latlng = function(x, y, z) {
|
|
|
|
return dynmap.getProjection().fromLocationToLatLng(new Location(undefined, x,y,z));
|
|
|
|
}
|
|
|
|
|
|
|
|
function create3DBoxLayer(maxx, minx, maxy, miny, maxz, minz, style) {
|
|
|
|
return new L.MultiPolygon([
|
|
|
|
[
|
|
|
|
latlng(minx,miny,minz),
|
|
|
|
latlng(maxx,miny,minz),
|
|
|
|
latlng(maxx,miny,maxz),
|
|
|
|
latlng(minx,miny,maxz)
|
|
|
|
],[
|
|
|
|
latlng(minx,maxy,minz),
|
|
|
|
latlng(maxx,maxy,minz),
|
|
|
|
latlng(maxx,maxy,maxz),
|
|
|
|
latlng(minx,maxy,maxz)
|
|
|
|
],[
|
|
|
|
latlng(minx,miny,minz),
|
|
|
|
latlng(minx,maxy,minz),
|
|
|
|
latlng(maxx,maxy,minz),
|
|
|
|
latlng(maxx,miny,minz)
|
|
|
|
],[
|
|
|
|
latlng(maxx,miny,minz),
|
|
|
|
latlng(maxx,maxy,minz),
|
|
|
|
latlng(maxx,maxy,maxz),
|
|
|
|
latlng(maxx,miny,maxz)
|
|
|
|
],[
|
|
|
|
latlng(minx,miny,maxz),
|
|
|
|
latlng(minx,maxy,maxz),
|
|
|
|
latlng(maxx,maxy,maxz),
|
|
|
|
latlng(maxx,miny,maxz)
|
|
|
|
],[
|
|
|
|
latlng(minx,miny,minz),
|
|
|
|
latlng(minx,maxy,minz),
|
|
|
|
latlng(minx,maxy,maxz),
|
|
|
|
latlng(minx,miny,maxz)
|
|
|
|
]], style);
|
|
|
|
}
|
|
|
|
|
|
|
|
function create2DBoxLayer(maxx, minx, maxy, miny, maxz, minz, style) {
|
|
|
|
return new L.Polygon([
|
|
|
|
latlng(minx,64,minz),
|
|
|
|
latlng(maxx,64,minz),
|
|
|
|
latlng(maxx,64,maxz),
|
|
|
|
latlng(minx,64,maxz)
|
|
|
|
], style);
|
|
|
|
}
|
|
|
|
|
|
|
|
function create3DOutlineLayer(xarray, maxy, miny, zarray, style) {
|
|
|
|
var toplist = [];
|
|
|
|
var botlist = [];
|
|
|
|
var i;
|
|
|
|
var polylist = [];
|
|
|
|
for(i = 0; i < xarray.length; i++) {
|
|
|
|
toplist[i] = latlng(xarray[i], maxy, zarray[i]);
|
|
|
|
botlist[i] = latlng(xarray[i], miny, zarray[i]);
|
|
|
|
}
|
|
|
|
for(i = 0; i < xarray.length; i++) {
|
|
|
|
var sidelist = [];
|
|
|
|
sidelist[0] = toplist[i];
|
|
|
|
sidelist[1] = botlist[i];
|
|
|
|
sidelist[2] = botlist[(i+1)%xarray.length];
|
|
|
|
sidelist[3] = toplist[(i+1)%xarray.length];
|
|
|
|
polylist[i] = sidelist;
|
|
|
|
}
|
|
|
|
polylist[xarray.length] = botlist;
|
|
|
|
polylist[xarray.length+1] = toplist;
|
|
|
|
|
|
|
|
return new L.MultiPolygon(polylist, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
function create2DOutlineLayer(xarray, maxy, miny, zarray, style) {
|
|
|
|
var llist = [];
|
|
|
|
var i;
|
|
|
|
for(i = 0; i < xarray.length; i++) {
|
|
|
|
llist[i] = latlng(xarray[i], 64, zarray[i]);
|
|
|
|
}
|
|
|
|
return new L.Polygon(llist, style);
|
|
|
|
}
|
2011-09-07 18:22:44 +02:00
|
|
|
|
|
|
|
$(dynmap).bind('component.markers', function(event, msg) {
|
|
|
|
if(msg.msg == 'markerupdated') {
|
|
|
|
var marker = dynmapmarkersets[msg.set].markers[msg.id];
|
|
|
|
if(marker && marker.our_marker) {
|
|
|
|
dynmapmarkersets[msg.set].layergroup.removeLayer(marker.our_marker);
|
|
|
|
delete marker.our_marker;
|
|
|
|
}
|
2011-10-13 02:21:28 +02:00
|
|
|
marker = { x: msg.x, y: msg.y, z: msg.z, icon: msg.icon, label: msg.label, markup: msg.markup, desc: msg.desc };
|
2011-09-07 18:22:44 +02:00
|
|
|
dynmapmarkersets[msg.set].markers[msg.id] = marker;
|
2011-10-22 23:02:41 +02:00
|
|
|
createMarker(dynmapmarkersets[msg.set], marker, msg.timestamp);
|
2011-09-07 18:22:44 +02:00
|
|
|
}
|
|
|
|
else if(msg.msg == 'markerdeleted') {
|
|
|
|
var marker = dynmapmarkersets[msg.set].markers[msg.id];
|
|
|
|
if(marker && marker.our_marker) {
|
|
|
|
dynmapmarkersets[msg.set].layergroup.removeLayer(marker.our_marker);
|
|
|
|
}
|
|
|
|
delete dynmapmarkersets[msg.set].markers[msg.id];
|
|
|
|
}
|
|
|
|
else if(msg.msg == 'setupdated') {
|
|
|
|
if(!dynmapmarkersets[msg.id]) {
|
2011-10-02 02:19:52 +02:00
|
|
|
dynmapmarkersets[msg.id] = { id: msg.id, label: msg.label, layerprio: msg.layerprio, markers:{} };
|
2011-09-07 18:22:44 +02:00
|
|
|
createMarkerSet(dynmapmarkersets[msg.id]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(dynmapmarkersets[msg.id].label != msg.label) {
|
|
|
|
dynmapmarkersets[msg.id].label = msg.label;
|
2011-09-14 07:00:35 +02:00
|
|
|
//dynmap.layercontrol.removeLayer(dynmapmarkersets[msg.id].layergroup);
|
|
|
|
//dynmap.layercontrol.addOverlay(dynmapmarkersets[msg.id].layergroup, dynmapmarkersets[msg.id].label);
|
|
|
|
dynmap.addToLayerSelector(dynmapmarkersets[msg.id].layergroup, dynmapmarkersets[msg.id].label,
|
|
|
|
dynmapmarkersets[msg.id].layerprio || 0);
|
|
|
|
|
2011-09-07 18:22:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(msg.msg == 'setdeleted') {
|
|
|
|
if(dynmapmarkersets[msg.id]) {
|
2011-09-14 07:00:35 +02:00
|
|
|
//dynmap.layercontrol.removeLayer(dynmapmarkersets[msg.id].layergroup);
|
|
|
|
dynmap.removeFromLayerSelector(dynmapmarkersets[msg.id].layergroup);
|
2011-09-07 18:22:44 +02:00
|
|
|
delete dynmapmarkersets[msg.id].layergroup;
|
|
|
|
delete dynmapmarkersets[msg.id];
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 23:02:41 +02:00
|
|
|
else if(msg.msg == 'areaupdated') {
|
|
|
|
var area = dynmapmarkersets[msg.set].areas[msg.id];
|
|
|
|
if(area && area.our_area) {
|
|
|
|
dynmapmarkersets[msg.set].layergroup.removeLayer(area.our_area);
|
|
|
|
delete area.our_area;
|
|
|
|
}
|
|
|
|
area = { x: msg.x, ytop: msg.ytop, ybottom: msg.ybottom, z: msg.z, label: msg.label, markup: msg.markup, desc: msg.desc,
|
|
|
|
color: msg.color, weight: msg.weight, opacity: msg.opacity, fillcolor: msg.fillcolor, fillopacity: msg.fillopacity };
|
|
|
|
dynmapmarkersets[msg.set].areas[msg.id] = area;
|
|
|
|
createArea(dynmapmarkersets[msg.set], area, msg.timestamp);
|
|
|
|
}
|
|
|
|
else if(msg.msg == 'areadeleted') {
|
|
|
|
var area = dynmapmarkersets[msg.set].areas[msg.id];
|
|
|
|
if(area && area.our_area) {
|
|
|
|
dynmapmarkersets[msg.set].layergroup.removeLayer(area.our_area);
|
|
|
|
}
|
|
|
|
delete dynmapmarkersets[msg.set].areas[msg.id];
|
|
|
|
}
|
2011-09-07 18:22:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Remove marker on start of map change
|
|
|
|
$(dynmap).bind('mapchanging', function(event) {
|
|
|
|
$.each(dynmapmarkersets, function(setname, set) {
|
|
|
|
$.each(set.markers, function(mname, marker) {
|
|
|
|
set.layergroup.removeLayer(marker.our_marker);
|
|
|
|
});
|
2011-10-22 23:02:41 +02:00
|
|
|
$.each(set.areas, function(aname, area) {
|
|
|
|
set.layergroup.removeLayer(area.our_area);
|
|
|
|
});
|
2011-09-07 18:22:44 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
// Remove marker on map change - let update place it again
|
|
|
|
$(dynmap).bind('mapchanged', function(event) {
|
|
|
|
$.each(dynmapmarkersets, function(setname, set) {
|
|
|
|
$.each(set.markers, function(mname, marker) {
|
|
|
|
var marker = set.markers[mname];
|
|
|
|
var markerPosition = getPosition(marker);
|
|
|
|
marker.our_marker.setLatLng(markerPosition);
|
|
|
|
if(dynmap.map.hasLayer(marker.our_marker) == false)
|
|
|
|
set.layergroup.addLayer(marker.our_marker);
|
|
|
|
});
|
2011-10-22 23:02:41 +02:00
|
|
|
$.each(set.areas, function(aname, area) {
|
|
|
|
createArea(set, area, area.timestamp);
|
|
|
|
});
|
2011-09-07 18:22:44 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
// Load markers for new world
|
|
|
|
$(dynmap).bind('worldchanged', function(event) {
|
|
|
|
loadmarkers(this.world.name);
|
|
|
|
});
|
|
|
|
|
|
|
|
loadmarkers(dynmap.world.name);
|
|
|
|
|
2011-09-03 23:49:18 +02:00
|
|
|
};
|