1
0
forked from MTSR/mapserver
mapserver/server/static/js/overlays/MinecartOverlay.js

125 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-04-24 17:21:04 +03:00
/* exported MinecartOverlay */
/* globals AbstractIconOverlay: true */
/* jshint unused: false */
var MinecartOverlay = L.LayerGroup.extend({
initialize: function(wsChannel, layerMgr) {
L.LayerGroup.prototype.initialize.call(this);
this.layerMgr = layerMgr;
this.wsChannel = wsChannel;
this.currentObjects = {}; // name => marker
2019-05-04 22:16:57 +03:00
this.minecarts = [];
2019-04-24 17:21:04 +03:00
this.reDraw = this.reDraw.bind(this);
this.onMinetestUpdate = this.onMinetestUpdate.bind(this);
//update players all the time
this.wsChannel.addListener("minetest-info", function(info){
this.minecarts = info.minecarts || [];
}.bind(this));
},
createMarker: function(cart){
var Icon = L.icon({
iconUrl: "pics/minecart_logo.png",
iconSize: [32, 32],
iconAnchor: [16, 16],
popupAnchor: [0, -32]
});
var marker = L.marker([cart.pos.z, cart.pos.x], {icon: Icon});
var html = "<b>Minecart</b><hr>";
2019-05-04 22:16:57 +03:00
html += "<b>Id: </b> " + cart.id + "<br>";
2019-04-24 17:21:04 +03:00
marker.bindPopup(html);
return marker;
},
isCartInCurrentLayer: function(cart){
var mapLayer = this.layerMgr.getCurrentLayer();
return (cart.pos.y >= (mapLayer.from*16) && cart.pos.y <= (mapLayer.to*16));
},
onMinetestUpdate: function(info){
var self = this;
this.minecarts.forEach(function(cart){
var isInLayer = self.isCartInCurrentLayer(cart);
if (!isInLayer){
2019-05-04 22:16:57 +03:00
if (self.currentObjects[cart.id]){
//cart is displayed and not on the layer anymore
2019-04-24 17:21:04 +03:00
//Remove the marker and reference
2019-05-04 22:16:57 +03:00
self.currentObjects[cart.id].remove();
delete self.currentObjects[cart.id];
2019-04-24 17:21:04 +03:00
}
return;
}
2019-05-04 22:16:57 +03:00
if (self.currentObjects[cart.id]){
2019-04-24 17:21:04 +03:00
//marker exists
2019-05-04 22:16:57 +03:00
self.currentObjects[cart.id].setLatLng([cart.pos.z, cart.pos.x]);
2019-04-24 17:21:04 +03:00
//setPopupContent
} else {
//marker does not exist
2019-05-04 22:16:57 +03:00
var marker = self.createMarker(cart);
2019-04-24 17:21:04 +03:00
marker.addTo(self);
2019-05-04 22:16:57 +03:00
self.currentObjects[cart.id] = marker;
2019-04-24 17:21:04 +03:00
}
});
Object.keys(self.currentObjects).forEach(function(existingId){
2019-05-04 22:16:57 +03:00
var cartIsActive = self.minecarts.find(function(t){
2019-04-24 17:21:04 +03:00
return t.id == existingId;
});
2019-05-04 22:16:57 +03:00
if (!cartIsActive){
2019-04-24 17:21:04 +03:00
self.currentObjects[existingId].remove();
delete self.currentObjects[existingId];
}
});
},
reDraw: function(){
var self = this;
this.currentObjects = {};
this.clearLayers();
var mapLayer = this.layerMgr.getCurrentLayer();
2019-05-04 22:16:57 +03:00
this.minecarts.forEach(function(cart){
if (!self.isCartInCurrentLayer(cart)){
2019-04-24 17:21:04 +03:00
//not in current layer
return;
}
2019-05-04 22:16:57 +03:00
var marker = self.createMarker(cart);
2019-04-24 17:21:04 +03:00
marker.addTo(self);
2019-05-04 22:16:57 +03:00
self.currentObjects[cart.id] = marker;
2019-04-24 17:21:04 +03:00
});
},
onAdd: function(map) {
this.layerMgr.addListener(this.reDraw);
this.wsChannel.addListener("minetest-info", this.onMinetestUpdate);
this.reDraw();
},
onRemove: function(map) {
this.clearLayers();
this.layerMgr.removeListener(this.reDraw);
this.wsChannel.removeListener("minetest-info", this.onMinetestUpdate);
}
});