1 // auto_gui.cxx -- autopilot gui interface
3 // Written by Norman Vine <nhv@cape.com>
4 // Arranged by Curt Olson <http://www.flightgear.org/~curt>
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 <Scenery/scenery.hxx>
41 #include <simgear/constants.h>
42 #include <simgear/sg_inlines.h>
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/math/sg_geodesy.hxx>
45 #include <simgear/misc/sg_path.hxx>
47 #include <Airports/simple.hxx>
49 #include <Main/fg_init.hxx>
50 #include <Main/globals.hxx>
51 #include <Main/fg_props.hxx>
52 #include <Navaids/fixlist.hxx>
53 #include <Navaids/navlist.hxx>
54 #include <Navaids/navrecord.hxx>
56 #include "auto_gui.hxx"
57 #include "route_mgr.hxx"
58 #include "xmlauto.hxx"
63 #define mySlider puSlider
65 // Climb speed constants
66 // const double min_climb = 70.0; // kts
67 // const double best_climb = 75.0; // kts
68 // const double ideal_climb_rate = 500.0; // fpm
70 /// These statics will eventually go into the class
71 /// they are just here while I am experimenting -- NHV :-)
72 // AutoPilot Gain Adjuster members
73 // static double MaxRollAdjust; // MaxRollAdjust = 2 * APData->MaxRoll;
74 // static double RollOutAdjust; // RollOutAdjust = 2 * APData->RollOut;
75 // static double MaxAileronAdjust; // MaxAileronAdjust = 2 * APData->MaxAileron;
76 // static double RollOutSmoothAdjust; // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
78 // static float MaxRollValue; // 0.1 -> 1.0
79 // static float RollOutValue;
80 // static float MaxAileronValue;
81 // static float RollOutSmoothValue;
83 // static float TmpMaxRollValue; // for cancel operation
84 // static float TmpRollOutValue;
85 // static float TmpMaxAileronValue;
86 // static float TmpRollOutSmoothValue;
88 // static puDialogBox *APAdjustDialog;
89 // static puFrame *APAdjustFrame;
90 // static puText *APAdjustDialogMessage;
91 // static puFont APAdjustLegendFont;
92 // static puFont APAdjustLabelFont;
94 // static puOneShot *APAdjustOkButton;
95 // static puOneShot *APAdjustResetButton;
96 // static puOneShot *APAdjustCancelButton;
98 // static puButton *APAdjustDragButton;
100 // static puText *APAdjustMaxRollTitle;
101 // static puText *APAdjustRollOutTitle;
102 // static puText *APAdjustMaxAileronTitle;
103 // static puText *APAdjustRollOutSmoothTitle;
105 // static puText *APAdjustMaxAileronText;
106 // static puText *APAdjustMaxRollText;
107 // static puText *APAdjustRollOutText;
108 // static puText *APAdjustRollOutSmoothText;
110 // static mySlider *APAdjustHS0;
111 // static mySlider *APAdjustHS1;
112 // static mySlider *APAdjustHS2;
113 // static mySlider *APAdjustHS3;
115 // static char SliderText[ 4 ][ 8 ];
117 ///////// AutoPilot New Heading Dialog
119 static puDialogBox *ApHeadingDialog;
120 static puFrame *ApHeadingDialogFrame;
121 static puText *ApHeadingDialogMessage;
122 static puInput *ApHeadingDialogInput;
123 static puOneShot *ApHeadingDialogOkButton;
124 static puOneShot *ApHeadingDialogCancelButton;
127 ///////// AutoPilot New Altitude Dialog
129 static puDialogBox *ApAltitudeDialog = 0;
130 static puFrame *ApAltitudeDialogFrame = 0;
131 static puText *ApAltitudeDialogMessage = 0;
132 static puInput *ApAltitudeDialogInput = 0;
134 static puOneShot *ApAltitudeDialogOkButton = 0;
135 static puOneShot *ApAltitudeDialogCancelButton = 0;
138 /// The beginnings of Lock AutoPilot to target location :-)
139 // Needs cleaning up but works
140 // These statics should disapear when this is a class
141 static puDialogBox *TgtAptDialog = 0;
142 static puFrame *TgtAptDialogFrame = 0;
143 // static puText *TgtAptDialogMessage = 0;
144 static puInput *TgtAptDialogInput = 0;
145 static puListBox *TgtAptDialogWPList = 0;
146 static puSlider *TgtAptDialogSlider = 0;
147 static puArrowButton *TgtAptDialogUPArrow = 0;
148 static puArrowButton *TgtAptDialogDNArrow = 0;
149 static char** WPList;
150 static int WPListsize;
152 static char NewTgtAirportId[16];
153 static char NewTgtAirportLabel[] = "New Apt/Fix ID";
155 static puOneShot *TgtAptDialogOkButton = 0;
156 static puOneShot *TgtAptDialogCancelButton = 0;
157 static puOneShot *TgtAptDialogResetButton = 0;
160 // extern char *coord_format_lat(float);
161 // extern char *coord_format_lon(float);
163 // THIS NEEDS IMPROVEMENT !!!!!!!!!!!!!
164 static int scan_number(char *s, double *new_value)
169 char *WordBufPtr = WordBuf;
173 *WordBufPtr++ = *cptr++;
175 while (isdigit(*cptr) ) {
176 *WordBufPtr++ = *cptr++;
180 *WordBufPtr++ = *cptr++; // put the '.' into the string
181 while (isdigit(*cptr)) {
182 *WordBufPtr++ = *cptr++;
187 sscanf(WordBuf, "%lf", new_value);
194 void ApHeadingDialog_Cancel(puObject *)
196 ApHeadingDialogInput->rejectInput();
197 FG_POP_PUI_DIALOG( ApHeadingDialog );
200 void ApHeadingDialog_OK (puObject *me)
205 ApHeadingDialogInput -> getValue( &c );
209 if( scan_number( c, &NewHeading ) ) {
210 fgSetString( "/autopilot/locks/heading", "dg-heading-hold" );
211 fgSetDouble( "/autopilot/settings/heading-bug-deg",
216 s += " is not a valid number.";
219 ApHeadingDialog_Cancel(me);
220 if ( error ) mkDialog(s.c_str());
223 void NewHeading(puObject *cb)
225 // string ApHeadingLabel( "Enter New Heading" );
226 // ApHeadingDialogMessage -> setLabel(ApHeadingLabel.c_str());
227 float heading = fgGetDouble( "/autopilot/settings/heading-bug-deg" );
228 while ( heading < 0.0 ) { heading += 360.0; }
229 ApHeadingDialogInput -> setValue ( heading );
230 ApHeadingDialogInput -> acceptInput();
231 FG_PUSH_PUI_DIALOG( ApHeadingDialog );
234 void NewHeadingInit()
236 // printf("NewHeadingInit\n");
237 char NewHeadingLabel[] = "Enter New Heading";
240 float heading = fgGetDouble("/orientation/heading-deg");
242 (puGetDefaultLabelFont().getStringWidth( NewHeadingLabel ) / 2 );
244 ApHeadingDialog = new puDialogBox (150, 50);
246 ApHeadingDialogFrame = new puFrame (0, 0, 260, 150);
248 ApHeadingDialogMessage = new puText (len, 110);
249 ApHeadingDialogMessage -> setDefaultValue (NewHeadingLabel);
250 ApHeadingDialogMessage -> getDefaultValue (&s);
251 ApHeadingDialogMessage -> setLabel (s);
253 ApHeadingDialogInput = new puInput ( 50, 70, 210, 100 );
254 ApHeadingDialogInput -> setValue ( heading );
256 ApHeadingDialogOkButton = new puOneShot (50, 10, 110, 50);
257 ApHeadingDialogOkButton -> setLegend (gui_msg_OK);
258 ApHeadingDialogOkButton -> makeReturnDefault (TRUE);
259 ApHeadingDialogOkButton -> setCallback (ApHeadingDialog_OK);
261 ApHeadingDialogCancelButton = new puOneShot (140, 10, 210, 50);
262 ApHeadingDialogCancelButton -> setLegend (gui_msg_CANCEL);
263 ApHeadingDialogCancelButton -> setCallback (ApHeadingDialog_Cancel);
266 FG_FINALIZE_PUI_DIALOG( ApHeadingDialog );
269 void ApAltitudeDialog_Cancel(puObject *)
271 ApAltitudeDialogInput -> rejectInput();
272 FG_POP_PUI_DIALOG( ApAltitudeDialog );
275 void ApAltitudeDialog_OK (puObject *me)
280 ApAltitudeDialogInput->getValue( &c );
284 if ( scan_number( c, &NewAltitude) ) {
285 fgSetString( "/autopilot/locks/altitude", "altitude-hold" );
286 fgSetDouble( "/autopilot/settings/altitude-ft", NewAltitude );
290 s += " is not a valid number.";
293 ApAltitudeDialog_Cancel(me);
294 if( error ) mkDialog(s.c_str());
297 void NewAltitude(puObject *cb)
299 float altitude = fgGetDouble("/autopilot/settings/altitude-ft")
301 ApAltitudeDialogInput -> setValue( altitude );
302 ApAltitudeDialogInput -> acceptInput();
303 FG_PUSH_PUI_DIALOG( ApAltitudeDialog );
306 void NewAltitudeInit()
308 // printf("NewAltitudeInit\n");
309 char NewAltitudeLabel[] = "Enter New Altitude";
312 float alt = cur_fdm_state->get_Altitude();
314 if ( !strcmp(fgGetString("/sim/startup/units"), "meters")) {
315 alt *= SG_FEET_TO_METER;
319 (puGetDefaultLabelFont().getStringWidth( NewAltitudeLabel ) / 2);
321 // ApAltitudeDialog = new puDialogBox (150, 50);
322 ApAltitudeDialog = new puDialogBox (150, 200);
324 ApAltitudeDialogFrame = new puFrame (0, 0, 260, 150);
325 ApAltitudeDialogMessage = new puText (len, 110);
326 ApAltitudeDialogMessage -> setDefaultValue (NewAltitudeLabel);
327 ApAltitudeDialogMessage -> getDefaultValue (&s);
328 ApAltitudeDialogMessage -> setLabel (s);
330 ApAltitudeDialogInput = new puInput ( 50, 70, 210, 100 );
331 ApAltitudeDialogInput -> setValue ( alt );
332 // Uncomment the next line to have input active on startup
333 // ApAltitudeDialogInput -> acceptInput ( );
334 // cursor at begining or end of line ?
337 // ApAltitudeDialogInput -> setCursor ( len );
338 // ApAltitudeDialogInput -> setSelectRegion ( 5, 9 );
340 ApAltitudeDialogOkButton = new puOneShot (50, 10, 110, 50);
341 ApAltitudeDialogOkButton -> setLegend (gui_msg_OK);
342 ApAltitudeDialogOkButton -> makeReturnDefault (TRUE);
343 ApAltitudeDialogOkButton -> setCallback (ApAltitudeDialog_OK);
345 ApAltitudeDialogCancelButton = new puOneShot (140, 10, 210, 50);
346 ApAltitudeDialogCancelButton -> setLegend (gui_msg_CANCEL);
347 ApAltitudeDialogCancelButton -> setCallback (ApAltitudeDialog_Cancel);
350 FG_FINALIZE_PUI_DIALOG( ApAltitudeDialog );
355 static void maxroll_adj( puObject *hs ) {
358 hs-> getValue ( &val ) ;
359 SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
360 // printf ( "maxroll_adj( %p ) %f %f\n", hs, val, MaxRollAdjust * val ) ;
361 fgSetDouble( "/autopilot/config/max-roll-deg", MaxRollAdjust * val );
362 sprintf( SliderText[ 0 ], "%05.2f",
363 fgGetDouble("/autopilot/config/max-roll-deg") );
364 APAdjustMaxRollText -> setLabel ( SliderText[ 0 ] ) ;
367 static void rollout_adj( puObject *hs ) {
370 hs-> getValue ( &val ) ;
371 SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
372 // printf ( "rollout_adj( %p ) %f %f\n", hs, val, RollOutAdjust * val ) ;
373 fgSetDouble( "/autopilot/config/roll-out-deg", RollOutAdjust * val );
374 sprintf( SliderText[ 1 ], "%05.2f",
375 fgGetDouble("/autopilot/config/roll-out-deg") );
376 APAdjustRollOutText -> setLabel ( SliderText[ 1 ] );
379 static void maxaileron_adj( puObject *hs ) {
382 hs-> getValue ( &val ) ;
383 SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
384 // printf ( "maxaileron_adj( %p ) %f %f\n", hs, val, MaxAileronAdjust * val ) ;
385 fgSetDouble( "/autopilot/config/max-aileron", MaxAileronAdjust * val );
386 sprintf( SliderText[ 3 ], "%05.2f",
387 fgGetDouble("/autopilot/config/max-aileron") );
388 APAdjustMaxAileronText -> setLabel ( SliderText[ 3 ] );
391 static void rolloutsmooth_adj( puObject *hs ) {
394 hs -> getValue ( &val ) ;
395 SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
396 // printf ( "rolloutsmooth_adj( %p ) %f %f\n", hs, val, RollOutSmoothAdjust * val ) ;
397 fgSetDouble( "/autopilot/config/roll-out-smooth-deg",
398 RollOutSmoothAdjust * val );
399 sprintf( SliderText[ 2 ], "%5.2f",
400 fgGetDouble("/autopilot/config/roll-out-smooth-deg") );
401 APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
405 static void goAwayAPAdjust (puObject *)
407 FG_POP_PUI_DIALOG( APAdjustDialog );
410 void cancelAPAdjust( puObject *self ) {
411 fgSetDouble( "/autopilot/config/max-roll-deg", TmpMaxRollValue );
412 fgSetDouble( "/autopilot/config/roll-out-deg", TmpRollOutValue );
413 fgSetDouble( "/autopilot/config/max-aileron", TmpMaxAileronValue );
414 fgSetDouble( "/autopilot/config/roll-out-smooth-deg",
415 TmpRollOutSmoothValue );
417 goAwayAPAdjust(self);
420 void resetAPAdjust( puObject *self ) {
421 fgSetDouble( "/autopilot/config/max-roll-deg", MaxRollAdjust / 2 );
422 fgSetDouble( "/autopilot/config/roll-out-deg", RollOutAdjust / 2 );
423 fgSetDouble( "/autopilot/config/max-aileron", MaxAileronAdjust / 2 );
424 fgSetDouble( "/autopilot/config/roll-out-smooth-deg",
425 RollOutSmoothAdjust / 2 );
427 FG_POP_PUI_DIALOG( APAdjustDialog );
432 void fgAPAdjust( puObject *self ) {
433 TmpMaxRollValue = fgGetDouble("/autopilot/config/max-roll-deg");
434 TmpRollOutValue = fgGetDouble("/autopilot/config/roll-out-deg");
435 TmpMaxAileronValue = fgGetDouble("/autopilot/config/max-aileron");
436 TmpRollOutSmoothValue = fgGetDouble("/autopilot/config/roll-out-smooth-deg");
438 MaxRollValue = fgGetDouble("/autopilot/config/max-roll-deg")
440 RollOutValue = fgGetDouble("/autopilot/config/roll-out-deg")
442 MaxAileronValue = fgGetDouble("/autopilot/config/max-aileron")
444 RollOutSmoothValue = fgGetDouble("/autopilot/config/roll-out-smooth-deg")
445 / RollOutSmoothAdjust;
447 APAdjustHS0-> setValue ( MaxRollValue ) ;
448 APAdjustHS1-> setValue ( RollOutValue ) ;
449 APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
450 APAdjustHS3-> setValue ( MaxAileronValue ) ;
452 FG_PUSH_PUI_DIALOG( APAdjustDialog );
455 // Done once at system initialization
456 void fgAPAdjustInit() {
458 // printf("fgAPAdjustInit\n");
459 #define HORIZONTAL FALSE
463 int DialogWidth = 230;
465 char Label[] = "AutoPilot Adjust";
468 int labelX = (DialogWidth / 2) -
469 (puGetDefaultLabelFont().getStringWidth( Label ) / 2);
470 labelX -= 30; // KLUDGEY
475 int slider_width = 210;
476 int slider_title_x = 15;
477 int slider_value_x = 160;
478 float slider_delta = 0.1f;
480 TmpMaxRollValue = fgGetDouble("/autopilot/config/max-roll-deg");
481 TmpRollOutValue = fgGetDouble("/autopilot/config/roll-out-deg");
482 TmpMaxAileronValue = fgGetDouble("/autopilot/config/max-aileron");
483 TmpRollOutSmoothValue = fgGetDouble("/autopilot/config/roll-out-smooth-deg");
484 MaxRollAdjust = 2 * fgGetDouble("/autopilot/config/max-roll-deg");
485 RollOutAdjust = 2 * fgGetDouble("/autopilot/config/roll-out-deg");
486 MaxAileronAdjust = 2 * fgGetDouble("/autopilot/config/max-aileron");
487 RollOutSmoothAdjust = 2 * fgGetDouble("/autopilot/config/roll-out-smooth-deg");
489 MaxRollValue = fgGetDouble("/autopilot/config/max-roll-deg")
491 RollOutValue = fgGetDouble("/autopilot/config/roll-out-deg")
493 MaxAileronValue = fgGetDouble("/autopilot/config/max-aileron")
495 RollOutSmoothValue = fgGetDouble("/autopilot/config/roll-out-smooth-deg")
496 / RollOutSmoothAdjust;
498 puGetDefaultFonts ( &APAdjustLegendFont, &APAdjustLabelFont );
499 APAdjustDialog = new puDialogBox ( DialogX, DialogY ); {
500 int horiz_slider_height = APAdjustLabelFont.getStringHeight() +
501 APAdjustLabelFont.getStringDescender() +
502 PUSTR_TGAP + PUSTR_BGAP + 5;
504 APAdjustFrame = new puFrame ( 0, 0,
506 85 + nSliders * horiz_slider_height );
508 APAdjustDialogMessage = new puText ( labelX,
510 * horiz_slider_height );
511 APAdjustDialogMessage -> setDefaultValue ( Label );
512 APAdjustDialogMessage -> getDefaultValue ( &s );
513 APAdjustDialogMessage -> setLabel ( s );
515 APAdjustHS0 = new mySlider ( slider_x, slider_y,
516 slider_width, HORIZONTAL ) ;
517 APAdjustHS0-> setDelta ( slider_delta ) ;
518 APAdjustHS0-> setValue ( MaxRollValue ) ;
519 APAdjustHS0-> setCBMode ( PUSLIDER_DELTA ) ;
520 APAdjustHS0-> setCallback ( maxroll_adj ) ;
522 sprintf( SliderText[ 0 ], "%05.2f",
523 fgGetDouble("/autopilot/config/max-roll-deg") );
524 APAdjustMaxRollTitle = new puText ( slider_title_x, slider_y ) ;
525 APAdjustMaxRollTitle-> setDefaultValue ( "MaxRoll" ) ;
526 APAdjustMaxRollTitle-> getDefaultValue ( &s ) ;
527 APAdjustMaxRollTitle-> setLabel ( s ) ;
528 APAdjustMaxRollText = new puText ( slider_value_x, slider_y ) ;
529 APAdjustMaxRollText-> setLabel ( SliderText[ 0 ] ) ;
531 slider_y += horiz_slider_height;
533 APAdjustHS1 = new mySlider ( slider_x, slider_y, slider_width,
535 APAdjustHS1-> setDelta ( slider_delta ) ;
536 APAdjustHS1-> setValue ( RollOutValue ) ;
537 APAdjustHS1-> setCBMode ( PUSLIDER_DELTA ) ;
538 APAdjustHS1-> setCallback ( rollout_adj ) ;
540 sprintf( SliderText[ 1 ], "%05.2f",
541 fgGetDouble("/autopilot/config/roll-out-deg") );
542 APAdjustRollOutTitle = new puText ( slider_title_x, slider_y ) ;
543 APAdjustRollOutTitle-> setDefaultValue ( "AdjustRollOut" ) ;
544 APAdjustRollOutTitle-> getDefaultValue ( &s ) ;
545 APAdjustRollOutTitle-> setLabel ( s ) ;
546 APAdjustRollOutText = new puText ( slider_value_x, slider_y ) ;
547 APAdjustRollOutText-> setLabel ( SliderText[ 1 ] );
549 slider_y += horiz_slider_height;
551 APAdjustHS2 = new mySlider ( slider_x, slider_y, slider_width,
553 APAdjustHS2-> setDelta ( slider_delta ) ;
554 APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
555 APAdjustHS2-> setCBMode ( PUSLIDER_DELTA ) ;
556 APAdjustHS2-> setCallback ( rolloutsmooth_adj ) ;
558 sprintf( SliderText[ 2 ], "%5.2f",
559 fgGetDouble("/autopilot/config/roll-out-smooth-deg") );
560 APAdjustRollOutSmoothTitle = new puText ( slider_title_x, slider_y ) ;
561 APAdjustRollOutSmoothTitle-> setDefaultValue ( "RollOutSmooth" ) ;
562 APAdjustRollOutSmoothTitle-> getDefaultValue ( &s ) ;
563 APAdjustRollOutSmoothTitle-> setLabel ( s ) ;
564 APAdjustRollOutSmoothText = new puText ( slider_value_x, slider_y ) ;
565 APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
567 slider_y += horiz_slider_height;
569 APAdjustHS3 = new mySlider ( slider_x, slider_y, slider_width,
571 APAdjustHS3-> setDelta ( slider_delta ) ;
572 APAdjustHS3-> setValue ( MaxAileronValue ) ;
573 APAdjustHS3-> setCBMode ( PUSLIDER_DELTA ) ;
574 APAdjustHS3-> setCallback ( maxaileron_adj ) ;
576 sprintf( SliderText[ 3 ], "%05.2f",
577 fgGetDouble("/autopilot/config/max-aileron") );
578 APAdjustMaxAileronTitle = new puText ( slider_title_x, slider_y ) ;
579 APAdjustMaxAileronTitle-> setDefaultValue ( "MaxAileron" ) ;
580 APAdjustMaxAileronTitle-> getDefaultValue ( &s ) ;
581 APAdjustMaxAileronTitle-> setLabel ( s ) ;
582 APAdjustMaxAileronText = new puText ( slider_value_x, slider_y ) ;
583 APAdjustMaxAileronText-> setLabel ( SliderText[ 3 ] );
585 APAdjustOkButton = new puOneShot ( 10, 10, 60, 50 );
586 APAdjustOkButton-> setLegend ( gui_msg_OK );
587 APAdjustOkButton-> makeReturnDefault ( TRUE );
588 APAdjustOkButton-> setCallback ( goAwayAPAdjust );
590 APAdjustCancelButton = new puOneShot ( 70, 10, 150, 50 );
591 APAdjustCancelButton-> setLegend ( gui_msg_CANCEL );
592 APAdjustCancelButton-> setCallback ( cancelAPAdjust );
594 APAdjustResetButton = new puOneShot ( 160, 10, 220, 50 );
595 APAdjustResetButton-> setLegend ( gui_msg_RESET );
596 APAdjustResetButton-> setCallback ( resetAPAdjust );
598 FG_FINALIZE_PUI_DIALOG( APAdjustDialog );
604 // Simple Dialog to input Target Airport
605 void TgtAptDialog_Cancel(puObject *)
607 FG_POP_PUI_DIALOG( TgtAptDialog );
610 void TgtAptDialog_OK (puObject *)
614 // FGTime *t = FGTime::cur_time_params;
615 // int PauseMode = t->getPause();
617 // t->togglePauseMode();
620 TgtAptDialogInput->getValue(&s);
623 unsigned int pos = tmp.find( "@" );
624 if ( pos != string::npos ) {
625 TgtAptId = tmp.substr( 0, pos );
630 TgtAptDialog_Cancel( NULL );
632 /* s = input string, either 'FIX' or FIX@4000' */
633 /* TgtAptId is name of fix only; may get appended to below */
635 if ( NewWaypoint( TgtAptId ) == 0)
637 TgtAptId += " not in database.";
638 mkDialog(TgtAptId.c_str());
642 /* add new waypoint (either from above popup window 'ok button or telnet session) */
644 int NewWaypoint( const string& Tgt_Alt )
651 unsigned int pos = Tgt_Alt.find( "@" );
652 if ( pos != string::npos ) {
653 TgtAptId = Tgt_Alt.substr( 0, pos );
654 string alt_str = Tgt_Alt.substr( pos + 1 );
655 alt = atof( alt_str.c_str() );
656 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
657 alt *= SG_FEET_TO_METER;
663 FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
665 if ( fgFindAirportID( TgtAptId, &a ) ) {
667 SG_LOG( SG_GENERAL, SG_INFO,
668 "Adding waypoint (airport) = " << TgtAptId );
670 sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
672 SGWayPoint wp( a.getLongitude(), a.getLatitude(), alt,
673 SGWayPoint::WGS84, TgtAptId );
674 rm->add_waypoint( wp );
676 /* and turn on the autopilot */
677 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
681 } else if ( globals->get_fixlist()->query( TgtAptId, &f ) ) {
682 SG_LOG( SG_GENERAL, SG_INFO,
683 "Adding waypoint (fix) = " << TgtAptId );
685 sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
687 SGWayPoint wp( f.get_lon(), f.get_lat(), alt,
688 SGWayPoint::WGS84, TgtAptId );
689 rm->add_waypoint( wp );
691 /* and turn on the autopilot */
692 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
695 // Try finding a nav matching the ID
697 // The base lon/lat are determined by the last WP,
698 // or the current pos if the WP list is empty.
699 const int wps = rm->size();
701 SGWayPoint wp = rm->get_waypoint(wps-1);
702 lat = wp.get_target_lat();
703 lon = wp.get_target_lon();
706 lat = fgGetNode("/position/latitude-deg")->getDoubleValue();
707 lon = fgGetNode("/position/longitude-deg")->getDoubleValue();
710 lat *= SGD_DEGREES_TO_RADIANS;
711 lon *= SGD_DEGREES_TO_RADIANS;
713 SG_LOG( SG_GENERAL, SG_INFO,
714 "Looking for nav " << TgtAptId << " at " << lon << " " << lat);
715 if (FGNavRecord* nav =
716 globals->get_navlist()->findByIdent(TgtAptId.c_str(), lon, lat))
718 SG_LOG( SG_GENERAL, SG_INFO,
719 "Adding waypoint (nav) = " << TgtAptId );
721 sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
723 SGWayPoint wp( nav->get_lon(), nav->get_lat(), alt,
724 SGWayPoint::WGS84, TgtAptId );
725 rm->add_waypoint( wp );
727 /* and turn on the autopilot */
728 fgSetString( "/autopilot/locks/heading", "true-heading-hold" );
738 void TgtAptDialog_Reset(puObject *)
740 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
741 TgtAptDialogInput->setValue ( NewTgtAirportId );
742 TgtAptDialogInput->setCursor( 0 ) ;
745 void TgtAptDialog_HandleSlider ( puObject * slider )
748 slider -> getValue ( &val ) ;
751 int index = int ( TgtAptDialogWPList -> getNumItems () * val ) ;
752 TgtAptDialogWPList -> setTopItem ( index ) ;
755 void TgtAptDialog_HandleArrow( puObject *arrow )
757 int type = ((puArrowButton *)arrow)->getArrowType() ;
758 int inc = ( type == PUARROW_DOWN ) ? 1 :
759 ( type == PUARROW_UP ) ? -1 :
760 ( type == PUARROW_FASTDOWN ) ? 10 :
761 ( type == PUARROW_FASTUP ) ? -10 : 0 ;
764 TgtAptDialogSlider -> getValue ( &val ) ;
766 int num_items = TgtAptDialogWPList->getNumItems () - 1 ;
769 int index = int ( num_items * val + 0.5 ) + inc ;
770 if ( index > num_items ) index = num_items ;
771 if ( index < 0 ) index = 0 ;
773 TgtAptDialogSlider -> setValue ( 1.0f - (float)index / num_items ) ;
774 TgtAptDialogWPList -> setTopItem ( index ) ;
779 void AddWayPoint(puObject *cb)
781 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
782 TgtAptDialogInput->setValue( NewTgtAirportId );
784 /* refresh waypoint list */
788 if ( WPList != NULL ) {
789 for (i = 0; i < WPListsize; i++ ) {
794 FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
795 WPListsize = rm->size();
796 if ( WPListsize > 0 ) {
797 WPList = new char* [ WPListsize + 1 ];
798 for (i = 0; i < WPListsize; i++ ) {
799 SGWayPoint wp = rm->get_waypoint(i);
800 sprintf( WPString, "%5s %3.2flon %3.2flat",
803 wp.get_target_lat() );
804 WPList [i] = new char[ strlen(WPString)+1 ];
805 strcpy ( WPList [i], WPString );
809 WPList = new char* [ 2 ];
810 WPList [0] = new char[18];
811 strcpy ( WPList [0], "** List Empty **");
813 WPList [ WPListsize ] = NULL;
814 TgtAptDialogWPList->newList( WPList );
816 // if non-empty list, adjust the size of the slider...
817 TgtAptDialogSlider->setSliderFraction (0.9999f) ;
818 TgtAptDialogSlider->hide();
819 TgtAptDialogUPArrow->hide();
820 TgtAptDialogDNArrow->hide();
821 if (WPListsize > 10) {
822 TgtAptDialogSlider->setSliderFraction (10.0f/(WPListsize-1)) ;
823 TgtAptDialogSlider->reveal();
824 TgtAptDialogUPArrow->reveal();
825 TgtAptDialogDNArrow->reveal();
828 FG_PUSH_PUI_DIALOG( TgtAptDialog );
831 void PopWayPoint(puObject *cb)
833 FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
837 void ClearRoute(puObject *cb)
839 FGRouteMgr *rm = (FGRouteMgr *)globals->get_subsystem("route-manager");
843 void NewTgtAirportInit()
845 SG_LOG( SG_AUTOPILOT, SG_INFO, " enter NewTgtAirportInit()" );
846 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
847 SG_LOG( SG_AUTOPILOT, SG_INFO, " NewTgtAirportId " << NewTgtAirportId );
849 TgtAptDialog = new puDialogBox (150, 350);
851 TgtAptDialogFrame = new puFrame (0,0,350, 350);
853 TgtAptDialogWPList = new puListBox ( 50, 130, 300, 320 ) ;
854 TgtAptDialogWPList -> setLabel ( "Flight Plan" );
855 TgtAptDialogWPList -> setLabelPlace ( PUPLACE_ABOVE ) ;
856 TgtAptDialogWPList -> setStyle ( -PUSTYLE_SMALL_SHADED ) ;
857 TgtAptDialogWPList -> setValue ( 0 ) ;
859 TgtAptDialogSlider = new puSlider (300, 150, 150 ,TRUE,20);
860 TgtAptDialogSlider->setValue(1.0f);
861 TgtAptDialogSlider->setSliderFraction (0.2f) ;
862 TgtAptDialogSlider->setDelta(0.1f);
863 TgtAptDialogSlider->setCBMode( PUSLIDER_DELTA );
864 TgtAptDialogSlider->setCallback( TgtAptDialog_HandleSlider );
866 TgtAptDialogUPArrow = new puArrowButton ( 300, 300, 320, 320, PUARROW_UP ) ;
867 TgtAptDialogUPArrow->setCallback ( TgtAptDialog_HandleArrow ) ;
869 TgtAptDialogDNArrow = new puArrowButton ( 300, 130, 320, 150, PUARROW_DOWN ) ;
870 TgtAptDialogDNArrow->setCallback ( TgtAptDialog_HandleArrow ) ;
873 TgtAptDialogInput = new puInput (50, 70, 300, 100);
874 TgtAptDialogInput -> setLabel ( NewTgtAirportLabel );
875 TgtAptDialogInput -> setLabelPlace ( PUPLACE_ABOVE ) ;
876 TgtAptDialogInput -> setValue (NewTgtAirportId);
877 TgtAptDialogInput -> acceptInput();
879 TgtAptDialogOkButton = new puOneShot (50, 10, 110, 50);
880 TgtAptDialogOkButton -> setLegend (gui_msg_OK);
881 TgtAptDialogOkButton -> setCallback (TgtAptDialog_OK);
882 TgtAptDialogOkButton -> makeReturnDefault(TRUE);
884 TgtAptDialogCancelButton = new puOneShot (140, 10, 210, 50);
885 TgtAptDialogCancelButton -> setLegend (gui_msg_CANCEL);
886 TgtAptDialogCancelButton -> setCallback (TgtAptDialog_Cancel);
888 TgtAptDialogResetButton = new puOneShot (240, 10, 300, 50);
889 TgtAptDialogResetButton -> setLegend (gui_msg_RESET);
890 TgtAptDialogResetButton -> setCallback (TgtAptDialog_Reset);
894 FG_FINALIZE_PUI_DIALOG( TgtAptDialog );
895 SG_LOG(SG_GENERAL, SG_DEBUG, "leave NewTgtAirportInit()");