]> git.mxchange.org Git - flightgear.git/blobdiff - src/Autopilot/auto_gui.cxx
The code to find the highest hit below you didn't work quite right when
[flightgear.git] / src / Autopilot / auto_gui.cxx
index 585689094404783120a49ed808b4b347bde763e3..51363288ceb0106b145950c815c9b23f1c8abcbe 100644 (file)
@@ -28,6 +28,8 @@
 
 #include <simgear/compiler.h>
 
+#include <simgear/route/route.hxx>
+
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
@@ -313,7 +315,7 @@ void NewAltitudeInit()
 
     float alt = cur_fdm_state->get_Altitude();
 
-    if ( fgGetString("/sim/startup/units") == "meters") {
+    if ( !strcmp(fgGetString("/sim/startup/units"), "meters")) {
        alt *= SG_FEET_TO_METER;
     }
 
@@ -606,63 +608,88 @@ void TgtAptDialog_OK (puObject *)
     TgtAptDialogInput->getValue(&s);
 
     string tmp = s;
-    double alt = 0.0;
     unsigned int pos = tmp.find( "@" );
     if ( pos != string::npos ) {
        TgtAptId = tmp.substr( 0, pos );
-       string alt_str = tmp.substr( pos + 1 );
-       alt = atof( alt_str.c_str() );
-       if ( fgGetString("/sim/startup/units") == "feet" ) {
-           alt *= SG_FEET_TO_METER;
-       }
     } else {
        TgtAptId = tmp;
     }
 
     TgtAptDialog_Cancel( NULL );
     
-    FGAirport a;
-    FGFix f;
-    double t1, t2;
-    if ( fgFindAirportID( TgtAptId, &a ) ) {
+    /* s = input string, either 'FIX' or FIX@4000' */
+    /* TgtAptId is name of fix only; may get appended to below */
 
-        SG_LOG( SG_GENERAL, SG_INFO,
-                "Adding waypoint (airport) = " << TgtAptId );
-        
-        sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
+    if ( NewWaypoint( TgtAptId ) == 0)
+    {
+        TgtAptId  += " not in database.";
+        mkDialog(TgtAptId.c_str());
+    }
+}
+
+/* add new waypoint (either from above popup window 'ok button or telnet session) */
+
+int NewWaypoint( string Tgt_Alt )
+{
+  string TgtAptId;
+  FGAirport a;
+  FGFix f;
+
+  double alt = 0.0;
+  unsigned int pos = Tgt_Alt.find( "@" );
+  if ( pos != string::npos ) {
+    TgtAptId = Tgt_Alt.substr( 0, pos );
+    string alt_str = Tgt_Alt.substr( pos + 1 );
+    alt = atof( alt_str.c_str() );
+    if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
+      alt *= SG_FEET_TO_METER;
+    }
+  } else {
+    TgtAptId = Tgt_Alt;
+  }
+
+  if ( fgFindAirportID( TgtAptId, &a ) ) {
 
-        SGWayPoint wp( a.longitude, a.latitude, alt,
-                       SGWayPoint::WGS84, TgtAptId );
-        globals->get_route()->add_waypoint( wp );
+    SG_LOG( SG_GENERAL, SG_INFO,
+                 "Adding waypoint (airport) = " << TgtAptId );
+
+    sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
+
+    SGWayPoint wp( a.longitude, a.latitude, alt,
+                        SGWayPoint::WGS84, TgtAptId );
+    globals->get_route()->add_waypoint( wp );
 
          /* and turn on the autopilot */
-         globals->get_autopilot()->set_HeadingEnabled( true );
-         globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_HEADING_WAYPOINT );
+    globals->get_autopilot()->set_HeadingEnabled( true );
+    globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_HEADING_WAYPOINT
+);
 
-    } else if ( current_fixlist->query( TgtAptId, 0.0, 0.0, 0.0,
-                                       &f, &t1, &t2 ) )
-    {
-        SG_LOG( SG_GENERAL, SG_INFO,
-                "Adding waypoint (fix) = " << TgtAptId );
-        
-        sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
+    return 1;
+
+  } else if ( current_fixlist->query( TgtAptId, &f ) )
+  {
+   SG_LOG( SG_GENERAL, SG_INFO,
+                 "Adding waypoint (fix) = " << TgtAptId );
+
+    sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
 
-        SGWayPoint wp( f.get_lon(), f.get_lat(), alt,
-                       SGWayPoint::WGS84, TgtAptId );
-        globals->get_route()->add_waypoint( wp );
+    SGWayPoint wp( f.get_lon(), f.get_lat(), alt,
+                        SGWayPoint::WGS84, TgtAptId );
+    globals->get_route()->add_waypoint( wp );
 
          /* and turn on the autopilot */
-         globals->get_autopilot()->set_HeadingEnabled( true );
-         globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_HEADING_WAYPOINT );
-    } else {
-       TgtAptId  += " not in database.";
-       mkDialog(TgtAptId.c_str());
-    }
+   globals->get_autopilot()->set_HeadingEnabled( true );
+   globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_HEADING_WAYPOINT );
+   return 2;
+
+  }
+  else return 0;
 }
 
+
 void TgtAptDialog_Reset(puObject *)
 {
-    sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id").c_str() );
+    sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
     TgtAptDialogInput->setValue ( NewTgtAirportId );
     TgtAptDialogInput->setCursor( 0 ) ;
 }
@@ -703,7 +730,7 @@ void TgtAptDialog_HandleArrow( puObject *arrow )
 
 void AddWayPoint(puObject *cb)
 {
-    sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id").c_str() );
+    sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
     TgtAptDialogInput->setValue( NewTgtAirportId );
     
     /* refresh waypoint list */
@@ -721,7 +748,7 @@ void AddWayPoint(puObject *cb)
         WPList = new char* [ WPListsize + 1 ];
         for (i = 0; i < globals->get_route()->size(); i++ ) {
            sprintf(WPString, "%5s %3.2flon %3.2flat", globals->get_route()->get_waypoint(i).get_id().c_str(), globals->get_route()->get_waypoint(i).get_target_lon(), globals->get_route()->get_waypoint(i).get_target_lat());
-           WPList [i] = new char[ strlen(WPString) ];
+           WPList [i] = new char[ strlen(WPString)+1 ];
            strcpy ( WPList [i], WPString );
         }
     } else {
@@ -774,11 +801,8 @@ void ClearRoute(puObject *cb)
 void NewTgtAirportInit()
 {
     SG_LOG( SG_AUTOPILOT, SG_INFO, " enter NewTgtAirportInit()" );
-    sprintf( NewTgtAirportId, "%s",
-             fgGetString("/sim/startup/airport-id").c_str() );
+    sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
     SG_LOG( SG_AUTOPILOT, SG_INFO, " NewTgtAirportId " << NewTgtAirportId );
-    int len = 150
-        - puGetDefaultLabelFont().getStringWidth( NewTgtAirportLabel ) / 2;
     
     TgtAptDialog = new puDialogBox (150, 350);
     {
@@ -826,7 +850,7 @@ void NewTgtAirportInit()
     }
 
     FG_FINALIZE_PUI_DIALOG( TgtAptDialog );
-    printf("leave NewTgtAirportInit()");
+    SG_LOG(SG_GENERAL, SG_DEBUG, "leave NewTgtAirportInit()");
 }