1
0
forked from MTSR/mapserver
mapserver/public/js/map/overlays/TrainOverlay.js

194 lines
5.1 KiB
JavaScript
Raw Normal View History

2019-09-18 12:53:58 +03:00
import wsChannel from '../../WebSocketChannel.js';
import layerMgr from '../../LayerManager.js';
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:detailed_steam_engine":
return "pics/advtrains/advtrains_detailed_engine_steam_inv.png";
2019-02-24 22:06:20 +03:00
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";
case "advtrains:wagon_default":
return "pics/advtrains/advtrains_wagon_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-09-18 12:53:58 +03:00
let trains = [];
//update trains all the time
wsChannel.addListener("minetest-info", function(info){
trains = info.trains || [];
});
2019-06-11 16:01:16 +03:00
export default L.LayerGroup.extend({
2019-09-18 12:53:58 +03:00
initialize: function() {
2019-02-20 16:17:37 +03:00
L.LayerGroup.prototype.initialize.call(this);
this.currentObjects = {}; // name => marker
2019-11-29 20:03:36 +03:00
this.onMinetestUpdate = this.onMinetestUpdate.bind(this);
2019-02-20 16:17:37 +03:00
},
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(){
2019-11-29 20:03:36 +03:00
return 10;
2019-07-16 09:35:36 +03:00
},
2019-02-20 16:17:37 +03:00
createMarker: function(train){
2019-02-24 22:06:20 +03:00
2019-07-25 16:41:02 +03:00
//search for wagon in front (whatever "front" is...)
2019-02-24 22:06:20 +03:00
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 + 0.5, train.pos.x + 0.5], {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-09-18 12:53:58 +03:00
var mapLayer = 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-09-18 12:53:58 +03:00
trains.forEach(train => {
2019-06-17 08:41:48 +03:00
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 + 0.5, train.pos.x + 0.5]);
2019-06-17 08:41:48 +03:00
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 => {
2019-09-18 12:53:58 +03:00
var trainIsActive = 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-09-18 12:53:58 +03:00
trains.forEach(train => {
2019-06-17 08:41:48 +03:00
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-11-29 20:03:36 +03:00
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-11-29 20:03:36 +03:00
wsChannel.removeListener("minetest-info", this.onMinetestUpdate);
2019-02-20 16:17:37 +03:00
}
});