5 #include "MapWidget.hxx"
8 #include <algorithm> // for std::sort
9 #include <plib/puAux.h>
11 #include <simgear/sg_inlines.h>
12 #include <simgear/misc/strutils.hxx>
13 #include <simgear/magvar/magvar.hxx>
14 #include <simgear/timing/sg_time.hxx> // for magVar julianDate
15 #include <simgear/structure/exception.hxx>
17 #include <Main/globals.hxx>
18 #include <Main/fg_props.hxx>
19 #include <Autopilot/route_mgr.hxx>
20 #include <Navaids/positioned.hxx>
21 #include <Navaids/navrecord.hxx>
22 #include <Navaids/navlist.hxx>
23 #include <Navaids/fix.hxx>
24 #include <Airports/airport.hxx>
25 #include <Airports/runways.hxx>
26 #include <Main/fg_os.hxx> // fgGetKeyModifiers()
27 #include <Navaids/routePath.hxx>
28 #include <Aircraft/FlightHistory.hxx>
29 #include <AIModel/AIAircraft.hxx>
30 #include <AIModel/AIFlightPlan.hxx>
32 const char* RULER_LEGEND_KEY = "ruler-legend";
34 /* equatorial and polar earth radius */
35 const float rec = 6378137; // earth radius, equator (?)
36 const float rpol = 6356752.314f; // earth radius, polar (?)
38 /************************************************************************
39 some trigonometric helper functions
40 (translated more or less directly from Alexei Novikovs perl original)
41 *************************************************************************/
43 //Returns Earth radius at a given latitude (Ellipsoide equation with two equal axis)
44 static float earth_radius_lat( float lat )
46 double a = cos(lat)/rec;
47 double b = sin(lat)/rpol;
48 return 1.0f / sqrt( a * a + b * b );
51 ///////////////////////////////////////////////////////////////////////////
53 static puBox makePuBox(int x, int y, int w, int h)
63 static bool puBoxIntersect(const puBox& a, const puBox& b)
65 int x0 = SG_MAX2(a.min[0], b.min[0]);
66 int y0 = SG_MAX2(a.min[1], b.min[1]);
67 int x1 = SG_MIN2(a.max[0], b.max[0]);
68 int y1 = SG_MIN2(a.max[1], b.max[1]);
70 return (x0 <= x1) && (y0 <= y1);
74 typedef std::vector<MapData*> MapDataVec;
79 static const int HALIGN_LEFT = 1;
80 static const int HALIGN_CENTER = 2;
81 static const int HALIGN_RIGHT = 3;
83 static const int VALIGN_TOP = 1 << 4;
84 static const int VALIGN_CENTER = 2 << 4;
85 static const int VALIGN_BOTTOM = 3 << 4;
87 MapData(int priority) :
93 _offsetDir(HALIGN_LEFT | VALIGN_CENTER),
99 void setLabel(const std::string& label)
101 if (label == _label) {
102 return; // common case, and saves invalidation
109 void setText(const std::string &text)
111 if (_rawText == text) {
112 return; // common case, and saves invalidation
119 void setDataVisible(bool vis) {
120 if (vis == _dataVisible) {
124 if (_rawText.empty()) {
132 static void setFont(puFont f)
135 _fontHeight = f.getStringHeight();
136 _fontDescender = f.getStringDescender();
139 static void setPalette(puColor* pal)
144 void setPriority(int pri)
150 { return _priority; }
152 void setAnchor(const SGVec2d& anchor)
157 void setOffset(int direction, int px)
159 if ((_offsetPx == px) && (_offsetDir == direction)) {
164 _offsetDir = direction;
168 bool isClipped(const puBox& vis) const
171 if ((_width < 1) || (_height < 1)) {
175 return !puBoxIntersect(vis, box());
178 bool overlaps(const MapDataVec& l) const
183 MapDataVec::const_iterator it;
184 for (it = l.begin(); it != l.end(); ++it) {
185 if (puBoxIntersect(b, (*it)->box())) {
188 } // of list iteration
197 _anchor.x() + _offset.x(),
198 _anchor.y() + _offset.y(),
206 int xx = _anchor.x() + _offset.x();
207 int yy = _anchor.y() + _offset.y();
210 puBox box(makePuBox(0,0,_width, _height));
212 box.draw(xx, yy, PUSTYLE_DROPSHADOW, _palette, FALSE, border);
215 int lineHeight = _fontHeight;
216 int xPos = xx + MARGIN;
217 int yPos = yy + _height - (lineHeight + MARGIN);
218 glColor3f(0.8, 0.8, 0.8);
220 for (unsigned int ln=0; ln<_lines.size(); ++ln) {
221 _font.drawString(_lines[ln].c_str(), xPos, yPos);
222 yPos -= lineHeight + LINE_LEADING;
225 glColor3f(0.8, 0.8, 0.8);
226 _font.drawString(_label.c_str(), xx, yy + _fontDescender);
240 bool isExpired() const
241 { return (_age > 100); }
243 static bool order(MapData* a, MapData* b)
245 return a->_priority > b->_priority;
248 void validate() const
268 void measureData() const
270 _lines = simgear::strutils::split(_rawText, "\n");
271 // measure text to find width and height
275 for (unsigned int ln=0; ln<_lines.size(); ++ln) {
276 _height += _fontHeight;
278 _height += LINE_LEADING;
281 int lw = _font.getStringWidth(_lines[ln].c_str());
282 _width = std::max(_width, lw);
283 } // of line measurement
285 if ((_width < 1) || (_height < 1)) {
290 _height += MARGIN * 2;
291 _width += MARGIN * 2;
294 void measureLabel() const
296 if (_label.empty()) {
297 _width = _height = -1;
301 _height = _fontHeight;
302 _width = _font.getStringWidth(_label.c_str());
305 void computeOffset() const
307 _dirtyOffset = false;
308 if ((_width <= 0) || (_height <= 0)) {
315 switch (_offsetDir & 0x0f) {
322 hOffset = -(_width>>1);
326 hOffset = -(_offsetPx + _width);
330 switch (_offsetDir & 0xf0) {
333 vOffset = -(_offsetPx + _height);
337 vOffset = -(_height>>1);
345 _offset = SGVec2d(hOffset, vOffset);
348 static const int LINE_LEADING = 3;
349 static const int MARGIN = 3;
351 mutable bool _dirtyText;
352 mutable bool _dirtyOffset;
354 std::string _rawText;
356 mutable std::vector<std::string> _lines;
358 mutable int _width, _height;
362 mutable SGVec2d _offset;
366 static puColor* _palette;
367 static int _fontHeight;
368 static int _fontDescender;
371 puFont MapData::_font;
372 puColor* MapData::_palette;
373 int MapData::_fontHeight = 0;
374 int MapData::_fontDescender = 0;
376 ///////////////////////////////////////////////////////////////////////////
378 const int MAX_ZOOM = 12;
379 const int SHOW_DETAIL_ZOOM = 8;
380 const int SHOW_DETAIL2_ZOOM = 5;
381 const int CURSOR_PAN_STEP = 32;
383 MapWidget::MapWidget(int x, int y, int maxX, int maxY) :
384 puObject(x,y,maxX, maxY)
386 _route = static_cast<FGRouteMgr*>(globals->get_subsystem("route-manager"));
387 _gps = fgGetNode("/instrumentation/gps");
392 _projection = PROJECTION_SAMSON_FLAMSTEED;
393 _magneticHeadings = false;
395 MapData::setFont(legendFont);
396 MapData::setPalette(colour);
398 _magVar = new SGMagVar();
401 MapWidget::~MapWidget()
407 void MapWidget::setProperty(SGPropertyNode_ptr prop)
410 int zoom = _root->getIntValue("zoom", -1);
412 _root->setIntValue("zoom", 6); // default zoom
415 // expose MAX_ZOOM to the UI
416 _root->setIntValue("max-zoom", MAX_ZOOM);
417 _root->setBoolValue("centre-on-aircraft", true);
418 _root->setBoolValue("draw-data", false);
419 _root->setBoolValue("draw-flight-history", false);
420 _root->setBoolValue("magnetic-headings", true);
423 void MapWidget::setSize(int w, int h)
425 puObject::setSize(w, h);
432 void MapWidget::doHit( int button, int updown, int x, int y )
434 puObject::doHit(button, updown, x, y);
435 if (updown == PU_DRAG) {
440 if (button == 3) { // mouse-wheel up
442 } else if (button == 4) { // mouse-wheel down
446 if (button != active_mouse_button) {
450 _hitLocation = SGVec2d(x - abox.min[0], y - abox.min[1]);
452 if (updown == PU_UP) {
453 puDeactivateWidget();
454 } else if (updown == PU_DOWN) {
455 puSetActiveWidget(this, x, y);
457 if (fgGetKeyModifiers() & KEYMOD_CTRL) {
458 _clickGeod = unproject(_hitLocation - SGVec2d(_width>>1, _height>>1));
463 void MapWidget::handlePan(int x, int y)
465 SGVec2d delta = SGVec2d(x, y) - _hitLocation;
467 _hitLocation = SGVec2d(x,y);
470 int MapWidget::checkKey (int key, int updown )
472 if ((updown == PU_UP) || !isVisible () || !isActive () || (window != puGetWindow())) {
480 pan(SGVec2d(0, -CURSOR_PAN_STEP));
484 pan(SGVec2d(0, CURSOR_PAN_STEP));
488 pan(SGVec2d(CURSOR_PAN_STEP, 0));
492 pan(SGVec2d(-CURSOR_PAN_STEP, 0));
511 void MapWidget::pan(const SGVec2d& delta)
514 _projectionCenter = unproject(-delta);
517 int MapWidget::zoom() const
519 int z = _root->getIntValue("zoom");
520 SG_CLAMP_RANGE(z, 0, MAX_ZOOM);
524 void MapWidget::zoomIn()
526 if (zoom() >= MAX_ZOOM) {
530 _root->setIntValue("zoom", zoom() + 1);
533 void MapWidget::zoomOut()
539 _root->setIntValue("zoom", zoom() - 1);
542 void MapWidget::draw(int dx, int dy)
544 _aircraft = globals->get_aircraft_position();
546 bool mag = _root->getBoolValue("magnetic-headings");
547 if (mag != _magneticHeadings) {
548 clearData(); // flush cached data text, since it often includes heading
549 _magneticHeadings = mag;
553 _root->setBoolValue("centre-on-aircraft", false);
556 else if (_root->getBoolValue("centre-on-aircraft")) {
557 _projectionCenter = _aircraft;
560 double julianDate = globals->get_time_params()->getJD();
561 _magVar->update(_projectionCenter, julianDate);
563 bool aircraftUp = _root->getBoolValue("aircraft-heading-up");
565 _upHeading = fgGetDouble("/orientation/heading-deg");
570 _cachedZoom = MAX_ZOOM - zoom();
571 SGGeod topLeft = unproject(SGVec2d(_width/2, _height/2));
572 // compute draw range, including a fudge factor for ILSs and other 'long'
574 _drawRangeNm = SGGeodesy::distanceNm(_projectionCenter, topLeft) + 10.0;
576 // drawing operations
577 GLint sx = (int) abox.min[0],
578 sy = (int) abox.min[1];
579 glScissor(dx + sx, dy + sy, _width, _height);
580 glEnable(GL_SCISSOR_TEST);
582 glMatrixMode(GL_MODELVIEW);
584 // cetere drawing about the widget center (which is also the
585 // projection centre)
586 glTranslated(dx + sx + (_width/2), dy + sy + (_height/2), 0.0);
591 int textHeight = legendFont.getStringHeight() + 5;
594 SGVec2d loc = project(_aircraft);
595 glColor3f(1.0, 1.0, 1.0);
596 drawLine(loc, SGVec2d(loc.x(), (_height / 2) - textHeight));
599 if (_magneticHeadings) {
600 displayHdg = (int) fgGetDouble("/orientation/heading-magnetic-deg");
602 displayHdg = (int) _upHeading;
605 double y = (_height / 2) - textHeight;
607 ::snprintf(buf, 16, "%d", displayHdg);
608 int sw = legendFont.getStringWidth(buf);
609 legendFont.drawString(buf, loc.x() - sw/2, y);
617 drawNavRadio(fgGetNode("/instrumentation/nav[0]", false));
618 drawNavRadio(fgGetNode("/instrumentation/nav[1]", false));
619 paintAircraftLocation(_aircraft);
627 glDisable(GL_SCISSOR_TEST);
630 void MapWidget::paintRuler()
632 if (_clickGeod == SGGeod()) {
636 SGVec2d acftPos = project(_aircraft);
637 SGVec2d clickPos = project(_clickGeod);
639 glColor4f(0.0, 1.0, 1.0, 0.6);
640 drawLine(acftPos, clickPos);
642 circleAtAlt(clickPos, 8, 10, 5);
644 double dist, az, az2;
645 SGGeodesy::inverse(_aircraft, _clickGeod, az, az2, dist);
647 ::snprintf(buffer, 1024, "%03d/%.1fnm",
648 displayHeading(az), dist * SG_METER_TO_NM);
650 MapData* d = getOrCreateDataForKey((void*) RULER_LEGEND_KEY);
652 d->setAnchor(clickPos);
653 d->setOffset(MapData::VALIGN_TOP | MapData::HALIGN_CENTER, 15);
654 d->setPriority(20000);
659 void MapWidget::paintAircraftLocation(const SGGeod& aircraftPos)
661 SGVec2d loc = project(aircraftPos);
663 double hdg = fgGetDouble("/orientation/heading-deg");
666 glColor4f(1.0, 1.0, 0.0, 1.0);
668 glTranslated(loc.x(), loc.y(), 0.0);
669 glRotatef(hdg - _upHeading, 0.0, 0.0, -1.0);
671 const SGVec2d wingspan(12, 0);
672 const SGVec2d nose(0, 8);
673 const SGVec2d tail(0, -14);
674 const SGVec2d tailspan(4,0);
676 drawLine(-wingspan, wingspan);
677 drawLine(nose, tail);
678 drawLine(tail - tailspan, tail + tailspan);
684 void MapWidget::paintRoute()
686 if (_route->numWaypts() < 2) {
690 RoutePath path(_route->flightPlan());
692 // first pass, draw the actual lines
695 for (int w=0; w<_route->numWaypts(); ++w) {
696 SGGeodVec gv(path.pathForIndex(w));
701 if (w < _route->currentIndex()) {
702 glColor4f(0.5, 0.5, 0.5, 0.7);
704 glColor4f(1.0, 0.0, 1.0, 1.0);
707 flightgear::WayptRef wpt(_route->wayptAtIndex(w));
708 if (wpt->flag(flightgear::WPT_MISS)) {
709 glEnable(GL_LINE_STIPPLE);
710 glLineStipple(1, 0x00FF);
713 glBegin(GL_LINE_STRIP);
714 for (unsigned int i=0; i<gv.size(); ++i) {
715 SGVec2d p = project(gv[i]);
716 glVertex2d(p.x(), p.y());
720 glDisable(GL_LINE_STIPPLE);
724 // second pass, draw waypoint symbols and data
725 for (int w=0; w < _route->numWaypts(); ++w) {
726 flightgear::WayptRef wpt(_route->wayptAtIndex(w));
727 SGGeod g = path.positionForIndex(w);
729 continue; // Vectors or similar
732 SGVec2d p = project(g);
733 glColor4f(1.0, 0.0, 1.0, 1.0);
734 circleAtAlt(p, 8, 12, 5);
736 std::ostringstream legend;
737 legend << wpt->ident();
738 if (wpt->altitudeRestriction() != flightgear::RESTRICT_NONE) {
739 legend << '\n' << SGMiscd::roundToInt(wpt->altitudeFt()) << '\'';
742 if (wpt->speedRestriction() == flightgear::SPEED_RESTRICT_MACH) {
743 legend << '\n' << wpt->speedMach() << "M";
744 } else if (wpt->speedRestriction() != flightgear::RESTRICT_NONE) {
745 legend << '\n' << SGMiscd::roundToInt(wpt->speedKts()) << "Kts";
748 MapData* d = getOrCreateDataForKey(reinterpret_cast<void*>(w * 2));
749 d->setText(legend.str());
750 d->setLabel(wpt->ident());
752 d->setOffset(MapData::VALIGN_TOP | MapData::HALIGN_CENTER, 15);
753 d->setPriority(w < _route->currentIndex() ? 9000 : 12000);
755 } // of second waypoint iteration
758 void MapWidget::drawFlightHistory()
760 FGFlightHistory* history = (FGFlightHistory*) globals->get_subsystem("history");
761 if (!history || !_root->getBoolValue("draw-flight-history")) {
765 // first pass, draw the actual lines
768 SGGeodVec gv(history->pathForHistory());
769 glColor4f(0.0, 0.0, 1.0, 0.7);
771 glBegin(GL_LINE_STRIP);
772 for (unsigned int i=0; i<gv.size(); ++i) {
773 SGVec2d p = project(gv[i]);
774 glVertex2d(p.x(), p.y());
781 * Round a SGGeod to an arbitrary precision.
782 * For example, passing precision of 0.5 will round to the nearest 0.5 of
783 * a degree in both lat and lon - passing in 3.0 rounds to the nearest 3 degree
784 * multiple, and so on.
786 static SGGeod roundGeod(double precision, const SGGeod& g)
788 double lon = SGMiscd::round(g.getLongitudeDeg() / precision);
789 double lat = SGMiscd::round(g.getLatitudeDeg() / precision);
791 return SGGeod::fromDeg(lon * precision, lat * precision);
794 bool MapWidget::drawLineClipped(const SGVec2d& a, const SGVec2d& b)
796 double minX = SGMiscd::min(a.x(), b.x()),
797 minY = SGMiscd::min(a.y(), b.y()),
798 maxX = SGMiscd::max(a.x(), b.x()),
799 maxY = SGMiscd::max(a.y(), b.y());
801 int hh = _height >> 1, hw = _width >> 1;
803 if ((maxX < -hw) || (minX > hw) || (minY > hh) || (maxY < -hh)) {
807 glVertex2dv(a.data());
808 glVertex2dv(b.data());
812 SGVec2d MapWidget::gridPoint(int ix, int iy)
814 int key = (ix + 0x7fff) | ((iy + 0x7fff) << 16);
815 GridPointCache::iterator it = _gridCache.find(key);
816 if (it != _gridCache.end()) {
820 SGGeod gp = SGGeod::fromDeg(
821 _gridCenter.getLongitudeDeg() + ix * _gridSpacing,
822 _gridCenter.getLatitudeDeg() + iy * _gridSpacing);
824 SGVec2d proj = project(gp);
825 _gridCache[key] = proj;
829 void MapWidget::drawLatLonGrid()
832 _gridCenter = roundGeod(_gridSpacing, _projectionCenter);
838 glColor4f(0.8, 0.8, 0.8, 0.4);
846 for (int x = -ix; x < ix; ++x) {
847 didDraw |= drawLineClipped(gridPoint(x, -iy), gridPoint(x+1, -iy));
848 didDraw |= drawLineClipped(gridPoint(x, iy), gridPoint(x+1, iy));
849 didDraw |= drawLineClipped(gridPoint(x, -iy), gridPoint(x, -iy + 1));
850 didDraw |= drawLineClipped(gridPoint(x, iy), gridPoint(x, iy - 1));
854 for (int y = -iy; y < iy; ++y) {
855 didDraw |= drawLineClipped(gridPoint(-ix, y), gridPoint(-ix, y+1));
856 didDraw |= drawLineClipped(gridPoint(-ix, y), gridPoint(-ix + 1, y));
857 didDraw |= drawLineClipped(gridPoint(ix, y), gridPoint(ix, y+1));
858 didDraw |= drawLineClipped(gridPoint(ix, y), gridPoint(ix - 1, y));
869 void MapWidget::drawGPSData()
871 std::string gpsMode = _gps->getStringValue("mode");
873 SGGeod wp0Geod = SGGeod::fromDeg(
874 _gps->getDoubleValue("wp/wp[0]/longitude-deg"),
875 _gps->getDoubleValue("wp/wp[0]/latitude-deg"));
877 SGGeod wp1Geod = SGGeod::fromDeg(
878 _gps->getDoubleValue("wp/wp[1]/longitude-deg"),
879 _gps->getDoubleValue("wp/wp[1]/latitude-deg"));
882 double gpsTrackDeg = _gps->getDoubleValue("indicated-track-true-deg");
883 double gpsSpeed = _gps->getDoubleValue("indicated-ground-speed-kt");
886 if (gpsSpeed > 3.0) { // only draw track line if valid
888 SGGeodesy::direct(_aircraft, gpsTrackDeg, _drawRangeNm * SG_NM_TO_METER, trackRadial, az2);
890 glColor4f(1.0, 1.0, 0.0, 1.0);
891 glEnable(GL_LINE_STIPPLE);
892 glLineStipple(1, 0x00FF);
893 drawLine(project(_aircraft), project(trackRadial));
894 glDisable(GL_LINE_STIPPLE);
897 if (gpsMode == "dto") {
898 SGVec2d wp0Pos = project(wp0Geod);
899 SGVec2d wp1Pos = project(wp1Geod);
901 glColor4f(1.0, 0.0, 1.0, 1.0);
902 drawLine(wp0Pos, wp1Pos);
906 if (_gps->getBoolValue("scratch/valid")) {
912 class MapAirportFilter : public FGAirport::AirportFilter
915 MapAirportFilter(SGPropertyNode_ptr nd)
917 _heliports = nd->getBoolValue("show-heliports", false);
918 _hardRunwaysOnly = nd->getBoolValue("hard-surfaced-airports", true);
919 _minLengthFt = fgGetDouble("/sim/navdb/min-runway-length-ft", 2000);
922 virtual FGPositioned::Type maxType() const {
923 return _heliports ? FGPositioned::HELIPORT : FGPositioned::AIRPORT;
926 virtual bool passAirport(FGAirport* aApt) const {
927 if (_hardRunwaysOnly) {
928 return aApt->hasHardRunwayOfLengthFt(_minLengthFt);
936 bool _hardRunwaysOnly;
940 void MapWidget::drawAirports()
942 MapAirportFilter af(_root);
943 bool partial = false;
944 FGPositionedList apts = FGPositioned::findWithinRangePartial(_projectionCenter, _drawRangeNm, &af, partial);
945 for (unsigned int i=0; i<apts.size(); ++i) {
946 drawAirport((FGAirport*) apts[i].get());
950 class NavaidFilter : public FGPositioned::Filter
953 NavaidFilter(bool fixesEnabled, bool navaidsEnabled) :
954 _fixes(fixesEnabled),
955 _navaids(navaidsEnabled)
958 virtual bool pass(FGPositioned* aPos) const {
959 if (_fixes && (aPos->type() == FGPositioned::FIX)) {
960 // ignore fixes which end in digits - expirmental
961 if (aPos->ident().length() > 4 && isdigit(aPos->ident()[3]) && isdigit(aPos->ident()[4])) {
969 virtual FGPositioned::Type minType() const {
970 return _fixes ? FGPositioned::FIX : FGPositioned::NDB;
973 virtual FGPositioned::Type maxType() const {
974 return _navaids ? FGPositioned::VOR : FGPositioned::FIX;
978 bool _fixes, _navaids;
981 void MapWidget::drawNavaids()
983 bool fixes = _root->getBoolValue("draw-fixes");
984 NavaidFilter f(fixes, _root->getBoolValue("draw-navaids"));
986 if (f.minType() <= f.maxType()) {
987 FGPositionedList navs = FGPositioned::findWithinRange(_projectionCenter, _drawRangeNm, &f);
990 for (unsigned int i=0; i<navs.size(); ++i) {
991 FGPositioned::Type ty = navs[i]->type();
992 if (ty == FGPositioned::NDB) {
993 drawNDB(false, (FGNavRecord*) navs[i].get());
994 } else if (ty == FGPositioned::VOR) {
995 drawVOR(false, (FGNavRecord*) navs[i].get());
996 } else if (ty == FGPositioned::FIX) {
997 drawFix((FGFix*) navs[i].get());
999 } // of navaid iteration
1000 } // of navaids || fixes are drawn test
1003 void MapWidget::drawPOIs()
1005 FGPositioned::TypeFilter f(FGPositioned::COUNTRY);
1006 f.addType(FGPositioned::CITY);
1007 f.addType(FGPositioned::TOWN);
1008 FGPositionedList poi = FGPositioned::findWithinRange(_projectionCenter, _drawRangeNm, &f);
1011 for (unsigned int i=0; i<poi.size(); ++i) {
1012 FGPositioned::Type ty = poi[i]->type();
1013 if (ty == FGPositioned::COUNTRY) {
1014 drawCountries((FGNavRecord*) poi[i].get());
1015 } else if (ty == FGPositioned::CITY) {
1016 drawCities((FGNavRecord*) poi[i].get());
1017 } else if (ty == FGPositioned::TOWN) {
1018 drawTowns((FGNavRecord*) poi[i].get());
1020 } // of navaid iteration
1023 void MapWidget::drawNDB(bool tuned, FGNavRecord* ndb)
1025 SGVec2d pos = project(ndb->geod());
1028 glColor3f(0.0, 1.0, 1.0);
1030 glColor3f(0.0, 0.0, 0.0);
1033 glEnable(GL_LINE_STIPPLE);
1034 glLineStipple(1, 0x00FF);
1035 circleAt(pos, 20, 6);
1036 circleAt(pos, 20, 10);
1037 glDisable(GL_LINE_STIPPLE);
1039 if (validDataForKey(ndb)) {
1040 setAnchorForKey(ndb, pos);
1045 ::snprintf(buffer, 1024, "%s\n%s %3.0fKhz",
1046 ndb->name().c_str(), ndb->ident().c_str(),ndb->get_freq()/100.0);
1048 MapData* d = createDataForKey(ndb);
1050 d->setLabel(ndb->ident());
1052 d->setOffset(MapData::HALIGN_CENTER | MapData::VALIGN_BOTTOM, 10);
1057 void MapWidget::drawVOR(bool tuned, FGNavRecord* vor)
1059 SGVec2d pos = project(vor->geod());
1061 glColor3f(0.0, 1.0, 1.0);
1063 glColor3f(0.0, 0.0, 1.0);
1066 circleAt(pos, 6, 9);
1067 circleAt(pos, 8, 1);
1072 if (validDataForKey(vor)) {
1073 setAnchorForKey(vor, pos);
1078 ::snprintf(buffer, 1024, "%s\n%s %6.3fMhz",
1079 vor->name().c_str(), vor->ident().c_str(),
1080 vor->get_freq() / 100.0);
1082 MapData* d = createDataForKey(vor);
1084 d->setLabel(vor->ident());
1085 d->setPriority(tuned ? 10000 : 100);
1086 d->setOffset(MapData::HALIGN_CENTER | MapData::VALIGN_BOTTOM, 12);
1090 void MapWidget::drawFix(FGFix* fix)
1092 SGVec2d pos = project(fix->geod());
1093 glColor3f(0.0, 0.0, 0.0);
1094 circleAt(pos, 3, 6);
1096 if (_cachedZoom > SHOW_DETAIL_ZOOM) {
1097 return; // hide fix labels beyond a certain zoom level
1100 if (validDataForKey(fix)) {
1101 setAnchorForKey(fix, pos);
1105 MapData* d = createDataForKey(fix);
1106 d->setLabel(fix->ident());
1108 d->setOffset(MapData::VALIGN_CENTER | MapData::HALIGN_LEFT, 10);
1112 void MapWidget::drawNavRadio(SGPropertyNode_ptr radio)
1114 if (!radio || radio->getBoolValue("slaved-to-gps", false)
1115 || !radio->getBoolValue("in-range", false)) {
1119 if (radio->getBoolValue("nav-loc", false)) {
1120 drawTunedLocalizer(radio);
1123 // identify the tuned station - unfortunately we don't get lat/lon directly,
1124 // need to do the frequency search again
1125 double mhz = radio->getDoubleValue("frequencies/selected-mhz", 0.0);
1127 FGNavRecord* nav = FGNavList::findByFreq(mhz, _aircraft,
1128 FGNavList::navFilter());
1129 if (!nav || (nav->ident() != radio->getStringValue("nav-id"))) {
1130 // mismatch between navradio selection logic and ours!
1137 SGVec2d pos = project(nav->geod());
1140 double trueRadial = radio->getDoubleValue("radials/target-radial-deg");
1141 SGGeodesy::direct(nav->geod(), trueRadial, nav->get_range() * SG_NM_TO_METER, range, az2);
1142 SGVec2d prange = project(range);
1144 SGVec2d norm = normalize(prange - pos);
1145 SGVec2d perp(norm.y(), -norm.x());
1147 circleAt(pos, 64, length(prange - pos));
1148 drawLine(pos, prange);
1150 // draw to/from arrows
1151 SGVec2d midPoint = (pos + prange) * 0.5;
1152 if (radio->getBoolValue("from-flag")) {
1158 SGVec2d arrowB = midPoint - (norm * sz) + (perp * sz);
1159 SGVec2d arrowC = midPoint - (norm * sz) - (perp * sz);
1160 drawLine(midPoint, arrowB);
1161 drawLine(arrowB, arrowC);
1162 drawLine(arrowC, midPoint);
1164 drawLine(pos, (2 * pos) - prange); // reciprocal radial
1167 void MapWidget::drawTunedLocalizer(SGPropertyNode_ptr radio)
1169 double mhz = radio->getDoubleValue("frequencies/selected-mhz", 0.0);
1170 FGNavRecord* loc = FGNavList::findByFreq(mhz, _aircraft, FGNavList::locFilter());
1171 if (!loc || (loc->ident() != radio->getStringValue("nav-id"))) {
1172 // mismatch between navradio selection logic and ours!
1176 if (loc->runway()) {
1177 drawILS(true, loc->runway());
1181 void MapWidget::drawCountries(FGNavRecord* rec)
1183 if (_cachedZoom < 9) {
1184 return; // hide labels beyond a certain zoom level
1187 SGVec2d pos = project(rec->geod());
1188 glColor3f(1.0, 1.0, 0.0);
1190 circleAt(pos, 4, 10);
1192 if (validDataForKey(rec)) {
1193 setAnchorForKey(rec, pos);
1198 ::snprintf(buffer, 1024, "%s",
1199 rec->name().c_str());
1201 MapData* d = createDataForKey(rec);
1202 d->setPriority(200);
1203 d->setLabel(rec->ident());
1205 d->setOffset(MapData::HALIGN_CENTER | MapData::VALIGN_BOTTOM, 10);
1210 void MapWidget::drawCities(FGNavRecord* rec)
1212 if (_cachedZoom > SHOW_DETAIL_ZOOM) {
1213 return; // hide labels beyond a certain zoom level
1216 SGVec2d pos = project(rec->geod());
1217 glColor3f(0.0, 1.0, 0.0);
1219 circleAt(pos, 4, 8);
1221 if (validDataForKey(rec)) {
1222 setAnchorForKey(rec, pos);
1227 ::snprintf(buffer, 1024, "%s",
1228 rec->name().c_str());
1230 MapData* d = createDataForKey(rec);
1232 d->setLabel(rec->ident());
1234 d->setOffset(MapData::HALIGN_CENTER | MapData::VALIGN_BOTTOM, 10);
1239 void MapWidget::drawTowns(FGNavRecord* rec)
1241 if (_cachedZoom > SHOW_DETAIL2_ZOOM) {
1242 return; // hide labels beyond a certain zoom level
1245 SGVec2d pos = project(rec->geod());
1246 glColor3f(0.2, 1.0, 0.0);
1248 circleAt(pos, 4, 5);
1250 if (validDataForKey(rec)) {
1251 setAnchorForKey(rec, pos);
1256 ::snprintf(buffer, 1024, "%s",
1257 rec->name().c_str());
1259 MapData* d = createDataForKey(rec);
1261 d->setLabel(rec->ident());
1263 d->setOffset(MapData::HALIGN_CENTER | MapData::VALIGN_BOTTOM, 10);
1269 void MapWidget::drawObstacle(FGPositioned* obs)
1271 SGVec2d pos = project(obs->geod());
1272 glColor3f(0.0, 0.0, 0.0);
1274 drawLine(pos, pos + SGVec2d());
1278 void MapWidget::drawAirport(FGAirport* apt)
1280 // draw tower location
1281 SGVec2d towerPos = project(apt->getTowerLocation());
1283 if (_cachedZoom <= SHOW_DETAIL_ZOOM) {
1284 glColor3f(1.0, 1.0, 1.0);
1287 drawLine(towerPos + SGVec2d(3, 0), towerPos + SGVec2d(3, 10));
1288 drawLine(towerPos + SGVec2d(-3, 0), towerPos + SGVec2d(-3, 10));
1289 drawLine(towerPos + SGVec2d(-6, 20), towerPos + SGVec2d(-3, 10));
1290 drawLine(towerPos + SGVec2d(6, 20), towerPos + SGVec2d(3, 10));
1291 drawLine(towerPos + SGVec2d(-6, 20), towerPos + SGVec2d(6, 20));
1294 if (validDataForKey(apt)) {
1295 setAnchorForKey(apt, towerPos);
1298 ::snprintf(buffer, 1024, "%s\n%s",
1299 apt->ident().c_str(), apt->name().c_str());
1301 MapData* d = createDataForKey(apt);
1303 d->setLabel(apt->ident());
1304 d->setPriority(100 + scoreAirportRunways(apt));
1305 d->setOffset(MapData::VALIGN_TOP | MapData::HALIGN_CENTER, 6);
1306 d->setAnchor(towerPos);
1309 if (_cachedZoom > SHOW_DETAIL_ZOOM) {
1313 FGRunwayList runways(apt->getRunwaysWithoutReciprocals());
1315 for (unsigned int r=0; r<runways.size(); ++r) {
1316 drawRunwayPre(runways[r]);
1319 for (unsigned int r=0; r<runways.size(); ++r) {
1320 FGRunway* rwy = runways[r];
1324 drawILS(false, rwy);
1327 if (rwy->reciprocalRunway()) {
1328 FGRunway* recip = rwy->reciprocalRunway();
1330 drawILS(false, recip);
1335 for (unsigned int r=0; r<apt->numHelipads(); ++r) {
1336 FGHelipad* hp = apt->getHelipadByIndex(r);
1338 } // of runway iteration
1342 int MapWidget::scoreAirportRunways(FGAirport* apt)
1344 bool needHardSurface = _root->getBoolValue("hard-surfaced-airports", true);
1345 double minLength = _root->getDoubleValue("min-runway-length-ft", 2000.0);
1347 FGRunwayList runways(apt->getRunwaysWithoutReciprocals());
1350 for (unsigned int r=0; r<runways.size(); ++r) {
1351 FGRunway* rwy = runways[r];
1352 if (needHardSurface && !rwy->isHardSurface()) {
1356 if (rwy->lengthFt() < minLength) {
1360 int scoreLength = SGMiscd::roundToInt(rwy->lengthFt() / 200.0);
1361 score += scoreLength;
1362 } // of runways iteration
1367 void MapWidget::drawRunwayPre(FGRunway* rwy)
1369 SGVec2d p1 = project(rwy->begin());
1370 SGVec2d p2 = project(rwy->end());
1373 glColor3f(1.0, 0.0, 1.0);
1377 void MapWidget::drawRunway(FGRunway* rwy)
1380 // optionally show active, stopway, etc
1381 // in legend, show published heading and length
1382 // and threshold elevation
1384 SGVec2d p1 = project(rwy->begin());
1385 SGVec2d p2 = project(rwy->end());
1387 glColor3f(1.0, 1.0, 1.0);
1388 SGVec2d inset = normalize(p2 - p1) * 2;
1390 drawLine(p1 + inset, p2 - inset);
1392 if (validDataForKey(rwy)) {
1393 setAnchorForKey(rwy, (p1 + p2) * 0.5);
1398 ::snprintf(buffer, 1024, "%s/%s\n%03d/%03d\n%.0f'",
1399 rwy->ident().c_str(),
1400 rwy->reciprocalRunway()->ident().c_str(),
1401 displayHeading(rwy->headingDeg()),
1402 displayHeading(rwy->reciprocalRunway()->headingDeg()),
1405 MapData* d = createDataForKey(rwy);
1407 d->setLabel(rwy->ident() + "/" + rwy->reciprocalRunway()->ident());
1409 d->setOffset(MapData::HALIGN_CENTER | MapData::VALIGN_BOTTOM, 12);
1410 d->setAnchor((p1 + p2) * 0.5);
1413 void MapWidget::drawILS(bool tuned, FGRunway* rwy)
1415 // arrow, tip centered on the landing threshold
1416 // using LOC transmitter position would be more accurate, but
1417 // is visually cluttered
1418 // arrow width is based upon the computed localizer width
1420 FGNavRecord* loc = rwy->ILS();
1421 double halfBeamWidth = loc->localizerWidth() * 0.5;
1422 SGVec2d t = project(rwy->threshold());
1424 double rangeM = loc->get_range() * SG_NM_TO_METER;
1425 double radial = loc->get_multiuse();
1426 SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
1429 // compute the three end points at the widge end of the arrow
1430 SGGeodesy::direct(loc->geod(), radial, -rangeM, locEnd, az2);
1431 SGVec2d endCentre = project(locEnd);
1433 SGGeodesy::direct(loc->geod(), radial + halfBeamWidth, -rangeM * 1.1, locEnd, az2);
1434 SGVec2d endR = project(locEnd);
1436 SGGeodesy::direct(loc->geod(), radial - halfBeamWidth, -rangeM * 1.1, locEnd, az2);
1437 SGVec2d endL = project(locEnd);
1439 // outline two triangles
1442 glColor3f(0.0, 1.0, 1.0);
1444 glColor3f(0.0, 0.0, 1.0);
1447 glBegin(GL_LINE_LOOP);
1448 glVertex2dv(t.data());
1449 glVertex2dv(endCentre.data());
1450 glVertex2dv(endL.data());
1452 glBegin(GL_LINE_LOOP);
1453 glVertex2dv(t.data());
1454 glVertex2dv(endCentre.data());
1455 glVertex2dv(endR.data());
1458 if (validDataForKey(loc)) {
1459 setAnchorForKey(loc, endR);
1464 ::snprintf(buffer, 1024, "%s\n%s\n%03d - %3.2fMHz",
1465 loc->ident().c_str(), loc->name().c_str(),
1466 displayHeading(radial),
1467 loc->get_freq()/100.0);
1469 MapData* d = createDataForKey(loc);
1471 d->setLabel(loc->ident());
1473 d->setOffset(MapData::HALIGN_CENTER | MapData::VALIGN_BOTTOM, 10);
1477 void MapWidget::drawTraffic()
1479 if (!_root->getBoolValue("draw-traffic")) {
1483 if (_cachedZoom > SHOW_DETAIL_ZOOM) {
1487 const SGPropertyNode* ai = fgGetNode("/ai/models", true);
1489 for (int i = 0; i < ai->nChildren(); ++i) {
1490 const SGPropertyNode *model = ai->getChild(i);
1491 // skip bad or dead entries
1492 if (!model || model->getIntValue("id", -1) == -1) {
1496 const std::string& name(model->getName());
1497 SGGeod pos = SGGeod::fromDegFt(
1498 model->getDoubleValue("position/longitude-deg"),
1499 model->getDoubleValue("position/latitude-deg"),
1500 model->getDoubleValue("position/altitude-ft"));
1502 double dist = SGGeodesy::distanceNm(_projectionCenter, pos);
1503 if (dist > _drawRangeNm) {
1507 double heading = model->getDoubleValue("orientation/true-heading-deg");
1508 if ((name == "aircraft") || (name == "multiplayer") ||
1509 (name == "wingman") || (name == "tanker")) {
1510 drawAIAircraft(model, pos, heading);
1511 } else if ((name == "ship") || (name == "carrier") || (name == "escort")) {
1512 drawAIShip(model, pos, heading);
1514 } // of ai/models iteration
1517 void MapWidget::drawHelipad(FGHelipad* hp)
1519 SGVec2d pos = project(hp->geod());
1521 glColor3f(1.0, 0.0, 1.0);
1522 circleAt(pos, 16, 5.0);
1524 if (validDataForKey(hp)) {
1525 setAnchorForKey(hp, pos);
1530 ::snprintf(buffer, 1024, "%s\n%03d\n%.0f'",
1531 hp->ident().c_str(),
1532 displayHeading(hp->headingDeg()),
1535 MapData* d = createDataForKey(hp);
1537 d->setLabel(hp->ident());
1539 d->setOffset(MapData::HALIGN_CENTER | MapData::VALIGN_BOTTOM, 8);
1543 void MapWidget::drawAIAircraft(const SGPropertyNode* model, const SGGeod& pos, double hdg)
1546 SGVec2d p = project(pos);
1548 glColor3f(0.0, 0.0, 0.0);
1550 circleAt(p, 4, 6.0); // black diamond
1552 // draw heading vector
1553 int speedKts = static_cast<int>(model->getDoubleValue("velocities/true-airspeed-kt"));
1557 const double dt = 15.0 / (3600.0); // 15 seconds look-ahead
1558 double distanceM = speedKts * SG_NM_TO_METER * dt;
1562 SGGeodesy::direct(pos, hdg, distanceM, advance, az2);
1564 drawLine(p, project(advance));
1567 // try to access the flight-plan of the aircraft. There are several layers
1568 // of potential NULL-ness here, so we have to be defensive at each stage.
1569 std::string originICAO, destinationICAO;
1570 FGAIManager* aiManager = static_cast<FGAIManager*>(globals->get_subsystem("ai-model"));
1571 FGAIBasePtr aircraft = aiManager ? aiManager->getObjectFromProperty(model) : NULL;
1573 FGAIAircraft* p = static_cast<FGAIAircraft*>(aircraft.get());
1574 if (p->GetFlightPlan()) {
1575 originICAO = p->GetFlightPlan()->departureAirport()->ident();
1576 destinationICAO = p->GetFlightPlan()->arrivalAirport()->ident();
1580 // draw callsign / altitude / speed
1581 int altFt50 = static_cast<int>(pos.getElevationFt() / 50.0) * 50;
1582 std::ostringstream ss;
1583 ss << model->getStringValue("callsign", "<>");
1585 ss << "\n" << altFt50 << "' " << speedKts << "kts";
1588 if (!originICAO.empty() || ! destinationICAO.empty()) {
1589 ss << "\n" << originICAO << " -> " << destinationICAO;
1592 MapData* d = getOrCreateDataForKey((void*) model);
1593 d->setText(ss.str().c_str());
1594 d->setLabel(model->getStringValue("callsign", "<>"));
1595 d->setPriority(speedKts > 5 ? 60 : 10); // low priority for parked aircraft
1596 d->setOffset(MapData::VALIGN_CENTER | MapData::HALIGN_LEFT, 10);
1600 void MapWidget::drawAIShip(const SGPropertyNode* model, const SGGeod& pos, double hdg)
1602 SGVec2d p = project(pos);
1604 glColor3f(0.0, 0.0, 0.5);
1606 circleAt(p, 4, 6.0); // blue diamond (to differentiate from aircraft.
1608 // draw heading vector
1609 int speedKts = static_cast<int>(model->getDoubleValue("velocities/speed-kts"));
1613 const double dt = 15.0 / (3600.0); // 15 seconds look-ahead
1614 double distanceM = speedKts * SG_NM_TO_METER * dt;
1618 SGGeodesy::direct(pos, hdg, distanceM, advance, az2);
1620 drawLine(p, project(advance));
1623 // draw callsign / speed
1625 ::snprintf(buffer, 1024, "%s\n%dkts",
1626 model->getStringValue("name", "<>"),
1629 MapData* d = getOrCreateDataForKey((void*) model);
1631 d->setLabel(model->getStringValue("name", "<>"));
1632 d->setPriority(speedKts > 2 ? 30 : 10); // low priority for slow moving ships
1633 d->setOffset(MapData::VALIGN_CENTER | MapData::HALIGN_LEFT, 10);
1637 SGVec2d MapWidget::project(const SGGeod& geod) const
1640 double r = earth_radius_lat(geod.getLatitudeRad());
1642 switch (_projection) {
1643 case PROJECTION_SAMSON_FLAMSTEED:
1645 // Sanson-Flamsteed projection, relative to the projection center
1646 double lonDiff = geod.getLongitudeRad() - _projectionCenter.getLongitudeRad(),
1647 latDiff = geod.getLatitudeRad() - _projectionCenter.getLatitudeRad();
1649 p = SGVec2d(cos(geod.getLatitudeRad()) * lonDiff, latDiff) * r * currentScale();
1653 case PROJECTION_ORTHO_AZIMUTH:
1655 // http://mathworld.wolfram.com/OrthographicProjection.html
1656 double cosTheta = cos(geod.getLatitudeRad());
1657 double sinDLambda = sin(geod.getLongitudeRad() - _projectionCenter.getLongitudeRad());
1658 double cosDLambda = cos(geod.getLongitudeRad() - _projectionCenter.getLongitudeRad());
1659 double sinTheta1 = sin(_projectionCenter.getLatitudeRad());
1660 double sinTheta = sin(geod.getLatitudeRad());
1661 double cosTheta1 = cos(_projectionCenter.getLatitudeRad());
1663 p = SGVec2d(cosTheta * sinDLambda,
1664 (cosTheta1 * sinTheta) - (sinTheta1 * cosTheta * cosDLambda)) * r * currentScale();
1668 case PROJECTION_SPHERICAL:
1670 SGVec3d cartCenter = SGVec3d::fromGeod(_projectionCenter);
1671 SGVec3d cartPt = SGVec3d::fromGeod(geod) - cartCenter;
1673 // rotate relative to projection center
1674 SGQuatd orient = SGQuatd::fromLonLat(_projectionCenter);
1675 cartPt = orient.rotateBack(cartPt);
1676 return SGVec2d(cartPt.y(), cartPt.x()) * currentScale();
1679 } // of projection mode switch
1682 // rotate as necessary
1683 double cost = cos(_upHeading * SG_DEGREES_TO_RADIANS),
1684 sint = sin(_upHeading * SG_DEGREES_TO_RADIANS);
1685 double rx = cost * p.x() - sint * p.y();
1686 double ry = sint * p.x() + cost * p.y();
1687 return SGVec2d(rx, ry);
1690 SGGeod MapWidget::unproject(const SGVec2d& p) const
1692 // unrotate, if necessary
1693 double cost = cos(-_upHeading * SG_DEGREES_TO_RADIANS),
1694 sint = sin(-_upHeading * SG_DEGREES_TO_RADIANS);
1695 SGVec2d ur(cost * p.x() - sint * p.y(),
1696 sint * p.x() + cost * p.y());
1700 switch (_projection) {
1701 case PROJECTION_SAMSON_FLAMSTEED:
1703 double r = earth_radius_lat(_projectionCenter.getLatitudeRad());
1704 SGVec2d unscaled = ur * (1.0 / (currentScale() * r));
1705 double lat = unscaled.y() + _projectionCenter.getLatitudeRad();
1706 double lon = (unscaled.x() / cos(lat)) + _projectionCenter.getLongitudeRad();
1707 return SGGeod::fromRad(lon, lat);
1710 case PROJECTION_ORTHO_AZIMUTH:
1712 double r = earth_radius_lat(_projectionCenter.getLatitudeRad());
1713 SGVec2d unscaled = ur * (1.0 / (currentScale() * r));
1715 double phi = length(p);
1716 double c = asin(phi);
1717 double sinTheta1 = sin(_projectionCenter.getLatitudeRad());
1718 double cosTheta1 = cos(_projectionCenter.getLatitudeRad());
1720 double lat = asin(cos(c) * sinTheta1 + ((unscaled.y() * sin(c) * cosTheta1) / phi));
1721 double lon = _projectionCenter.getLongitudeRad() +
1722 atan((unscaled.x()* sin(c)) / (phi * cosTheta1 * cos(c) - unscaled.y() * sinTheta1 * sin(c)));
1723 return SGGeod::fromRad(lon, lat);
1726 case PROJECTION_SPHERICAL:
1728 SGVec2d unscaled = ur * (1.0 / currentScale());
1729 SGQuatd orient = SGQuatd::fromLonLat(_projectionCenter);
1730 SGVec3d cartCenter = SGVec3d::fromGeod(_projectionCenter);
1731 SGVec3d cartPt = orient.rotate(SGVec3d(unscaled.x(), unscaled.y(), 0.0));
1732 return SGGeod::fromCart(cartPt + cartCenter);
1734 } // of projection mode switch
1737 double MapWidget::currentScale() const
1739 return 1.0 / pow(2.0, _cachedZoom);
1742 void MapWidget::circleAt(const SGVec2d& center, int nSides, double r)
1744 glBegin(GL_LINE_LOOP);
1745 double advance = (SGD_PI * 2) / nSides;
1746 glVertex2d(center.x() +r, center.y());
1748 for (int i=1; i<nSides; ++i) {
1749 glVertex2d(center.x() + (cos(t) * r), center.y() + (sin(t) * r));
1755 void MapWidget::squareAt(const SGVec2d& center, double r)
1757 glBegin(GL_LINE_LOOP);
1758 glVertex2d(center.x() + r, center.y() + r);
1759 glVertex2d(center.x() + r, center.y() - r);
1760 glVertex2d(center.x() - r, center.y() - r);
1761 glVertex2d(center.x() - r, center.y() + r);
1765 void MapWidget::circleAtAlt(const SGVec2d& center, int nSides, double r, double r2)
1767 glBegin(GL_LINE_LOOP);
1768 double advance = (SGD_PI * 2) / nSides;
1769 glVertex2d(center.x(), center.y() + r);
1771 for (int i=1; i<nSides; ++i) {
1772 double rr = (i%2 == 0) ? r : r2;
1773 glVertex2d(center.x() + (sin(t) * rr), center.y() + (cos(t) * rr));
1779 void MapWidget::drawLine(const SGVec2d& p1, const SGVec2d& p2)
1782 glVertex2dv(p1.data());
1783 glVertex2dv(p2.data());
1787 void MapWidget::drawLegendBox(const SGVec2d& pos, const std::string& t)
1789 std::vector<std::string> lines(simgear::strutils::split(t, "\n"));
1790 const int LINE_LEADING = 4;
1791 const int MARGIN = 4;
1794 int maxWidth = -1, totalHeight = 0;
1795 int lineHeight = legendFont.getStringHeight();
1797 for (unsigned int ln=0; ln<lines.size(); ++ln) {
1798 totalHeight += lineHeight;
1800 totalHeight += LINE_LEADING;
1803 int lw = legendFont.getStringWidth(lines[ln].c_str());
1804 maxWidth = std::max(maxWidth, lw);
1805 } // of line measurement
1808 return; // all lines are empty, don't draw
1811 totalHeight += MARGIN * 2;
1816 box.min[1] = -totalHeight;
1817 box.max[0] = maxWidth + (MARGIN * 2);
1820 box.draw (pos.x(), pos.y(), PUSTYLE_DROPSHADOW, colour, FALSE, border);
1823 int xPos = pos.x() + MARGIN;
1824 int yPos = pos.y() - (lineHeight + MARGIN);
1825 glColor3f(0.8, 0.8, 0.8);
1827 for (unsigned int ln=0; ln<lines.size(); ++ln) {
1828 legendFont.drawString(lines[ln].c_str(), xPos, yPos);
1829 yPos -= lineHeight + LINE_LEADING;
1833 void MapWidget::drawData()
1835 std::sort(_dataQueue.begin(), _dataQueue.end(), MapData::order);
1837 int hw = _width >> 1,
1839 puBox visBox(makePuBox(-hw, -hh, _width, _height));
1843 std::vector<MapData*> drawQueue;
1845 bool drawData = _root->getBoolValue("draw-data");
1846 const int MAX_DRAW_DATA = 25;
1847 const int MAX_DRAW = 50;
1849 for (; (d < _dataQueue.size()) && (drawn < MAX_DRAW); ++d) {
1850 MapData* md = _dataQueue[d];
1851 md->setDataVisible(drawData);
1853 if (md->isClipped(visBox)) {
1857 if (md->overlaps(drawQueue)) {
1858 if (drawData) { // overlapped with data, let's try just the label
1859 md->setDataVisible(false);
1860 if (md->overlaps(drawQueue)) {
1866 } // of overlaps case
1868 drawQueue.push_back(md);
1870 if (drawData && (drawn >= MAX_DRAW_DATA)) {
1875 // draw lowest-priority first, so higher-priorty items appear on top
1876 std::vector<MapData*>::reverse_iterator r;
1877 for (r = drawQueue.rbegin(); r!= drawQueue.rend(); ++r) {
1882 KeyDataMap::iterator it = _mapData.begin();
1883 for (; it != _mapData.end(); ) {
1885 if (it->second->isExpired()) {
1887 KeyDataMap::iterator cur = it++;
1888 _mapData.erase(cur);
1892 } // of expiry iteration
1895 bool MapWidget::validDataForKey(void* key)
1897 KeyDataMap::iterator it = _mapData.find(key);
1898 if (it == _mapData.end()) {
1899 return false; // no valid data for the key!
1902 it->second->resetAge(); // mark data as valid this frame
1903 _dataQueue.push_back(it->second);
1907 void MapWidget::setAnchorForKey(void* key, const SGVec2d& anchor)
1909 KeyDataMap::iterator it = _mapData.find(key);
1910 if (it == _mapData.end()) {
1911 throw sg_exception("no valid data for key!");
1914 it->second->setAnchor(anchor);
1917 MapData* MapWidget::getOrCreateDataForKey(void* key)
1919 KeyDataMap::iterator it = _mapData.find(key);
1920 if (it == _mapData.end()) {
1921 return createDataForKey(key);
1924 it->second->resetAge(); // mark data as valid this frame
1925 _dataQueue.push_back(it->second);
1929 MapData* MapWidget::createDataForKey(void* key)
1931 KeyDataMap::iterator it = _mapData.find(key);
1932 if (it != _mapData.end()) {
1933 throw sg_exception("duplicate data requested for key!");
1936 MapData* d = new MapData(0);
1938 _dataQueue.push_back(d);
1943 void MapWidget::clearData()
1945 KeyDataMap::iterator it = _mapData.begin();
1946 for (; it != _mapData.end(); ++it) {
1953 int MapWidget::displayHeading(double h) const
1955 if (_magneticHeadings) {
1956 h -= _magVar->get_magvar() * SG_RADIANS_TO_DEGREES;
1959 SG_NORMALIZE_RANGE(h, 0.0, 360.0);
1960 return SGMiscd::roundToInt(h);