Add support for HTML-markup-encoded marker labels, with associated API

This commit is contained in:
Mike Primm 2011-09-30 11:26:31 +08:00 committed by mikeprimm
parent 5f6f453dc8
commit 79856bd93e
6 changed files with 60 additions and 8 deletions

View File

@ -68,7 +68,17 @@ public interface Marker {
*/
public String getLabel();
/**
* Update the marker's label
* Update the marker's label (plain text)
*/
public void setLabel(String lbl);
/**
* Update the marker's label and markup flag
* @param label - label string
* @param markup - if true, label is processed as HTML (innerHTML for <span> used for label); false implies plaintext
*/
public void setLabel(String lbl, boolean markup);
/**
* Test if marker label is processed as HTML
*/
public boolean isLabelMarkup();
}

View File

@ -19,7 +19,7 @@ public interface MarkerSet {
* Create a new marker in the marker set
*
* @param id - ID of the marker - must be unique within the set: if null, unique ID is generated
* @param label - Label for the marker
* @param label - Label for the marker (plain text)
* @param world - world ID
* @param x - x coord
* @param y - y coord
@ -29,6 +29,21 @@ public interface MarkerSet {
* @return created marker, or null if cannot be created.
*/
public Marker createMarker(String id, String label, String world, double x, double y, double z, MarkerIcon icon, boolean is_persistent);
/**
* Create a new marker in the marker set
*
* @param id - ID of the marker - must be unique within the set: if null, unique ID is generated
* @param label - Label for the marker
* @param markup - if true, label is processed as HTML. if false, label is processed as plain text.
* @param world - world ID
* @param x - x coord
* @param y - y coord
* @param z - z coord
* @param icon - Icon for the marker
* @param is_persistent - if true, marker is persistent (saved and reloaded on restart). If set is not persistent, this must be false.
* @return created marker, or null if cannot be created.
*/
public Marker createMarker(String id, String label, boolean markup, String world, double x, double y, double z, MarkerIcon icon, boolean is_persistent);
/**
* Get marker by ID
* @param id - ID of the marker

View File

@ -1053,6 +1053,7 @@ public class MarkerAPIImpl implements MarkerAPI, Event.Listener<DynmapWorld> {
mdata.put("z", m.getZ());
mdata.put("icon", m.getMarkerIcon().getMarkerIconID());
mdata.put("label", m.getLabel());
mdata.put("markup", m.isLabelMarkup());
/* Add to markers */
markers.put(m.getMarkerID(), mdata);
}

View File

@ -14,6 +14,7 @@ import org.dynmap.markers.impl.MarkerAPIImpl.MarkerUpdate;
class MarkerImpl implements Marker {
private String markerid;
private String label;
private boolean markup;
private MarkerSetImpl markerset;
private double x, y, z;
private String world;
@ -24,6 +25,7 @@ class MarkerImpl implements Marker {
* Create marker
* @param id - marker ID
* @param lbl - label
* @param markup - if true, label is HTML markup
* @param world - world id
* @param x - x coord
* @param y - y coord
@ -31,12 +33,13 @@ class MarkerImpl implements Marker {
* @param icon - marker icon
* @param persistent - true if persistent
*/
MarkerImpl(String id, String lbl, String world, double x, double y, double z, MarkerIconImpl icon, boolean persistent, MarkerSetImpl set) {
MarkerImpl(String id, String lbl, boolean markup, String world, double x, double y, double z, MarkerIconImpl icon, boolean persistent, MarkerSetImpl set) {
markerid = id;
if(lbl != null)
label = lbl;
else
label = id;
this.markup = markup;
this.x = x; this.y = y; this.z = z;
this.world = world;
this.icon = icon;
@ -52,6 +55,7 @@ class MarkerImpl implements Marker {
markerid = id;
markerset = set;
label = id;
markup = false;
x = z = 0; y = 64; world = "world";
icon = MarkerAPIImpl.getMarkerIconImpl(MarkerIcon.DEFAULT);
}
@ -61,6 +65,7 @@ class MarkerImpl implements Marker {
*/
boolean loadPersistentData(ConfigurationNode node) {
label = node.getString("label", markerid);
markup = node.getBoolean("markup", false);
x = node.getDouble("x", 0);
y = node.getDouble("y", 64);
z = node.getDouble("z", 0);
@ -127,7 +132,13 @@ class MarkerImpl implements Marker {
@Override
public void setLabel(String lbl) {
setLabel(lbl, false);
}
@Override
public void setLabel(String lbl, boolean markup) {
label = lbl;
this.markup = markup;
MarkerAPIImpl.markerUpdated(this, MarkerUpdate.UPDATED);
if(ispersistent)
MarkerAPIImpl.saveMarkers();
@ -142,6 +153,7 @@ class MarkerImpl implements Marker {
return null;
HashMap<String, Object> node = new HashMap<String, Object>();
node.put("label", label);
node.put("markup", markup);
node.put("x", Double.valueOf(x));
node.put("y", Double.valueOf(y));
node.put("z", Double.valueOf(z));
@ -176,4 +188,8 @@ class MarkerImpl implements Marker {
if(ispersistent)
MarkerAPIImpl.saveMarkers();
}
@Override
public boolean isLabelMarkup() {
return markup;
}
}

View File

@ -59,6 +59,10 @@ class MarkerSetImpl implements MarkerSet {
@Override
public Marker createMarker(String id, String label, String world, double x, double y, double z, MarkerIcon icon, boolean is_persistent) {
return createMarker(id, label, false, world, x, y, z, icon, is_persistent);
}
@Override
public Marker createMarker(String id, String label, boolean markup, String world, double x, double y, double z, MarkerIcon icon, boolean is_persistent) {
if(id == null) { /* If not defined, generate unique one */
int i = 0;
do {
@ -72,7 +76,7 @@ class MarkerSetImpl implements MarkerSet {
if((allowedicons != null) && (allowedicons.containsKey(icon.getMarkerIconID()) == false)) return null;
/* Create marker */
is_persistent = is_persistent && this.ispersistent;
MarkerImpl marker = new MarkerImpl(id, label, world, x, y, z, (MarkerIconImpl)icon, is_persistent, this);
MarkerImpl marker = new MarkerImpl(id, label, markup, world, x, y, z, (MarkerIconImpl)icon, is_persistent, this);
markers.put(id, marker); /* Add to set */
if(is_persistent)
MarkerAPIImpl.saveMarkers();

View File

@ -36,7 +36,7 @@ componentconstructors['markers'] = function(dynmap, configuration) {
}
dynmapmarkersets[name] = ms;
$.each(markerset.markers, function(mname, marker) {
ms.markers[mname] = { label: marker.label, x: marker.x, y: marker.y, z:marker.z,
ms.markers[mname] = { label: marker.label, markup: marker.markup, x: marker.x, y: marker.y, z:marker.z,
icon: marker.icon };
createMarker(ms, ms.markers[mname], ts);
});
@ -59,8 +59,14 @@ componentconstructors['markers'] = function(dynmap, configuration) {
$(div)
.addClass('Marker')
.addClass('mapMarker')
.append($('<img/>').addClass('markerIcon16x16').attr({ src: dynmap.options.tileUrl+'_markers_/'+marker.icon+'.png' }))
.append($('<span/>')
.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')
.append(marker.label));
}
else
$(div).append($('<span/>')
.addClass(configuration.showlabel?'markerName-show':'markerName')
.text(marker.label));
return div;
@ -86,7 +92,7 @@ componentconstructors['markers'] = function(dynmap, configuration) {
dynmapmarkersets[msg.set].layergroup.removeLayer(marker.our_marker);
delete marker.our_marker;
}
marker = { x: msg.x, y: msg.y, z: msg.z, icon: msg.icon, label: msg.label };
marker = { x: msg.x, y: msg.y, z: msg.z, icon: msg.icon, label: msg.label, markup: msg.markup };
dynmapmarkersets[msg.set].markers[msg.id] = marker;
createMarker(dynmapmarkersets[msg.set], marker);
}