1 // auto_gui.cxx -- autopilot gui interface
3 // Written by Norman Vine <nhv@cape.com>
4 // Arranged by Curt Olson <curt@flightgear.org>
6 // Copyright (C) 1998 - 2000
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <simgear/compiler.h>
37 #include <Aircraft/aircraft.hxx>
38 #include <FDM/flight.hxx>
39 #include <Controls/controls.hxx>
40 #include <Scenery/scenery.hxx>
42 #include <simgear/constants.h>
43 #include <simgear/sg_inlines.h>
44 #include <simgear/debug/logstream.hxx>
45 #include <simgear/math/sg_geodesy.hxx>
46 #include <simgear/misc/sg_path.hxx>
48 #include <Airports/simple.hxx>
50 #include <Main/fg_init.hxx>
51 #include <Main/globals.hxx>
52 #include <Main/fg_props.hxx>
53 #include <Navaids/fixlist.hxx>
55 #include "auto_gui.hxx"
56 #include "route_mgr.hxx"
57 #include "xmlauto.hxx"
62 #define mySlider puSlider
64 // Climb speed constants
65 const double min_climb = 70.0; // kts
66 const double best_climb = 75.0; // kts
67 const double ideal_climb_rate = 500.0; // fpm
69 /// These statics will eventually go into the class
70 /// they are just here while I am experimenting -- NHV :-)
71 // AutoPilot Gain Adjuster members
72 static double MaxRollAdjust; // MaxRollAdjust = 2 * APData->MaxRoll;
73 static double RollOutAdjust; // RollOutAdjust = 2 * APData->RollOut;
74 static double MaxAileronAdjust; // MaxAileronAdjust = 2 * APData->MaxAileron;
75 static double RollOutSmoothAdjust; // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
77 static float MaxRollValue; // 0.1 -> 1.0
78 static float RollOutValue;
79 static float MaxAileronValue;
80 static float RollOutSmoothValue;
82 static float TmpMaxRollValue; // for cancel operation
83 static float TmpRollOutValue;
84 static float TmpMaxAileronValue;
85 static float TmpRollOutSmoothValue;
87 static puDialogBox *APAdjustDialog;
88 static puFrame *APAdjustFrame;
89 static puText *APAdjustDialogMessage;
90 static puFont APAdjustLegendFont;
91 static puFont APAdjustLabelFont;
93 static puOneShot *APAdjustOkButton;
94 static puOneShot *APAdjustResetButton;
95 static puOneShot *APAdjustCancelButton;
97 //static puButton *APAdjustDragButton;
99 static puText *APAdjustMaxRollTitle;
100 static puText *APAdjustRollOutTitle;
101 static puText *APAdjustMaxAileronTitle;
102 static puText *APAdjustRollOutSmoothTitle;
104 static puText *APAdjustMaxAileronText;
105 static puText *APAdjustMaxRollText;
106 static puText *APAdjustRollOutText;
107 static puText *APAdjustRollOutSmoothText;
109 static mySlider *APAdjustHS0;
110 static mySlider *APAdjustHS1;
111 static mySlider *APAdjustHS2;
112 static mySlider *APAdjustHS3;
114 static char SliderText[ 4 ][ 8 ];
116 ///////// AutoPilot New Heading Dialog
118 static puDialogBox *ApHeadingDialog;
119 static puFrame *ApHeadingDialogFrame;
120 static puText *ApHeadingDialogMessage;
121 static puInput *ApHeadingDialogInput;
122 static puOneShot *ApHeadingDialogOkButton;
123 static puOneShot *ApHeadingDialogCancelButton;
126 ///////// AutoPilot New Altitude Dialog
128 static puDialogBox *ApAltitudeDialog = 0;
129 static puFrame *ApAltitudeDialogFrame = 0;
130 static puText *ApAltitudeDialogMessage = 0;
131 static puInput *ApAltitudeDialogInput = 0;
133 static puOneShot *ApAltitudeDialogOkButton = 0;
134 static puOneShot *ApAltitudeDialogCancelButton = 0;
137 /// The beginnings of Lock AutoPilot to target location :-)
138 // Needs cleaning up but works
139 // These statics should disapear when this is a class
140 static puDialogBox *TgtAptDialog = 0;
141 static puFrame *TgtAptDialogFrame = 0;
142 static puText *TgtAptDialogMessage = 0;
143 static puInput *TgtAptDialogInput = 0;
144 static puListBox *TgtAptDialogWPList = 0;
145 static puSlider *TgtAptDialogSlider = 0;
146 static puArrowButton *TgtAptDialogUPArrow = 0;
147 static puArrowButton *TgtAptDialogDNArrow = 0;
148 static char** WPList;
149 static int WPListsize;
151 static char NewTgtAirportId[16];
152 static char NewTgtAirportLabel[] = "New Apt/Fix ID";
154 static puOneShot *TgtAptDialogOkButton = 0;
155 static puOneShot *TgtAptDialogCancelButton = 0;
156 static puOneShot *TgtAptDialogResetButton = 0;
159 // extern char *coord_format_lat(float);
160 // extern char *coord_format_lon(float);
162 // THIS NEEDS IMPROVEMENT !!!!!!!!!!!!!
163 static int scan_number(char *s, double *new_value)
168 char *WordBufPtr = WordBuf;
172 *WordBufPtr++ = *cptr++;
174 while (isdigit(*cptr) ) {
175 *WordBufPtr++ = *cptr++;
179 *WordBufPtr++ = *cptr++; // put the '.' into the string
180 while (isdigit(*cptr)) {
181 *WordBufPtr++ = *cptr++;
186 sscanf(WordBuf, "%lf", new_value);
193 void ApHeadingDialog_Cancel(puObject *)
195 ApHeadingDialogInput->rejectInput();
196 FG_POP_PUI_DIALOG( ApHeadingDialog );
199 void ApHeadingDialog_OK (puObject *me)
204 ApHeadingDialogInput -> getValue( &c );
208 if( scan_number( c, &NewHeading ) ) {
209 fgSetString( "/autopilot/locks/heading", "dg-heading-hold" );
210 fgSetDouble( "/autopilot/settings/heading-bug-deg",
215 s += " is not a valid number.";
218 ApHeadingDialog_Cancel(me);
219 if ( error ) mkDialog(s.c_str());
222 void NewHeading(puObject *cb)
224 // string ApHeadingLabel( "Enter New Heading" );
225 // ApHeadingDialogMessage -> setLabel(ApHeadingLabel.c_str());
226 float heading = fgGetDouble( "/autopilot/settings/heading-bug-deg" );
227 while ( heading < 0.0 ) { heading += 360.0; }
228 ApHeadingDialogInput -> setValue ( heading );
229 ApHeadingDialogInput -> acceptInput();
230 FG_PUSH_PUI_DIALOG( ApHeadingDialog );
233 void NewHeadingInit()
235 // printf("NewHeadingInit\n");
236 char NewHeadingLabel[] = "Enter New Heading";
239 float heading = fgGetDouble("/orientation/heading-deg");
241 (puGetDefaultLabelFont().getStringWidth( NewHeadingLabel ) / 2 );
243 ApHeadingDialog = new puDialogBox (150, 50);
245 ApHeadingDialogFrame = new puFrame (0, 0, 260, 150);
247 ApHeadingDialogMessage = new puText (len, 110);
248 ApHeadingDialogMessage -> setDefaultValue (NewHeadingLabel);
249 ApHeadingDialogMessage -> getDefaultValue (&s);
250 ApHeadingDialogMessage -> setLabel (s);
252 ApHeadingDialogInput = new puInput ( 50, 70, 210, 100 );
253 ApHeadingDialogInput -> setValue ( heading );
255 ApHeadingDialogOkButton = new puOneShot (50, 10, 110, 50);
256 ApHeadingDialogOkButton -> setLegend (gui_msg_OK);
257 ApHeadingDialogOkButton -> makeReturnDefault (TRUE);
258 ApHeadingDialogOkButton -> setCallback (ApHeadingDialog_OK);
260 ApHeadingDialogCancelButton = new puOneShot (140, 10, 210, 50);
261 ApHeadingDialogCancelButton -> setLegend (gui_msg_CANCEL);
262 ApHeadingDialogCancelButton -> setCallback (ApHeadingDialog_Cancel);
265 FG_FINALIZE_PUI_DIALOG( ApHeadingDialog );
268 void ApAltitudeDialog_Cancel(puObject *)
270 ApAltitudeDialogInput -> rejectInput();
271 FG_POP_PUI_DIALOG( ApAltitudeDialog );
274 void ApAltitudeDialog_OK (puObject *me)
279 ApAltitudeDialogInput->getValue( &c );
283 if ( scan_number( c, &NewAltitude) ) {
284 fgSetString( "/autopilot/locks/altitude", "altitude-hold" );
285 fgSetDouble( "/autopilot/settings/altitude-ft", NewAltitude );
289 s += " is not a valid number.";
292 ApAltitudeDialog_Cancel(me);
293 if( error ) mkDialog(s.c_str());
296 void NewAltitude(puObject *cb)
298 float altitude = fgGetDouble("/autopilot/settings/altitude-ft")
300 ApAltitudeDialogInput -> setValue( altitude );
301 ApAltitudeDialogInput -> acceptInput();
302 FG_PUSH_PUI_DIALOG( ApAltitudeDialog );
305 void NewAltitudeInit()
307 // printf("NewAltitudeInit\n");
308 char NewAltitudeLabel[] = "Enter New Altitude";
311 float alt = cur_fdm_state->get_Altitude();
313 if ( !strcmp(fgGetString("/sim/startup/units"), "meters")) {
314 alt *= SG_FEET_TO_METER;
318 (puGetDefaultLabelFont().getStringWidth( NewAltitudeLabel ) / 2);
320 // ApAltitudeDialog = new puDialogBox (150, 50);
321 ApAltitudeDialog = new puDialogBox (150, 200);
323 ApAltitudeDialogFrame = new puFrame (0, 0, 260, 150);
324 ApAltitudeDialogMessage = new puText (len, 110);
325 ApAltitudeDialogMessage -> setDefaultValue (NewAltitudeLabel);
326 ApAltitudeDialogMessage -> getDefaultValue (&s);
327 ApAltitudeDialogMessage -> setLabel (s);
329 ApAltitudeDialogInput = new puInput ( 50, 70, 210, 100 );
330 ApAltitudeDialogInput -> setValue ( alt );
331 // Uncomment the next line to have input active on startup
332 // ApAltitudeDialogInput -> acceptInput ( );
333 // cursor at begining or end of line ?
336 // ApAltitudeDialogInput -> setCursor ( len );
337 // ApAltitudeDialogInput -> setSelectRegion ( 5, 9 );
339 ApAltitudeDialogOkButton = new puOneShot (50, 10, 110, 50);
340 ApAltitudeDialogOkButton -> setLegend (gui_msg_OK);
341 ApAltitudeDialogOkButton -> makeReturnDefault (TRUE);
342 ApAltitudeDialogOkButton -> setCallback (ApAltitudeDialog_OK);
344 ApAltitudeDialogCancelButton = new puOneShot (140, 10, 210, 50);
345 ApAltitudeDialogCancelButton -> setLegend (gui_msg_CANCEL);
346 ApAltitudeDialogCancelButton -> setCallback (ApAltitudeDialog_Cancel);
349 FG_FINALIZE_PUI_DIALOG( ApAltitudeDialog );
353 static void maxroll_adj( puObject *hs ) {
356 hs-> getValue ( &val ) ;
357 SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
358 // printf ( "maxroll_adj( %p ) %f %f\n", hs, val, MaxRollAdjust * val ) ;
359 fgSetDouble( "/autopilot/config/max-roll-deg", MaxRollAdjust * val );
360 sprintf( SliderText[ 0 ], "%05.2f",
361 fgGetDouble("/autopilot/config/max-roll-deg") );
362 APAdjustMaxRollText -> setLabel ( SliderText[ 0 ] ) ;
365 static void rollout_adj( puObject *hs ) {
368 hs-> getValue ( &val ) ;
369 SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
370 // printf ( "rollout_adj( %p ) %f %f\n", hs, val, RollOutAdjust * val ) ;
371 fgSetDouble( "/autopilot/config/roll-out-deg", RollOutAdjust * val );
372 sprintf( SliderText[ 1 ], "%05.2f",
373 fgGetDouble("/autopilot/config/roll-out-deg") );
374 APAdjustRollOutText -> setLabel ( SliderText[ 1 ] );
377 static void maxaileron_adj( puObject *hs ) {
380 hs-> getValue ( &val ) ;
381 SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
382 // printf ( "maxaileron_adj( %p ) %f %f\n", hs, val, MaxAileronAdjust * val ) ;
383 fgSetDouble( "/autopilot/config/max-aileron", MaxAileronAdjust * val );
384 sprintf( SliderText[ 3 ], "%05.2f",
385 fgGetDouble("/autopilot/config/max-aileron") );
386 APAdjustMaxAileronText -> setLabel ( SliderText[ 3 ] );
389 static void rolloutsmooth_adj( puObject *hs ) {
392 hs -> getValue ( &val ) ;
393 SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
394 // printf ( "rolloutsmooth_adj( %p ) %f %f\n", hs, val, RollOutSmoothAdjust * val ) ;
395 fgSetDouble( "/autopilot/config/roll-out-smooth-deg",
396 RollOutSmoothAdjust * val );
397 sprintf( SliderText[ 2 ], "%5.2f",
398 fgGetDouble("/autopilot/config/roll-out-smooth-deg") );
399 APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
403 static void goAwayAPAdjust (puObject *)
405 FG_POP_PUI_DIALOG( APAdjustDialog );
408 void cancelAPAdjust( puObject *self ) {
409 fgSetDouble( "/autopilot/config/max-roll-deg", TmpMaxRollValue );
410 fgSetDouble( "/autopilot/config/roll-out-deg", TmpRollOutValue );
411 fgSetDouble( "/autopilot/config/max-aileron", TmpMaxAileronValue );
412 fgSetDouble( "/autopilot/config/roll-out-smooth-deg",
413 TmpRollOutSmoothValue );
415 goAwayAPAdjust(self);
418 void resetAPAdjust( puObject *self ) {
419 fgSetDouble( "/autopilot/config/max-roll-deg", MaxRollAdjust / 2 );
420 fgSetDouble( "/autopilot/config/roll-out-deg", RollOutAdjust / 2 );
421 fgSetDouble( "/autopilot/config/max-aileron", MaxAileronAdjust / 2 );
422 fgSetDouble( "/autopilot/config/roll-out-smooth-deg",
423 RollOutSmoothAdjust / 2 );
425 FG_POP_PUI_DIALOG( APAdjustDialog );
430 void fgAPAdjust( puObject *self ) {
431 TmpMaxRollValue = fgGetDouble("/autopilot/config/max-roll-deg");
432 TmpRollOutValue = fgGetDouble("/autopilot/config/roll-out-deg");
433 TmpMaxAileronValue = fgGetDouble("/autopilot/config/max-aileron");
434 TmpRollOutSmoothValue = fgGetDouble("/autopilot/config/roll-out-smooth-deg");
436 MaxRollValue = fgGetDouble("/autopilot/config/max-roll-deg")
438 RollOutValue = fgGetDouble("/autopilot/config/roll-out-deg")
440 MaxAileronValue = fgGetDouble("/autopilot/config/max-aileron")
442 RollOutSmoothValue = fgGetDouble("/autopilot/config/roll-out-smooth-deg")
443 / RollOutSmoothAdjust;
445 APAdjustHS0-> setValue ( MaxRollValue ) ;
446 APAdjustHS1-> setValue ( RollOutValue ) ;
447 APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
448 APAdjustHS3-> setValue ( MaxAileronValue ) ;
450 FG_PUSH_PUI_DIALOG( APAdjustDialog );
453 // Done once at system initialization
454 void fgAPAdjustInit() {
456 // printf("fgAPAdjustInit\n");
457 #define HORIZONTAL FALSE
461 int DialogWidth = 230;
463 char Label[] = "AutoPilot Adjust";
466 int labelX = (DialogWidth / 2) -
467 (puGetDefaultLabelFont().getStringWidth( Label ) / 2);
468 labelX -= 30; // KLUDGEY
473 int slider_width = 210;
474 int slider_title_x = 15;
475 int slider_value_x = 160;
476 float slider_delta = 0.1f;
478 TmpMaxRollValue = fgGetDouble("/autopilot/config/max-roll-deg");
479 TmpRollOutValue = fgGetDouble("/autopilot/config/roll-out-deg");
480 TmpMaxAileronValue = fgGetDouble("/autopilot/config/max-aileron");
481 TmpRollOutSmoothValue = fgGetDouble("/autopilot/config/roll-out-smooth-deg");
482 MaxRollAdjust = 2 * fgGetDouble("/autopilot/config/max-roll-deg");
483 RollOutAdjust = 2 * fgGetDouble("/autopilot/config/roll-out-deg");
484 MaxAileronAdjust = 2 * fgGetDouble("/autopilot/config/max-aileron");
485 RollOutSmoothAdjust = 2 * fgGetDouble("/autopilot/config/roll-out-smooth-deg");
487 MaxRollValue = fgGetDouble("/autopilot/config/max-roll-deg")
489 RollOutValue = fgGetDouble("/autopilot/config/roll-out-deg")
491 MaxAileronValue = fgGetDouble("/autopilot/config/max-aileron")
493 RollOutSmoothValue = fgGetDouble("/autopilot/config/roll-out-smooth-deg")
494 / RollOutSmoothAdjust;
496 puGetDefaultFonts ( &APAdjustLegendFont, &APAdjustLabelFont );
497 APAdjustDialog = new puDialogBox ( DialogX, DialogY ); {
498 int horiz_slider_height = APAdjustLabelFont.getStringHeight() +
499 APAdjustLabelFont.getStringDescender() +
500 PUSTR_TGAP + PUSTR_BGAP + 5;
502 APAdjustFrame = new puFrame ( 0, 0,
504 85 + nSliders * horiz_slider_height );
506 APAdjustDialogMessage = new puText ( labelX,
508 * horiz_slider_height );
509 APAdjustDialogMessage -> setDefaultValue ( Label );
510 APAdjustDialogMessage -> getDefaultValue ( &s );
511 APAdjustDialogMessage -> setLabel ( s );
513 APAdjustHS0 = new mySlider ( slider_x, slider_y,
514 slider_width, HORIZONTAL ) ;
515 APAdjustHS0-> setDelta ( slider_delta ) ;
516 APAdjustHS0-> setValue ( MaxRollValue ) ;
517 APAdjustHS0-> setCBMode ( PUSLIDER_DELTA ) ;
518 APAdjustHS0-> setCallback ( maxroll_adj ) ;
520 sprintf( SliderText[ 0 ], "%05.2f",
521 fgGetDouble("/autopilot/config/max-roll-deg") );
522 APAdjustMaxRollTitle = new puText ( slider_title_x, slider_y ) ;
523 APAdjustMaxRollTitle-> setDefaultValue ( "MaxRoll" ) ;
524 APAdjustMaxRollTitle-> getDefaultValue ( &s ) ;
525 APAdjustMaxRollTitle-> setLabel ( s ) ;
526 APAdjustMaxRollText = new puText ( slider_value_x, slider_y ) ;
527 APAdjustMaxRollText-> setLabel ( SliderText[ 0 ] ) ;
529 slider_y += horiz_slider_height;
531 APAdjustHS1 = new mySlider ( slider_x, slider_y, slider_width,
533 APAdjustHS1-> setDelta ( slider_delta ) ;
534 APAdjustHS1-> setValue ( RollOutValue ) ;
535 APAdjustHS1-> setCBMode ( PUSLIDER_DELTA ) ;
536 APAdjustHS1-> setCallback ( rollout_adj ) ;
538 sprintf( SliderText[ 1 ], "%05.2f",
539 fgGetDouble("/autopilot/config/roll-out-deg") );
540 APAdjustRollOutTitle = new puText ( slider_title_x, slider_y ) ;
541 APAdjustRollOutTitle-> setDefaultValue ( "AdjustRollOut" ) ;
542 APAdjustRollOutTitle-> getDefaultValue ( &s ) ;
543 APAdjustRollOutTitle-> setLabel ( s ) ;
544 APAdjustRollOutText = new puText ( slider_value_x, slider_y ) ;
545 APAdjustRollOutText-> setLabel ( SliderText[ 1 ] );
547 slider_y += horiz_slider_height;
549 APAdjustHS2 = new mySlider ( slider_x, slider_y, slider_width,
551 APAdjustHS2-> setDelta ( slider_delta ) ;
552 APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
553 APAdjustHS2-> setCBMode ( PUSLIDER_DELTA ) ;
554 APAdjustHS2-> setCallback ( rolloutsmooth_adj ) ;
556 sprintf( SliderText[ 2 ], "%5.2f",
557 fgGetDouble("/autopilot/config/roll-out-smooth-deg") );
558 APAdjustRollOutSmoothTitle = new puText ( slider_title_x, slider_y ) ;
559 APAdjustRollOutSmoothTitle-> setDefaultValue ( "RollOutSmooth" ) ;
560 APAdjustRollOutSmoothTitle-> getDefaultValue ( &s ) ;
561 APAdjustRollOutSmoothTitle-> setLabel ( s ) ;
562 APAdjustRollOutSmoothText = new puText ( slider_value_x, slider_y ) ;
563 APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
565 slider_y += horiz_slider_height;
567 APAdjustHS3 = new mySlider ( slider_x, slider_y, slider_width,
569 APAdjustHS3-> setDelta ( slider_delta ) ;
570 APAdjustHS3-> setValue ( MaxAileronValue ) ;
571 APAdjustHS3-> setCBMode ( PUSLIDER_DELTA ) ;
572 APAdjustHS3-> setCallback ( maxaileron_adj ) ;
574 sprintf( SliderText[ 3 ], "%05.2f",
575 fgGetDouble("/autopilot/config/max-aileron") );
576 APAdjustMaxAileronTitle = new puText ( slider_title_x, slider_y ) ;
577 APAdjustMaxAileronTitle-> setDefaultValue ( "MaxAileron" ) ;
578 APAdjustMaxAileronTitle-> getDefaultValue ( &s ) ;
579 APAdjustMaxAileronTitle-> setLabel ( s ) ;
580 APAdjustMaxAileronText = new puText ( slider_value_x, slider_y ) ;
581 APAdjustMaxAileronText-> setLabel ( SliderText[ 3 ] );
583 APAdjustOkButton = new puOneShot ( 10, 10, 60, 50 );
584 APAdjustOkButton-> setLegend ( gui_msg_OK );
585 APAdjustOkButton-> makeReturnDefault ( TRUE );
586 APAdjustOkButton-> setCallback ( goAwayAPAdjust );
588 APAdjustCancelButton = new puOneShot ( 70, 10, 150, 50 );
589 APAdjustCancelButton-> setLegend ( gui_msg_CANCEL );
590 APAdjustCancelButton-> setCallback ( cancelAPAdjust );
592 APAdjustResetButton = new puOneShot ( 160, 10, 220, 50 );
593 APAdjustResetButton-> setLegend ( gui_msg_RESET );
594 APAdjustResetButton-> setCallback ( resetAPAdjust );
596 FG_FINALIZE_PUI_DIALOG( APAdjustDialog );
601 // Simple Dialog to input Target Airport
602 void TgtAptDialog_Cancel(puObject *)
604 FG_POP_PUI_DIALOG( TgtAptDialog );
607 void TgtAptDialog_OK (puObject *)
611 // FGTime *t = FGTime::cur_time_params;
612 // int PauseMode = t->getPause();
614 // t->togglePauseMode();
617 TgtAptDialogInput->getValue(&s);
620 unsigned int pos = tmp.find( "@" );
621 if ( pos != string::npos ) {
622 TgtAptId = tmp.substr( 0, pos );
627 TgtAptDialog_Cancel( NULL );
629 /* s = input string, either 'FIX' or FIX@4000' */
630 /* TgtAptId is name of fix only; may get appended to below */
632 if ( NewWaypoint( TgtAptId ) == 0)
634 TgtAptId += " not in database.";
635 mkDialog(TgtAptId.c_str());
639 /* add new waypoint (either from above popup window 'ok button or telnet session) */
641 int NewWaypoint( string Tgt_Alt )
648 unsigned int pos = Tgt_Alt.find( "@" );
649 if ( pos != string::npos ) {
650 TgtAptId = Tgt_Alt.substr( 0, pos );
651 string alt_str = Tgt_Alt.substr( pos + 1 );
652 alt = atof( alt_str.c_str() );
653 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
654 alt *= SG_FEET_TO_METER;
660 FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
662 if ( fgFindAirportID( TgtAptId, &a ) ) {
664 SG_LOG( SG_GENERAL, SG_INFO,
665 "Adding waypoint (airport) = " << TgtAptId );
667 sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
669 SGWayPoint wp( a.longitude, a.latitude, alt,
670 SGWayPoint::WGS84, TgtAptId );
671 rm->add_waypoint( wp );
673 /* and turn on the autopilot */
674 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
678 } else if ( current_fixlist->query( TgtAptId, &f ) ) {
679 SG_LOG( SG_GENERAL, SG_INFO,
680 "Adding waypoint (fix) = " << TgtAptId );
682 sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
684 SGWayPoint wp( f.get_lon(), f.get_lat(), alt,
685 SGWayPoint::WGS84, TgtAptId );
686 rm->add_waypoint( wp );
688 /* and turn on the autopilot */
689 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
697 void TgtAptDialog_Reset(puObject *)
699 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
700 TgtAptDialogInput->setValue ( NewTgtAirportId );
701 TgtAptDialogInput->setCursor( 0 ) ;
704 void TgtAptDialog_HandleSlider ( puObject * slider )
707 slider -> getValue ( &val ) ;
710 int index = int ( TgtAptDialogWPList -> getNumItems () * val ) ;
711 TgtAptDialogWPList -> setTopItem ( index ) ;
714 void TgtAptDialog_HandleArrow( puObject *arrow )
716 int type = ((puArrowButton *)arrow)->getArrowType() ;
717 int inc = ( type == PUARROW_DOWN ) ? 1 :
718 ( type == PUARROW_UP ) ? -1 :
719 ( type == PUARROW_FASTDOWN ) ? 10 :
720 ( type == PUARROW_FASTUP ) ? -10 : 0 ;
723 TgtAptDialogSlider -> getValue ( &val ) ;
725 int num_items = TgtAptDialogWPList->getNumItems () - 1 ;
728 int index = int ( num_items * val + 0.5 ) + inc ;
729 if ( index > num_items ) index = num_items ;
730 if ( index < 0 ) index = 0 ;
732 TgtAptDialogSlider -> setValue ( 1.0f - (float)index / num_items ) ;
733 TgtAptDialogWPList -> setTopItem ( index ) ;
738 void AddWayPoint(puObject *cb)
740 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
741 TgtAptDialogInput->setValue( NewTgtAirportId );
743 /* refresh waypoint list */
747 if ( WPList != NULL ) {
748 for (i = 0; i < WPListsize; i++ ) {
753 FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
754 WPListsize = rm->size();
755 if ( WPListsize > 0 ) {
756 WPList = new char* [ WPListsize + 1 ];
757 for (i = 0; i < WPListsize; i++ ) {
758 SGWayPoint wp = rm->get_waypoint(i);
759 sprintf( WPString, "%5s %3.2flon %3.2flat",
762 wp.get_target_lat() );
763 WPList [i] = new char[ strlen(WPString)+1 ];
764 strcpy ( WPList [i], WPString );
768 WPList = new char* [ 2 ];
769 WPList [0] = new char[18];
770 strcpy ( WPList [0], "** List Empty **");
772 WPList [ WPListsize ] = NULL;
773 TgtAptDialogWPList->newList( WPList );
775 // if non-empty list, adjust the size of the slider...
776 TgtAptDialogSlider->setSliderFraction (0.9999f) ;
777 TgtAptDialogSlider->hide();
778 TgtAptDialogUPArrow->hide();
779 TgtAptDialogDNArrow->hide();
780 if (WPListsize > 10) {
781 TgtAptDialogSlider->setSliderFraction (10.0f/(WPListsize-1)) ;
782 TgtAptDialogSlider->reveal();
783 TgtAptDialogUPArrow->reveal();
784 TgtAptDialogDNArrow->reveal();
787 FG_PUSH_PUI_DIALOG( TgtAptDialog );
790 void PopWayPoint(puObject *cb)
792 FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
796 void ClearRoute(puObject *cb)
798 FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
802 void NewTgtAirportInit()
804 SG_LOG( SG_AUTOPILOT, SG_INFO, " enter NewTgtAirportInit()" );
805 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
806 SG_LOG( SG_AUTOPILOT, SG_INFO, " NewTgtAirportId " << NewTgtAirportId );
808 TgtAptDialog = new puDialogBox (150, 350);
810 TgtAptDialogFrame = new puFrame (0,0,350, 350);
812 TgtAptDialogWPList = new puListBox ( 50, 130, 300, 320 ) ;
813 TgtAptDialogWPList -> setLabel ( "Flight Plan" );
814 TgtAptDialogWPList -> setLabelPlace ( PUPLACE_ABOVE ) ;
815 TgtAptDialogWPList -> setStyle ( -PUSTYLE_SMALL_SHADED ) ;
816 TgtAptDialogWPList -> setValue ( 0 ) ;
818 TgtAptDialogSlider = new puSlider (300, 150, 150 ,TRUE,20);
819 TgtAptDialogSlider->setValue(1.0f);
820 TgtAptDialogSlider->setSliderFraction (0.2f) ;
821 TgtAptDialogSlider->setDelta(0.1f);
822 TgtAptDialogSlider->setCBMode( PUSLIDER_DELTA );
823 TgtAptDialogSlider->setCallback( TgtAptDialog_HandleSlider );
825 TgtAptDialogUPArrow = new puArrowButton ( 300, 300, 320, 320, PUARROW_UP ) ;
826 TgtAptDialogUPArrow->setCallback ( TgtAptDialog_HandleArrow ) ;
828 TgtAptDialogDNArrow = new puArrowButton ( 300, 130, 320, 150, PUARROW_DOWN ) ;
829 TgtAptDialogDNArrow->setCallback ( TgtAptDialog_HandleArrow ) ;
832 TgtAptDialogInput = new puInput (50, 70, 300, 100);
833 TgtAptDialogInput -> setLabel ( NewTgtAirportLabel );
834 TgtAptDialogInput -> setLabelPlace ( PUPLACE_ABOVE ) ;
835 TgtAptDialogInput -> setValue (NewTgtAirportId);
836 TgtAptDialogInput -> acceptInput();
838 TgtAptDialogOkButton = new puOneShot (50, 10, 110, 50);
839 TgtAptDialogOkButton -> setLegend (gui_msg_OK);
840 TgtAptDialogOkButton -> setCallback (TgtAptDialog_OK);
841 TgtAptDialogOkButton -> makeReturnDefault(TRUE);
843 TgtAptDialogCancelButton = new puOneShot (140, 10, 210, 50);
844 TgtAptDialogCancelButton -> setLegend (gui_msg_CANCEL);
845 TgtAptDialogCancelButton -> setCallback (TgtAptDialog_Cancel);
847 TgtAptDialogResetButton = new puOneShot (240, 10, 300, 50);
848 TgtAptDialogResetButton -> setLegend (gui_msg_RESET);
849 TgtAptDialogResetButton -> setCallback (TgtAptDialog_Reset);
853 FG_FINALIZE_PUI_DIALOG( TgtAptDialog );
854 SG_LOG(SG_GENERAL, SG_DEBUG, "leave NewTgtAirportInit()");