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>
31 #include <simgear/route/route.hxx>
39 #include <Aircraft/aircraft.hxx>
40 #include <FDM/flight.hxx>
41 #include <Controls/controls.hxx>
42 #include <Scenery/scenery.hxx>
44 #include <simgear/constants.h>
45 #include <simgear/sg_inlines.h>
46 #include <simgear/debug/logstream.hxx>
47 #include <simgear/math/sg_geodesy.hxx>
48 #include <simgear/misc/sg_path.hxx>
50 #include <Airports/simple.hxx>
52 #include <Main/fg_init.hxx>
53 #include <Main/globals.hxx>
54 #include <Main/fg_props.hxx>
55 #include <Navaids/fixlist.hxx>
57 #include "auto_gui.hxx"
58 #include "newauto.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 ) )
211 if ( !globals->get_autopilot()->get_HeadingEnabled() ) {
212 globals->get_autopilot()->set_HeadingEnabled( true );
214 globals->get_autopilot()->HeadingSet( NewHeading );
218 s += " is not a valid number.";
221 ApHeadingDialog_Cancel(me);
222 if( error ) mkDialog(s.c_str());
225 void NewHeading(puObject *cb)
227 // string ApHeadingLabel( "Enter New Heading" );
228 // ApHeadingDialogMessage -> setLabel(ApHeadingLabel.c_str());
229 float heading = globals->get_autopilot()->get_DGTargetHeading();
230 while ( heading < 0.0 ) { heading += 360.0; }
231 ApHeadingDialogInput -> setValue ( heading );
232 ApHeadingDialogInput -> acceptInput();
233 FG_PUSH_PUI_DIALOG( ApHeadingDialog );
236 void NewHeadingInit()
238 // printf("NewHeadingInit\n");
239 char NewHeadingLabel[] = "Enter New Heading";
242 float heading = fgGetDouble("/orientation/heading-deg");
244 (puGetDefaultLabelFont().getStringWidth( NewHeadingLabel ) / 2 );
246 ApHeadingDialog = new puDialogBox (150, 50);
248 ApHeadingDialogFrame = new puFrame (0, 0, 260, 150);
250 ApHeadingDialogMessage = new puText (len, 110);
251 ApHeadingDialogMessage -> setDefaultValue (NewHeadingLabel);
252 ApHeadingDialogMessage -> getDefaultValue (&s);
253 ApHeadingDialogMessage -> setLabel (s);
255 ApHeadingDialogInput = new puInput ( 50, 70, 210, 100 );
256 ApHeadingDialogInput -> setValue ( heading );
258 ApHeadingDialogOkButton = new puOneShot (50, 10, 110, 50);
259 ApHeadingDialogOkButton -> setLegend (gui_msg_OK);
260 ApHeadingDialogOkButton -> makeReturnDefault (TRUE);
261 ApHeadingDialogOkButton -> setCallback (ApHeadingDialog_OK);
263 ApHeadingDialogCancelButton = new puOneShot (140, 10, 210, 50);
264 ApHeadingDialogCancelButton -> setLegend (gui_msg_CANCEL);
265 ApHeadingDialogCancelButton -> setCallback (ApHeadingDialog_Cancel);
268 FG_FINALIZE_PUI_DIALOG( ApHeadingDialog );
271 void ApAltitudeDialog_Cancel(puObject *)
273 ApAltitudeDialogInput -> rejectInput();
274 FG_POP_PUI_DIALOG( ApAltitudeDialog );
277 void ApAltitudeDialog_OK (puObject *me)
282 ApAltitudeDialogInput->getValue( &c );
286 if( scan_number( c, &NewAltitude) )
288 if ( !globals->get_autopilot()->get_AltitudeEnabled() ) {
289 globals->get_autopilot()->set_AltitudeEnabled( true );
291 globals->get_autopilot()->AltitudeSet( NewAltitude );
295 s += " is not a valid number.";
298 ApAltitudeDialog_Cancel(me);
299 if( error ) mkDialog(s.c_str());
302 void NewAltitude(puObject *cb)
304 float altitude = globals->get_autopilot()->get_TargetAltitude() * SG_METER_TO_FEET;
305 ApAltitudeDialogInput -> setValue( altitude );
306 ApAltitudeDialogInput -> acceptInput();
307 FG_PUSH_PUI_DIALOG( ApAltitudeDialog );
310 void NewAltitudeInit()
312 // printf("NewAltitudeInit\n");
313 char NewAltitudeLabel[] = "Enter New Altitude";
316 float alt = cur_fdm_state->get_Altitude();
318 if ( !strcmp(fgGetString("/sim/startup/units"), "meters")) {
319 alt *= SG_FEET_TO_METER;
323 (puGetDefaultLabelFont().getStringWidth( NewAltitudeLabel ) / 2);
325 // ApAltitudeDialog = new puDialogBox (150, 50);
326 ApAltitudeDialog = new puDialogBox (150, 200);
328 ApAltitudeDialogFrame = new puFrame (0, 0, 260, 150);
329 ApAltitudeDialogMessage = new puText (len, 110);
330 ApAltitudeDialogMessage -> setDefaultValue (NewAltitudeLabel);
331 ApAltitudeDialogMessage -> getDefaultValue (&s);
332 ApAltitudeDialogMessage -> setLabel (s);
334 ApAltitudeDialogInput = new puInput ( 50, 70, 210, 100 );
335 ApAltitudeDialogInput -> setValue ( alt );
336 // Uncomment the next line to have input active on startup
337 // ApAltitudeDialogInput -> acceptInput ( );
338 // cursor at begining or end of line ?
341 // ApAltitudeDialogInput -> setCursor ( len );
342 // ApAltitudeDialogInput -> setSelectRegion ( 5, 9 );
344 ApAltitudeDialogOkButton = new puOneShot (50, 10, 110, 50);
345 ApAltitudeDialogOkButton -> setLegend (gui_msg_OK);
346 ApAltitudeDialogOkButton -> makeReturnDefault (TRUE);
347 ApAltitudeDialogOkButton -> setCallback (ApAltitudeDialog_OK);
349 ApAltitudeDialogCancelButton = new puOneShot (140, 10, 210, 50);
350 ApAltitudeDialogCancelButton -> setLegend (gui_msg_CANCEL);
351 ApAltitudeDialogCancelButton -> setCallback (ApAltitudeDialog_Cancel);
354 FG_FINALIZE_PUI_DIALOG( ApAltitudeDialog );
358 static void maxroll_adj( puObject *hs ) {
361 hs-> getValue ( &val ) ;
362 SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
363 // printf ( "maxroll_adj( %p ) %f %f\n", hs, val, MaxRollAdjust * val ) ;
364 globals->get_autopilot()->set_MaxRoll( MaxRollAdjust * val );
365 sprintf( SliderText[ 0 ], "%05.2f", globals->get_autopilot()->get_MaxRoll() );
366 APAdjustMaxRollText -> setLabel ( SliderText[ 0 ] ) ;
369 static void rollout_adj( puObject *hs ) {
372 hs-> getValue ( &val ) ;
373 SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
374 // printf ( "rollout_adj( %p ) %f %f\n", hs, val, RollOutAdjust * val ) ;
375 globals->get_autopilot()->set_RollOut( RollOutAdjust * val );
376 sprintf( SliderText[ 1 ], "%05.2f", globals->get_autopilot()->get_RollOut() );
377 APAdjustRollOutText -> setLabel ( SliderText[ 1 ] );
380 static void maxaileron_adj( puObject *hs ) {
383 hs-> getValue ( &val ) ;
384 SG_CLAMP_RANGE ( val, 0.1f, 1.0f ) ;
385 // printf ( "maxaileron_adj( %p ) %f %f\n", hs, val, MaxAileronAdjust * val ) ;
386 globals->get_autopilot()->set_MaxAileron( MaxAileronAdjust * val );
387 sprintf( SliderText[ 3 ], "%05.2f", globals->get_autopilot()->get_MaxAileron() );
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 globals->get_autopilot()->set_RollOutSmooth( RollOutSmoothAdjust * val );
398 sprintf( SliderText[ 2 ], "%5.2f", globals->get_autopilot()->get_RollOutSmooth() );
399 APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
403 static void goAwayAPAdjust (puObject *)
405 FG_POP_PUI_DIALOG( APAdjustDialog );
408 void cancelAPAdjust( puObject *self ) {
409 globals->get_autopilot()->set_MaxRoll( TmpMaxRollValue );
410 globals->get_autopilot()->set_RollOut( TmpRollOutValue );
411 globals->get_autopilot()->set_MaxAileron( TmpMaxAileronValue );
412 globals->get_autopilot()->set_RollOutSmooth( TmpRollOutSmoothValue );
414 goAwayAPAdjust(self);
417 void resetAPAdjust( puObject *self ) {
418 globals->get_autopilot()->set_MaxRoll( MaxRollAdjust / 2 );
419 globals->get_autopilot()->set_RollOut( RollOutAdjust / 2 );
420 globals->get_autopilot()->set_MaxAileron( MaxAileronAdjust / 2 );
421 globals->get_autopilot()->set_RollOutSmooth( RollOutSmoothAdjust / 2 );
423 FG_POP_PUI_DIALOG( APAdjustDialog );
428 void fgAPAdjust( puObject *self ) {
429 TmpMaxRollValue = globals->get_autopilot()->get_MaxRoll();
430 TmpRollOutValue = globals->get_autopilot()->get_RollOut();
431 TmpMaxAileronValue = globals->get_autopilot()->get_MaxAileron();
432 TmpRollOutSmoothValue = globals->get_autopilot()->get_RollOutSmooth();
434 MaxRollValue = globals->get_autopilot()->get_MaxRoll() / MaxRollAdjust;
435 RollOutValue = globals->get_autopilot()->get_RollOut() / RollOutAdjust;
436 MaxAileronValue = globals->get_autopilot()->get_MaxAileron() / MaxAileronAdjust;
437 RollOutSmoothValue = globals->get_autopilot()->get_RollOutSmooth()
438 / RollOutSmoothAdjust;
440 APAdjustHS0-> setValue ( MaxRollValue ) ;
441 APAdjustHS1-> setValue ( RollOutValue ) ;
442 APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
443 APAdjustHS3-> setValue ( MaxAileronValue ) ;
445 FG_PUSH_PUI_DIALOG( APAdjustDialog );
448 // Done once at system initialization
449 void fgAPAdjustInit() {
451 // printf("fgAPAdjustInit\n");
452 #define HORIZONTAL FALSE
456 int DialogWidth = 230;
458 char Label[] = "AutoPilot Adjust";
461 int labelX = (DialogWidth / 2) -
462 (puGetDefaultLabelFont().getStringWidth( Label ) / 2);
463 labelX -= 30; // KLUDGEY
468 int slider_width = 210;
469 int slider_title_x = 15;
470 int slider_value_x = 160;
471 float slider_delta = 0.1f;
473 TmpMaxRollValue = globals->get_autopilot()->get_MaxRoll();
474 TmpRollOutValue = globals->get_autopilot()->get_RollOut();
475 TmpMaxAileronValue = globals->get_autopilot()->get_MaxAileron();
476 TmpRollOutSmoothValue = globals->get_autopilot()->get_RollOutSmooth();
478 MaxRollAdjust = 2 * globals->get_autopilot()->get_MaxRoll();
479 RollOutAdjust = 2 * globals->get_autopilot()->get_RollOut();
480 MaxAileronAdjust = 2 * globals->get_autopilot()->get_MaxAileron();
481 RollOutSmoothAdjust = 2 * globals->get_autopilot()->get_RollOutSmooth();
483 MaxRollValue = globals->get_autopilot()->get_MaxRoll() / MaxRollAdjust;
484 RollOutValue = globals->get_autopilot()->get_RollOut() / RollOutAdjust;
485 MaxAileronValue = globals->get_autopilot()->get_MaxAileron() / MaxAileronAdjust;
486 RollOutSmoothValue = globals->get_autopilot()->get_RollOutSmooth()
487 / RollOutSmoothAdjust;
489 puGetDefaultFonts ( &APAdjustLegendFont, &APAdjustLabelFont );
490 APAdjustDialog = new puDialogBox ( DialogX, DialogY ); {
491 int horiz_slider_height = APAdjustLabelFont.getStringHeight() +
492 APAdjustLabelFont.getStringDescender() +
493 PUSTR_TGAP + PUSTR_BGAP + 5;
495 APAdjustFrame = new puFrame ( 0, 0,
497 85 + nSliders * horiz_slider_height );
499 APAdjustDialogMessage = new puText ( labelX,
501 * horiz_slider_height );
502 APAdjustDialogMessage -> setDefaultValue ( Label );
503 APAdjustDialogMessage -> getDefaultValue ( &s );
504 APAdjustDialogMessage -> setLabel ( s );
506 APAdjustHS0 = new mySlider ( slider_x, slider_y,
507 slider_width, HORIZONTAL ) ;
508 APAdjustHS0-> setDelta ( slider_delta ) ;
509 APAdjustHS0-> setValue ( MaxRollValue ) ;
510 APAdjustHS0-> setCBMode ( PUSLIDER_DELTA ) ;
511 APAdjustHS0-> setCallback ( maxroll_adj ) ;
513 sprintf( SliderText[ 0 ], "%05.2f", globals->get_autopilot()->get_MaxRoll() );
514 APAdjustMaxRollTitle = new puText ( slider_title_x, slider_y ) ;
515 APAdjustMaxRollTitle-> setDefaultValue ( "MaxRoll" ) ;
516 APAdjustMaxRollTitle-> getDefaultValue ( &s ) ;
517 APAdjustMaxRollTitle-> setLabel ( s ) ;
518 APAdjustMaxRollText = new puText ( slider_value_x, slider_y ) ;
519 APAdjustMaxRollText-> setLabel ( SliderText[ 0 ] ) ;
521 slider_y += horiz_slider_height;
523 APAdjustHS1 = new mySlider ( slider_x, slider_y, slider_width,
525 APAdjustHS1-> setDelta ( slider_delta ) ;
526 APAdjustHS1-> setValue ( RollOutValue ) ;
527 APAdjustHS1-> setCBMode ( PUSLIDER_DELTA ) ;
528 APAdjustHS1-> setCallback ( rollout_adj ) ;
530 sprintf( SliderText[ 1 ], "%05.2f", globals->get_autopilot()->get_RollOut() );
531 APAdjustRollOutTitle = new puText ( slider_title_x, slider_y ) ;
532 APAdjustRollOutTitle-> setDefaultValue ( "AdjustRollOut" ) ;
533 APAdjustRollOutTitle-> getDefaultValue ( &s ) ;
534 APAdjustRollOutTitle-> setLabel ( s ) ;
535 APAdjustRollOutText = new puText ( slider_value_x, slider_y ) ;
536 APAdjustRollOutText-> setLabel ( SliderText[ 1 ] );
538 slider_y += horiz_slider_height;
540 APAdjustHS2 = new mySlider ( slider_x, slider_y, slider_width,
542 APAdjustHS2-> setDelta ( slider_delta ) ;
543 APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
544 APAdjustHS2-> setCBMode ( PUSLIDER_DELTA ) ;
545 APAdjustHS2-> setCallback ( rolloutsmooth_adj ) ;
547 sprintf( SliderText[ 2 ], "%5.2f",
548 globals->get_autopilot()->get_RollOutSmooth() );
549 APAdjustRollOutSmoothTitle = new puText ( slider_title_x, slider_y ) ;
550 APAdjustRollOutSmoothTitle-> setDefaultValue ( "RollOutSmooth" ) ;
551 APAdjustRollOutSmoothTitle-> getDefaultValue ( &s ) ;
552 APAdjustRollOutSmoothTitle-> setLabel ( s ) ;
553 APAdjustRollOutSmoothText = new puText ( slider_value_x, slider_y ) ;
554 APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
556 slider_y += horiz_slider_height;
558 APAdjustHS3 = new mySlider ( slider_x, slider_y, slider_width,
560 APAdjustHS3-> setDelta ( slider_delta ) ;
561 APAdjustHS3-> setValue ( MaxAileronValue ) ;
562 APAdjustHS3-> setCBMode ( PUSLIDER_DELTA ) ;
563 APAdjustHS3-> setCallback ( maxaileron_adj ) ;
565 sprintf( SliderText[ 3 ], "%05.2f",
566 globals->get_autopilot()->get_MaxAileron() );
567 APAdjustMaxAileronTitle = new puText ( slider_title_x, slider_y ) ;
568 APAdjustMaxAileronTitle-> setDefaultValue ( "MaxAileron" ) ;
569 APAdjustMaxAileronTitle-> getDefaultValue ( &s ) ;
570 APAdjustMaxAileronTitle-> setLabel ( s ) ;
571 APAdjustMaxAileronText = new puText ( slider_value_x, slider_y ) ;
572 APAdjustMaxAileronText-> setLabel ( SliderText[ 3 ] );
574 APAdjustOkButton = new puOneShot ( 10, 10, 60, 50 );
575 APAdjustOkButton-> setLegend ( gui_msg_OK );
576 APAdjustOkButton-> makeReturnDefault ( TRUE );
577 APAdjustOkButton-> setCallback ( goAwayAPAdjust );
579 APAdjustCancelButton = new puOneShot ( 70, 10, 150, 50 );
580 APAdjustCancelButton-> setLegend ( gui_msg_CANCEL );
581 APAdjustCancelButton-> setCallback ( cancelAPAdjust );
583 APAdjustResetButton = new puOneShot ( 160, 10, 220, 50 );
584 APAdjustResetButton-> setLegend ( gui_msg_RESET );
585 APAdjustResetButton-> setCallback ( resetAPAdjust );
587 FG_FINALIZE_PUI_DIALOG( APAdjustDialog );
592 // Simple Dialog to input Target Airport
593 void TgtAptDialog_Cancel(puObject *)
595 FG_POP_PUI_DIALOG( TgtAptDialog );
598 void TgtAptDialog_OK (puObject *)
602 // FGTime *t = FGTime::cur_time_params;
603 // int PauseMode = t->getPause();
605 // t->togglePauseMode();
608 TgtAptDialogInput->getValue(&s);
612 unsigned int pos = tmp.find( "@" );
613 if ( pos != string::npos ) {
614 TgtAptId = tmp.substr( 0, pos );
615 string alt_str = tmp.substr( pos + 1 );
616 alt = atof( alt_str.c_str() );
617 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
618 alt *= SG_FEET_TO_METER;
624 TgtAptDialog_Cancel( NULL );
629 if ( fgFindAirportID( TgtAptId, &a ) ) {
631 SG_LOG( SG_GENERAL, SG_INFO,
632 "Adding waypoint (airport) = " << TgtAptId );
634 sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
636 SGWayPoint wp( a.longitude, a.latitude, alt,
637 SGWayPoint::WGS84, TgtAptId );
638 globals->get_route()->add_waypoint( wp );
640 /* and turn on the autopilot */
641 globals->get_autopilot()->set_HeadingEnabled( true );
642 globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_HEADING_WAYPOINT );
644 } else if ( current_fixlist->query( TgtAptId, 0.0, 0.0, 0.0,
647 SG_LOG( SG_GENERAL, SG_INFO,
648 "Adding waypoint (fix) = " << TgtAptId );
650 sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
652 SGWayPoint wp( f.get_lon(), f.get_lat(), alt,
653 SGWayPoint::WGS84, TgtAptId );
654 globals->get_route()->add_waypoint( wp );
656 /* and turn on the autopilot */
657 globals->get_autopilot()->set_HeadingEnabled( true );
658 globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_HEADING_WAYPOINT );
660 TgtAptId += " not in database.";
661 mkDialog(TgtAptId.c_str());
665 void TgtAptDialog_Reset(puObject *)
667 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id") );
668 TgtAptDialogInput->setValue ( NewTgtAirportId );
669 TgtAptDialogInput->setCursor( 0 ) ;
672 void TgtAptDialog_HandleSlider ( puObject * slider )
675 slider -> getValue ( &val ) ;
678 int index = int ( TgtAptDialogWPList -> getNumItems () * val ) ;
679 TgtAptDialogWPList -> setTopItem ( index ) ;
682 void TgtAptDialog_HandleArrow( puObject *arrow )
684 int type = ((puArrowButton *)arrow)->getArrowType() ;
685 int inc = ( type == PUARROW_DOWN ) ? 1 :
686 ( type == PUARROW_UP ) ? -1 :
687 ( type == PUARROW_FASTDOWN ) ? 10 :
688 ( type == PUARROW_FASTUP ) ? -10 : 0 ;
691 TgtAptDialogSlider -> getValue ( &val ) ;
693 int num_items = TgtAptDialogWPList->getNumItems () - 1 ;
696 int index = int ( num_items * val + 0.5 ) + inc ;
697 if ( index > num_items ) index = num_items ;
698 if ( index < 0 ) index = 0 ;
700 TgtAptDialogSlider -> setValue ( 1.0f - (float)index / num_items ) ;
701 TgtAptDialogWPList -> setTopItem ( index ) ;
706 void AddWayPoint(puObject *cb)
708 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id") );
709 TgtAptDialogInput->setValue( NewTgtAirportId );
711 /* refresh waypoint list */
715 if ( WPList != NULL ) {
716 for (i = 0; i < WPListsize; i++ ) {
721 if ( globals->get_route()->size() > 0 ) {
722 WPListsize = globals->get_route()->size();
723 WPList = new char* [ WPListsize + 1 ];
724 for (i = 0; i < globals->get_route()->size(); i++ ) {
725 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());
726 WPList [i] = new char[ strlen(WPString) ];
727 strcpy ( WPList [i], WPString );
731 WPList = new char* [ 2 ];
732 WPList [0] = new char[18];
733 strcpy ( WPList [0], "** List Empty **");
735 WPList [ WPListsize ] = NULL;
736 TgtAptDialogWPList->newList( WPList );
738 // if non-empty list, adjust the size of the slider...
739 TgtAptDialogSlider->setSliderFraction (0.9999f) ;
740 TgtAptDialogSlider->hide();
741 TgtAptDialogUPArrow->hide();
742 TgtAptDialogDNArrow->hide();
743 if (WPListsize > 10) {
744 TgtAptDialogSlider->setSliderFraction (10.0f/(WPListsize-1)) ;
745 TgtAptDialogSlider->reveal();
746 TgtAptDialogUPArrow->reveal();
747 TgtAptDialogDNArrow->reveal();
750 FG_PUSH_PUI_DIALOG( TgtAptDialog );
753 void PopWayPoint(puObject *cb)
755 globals->get_route()->delete_first();
757 // see if there are more waypoints on the list
758 if ( globals->get_route()->size() ) {
760 globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_HEADING_WAYPOINT );
763 globals->get_autopilot()->set_HeadingMode( FGAutopilot::FG_TC_HEADING_LOCK );
765 // use current heading
766 globals->get_autopilot()
767 ->set_TargetHeading(fgGetDouble("/orientation/heading-deg"));
771 void ClearRoute(puObject *cb)
773 globals->get_route()->clear();
776 void NewTgtAirportInit()
778 SG_LOG( SG_AUTOPILOT, SG_INFO, " enter NewTgtAirportInit()" );
779 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id") );
780 SG_LOG( SG_AUTOPILOT, SG_INFO, " NewTgtAirportId " << NewTgtAirportId );
782 - puGetDefaultLabelFont().getStringWidth( NewTgtAirportLabel ) / 2;
784 TgtAptDialog = new puDialogBox (150, 350);
786 TgtAptDialogFrame = new puFrame (0,0,350, 350);
788 TgtAptDialogWPList = new puListBox ( 50, 130, 300, 320 ) ;
789 TgtAptDialogWPList -> setLabel ( "Flight Plan" );
790 TgtAptDialogWPList -> setLabelPlace ( PUPLACE_ABOVE ) ;
791 TgtAptDialogWPList -> setStyle ( -PUSTYLE_SMALL_SHADED ) ;
792 TgtAptDialogWPList -> setValue ( 0 ) ;
794 TgtAptDialogSlider = new puSlider (300, 150, 150 ,TRUE,20);
795 TgtAptDialogSlider->setValue(1.0f);
796 TgtAptDialogSlider->setSliderFraction (0.2f) ;
797 TgtAptDialogSlider->setDelta(0.1f);
798 TgtAptDialogSlider->setCBMode( PUSLIDER_DELTA );
799 TgtAptDialogSlider->setCallback( TgtAptDialog_HandleSlider );
801 TgtAptDialogUPArrow = new puArrowButton ( 300, 300, 320, 320, PUARROW_UP ) ;
802 TgtAptDialogUPArrow->setCallback ( TgtAptDialog_HandleArrow ) ;
804 TgtAptDialogDNArrow = new puArrowButton ( 300, 130, 320, 150, PUARROW_DOWN ) ;
805 TgtAptDialogDNArrow->setCallback ( TgtAptDialog_HandleArrow ) ;
808 TgtAptDialogInput = new puInput (50, 70, 300, 100);
809 TgtAptDialogInput -> setLabel ( NewTgtAirportLabel );
810 TgtAptDialogInput -> setLabelPlace ( PUPLACE_ABOVE ) ;
811 TgtAptDialogInput -> setValue (NewTgtAirportId);
812 TgtAptDialogInput -> acceptInput();
814 TgtAptDialogOkButton = new puOneShot (50, 10, 110, 50);
815 TgtAptDialogOkButton -> setLegend (gui_msg_OK);
816 TgtAptDialogOkButton -> setCallback (TgtAptDialog_OK);
817 TgtAptDialogOkButton -> makeReturnDefault(TRUE);
819 TgtAptDialogCancelButton = new puOneShot (140, 10, 210, 50);
820 TgtAptDialogCancelButton -> setLegend (gui_msg_CANCEL);
821 TgtAptDialogCancelButton -> setCallback (TgtAptDialog_Cancel);
823 TgtAptDialogResetButton = new puOneShot (240, 10, 300, 50);
824 TgtAptDialogResetButton -> setLegend (gui_msg_RESET);
825 TgtAptDialogResetButton -> setCallback (TgtAptDialog_Reset);
829 FG_FINALIZE_PUI_DIALOG( TgtAptDialog );
830 printf("leave NewTgtAirportInit()");