Add icons on to map display, and marker update logic

This commit is contained in:
Mike Primm 2011-09-04 14:11:48 -05:00
parent c034a10951
commit 9f8ab5da03
3 changed files with 116 additions and 1 deletions

View File

@ -40,6 +40,7 @@ import org.dynmap.web.Json;
*/
public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
private File markerpersist;
private File markerpersist_old;
private File markerdir; /* Local store for markers (internal) */
private File markertiledir; /* Marker directory for web server (under tiles) */
private HashMap<String, MarkerIconImpl> markericons = new HashMap<String, MarkerIconImpl>();
@ -113,6 +114,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
api.server = plugin.getServer();
/* Initialize persistence file name */
api.markerpersist = new File(plugin.getDataFolder(), "markers.yml");
api.markerpersist_old = new File(plugin.getDataFolder(), "markers.yml.old");
/* Load persistence */
api.loadMarkers();
/* Fill in default icons and sets, if needed */
@ -308,6 +310,9 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
}
}
conf.setProperty("sets", sets);
/* And shift old file file out */
if(api.markerpersist_old.exists()) api.markerpersist_old.delete();
if(api.markerpersist.exists()) api.markerpersist.renameTo(api.markerpersist_old);
/* And write it out */
if(!conf.save())
Log.severe("Error writing markers - " + api.markerpersist.getPath());
@ -467,6 +472,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
Map<String, Object> markerdata = new HashMap<String, Object>();
File f = new File(markertiledir, "marker_" + wname + ".json");
File fnew = new File(markertiledir, "marker_" + wname + ".json.new");
Map<String, Object> worlddata = new HashMap<String, Object>();
worlddata.put("timestamp", Long.valueOf(System.currentTimeMillis())); /* Add timestamp */
@ -495,7 +501,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
fos = new FileOutputStream(fnew);
fos.write(Json.stringifyJson(worlddata).getBytes());
} catch (FileNotFoundException ex) {
Log.severe("Exception while writing JSON-file.", ex);
@ -503,6 +509,8 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
Log.severe("Exception while writing JSON-file.", ioe);
} finally {
if(fos != null) try { fos.close(); } catch (IOException x) {}
if(f.exists()) f.delete();
fnew.renameTo(f);
}
}

View File

@ -747,3 +747,35 @@
.balloonmessage {
word-wrap: break-word;
}
/* Marker styles */
.dynmap .mapMarker .markerName {
display: none;
z-index: 101;
}
.dynmap .mapMarker:hover .markerName,
.dynmap .mapMarker .markerName-show {
display: block;
position: absolute;
top: -8px;
left: 10px;
z-index: 18;
white-space: nowrap;
color: #fff;
background: rgba(0,0,0,0.6);
padding: 2px;
-moz-border-radius: 3px;
border-radius: 3px;
}
.dynmap .mapMarker .markerIcon16x16 {
position: absolute;
top: -8px;
left: -8px;
z-index: 18;
}

View File

@ -4,25 +4,100 @@ var dynmapmarkersets = {};
componentconstructors['markers'] = function(dynmap, configuration) {
var me = this;
function removeall() {
$.each(dynmapmarkersets, function(setname, set) {
$.each(set.markers, function(mname, marker) {
set.layergroup.removeLayer(marker.our_marker);
});
dynmap.layercontrol.removeLayer(set.layergroup);
});
}
function loadmarkers(world) {
removeall();
dynmapmarkersets = {};
$.getJSON(dynmap.options.tileUrl+'_markers_/marker_'+world+'.json', function(data) {
var ts = data.timestamp;
$.each(data.sets, function(name, markerset) {
dynmapmarkersets[name] = markerset;
createMarkerSet(markerset);
$.each(markerset.markers, function(name, marker) {
createMarker(markerset, marker);
});
});
});
}
function getPosition(marker) {
return dynmap.getProjection().fromLocationToLatLng({ x: marker.x, y: marker.y, z: marker.z });
}
function createMarker(set, marker) {
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')
.append($('<img/>').addClass('markerIcon16x16').attr({ src: dynmap.options.tileUrl+'_markers_/'+marker.icon+'.png' }))
.append($('<span/>')
.addClass('markerName')
.text(marker.label));
return div;
}});
set.layergroup.addLayer(marker.our_marker);
}
function createMarkerSet(set) {
set.layergroup = new L.LayerGroup();
dynmap.map.addLayer(set.layergroup);
dynmap.layercontrol.addOverlay(set.layergroup, set.label);
}
$(dynmap).bind('component.markers', function(event, msg) {
console.log('got marker event - ' + msg.ctype + ', ' + msg.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);
}
marker = { x: msg.x, y: msg.y, z: msg.z, icon: msg.icon, label: msg.label };
dynmapmarkersets[msg.set].markers[msg.id] = marker;
createMarker(dynmapmarkersets[msg.set], marker);
}
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];
}
});
// 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);
});
});
});
// 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);
});
});
});
// Load markers for new world
$(dynmap).bind('worldchanged', function(event) {