]> git.mxchange.org Git - flightgear.git/blobdiff - src/Instrumentation/KLN89/kln89_page_apt.cxx
Give the FGAirport class a sane filename.
[flightgear.git] / src / Instrumentation / KLN89 / kln89_page_apt.cxx
index db4a8c115fcb611c15f1312692e521656b0f52d6..52a0778c319d202a75279171ae4ca1b15b3de47b 100644 (file)
@@ -3,7 +3,7 @@
 //
 // Written by David Luff, started 2005.
 //
-// Copyright (C) 2005 - David C Luff - david.luff@nottingham.ac.uk
+// Copyright (C) 2005 - David C Luff - daveluff AT ntlworld.com
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
 #endif
 
 #include "kln89_page_apt.hxx"
-#include <ATCDCL/commlist.hxx>
+
+#include <simgear/structure/exception.hxx>
+#include <cassert>
+#include <cstdio>
+
+#include <ATC/CommStation.hxx>
 #include <Main/globals.hxx>
 #include <Airports/runways.hxx>
-#include <Airports/simple.hxx>
+#include <Airports/airport.hxx>
 
-// This function is copied from Airports/runways.cxx
-// TODO - Make the original properly available and remove this instance!!!!
-// Return reverse rwy number
-// eg 01 -> 19
-// 03L -> 21R
-static string GetReverseRunwayNo(string rwyno) {       
-    // cout << "Original rwyno = " << rwyNo << '\n';
-    
-    // standardize input number
-    string tmp = rwyno.substr(1, 1);
-    if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (rwyno.size() == 1)) {
-               tmp = rwyno;
-               rwyno = "0" + tmp;
-               SG_LOG( SG_GENERAL, SG_INFO,
-                       "Standardising rwy number from " << tmp << " to " << rwyno );
-    }
-    
-    char buf[4];
-    int rn = atoi(rwyno.substr(0,2).c_str());
-    rn += 18;
-    while(rn > 36) {
-               rn -= 36;
-    }
-    sprintf(buf, "%02i", rn);
-    if(rwyno.size() == 3) {
-               if(rwyno.substr(2,1) == "L") {
-                       buf[2] = 'R';
-                       buf[3] = '\0';
-               } else if (rwyno.substr(2,1) == "R") {
-                       buf[2] = 'L';
-                       buf[3] = '\0';
-               } else if (rwyno.substr(2,1) == "C") {
-                       buf[2] = 'C';
-                       buf[3] = '\0';
-               } else if (rwyno.substr(2,1) == "T") {
-                       buf[2] = 'T';
-                       buf[3] = '\0';
-               } else {
-                       SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
-                       << rwyno << " passed to GetReverseRunwayNo(...)");
-               }
-    }
-    return(buf);
-}
+using std::string;
 
 KLN89AptPage::KLN89AptPage(KLN89* parent) 
 : KLN89Page(parent) {
@@ -227,7 +189,8 @@ void KLN89AptPage::Update(double dt) {
                                string s = _aptRwys[i]->ident();
                                _kln89->DrawText(s, 2, 9, 3);
                                _kln89->DrawText("/", 2, 12, 3);
-                               _kln89->DrawText(GetReverseRunwayNo(s), 2, 13, 3);
+                string recipIdent = _aptRwys[i]->reciprocalRunway()->ident();
+                               _kln89->DrawText(recipIdent, 2, 13, 3);
                                // Length
                                s = GPSitoa(int(float(_aptRwys[i]->lengthFt()) * (_kln89->_altUnits == GPS_ALT_UNITS_FT ? 1.0 : SG_FEET_TO_METER) + 0.5));
                                _kln89->DrawText(s, 2, 5 - s.size(), 2);
@@ -275,7 +238,8 @@ void KLN89AptPage::Update(double dt) {
                                string s = _aptRwys[i]->ident();
                                _kln89->DrawText(s, 2, 9, 1);
                                _kln89->DrawText("/", 2, 12, 1);
-                               _kln89->DrawText(GetReverseRunwayNo(s), 2, 13, 1);
+                string recip = _aptRwys[i]->reciprocalRunway()->ident();
+                               _kln89->DrawText(recip, 2, 13, 1);
                                // Length
                                s = GPSitoa(int(float(_aptRwys[i]->lengthFt()) * (_kln89->_altUnits == GPS_ALT_UNITS_FT ? 1.0 : SG_FEET_TO_METER) + 0.5));
                                _kln89->DrawText(s, 2, 5 - s.size(), 0);
@@ -411,24 +375,35 @@ void KLN89AptPage::Update(double dt) {
                                        }
                                } else {
                                        _kln89->DrawText("IAP", 2, 11, 3);
-                                       int check = 0;
                                        bool selApp = false;
                                        if(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos > 4) {
                                                selApp = true;
                                                if(!_kln89->_blink) _kln89->DrawEnt();
                                        }
-                                       for(unsigned int i=0; i<_iaps.size(); ++i) {    // TODO - do this properly when > 3 IAPs
-                                               string s = GPSitoa(i+1);
-                                               _kln89->DrawText(s, 2, 2 - s.size(), 2-i);
-                                               if(!(selApp && _uLinePos == 5+i && _kln89->_blink)) {
-                                                       _kln89->DrawText(_iaps[i]->_ident, 2, 3, 2-i);
-                                                       _kln89->DrawText(_iaps[i]->_rwyStr, 2, 9, 2-i);
-                                               }
-                                               if(selApp && _uLinePos == 5+i && !_kln89->_blink) {
-                                                       _kln89->Underline(2, 3, 2-i, 9);
+                                       // _maxULine pos should be 4 + iaps.size() at this point.
+                                       // Draw a maximum of 3 IAPs.
+                                       // If there are more than 3 IAPs for this airport, then we need to offset the start
+                                       // of the list if _uLinePos is pointing at the 4th or later IAP.
+                                       unsigned int offset = 0;
+                                       unsigned int index;
+                                       if(_uLinePos > 7) {
+                                               offset = _uLinePos - 7;
+                                       }
+                                       for(unsigned int i=0; i<3; ++i) {
+                                               index = offset + i;
+                                               if(index < _iaps.size()) {
+                                                       string s = GPSitoa(index+1);
+                                                       _kln89->DrawText(s, 2, 2 - s.size(), 2-i);
+                                                       if(!(selApp && _uLinePos == index+5 && _kln89->_blink)) {
+                                                               _kln89->DrawText(_iaps[index]->_ident, 2, 3, 2-i);
+                                                               _kln89->DrawText(_iaps[index]->_rwyStr, 2, 9, 2-i);
+                                                       }
+                                                       if(selApp && _uLinePos == index+5 && !_kln89->_blink) {
+                                                               _kln89->Underline(2, 3, 2-i, 9);
+                                                       }
+                                               } else {
+                                                       break;
                                                }
-                                               check++;
-                                               if(check > 2) break;
                                        }
                                }
                        }
@@ -485,38 +460,35 @@ void KLN89AptPage::SetId(const string& s) {
 // Update the cached airport details
 void KLN89AptPage::UpdateAirport(const string& id) {
        // Frequencies
-       _aptFreqs.clear();
-       ATCData ad;
-       AptFreq aq;
-       //cout << "UpdateAirport called, id = " << id << '\n';
-       // TODO - the logic below only returns one service per type per airport - they can be on more than one freq though.
-       if(current_commlist->FindByCode(id, ad, ATIS)) {
-               //cout << "Found ATIS\n";
-               aq.service = "ATIS*";
-               aq.freq = ad.freq;
-               _aptFreqs.push_back(aq);
-       }
-       if(current_commlist->FindByCode(id, ad, GROUND)) {
-               aq.service = "GRND*";
-               aq.freq = ad.freq;
-               _aptFreqs.push_back(aq);
-       }
-       if(current_commlist->FindByCode(id, ad, TOWER)) {
-               aq.service = "TWR *";
-               aq.freq = ad.freq;
-               _aptFreqs.push_back(aq);
-       }
-       if(current_commlist->FindByCode(id, ad, APPROACH)) {
-               aq.service = "APR";
-               aq.freq = ad.freq;
-               _aptFreqs.push_back(aq);
-       }
+       _aptFreqs.clear();      
+       
+    const FGAirport* apt = fgFindAirportID(id);
+    if (!apt) {
+        throw sg_exception("UpdateAirport: unknown airport id " + id);
+    }
+       
+    for (unsigned int c=0; c<apt->commStations().size(); ++c) {
+        flightgear::CommStation* comm = apt->commStations()[c];
+        AptFreq aq;
+        aq.freq = comm->freqKHz();
+        switch (comm->type()) {
+        case FGPositioned::FREQ_ATIS:
+            aq.service = "ATIS*"; break;
+        case FGPositioned::FREQ_GROUND:
+            aq.service = "GRND*"; break;
+        case FGPositioned::FREQ_TOWER:
+            aq.service = "TWR *"; break;
+        case FGPositioned::FREQ_APP_DEP:
+            aq.service = "APR *"; break;
+        default:
+            continue;
+        }
+    }
+
        _nFreqPages = (unsigned int)ceil((float(_aptFreqs.size())) / 3.0f);
        
        // Runways
        _aptRwys.clear();
-  const FGAirport* apt = fgFindAirportID(id);
-  assert(apt);
   
   // build local array, longest runway first
   for (unsigned int r=0; r<apt->numRunways(); ++r) {
@@ -632,8 +604,12 @@ void KLN89AptPage::ClrPressed() {
 void KLN89AptPage::EntPressed() {
        if(_entInvert) {
                _entInvert = false;
-               _last_apt_id = _apt_id;
-               _apt_id = _save_apt_id;
+               if(_kln89->_dtoReview) {
+                       _kln89->DtoInitiate(_apt_id);
+               } else {
+                       _last_apt_id = _apt_id;
+                       _apt_id = _save_apt_id;
+               }
        } else if(_subPage == 7 && _kln89->_mode == KLN89_MODE_CRSR && _uLinePos > 0) {
                // We are selecting an approach
                if(_iafDialog) {
@@ -650,7 +626,6 @@ void KLN89AptPage::EntPressed() {
                                GPSWaypoint* wp = new GPSWaypoint;
                                *wp = *(_approachRoutes[_curIaf]->waypoints[0]);        // Need to make copies here since we're going to alter ID and type sometimes
                                string iafid = wp->id;
-                               //wp->id += 'i';
                                _kln89->_approachFP->waypoints.push_back(wp);
                                for(unsigned int i=0; i<_IAP.size(); ++i) {
                                        if(_IAP[i]->id != iafid) {      // Don't duplicate waypoints that are part of the initial fix list and the approach procedure list.
@@ -666,11 +641,6 @@ void KLN89AptPage::EntPressed() {
                                                _kln89->_approachFP->waypoints.push_back(wp);
                                        }
                                }
-                               // Only add 1 missed approach procedure waypoint for now.  I think this might be standard always anyway.
-                               wp = new GPSWaypoint;
-                               *wp = *_MAP[0];
-                               //wp->id += 'h';
-                               _kln89->_approachFP->waypoints.push_back(wp);
                                _iafDialog = false;
                                _addDialog = true;
                                _maxULinePos = _kln89->_approachFP->waypoints.size() + 1;
@@ -719,11 +689,9 @@ void KLN89AptPage::EntPressed() {
                } else if(_uLinePos > 4) {
                        _approachRoutes.clear();
                        _IAP.clear();
-                       _MAP.clear();
                        _curIaf = 0;
                        _approachRoutes = ((FGNPIAP*)(_iaps[_uLinePos-5]))->_approachRoutes;
                        _IAP = ((FGNPIAP*)(_iaps[_uLinePos-5]))->_IAP;
-                       _MAP = ((FGNPIAP*)(_iaps[_uLinePos-5]))->_MAP;
                        _curIap = _uLinePos - 5;        // TODO - handle the start of list ! no. 1, and the end of list not sequential!
                        _uLinePos = 1;
                        if(_approachRoutes.size() > 1) {
@@ -749,12 +717,6 @@ void KLN89AptPage::EntPressed() {
                                                _kln89->_approachFP->waypoints.push_back(wp);
                                        }
                                }
-                               // Only add 1 missed approach procedure waypoint for now.  I think this might be standard always anyway.
-                               wp = new GPSWaypoint;
-                               *wp = *_MAP[0];
-                               //wp->id += 'h';
-                               _kln89->_approachFP->waypoints.push_back(wp);
-                               
                                _addDialog = true;
                                _maxULinePos = 1;
                        }
@@ -826,6 +788,15 @@ void KLN89AptPage::Knob2Left1() {
                        } else {
                                _curRwyPage--;
                        }
+               } else if(_subPage == 0) {
+                       _subPage = 7;
+                       // We have to set _uLinePos here even though the cursor isn't pressed, to
+                       // ensure that the list displays properly.
+                       if(_iaps.empty()) {
+                               _uLinePos = 1;
+                       } else {
+                               _uLinePos = 5;
+                       }
                } else {
                        KLN89Page::Knob2Left1();
                }
@@ -869,6 +840,15 @@ void KLN89AptPage::Knob2Right1() {
                        } else {
                                _curFreqPage++;
                        }
+               } else if(_subPage == 6) {
+                       _subPage = 7;
+                       // We have to set _uLinePos here even though the cursor isn't pressed, to
+                       // ensure that the list displays properly.
+                       if(_iaps.empty()) {
+                               _uLinePos = 1;
+                       } else {
+                               _uLinePos = 5;
+                       }
                } else {
                        KLN89Page::Knob2Right1();
                }