]> git.mxchange.org Git - flightgear.git/blobdiff - src/GUI/MapWidget.cxx
Some autopilot fixes
[flightgear.git] / src / GUI / MapWidget.cxx
index cd648fefb3ad1f91917b8a60ea319ca7320ff4be..39066851088af8fc0959be59b5c03629ad559d83 100644 (file)
@@ -8,6 +8,7 @@
 #include <algorithm> // for std::sort
 #include <plib/puAux.h>
 
+#include <simgear/sg_inlines.h>
 #include <simgear/route/waypoint.hxx>
 #include <simgear/sg_inlines.h>
 #include <simgear/misc/strutils.hxx>
@@ -383,9 +384,9 @@ MapWidget::MapWidget(int x, int y, int maxX, int maxY) :
   _route = static_cast<FGRouteMgr*>(globals->get_subsystem("route-manager"));
   _gps = fgGetNode("/instrumentation/gps");
 
-  _zoom = 6;
   _width = maxX - x;
   _height = maxY - y;
+  _hasPanned = false;
 
   MapData::setFont(legendFont);
   MapData::setPalette(colour);
@@ -396,11 +397,17 @@ MapWidget::MapWidget(int x, int y, int maxX, int maxY) :
 MapWidget::~MapWidget()
 {
   delete _magVar;
+  clearData();
 }
 
 void MapWidget::setProperty(SGPropertyNode_ptr prop)
 {
   _root = prop;
+  int zoom = _root->getBoolValue("zoom", -1);
+  if (zoom < 0) {
+    _root->setIntValue("zoom", 6); // default zoom
+  }
+  
   _root->setBoolValue("centre-on-aircraft", true);
   _root->setBoolValue("draw-data", false);
   _root->setBoolValue("magnetic-headings", true);
@@ -496,36 +503,51 @@ int MapWidget::checkKey (int key, int updown )
 
 void MapWidget::pan(const SGVec2d& delta)
 {
+  _hasPanned = true; 
   _projectionCenter = unproject(-delta);
 }
 
+int MapWidget::zoom() const
+{
+  int z = _root->getIntValue("zoom");
+  SG_CLAMP_RANGE(z, 0, MAX_ZOOM);
+  return z;
+}
+
 void MapWidget::zoomIn()
 {
-  if (_zoom <= 0) {
+  if (zoom() <= 0) {
     return;
   }
 
-  --_zoom;
-  SG_LOG(SG_GENERAL, SG_INFO, "zoom is now:" << _zoom);
+  _root->setIntValue("zoom", zoom() - 1);
 }
 
 void MapWidget::zoomOut()
 {
-  if (_zoom >= MAX_ZOOM) {
+  if (zoom() >= MAX_ZOOM) {
     return;
   }
 
-  ++_zoom;
-  SG_LOG(SG_GENERAL, SG_INFO, "zoom is now:" << _zoom);
+  _root->setIntValue("zoom", zoom() + 1);
 }
 
 void MapWidget::draw(int dx, int dy)
 {
   _aircraft = SGGeod::fromDeg(fgGetDouble("/position/longitude-deg"),
     fgGetDouble("/position/latitude-deg"));
-  _magneticHeadings = _root->getBoolValue("magnetic-headings");
-
-  if (_root->getBoolValue("centre-on-aircraft")) {
+    
+  bool mag = _root->getBoolValue("magnetic-headings");
+  if (mag != _magneticHeadings) {
+    clearData(); // flush cached data text, since it often includes heading
+    _magneticHeadings =  mag;
+  }
+  
+  if (_hasPanned) {
+      _root->setBoolValue("centre-on-aircraft", false);
+      _hasPanned = false;
+  }
+  else if (_root->getBoolValue("centre-on-aircraft")) {
     _projectionCenter = _aircraft;
   }
 
@@ -539,6 +561,7 @@ void MapWidget::draw(int dx, int dy)
     _upHeading = 0.0;
   }
 
+  _cachedZoom = zoom();
   SGGeod topLeft = unproject(SGVec2d(_width/2, _height/2));
   // compute draw range, including a fudge factor for ILSs and other 'long'
   // symbols
@@ -612,14 +635,9 @@ void MapWidget::paintRuler()
 
   double dist, az, az2;
   SGGeodesy::inverse(_aircraft, _clickGeod, az, az2, dist);
-  if (_magneticHeadings) {
-    az -= _magVar->get_magvar();
-    SG_NORMALIZE_RANGE(az, 0.0, 360.0);
-  }
-
   char buffer[1024];
        ::snprintf(buffer, 1024, "%03d/%.1fnm",
-               SGMiscd::roundToInt(az), dist * SG_METER_TO_NM);
+               displayHeading(az), dist * SG_METER_TO_NM);
 
   MapData* d = getOrCreateDataForKey((void*) RULER_LEGEND_KEY);
   d->setLabel(buffer);
@@ -909,7 +927,7 @@ public:
   virtual bool pass(FGPositioned* aPos) const {
     if (_fixes && (aPos->type() == FGPositioned::FIX)) {
       // ignore fixes which end in digits - expirmental
-      if (isdigit(aPos->ident()[3]) && isdigit(aPos->ident()[4])) {
+      if (aPos->ident().length() > 4 && isdigit(aPos->ident()[3]) && isdigit(aPos->ident()[4])) {
         return false;
       }
     }
@@ -1020,7 +1038,7 @@ void MapWidget::drawFix(FGFix* fix)
   glColor3f(0.0, 0.0, 0.0);
   circleAt(pos, 3, 6);
 
-  if (_zoom > SHOW_DETAIL_ZOOM) {
+  if (_cachedZoom > SHOW_DETAIL_ZOOM) {
     return; // hide fix labels beyond a certain zoom level
   }
 
@@ -1118,7 +1136,7 @@ void MapWidget::drawAirport(FGAirport* apt)
        // draw tower location
        SGVec2d towerPos = project(apt->getTowerLocation());
 
-  if (_zoom <= SHOW_DETAIL_ZOOM) {
+  if (_cachedZoom <= SHOW_DETAIL_ZOOM) {
     glColor3f(1.0, 1.0, 1.0);
     glLineWidth(1.0);
 
@@ -1144,7 +1162,7 @@ void MapWidget::drawAirport(FGAirport* apt)
     d->setAnchor(towerPos);
   }
 
-  if (_zoom > SHOW_DETAIL_ZOOM) {
+  if (_cachedZoom > SHOW_DETAIL_ZOOM) {
     return;
   }
 
@@ -1225,13 +1243,13 @@ void MapWidget::drawRunway(FGRunway* rwy)
     setAnchorForKey(rwy, (p1 + p2) * 0.5);
     return;
   }
-
+  
        char buffer[1024];
-       ::snprintf(buffer, 1024, "%s/%s\n%3.0f/%3.0f\n%.0f'",
+       ::snprintf(buffer, 1024, "%s/%s\n%03d/%03d\n%.0f'",
                rwy->ident().c_str(),
                rwy->reciprocalRunway()->ident().c_str(),
-               rwy->headingDeg(),
-               rwy->reciprocalRunway()->headingDeg(),
+               displayHeading(rwy->headingDeg()),
+               displayHeading(rwy->reciprocalRunway()->headingDeg()),
                rwy->lengthFt());
 
   MapData* d = createDataForKey(rwy);
@@ -1293,8 +1311,10 @@ void MapWidget::drawILS(bool tuned, FGRunway* rwy)
   }
 
        char buffer[1024];
-       ::snprintf(buffer, 1024, "%s\n%s\n%3.2fMHz",
-               loc->name().c_str(), loc->ident().c_str(),loc->get_freq()/100.0);
+       ::snprintf(buffer, 1024, "%s\n%s\n%03d - %3.2fMHz",
+               loc->ident().c_str(), loc->name().c_str(),
+    displayHeading(radial),
+    loc->get_freq()/100.0);
 
   MapData* d = createDataForKey(loc);
   d->setPriority(40);
@@ -1310,7 +1330,7 @@ void MapWidget::drawTraffic()
     return;
   }
 
-  if (_zoom > SHOW_DETAIL_ZOOM) {
+  if (_cachedZoom > SHOW_DETAIL_ZOOM) {
     return;
   }
 
@@ -1368,21 +1388,15 @@ void MapWidget::drawAIAircraft(const SGPropertyNode* model, const SGGeod& pos, d
     drawLine(p, project(advance));
   }
 
-  if (validDataForKey((void*) model)) {
-    setAnchorForKey((void*) model, p);
-    return;
-  }
 
   // draw callsign / altitude / speed
-
-
   char buffer[1024];
        ::snprintf(buffer, 1024, "%s\n%d'\n%dkts",
                model->getStringValue("callsign", "<>"),
                static_cast<int>(pos.getElevationFt() / 50.0) * 50,
     speedKts);
 
-  MapData* d = createDataForKey((void*) model);
+  MapData* d = getOrCreateDataForKey((void*) model);
   d->setText(buffer);
   d->setLabel(model->getStringValue("callsign", "<>"));
   d->setPriority(speedKts > 5 ? 60 : 10); // low priority for parked aircraft
@@ -1414,18 +1428,13 @@ void MapWidget::drawAIShip(const SGPropertyNode* model, const SGGeod& pos, doubl
     drawLine(p, project(advance));
   }
 
-  if (validDataForKey((void*) model)) {
-    setAnchorForKey((void*) model, p);
-    return;
-  }
-
   // draw callsign / speed
   char buffer[1024];
        ::snprintf(buffer, 1024, "%s\n%dkts",
                model->getStringValue("name", "<>"),
     speedKts);
 
-  MapData* d = createDataForKey((void*) model);
+  MapData* d = getOrCreateDataForKey((void*) model);
   d->setText(buffer);
   d->setLabel(model->getStringValue("name", "<>"));
   d->setPriority(speedKts > 2 ? 30 : 10); // low priority for slow moving ships
@@ -1469,7 +1478,7 @@ SGGeod MapWidget::unproject(const SGVec2d& p) const
 
 double MapWidget::currentScale() const
 {
-  return 1.0 / pow(2.0, _zoom);
+  return 1.0 / pow(2.0, _cachedZoom);
 }
 
 void MapWidget::circleAt(const SGVec2d& center, int nSides, double r)
@@ -1662,3 +1671,23 @@ MapData* MapWidget::createDataForKey(void* key)
   d->resetAge();
   return d;
 }
+
+void MapWidget::clearData()
+{
+  KeyDataMap::iterator it = _mapData.begin();
+  for (; it != _mapData.end(); ++it) {
+    delete it->second;
+  }
+  
+  _mapData.clear();
+}
+
+int MapWidget::displayHeading(double h) const
+{
+  if (_magneticHeadings) {
+    h -= _magVar->get_magvar() * SG_RADIANS_TO_DEGREES;
+  }
+  
+  SG_NORMALIZE_RANGE(h, 0.0, 360.0);
+  return SGMiscd::roundToInt(h);
+}