1
0
forked from MTSR/mapserver
mapserver/static/js/overlays/TrainOverlay.js

195 lines
5.2 KiB
JavaScript
Raw Normal View History

2019-02-20 16:17:37 +03:00
2019-02-24 22:06:20 +03:00
function getTrainImageUrlForType(type){
switch(type){
case "advtrains:subway_wagon":
return "pics/advtrains/advtrains_subway_wagon_inv.png";
case "advtrains:engine_japan":
return "pics/advtrains/advtrains_engine_japan_inv.png";
2019-04-04 17:26:52 +03:00
case "advtrains:wagon_japan":
return "pics/advtrains/advtrains_wagon_japan_inv.png";
2019-02-24 22:06:20 +03:00
case "advtrains:engine_steam":
return "pics/advtrains/advtrains_engine_steam_inv.png";
case "advtrains:engine_industrial":
return "pics/advtrains/advtrains_engine_industrial_inv.png";
case "advtrains:wagon_wood":
return "pics/advtrains/advtrains_wagon_wood_inv.png";
case "advtrains:wagon_box":
return "pics/advtrains/advtrains_wagon_box_inv.png";
2019-06-08 15:43:01 +03:00
case "advtrains:subway_wagon_blue":
return "pics/advtrains/advtrains_subway_wagon_inv_blue.png";
case "advtrains:subway_wagon_red":
return "pics/advtrains/advtrains_subway_wagon_inv_red.png";
case "advtrains:subway_wagon_green":
return "pics/advtrains/advtrains_subway_wagon_inv_green.png";
2019-02-24 22:06:20 +03:00
default:
//TODO: fallback image
return "pics/advtrains/advtrains_subway_wagon_inv.png";
}
}
2019-02-20 16:17:37 +03:00
2019-06-11 16:01:16 +03:00
export default L.LayerGroup.extend({
2019-02-20 16:17:37 +03:00
initialize: function(wsChannel, layerMgr) {
L.LayerGroup.prototype.initialize.call(this);
this.layerMgr = layerMgr;
this.wsChannel = wsChannel;
this.currentObjects = {}; // name => marker
this.trains = [];
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.trains = info.trains || [];
}.bind(this));
},
2019-06-17 08:41:48 +03:00
createPopup: function(train){
var html = "<b>Train</b><hr>";
html += "<b>Name:</b> " + train.text_outside + "<br>";
html += "<b>Line:</b> " + train.line + "<br>";
html += "<b>Velocity:</b> "+ Math.floor(train.velocity*10)/10 + "<br>";
if (train.wagons){
html += "<b>Composition: </b>";
train.wagons.forEach(function(w){
var iconUrl = getTrainImageUrlForType(w.type);
html += "<img src='"+iconUrl+"'>";
});
}
return html;
},
2019-07-16 09:35:36 +03:00
getMaxDisplayedZoom: function(){
return 8;
},
2019-02-20 16:17:37 +03:00
createMarker: function(train){
2019-02-24 22:06:20 +03:00
//search for wagin in front (whatever "front" is...)
var type;
var lowest_pos = 100;
if (train.wagons){
train.wagons.forEach(function(w){
if (w.pos_in_train < lowest_pos){
lowest_pos = w.pos_in_train;
type = w.type;
}
});
}
2019-02-24 22:06:20 +03:00
var Icon = L.icon({
iconUrl: getTrainImageUrlForType(type),
iconSize: [16, 16],
iconAnchor: [8, 8],
popupAnchor: [0, -16]
});
var marker = L.marker([train.pos.z, train.pos.x], {icon: Icon});
2019-06-17 08:41:48 +03:00
marker.bindPopup(this.createPopup(train));
2019-02-20 16:17:37 +03:00
return marker;
},
2019-03-31 19:14:21 +03:00
isTrainInCurrentLayer: function(train){
2019-04-04 11:49:55 +03:00
var mapLayer = this.layerMgr.getCurrentLayer();
2019-03-31 19:14:21 +03:00
2019-04-04 11:49:55 +03:00
return (train.pos.y >= (mapLayer.from*16) && train.pos.y <= (mapLayer.to*16));
2019-03-31 19:14:21 +03:00
},
2019-06-11 16:01:16 +03:00
onMinetestUpdate: function(/*info*/){
2019-07-16 09:35:36 +03:00
if (this.map.getZoom() < this.getMaxDisplayedZoom()) {
this.clearLayers();
this.currentObjects = {};
return;
}
2019-06-17 08:41:48 +03:00
this.trains.forEach(train => {
var isInLayer = this.isTrainInCurrentLayer(train);
2019-03-31 19:14:21 +03:00
if (!isInLayer){
2019-06-17 08:41:48 +03:00
if (this.currentObjects[train.id]){
2019-03-31 19:14:21 +03:00
//train is displayed and not on the layer anymore
//Remove the marker and reference
2019-06-17 08:41:48 +03:00
this.currentObjects[train.id].remove();
delete this.currentObjects[train.id];
2019-03-31 19:14:21 +03:00
}
return;
}
2019-06-17 08:41:48 +03:00
if (this.currentObjects[train.id]){
2019-02-20 16:17:37 +03:00
//marker exists
2019-06-17 08:41:48 +03:00
let marker = this.currentObjects[train.id];
marker.setLatLng([train.pos.z, train.pos.x]);
marker.setPopupContent(this.createPopup(train));
2019-02-20 16:17:37 +03:00
} else {
//marker does not exist
2019-06-17 08:41:48 +03:00
let marker = this.createMarker(train);
marker.addTo(this);
2019-02-20 16:17:37 +03:00
2019-06-17 08:41:48 +03:00
this.currentObjects[train.id] = marker;
2019-02-20 16:17:37 +03:00
}
});
2019-06-17 08:41:48 +03:00
Object.keys(this.currentObjects).forEach(existingId => {
var trainIsActive = this.trains.find(function(t){
2019-02-20 16:17:37 +03:00
return t.id == existingId;
});
if (!trainIsActive){
//train
2019-06-17 08:41:48 +03:00
this.currentObjects[existingId].remove();
delete this.currentObjects[existingId];
2019-02-20 16:17:37 +03:00
}
});
},
reDraw: function(){
this.currentObjects = {};
this.clearLayers();
2019-07-16 09:35:36 +03:00
if (this.map.getZoom() < this.getMaxDisplayedZoom()) {
return;
}
2019-04-04 11:49:55 +03:00
var mapLayer = this.layerMgr.getCurrentLayer();
2019-02-20 16:17:37 +03:00
2019-06-17 08:41:48 +03:00
this.trains.forEach(train => {
if (!this.isTrainInCurrentLayer(train)){
2019-03-31 19:14:21 +03:00
//not in current layer
return;
}
2019-02-20 16:17:37 +03:00
2019-06-17 08:41:48 +03:00
var marker = this.createMarker(train);
marker.addTo(this);
this.currentObjects[train.id] = marker;
2019-02-20 16:17:37 +03:00
});
},
2019-07-19 20:58:59 +03:00
onAdd: function(map) {
this.map = map;
2019-07-19 20:52:19 +03:00
this.layerMgr.addListener(() => this.reDraw());
this.wsChannel.addListener("minetest-info", () => this.onMinetestUpdate());
2019-02-20 16:17:37 +03:00
this.reDraw();
},
2019-06-11 16:01:16 +03:00
onRemove: function(/*map*/) {
2019-02-20 16:17:37 +03:00
this.clearLayers();
2019-07-19 20:52:19 +03:00
this.layerMgr.removeListener(() => this.reDraw());
this.wsChannel.removeListener("minetest-info", () => this.onMinetestUpdate());
2019-02-20 16:17:37 +03:00
}
});