1 /**************************************************************************
4 * Written 1998 by Durk Talsma, started Juni, 1998. For the flight gear
7 * Additional mouse supported added by David Megginson, 1999.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 **************************************************************************/
31 #include <Include/compiler.h>
33 #ifdef FG_MATH_EXCEPTION_CLASH
44 #if defined(FX) && defined(XMESA)
45 # include <GL/xmesa.h>
53 #include <Include/general.hxx>
54 #include <Include/fg_constants.h>
55 #include <Debug/logstream.hxx>
56 #include <Aircraft/aircraft.hxx>
57 #include <Airports/simple.hxx>
58 #include <Cockpit/panel.hxx>
59 #include <Controls/controls.hxx>
60 #include <FDM/flight.hxx>
61 #include <Main/options.hxx>
62 #include <Main/fg_init.hxx>
63 #include <Main/views.hxx>
64 #include <Misc/fgpath.hxx>
65 #include <Network/network.h>
66 #include <Screen/screen-dump.hxx>
67 #include <Time/fg_time.hxx>
69 #if defined( WIN32 ) && !defined( __CYGWIN__ )
70 # include <Screen/win32-printer.h>
75 * A virtual trackball implementation
76 * Written by Gavin Bell for Silicon Graphics, November 1988.
79 * Pass the x and y coordinates of the last and current positions of
80 * the mouse, scaled so they are from (-1.0 ... 1.0).
82 * The resulting rotation is returned as a quaternion rotation in the
86 trackball(float q[4], float p1x, float p1y, float p2x, float p2y);
89 * Given two quaternions, add them together to get a third quaternion.
90 * Adding quaternions to get a compound rotation is analagous to adding
91 * translations to get a compound translation. When incrementally
92 * adding rotations, the first argument here should be the new
94 * rotation, the second and third the total rotation (which will be
95 * over-written with the resulting new total rotation).
98 add_quats(float *q1, float *q2, float *dest);
101 * A useful function, builds a rotation matrix in Matrix based on
105 build_rotmatrix(float m[4][4], float q[4]);
108 * This function computes a quaternion based on an axis (defined by
109 * the given vector) and an angle about which to rotate. The angle is
110 * expressed in radians. The result is put into the third argument.
113 axis_to_quat(float a[3], float phi, float q[4]);
118 FG_USING_STD(string);
120 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
124 #if defined(WIN32) || defined(__CYGWIN32__)
125 #define WIN32_CURSOR_TWEAKS
126 #elif (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 9)
127 #define X_CURSOR_TWEAKS
130 // hack, should come from an include someplace
131 extern void fgInitVisuals( void );
132 extern void fgReshape( int width, int height );
133 extern void fgRenderFrame( void );
136 fntTexFont *guiFntHandle = 0;
138 static puMenuBar *mainMenuBar = 0;
139 //static puButton *hideMenuButton = 0;
141 static puDialogBox *dialogBox = 0;
142 static puFrame *dialogFrame = 0;
143 static puText *dialogBoxMessage = 0;
144 static puOneShot *dialogBoxOkButton = 0;
147 static puDialogBox *YNdialogBox = 0;
148 static puFrame *YNdialogFrame = 0;
149 static puText *YNdialogBoxMessage = 0;
150 static puOneShot *YNdialogBoxOkButton = 0;
151 static puOneShot *YNdialogBoxNoButton = 0;
153 static char msg_OK[] = "OK";
154 static char msg_NO[] = "NO";
155 static char msg_YES[] = "YES";
156 static char msg_CANCEL[] = "Cancel";
157 static char msg_RESET[] = "Reset";
159 char *gui_msg_OK; // "OK"
160 char *gui_msg_NO; // "NO"
161 char *gui_msg_YES; // "YES"
162 char *gui_msg_CANCEL; // "CANCEL"
163 char *gui_msg_RESET; // "RESET"
165 static char global_dialog_string[256];
167 // from autopilot.cxx
168 extern void NewAltitude( puObject *cb );
169 extern void NewHeading( puObject *cb );
170 extern void fgAPAdjust( puObject * );
171 extern void NewTgtAirport( puObject *cb );
172 bool fgAPTerrainFollowEnabled( void );
173 bool fgAPAltitudeEnabled( void );
174 bool fgAPHeadingEnabled( void );
175 bool fgAPWayPointEnabled( void );
176 bool fgAPAutoThrottleEnabled( void );
179 extern void fgLatLonFormatToggle( puObject *);
181 /* --------------------------------------------------------------------
183 ---------------------------------------------------------------------*/
187 static int _savedX = 0;
188 static int _savedY = 0;
189 static int last_buttons = 0 ;
190 static int mouse_active = 0;
191 static int menu_on = 0;
192 static int mouse_joystick_control = 0;
194 //static time_t mouse_off_time;
195 //static int mouse_timed_out;
197 // to allow returning to previous view
198 // on second left click in MOUSE_VIEW mode
199 // This has file scope so that it can be reset
200 // if the little rodent is moved NHV
201 static int _mVtoggle;
203 // we break up the glutGetModifiers return mask
204 // once per loop and stash what we need in these
205 static int glut_active_shift;
206 static int glut_active_ctrl;
207 static int glut_active_alt;
209 static float lastquat[4];
210 static float curquat[4];
211 static float _quat0[4];
212 float quat_mat[4][4];
214 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
215 // else smooth out the view panning to .01 radian per frame
216 // see view_offset smoothing mechanism in main.cxx
217 #define NO_SMOOTH_MOUSE_VIEW
219 // uncomment following to
220 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
222 /* --------------------------------------------------------------------
223 Support for mouse as control yoke (david@megginson.com)
225 - right button toggles between pointer and yoke
226 - horizontal drag with no buttons moves ailerons
227 - vertical drag with no buttons moves elevators
228 - horizontal drag with left button moves brakes (left=on)
229 - vertical drag with left button moves throttle (up=more)
230 - horizontal drag with middle button moves rudder
231 - vertical drag with middle button moves trim
233 For the *_sensitivity variables, a lower number means more sensitive.
235 TODO: figure out how to keep pointer from leaving window in yoke mode.
236 TODO: add thresholds and null zones
237 TODO: sensitivity should be configurable at user option.
238 TODO: allow differential braking (this will be useful if FlightGear
239 ever supports tail-draggers like the DC-3)
240 ---------------------------------------------------------------------*/
248 MouseMode mouse_mode = MOUSE_POINTER;
250 static double aileron_sensitivity = 1.0/500.0;
251 static double elevator_sensitivity = 1.0/500.0;
252 static double brake_sensitivity = 1.0/250.0;
253 static double throttle_sensitivity = 1.0/250.0;
254 static double rudder_sensitivity = 1.0/500.0;
255 static double trim_sensitivity = 1.0/1000.0;
257 static inline void Quat0( void ) {
258 curquat[0] = _quat0[0];
259 curquat[1] = _quat0[1];
260 curquat[2] = _quat0[2];
261 curquat[3] = _quat0[3];
264 static inline int left_button( void ) {
265 return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
268 static inline int middle_button( void ) {
269 return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
272 static inline int right_button( void ) {
273 return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
276 static inline void TurnCursorOn( void )
279 #if defined(WIN32_CURSOR_TWEAKS)
280 switch (mouse_mode) {
282 glutSetCursor(GLUT_CURSOR_INHERIT);
285 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
288 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
292 #if defined(X_CURSOR_TWEAKS)
293 glutWarpPointer( current_view.get_winWidth()/2, current_view.get_winHeight()/2);
297 static inline void TurnCursorOff( void )
300 #if defined(WIN32_CURSOR_TWEAKS)
301 glutSetCursor(GLUT_CURSOR_NONE);
302 #elif defined(X_CURSOR_TWEAKS)
303 glutWarpPointer( current_view.get_winWidth(), current_view.get_winHeight());
307 void maybeToggleMouse( void )
309 #if defined(WIN32_CURSOR_TWEAKS)
310 static int first_time = ~0;
311 static int mouse_changed = 0;
315 mouse_changed = ~mouse_changed;
319 if( mouse_mode != MOUSE_POINTER )
321 if( mouse_changed ) {
322 mouse_changed = ~mouse_changed;
328 first_time = ~first_time;
329 #endif // #ifdef WIN32
332 // Call with FALSE to init and TRUE to restore
333 void BusyCursor( int restore )
335 static GLenum cursor = (GLenum) 0;
337 glutSetCursor(cursor);
339 cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
340 #if defined(WIN32_CURSOR_TWEAKS)
343 glutSetCursor( GLUT_CURSOR_WAIT );
347 int guiGetMouseButton(void)
352 void guiGetMouse(int *x, int *y)
358 void guiMotionFunc ( int x, int y )
360 int ww, wh, need_warp = 0;
363 // FGTime *t = FGTime::cur_time_params;
364 // if( mouse_timed_out ) {
365 // if( t->get_cur_time() > mouse_off_time ) {
366 // moused_timed_out = 0;
368 // glutPostRedisplay () ;
372 if (mouse_mode == MOUSE_POINTER) {
374 glutPostRedisplay () ;
376 if( x == _mX && y == _mY)
379 // reset left click MOUSE_VIEW toggle feature
382 ww = current_view.get_winWidth();
383 wh = current_view.get_winHeight();
385 switch (mouse_mode) {
387 if( !mouse_joystick_control ) {
388 mouse_joystick_control = 1;
389 current_options.set_control_mode( fgOPTIONS::FG_MOUSE );
391 if ( left_button() ) {
392 offset = (_mX - x) * brake_sensitivity;
393 controls.move_brake(FGControls::ALL_WHEELS, offset);
394 offset = (_mY - y) * throttle_sensitivity;
395 controls.move_throttle(FGControls::ALL_ENGINES, offset);
396 } else if ( right_button() ) {
397 if( !fgAPHeadingEnabled() ) {
398 offset = (x - _mX) * rudder_sensitivity;
399 controls.move_rudder(offset);
401 if( !fgAPAltitudeEnabled() ) {
402 offset = (_mY - y) * trim_sensitivity;
403 controls.move_elevator_trim(offset);
406 if( !fgAPHeadingEnabled() ) {
407 offset = (x - _mX) * aileron_sensitivity;
408 controls.move_aileron(offset);
410 if( !fgAPAltitudeEnabled() ) {
411 offset = (_mY - y) * elevator_sensitivity;
412 controls.move_elevator(offset);
416 // Keep the mouse in the window.
417 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
426 #define CONTRAINED_MOUSE_VIEW_Y
427 #ifdef CONTRAINED_MOUSE_VIEW_Y
431 #endif // CONTRAINED_MOUSE_VIEW_Y
433 } else if( y >= wh-1) {
434 #ifdef CONTRAINED_MOUSE_VIEW_Y
438 #endif // CONTRAINED_MOUSE_VIEW_Y
441 // wrap MOUSE_VIEW mode cursor x position
445 } else if ( x >= ww-1 ) {
449 // try to get FG_PI movement in each half of screen
453 if( middle_button() ) {
455 (2.0f * _mX - W) / W,
456 0, //(H - 2.0f * y) / H, // 3
458 0 //(H - 2.0f * _mY) / H // 1
465 0, //(2.0f * _mX - W) / W, // 0
466 (H - 2.0f * y) / H, // 3
467 0, //(2.0f * x - W) / W, // 2
468 (H - 2.0f * _mY) / H // 1
471 add_quats(lastquat, curquat, curquat);
472 build_rotmatrix(quat_mat, curquat);
475 // this could be done in above quat
476 // but requires redoing view pipeline
477 offset = current_view.get_goal_view_offset();
478 offset += ((_mX - x) * FG_2PI / W );
479 while (offset < 0.0) {
482 while (offset > FG_2PI) {
485 current_view.set_goal_view_offset(offset);
486 #ifdef NO_SMOOTH_MOUSE_VIEW
487 current_view.set_view_offset(offset);
496 glutWarpPointer(x, y);
498 // Record the new mouse position.
504 void guiMouseFunc(int button, int updown, int x, int y)
508 // private MOUSE_VIEW state variables
509 // to allow alternate left clicks in MOUSE_VIEW mode
510 // to toggle between current offsets and straight ahead
512 static int _mVx, _mVy, _Vx, _Vy;
513 static float _quat[4];
514 static double _view_offset;
516 // general purpose variables
519 glutModifiers = glutGetModifiers();
520 glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
521 glut_active_ctrl = glutModifiers & GLUT_ACTIVE_CTRL;
522 glut_active_alt = glutModifiers & GLUT_ACTIVE_ALT;
524 // Was the left button pressed?
525 if (updown == GLUT_DOWN ) {
526 if( button == GLUT_LEFT_BUTTON)
528 switch (mouse_mode) {
535 // resume previous view offsets
540 curquat[0] = _quat[0];
541 curquat[1] = _quat[1];
542 curquat[2] = _quat[2];
543 curquat[3] = _quat[3];
544 current_view.set_goal_view_offset(_view_offset);
545 #ifdef NO_SMOOTH_MOUSE_VIEW
546 current_view.set_view_offset(_view_offset);
554 _quat[0] = curquat[0];
555 _quat[1] = curquat[1];
556 _quat[2] = curquat[2];
557 _quat[3] = curquat[3];
558 x = current_view.get_winWidth()/2;
559 y = current_view.get_winHeight()/2;
561 _view_offset = current_view.get_goal_view_offset();
562 current_view.set_goal_view_offset(0.0);
563 #ifdef NO_SMOOTH_MOUSE_VIEW
564 current_view.set_view_offset(0.0);
567 glutWarpPointer( x , y);
568 build_rotmatrix(quat_mat, curquat);
569 _mVtoggle = ~_mVtoggle;
572 }else if ( button == GLUT_RIGHT_BUTTON) {
573 switch (mouse_mode) {
575 mouse_mode = MOUSE_YOKE;
576 mouse_joystick_control = 0;
579 // start with zero point in center of screen
580 _mX = current_view.get_winWidth()/2;
581 _mY = current_view.get_winHeight()/2;
583 // try to have the MOUSE_YOKE position
584 // reflect the current stick position
585 offset = controls.get_aileron();
586 x = _mX - (int)(offset * aileron_sensitivity);
587 offset = controls.get_elevator();
588 y = _mY - (int)(offset * elevator_sensitivity);
590 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
591 FG_LOG( FG_INPUT, FG_INFO, "Mouse in yoke mode" );
595 mouse_mode = MOUSE_VIEW;
596 current_options.set_control_mode( fgOPTIONS::FG_JOYSTICK );
597 x = current_view.get_winWidth()/2;
598 y = current_view.get_winHeight()/2;
601 build_rotmatrix(quat_mat, curquat);
602 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
603 FG_LOG( FG_INPUT, FG_INFO, "Mouse in view mode" );
607 mouse_mode = MOUSE_POINTER;
610 #ifdef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
612 build_rotmatrix(quat_mat, curquat);
613 current_view.set_goal_view_offset(0.0);
614 #ifdef NO_SMOOTH_MOUSE_VIEW
615 current_view.set_view_offset(0.0);
617 #endif // RESET_VIEW_ON_LEAVING_MOUSE_VIEW
618 glutSetCursor(GLUT_CURSOR_INHERIT);
623 FG_LOG( FG_INPUT, FG_INFO, "Mouse in pointer mode" );
626 glutWarpPointer( x, y );
627 } // END RIGHT BUTTON
628 } // END UPDOWN == GLUT_DOWN
630 // Note which button is pressed.
631 if ( updown == GLUT_DOWN ) {
632 last_buttons |= ( 1 << button ) ;
634 last_buttons &= ~( 1 << button ) ;
637 // If we're in pointer mode, let PUI
638 // know what's going on.
639 if (mouse_mode == MOUSE_POINTER) {
640 puMouse (button, updown, x,y);
643 // Register the new position (if it
644 // hasn't been registered already).
648 glutPostRedisplay ();
651 /* ================ General Purpose Functions ================ */
653 // Intercept the Escape Key
654 void ConfirmExitDialog(void)
656 FG_PUSH_PUI_DIALOG( YNdialogBox );
659 // General Purpose Message Box
660 void mkDialog (const char *txt)
662 strncpy(global_dialog_string, txt, 256);
663 dialogBoxMessage->setLabel(global_dialog_string);
664 FG_PUSH_PUI_DIALOG( dialogBox );
667 // Repair any damage done to the Panel by other Gui Items
668 void guiFixPanel( void )
672 if ( current_options.get_panel_status() ) {
673 FGView *v = ¤t_view;
674 FGTime *t = FGTime::cur_time_params;
676 if( (toggle_pause = !t->getPause()) )
677 t->togglePauseMode();
679 // this seems to be the only way to do this :-(
680 // problem is the viewport has been mucked with
681 xglViewport(0, 0 , (GLint)(v->winWidth), (GLint)(v->winHeight) );
682 FGPanel::OurPanel->ReInit(0, 0, 1024, 768);
685 t->togglePauseMode();
689 // Toggle the Menu and Mouse display state
690 void guiToggleMenu(void)
693 // printf("Hiding Menu\n");
694 mainMenuBar->hide ();
695 #if defined(WIN32_CURSOR_TWEAKS)
696 if( mouse_mode == MOUSE_POINTER )
698 #endif // #ifdef WIN32_CURSOR_TWEAKS
700 // printf("Showing Menu\n");
701 mainMenuBar->reveal();
704 #endif // #ifdef WIN32
709 /* -----------------------------------------------------------------------
710 the Gui callback functions
711 ____________________________________________________________________*/
713 void reInit(puObject *cb)
717 build_rotmatrix(quat_mat, curquat);
718 fgReInitSubsystems();
722 // This is the accessor function
723 void guiTogglePanel(puObject *cb)
725 current_options.toggle_panel();
728 //void MenuHideMenuCb(puObject *cb)
729 void hideMenuCb (puObject *cb)
734 void goodBye(puObject *)
736 // FG_LOG( FG_INPUT, FG_ALERT,
737 // "Program exiting normally at user request." );
738 cout << "Program exiting normally at user request." << endl;
747 void goAwayCb (puObject *me)
749 FG_POP_PUI_DIALOG( dialogBox );
752 void mkDialogInit (void)
754 // printf("mkDialogInit\n");
755 int x = (current_options.get_xsize()/2 - 400/2);
756 int y = (current_options.get_ysize()/2 - 100/2);
757 dialogBox = new puDialogBox (x, y); // 150, 50
759 dialogFrame = new puFrame (0,0,400,100);
760 dialogBoxMessage = new puText (10, 70);
761 dialogBoxMessage -> setLabel ("");
762 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
763 dialogBoxOkButton -> setLegend (gui_msg_OK);
764 dialogBoxOkButton -> makeReturnDefault (TRUE );
765 dialogBoxOkButton -> setCallback (goAwayCb);
767 FG_FINALIZE_PUI_DIALOG( dialogBox );
770 void MayBeGoodBye(puObject *)
775 void goAwayYesNoCb(puObject *me)
777 FG_POP_PUI_DIALOG( YNdialogBox);
780 void ConfirmExitDialogInit(void)
782 char msg[] = "Really Quit";
785 // printf("ConfirmExitDialogInit\n");
786 int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
788 int x = (current_options.get_xsize()/2 - 400/2);
789 int y = (current_options.get_ysize()/2 - 100/2);
791 YNdialogBox = new puDialogBox (x, y); // 150, 50
792 // YNdialogBox = new puDialogBox (150, 50);
794 YNdialogFrame = new puFrame (0,0,400, 100);
796 YNdialogBoxMessage = new puText (len, 70);
797 YNdialogBoxMessage -> setDefaultValue (msg);
798 YNdialogBoxMessage -> getDefaultValue (&s);
799 YNdialogBoxMessage -> setLabel (s);
801 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
802 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
803 YNdialogBoxOkButton -> setCallback (goodBye);
805 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
806 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
807 // YNdialogBoxNoButton -> makeReturnDefault (TRUE );
808 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
810 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
813 void notCb (puObject *)
815 mkDialog ("This function isn't implemented yet");
818 void helpCb (puObject *)
822 #if defined(FX) && !defined(WIN32)
823 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
824 if ( global_fullscreen ) {
825 global_fullscreen = false;
826 XMesaSetFXmode( XMESA_FX_WINDOW );
832 string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
834 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
835 command = "netscape -remote \"openURL(" + url + ")\" &";
837 command = "netscape " + url + " &";
840 command = "webrun.bat";
843 system( command.c_str() );
844 //string text = "Help started in netscape window.";
846 //mkDialog (text.c_str());
847 mkDialog ("Help started in netscape window.");
852 #if defined( WIN32 ) && !defined( __CYGWIN__)
853 // win32 print screen function
854 void printScreen ( puObject *obj ) {
855 bool show_pu_cursor = false;
857 if ( !puCursorIsHidden() ) {
858 show_pu_cursor = true;
865 p.Begin( "FlightGear" );
867 fgReshape( p.GetHorzRes(), p.GetVertRes() );
872 mainMenuBar->reveal();
875 if ( show_pu_cursor ) {
880 #endif // #ifdef WIN32
883 // do a screen snap shot
884 void dumpSnapShot ( puObject *obj ) {
885 bool show_pu_cursor = false;
889 if ( !puCursorIsHidden() ) {
890 show_pu_cursor = true;
895 fgReshape( current_options.get_xsize(), current_options.get_ysize() );
897 // we need two render frames here to clear the menu and cursor
898 // ... not sure why but doing an extra fgFenderFrame() shoulnd't
903 my_glDumpWindow( "fgfs-screen.ppm",
904 current_options.get_xsize(),
905 current_options.get_ysize() );
907 mkDialog ("Snap shot saved to fgfs-screen.ppm");
909 if ( show_pu_cursor ) {
915 mainMenuBar->reveal();
921 /// The beginnings of teleportation :-)
922 // Needs cleaning up but works
923 // These statics should disapear when this is a class
924 static puDialogBox *AptDialog = 0;
925 static puFrame *AptDialogFrame = 0;
926 static puText *AptDialogMessage = 0;
927 static puInput *AptDialogInput = 0;
929 static char NewAirportId[16];
930 static char NewAirportLabel[] = "Enter New Airport ID";
932 static puOneShot *AptDialogOkButton = 0;
933 static puOneShot *AptDialogCancelButton = 0;
934 static puOneShot *AptDialogResetButton = 0;
936 void AptDialog_Cancel(puObject *)
938 FG_POP_PUI_DIALOG( AptDialog );
941 void AptDialog_OK (puObject *)
946 FGTime *t = FGTime::cur_time_params;
947 int PauseMode = t->getPause();
949 t->togglePauseMode();
952 AptDialogInput->getValue(&s);
955 cout << "AptDialog_OK " << AptId << " " << AptId.length() << endl;
957 AptDialog_Cancel( NULL );
959 if ( AptId.length() ) {
960 // set initial position from airport id
961 FG_LOG( FG_GENERAL, FG_INFO,
962 "Attempting to set starting position from airport code "
965 airports.load("apt_simple");
966 if ( airports.search( AptId, &a ) )
968 current_options.set_airport_id( AptId.c_str() );
970 fgReInitSubsystems();
973 AptId += " not in database.";
974 mkDialog(AptId.c_str());
977 if( PauseMode != t->getPause() )
978 t->togglePauseMode();
981 void AptDialog_Reset(puObject *)
983 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
984 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
985 AptDialogInput->setValue ( NewAirportId );
986 AptDialogInput->setCursor( 0 ) ;
989 void NewAirport(puObject *cb)
991 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
992 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
993 // cout << "NewAirport " << NewAirportId << endl;
994 AptDialogInput->setValue( NewAirportId );
996 FG_PUSH_PUI_DIALOG( AptDialog );
999 static void NewAirportInit(void)
1001 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1002 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1003 NewAirportLabel ) / 2;
1005 AptDialog = new puDialogBox (150, 50);
1007 AptDialogFrame = new puFrame (0,0,350, 150);
1008 AptDialogMessage = new puText (len, 110);
1009 AptDialogMessage -> setLabel (NewAirportLabel);
1011 AptDialogInput = new puInput (50, 70, 300, 100);
1012 AptDialogInput -> setValue (NewAirportId);
1013 AptDialogInput -> acceptInput();
1015 AptDialogOkButton = new puOneShot (50, 10, 110, 50);
1016 AptDialogOkButton -> setLegend (gui_msg_OK);
1017 AptDialogOkButton -> setCallback (AptDialog_OK);
1018 AptDialogOkButton -> makeReturnDefault(TRUE);
1020 AptDialogCancelButton = new puOneShot (140, 10, 210, 50);
1021 AptDialogCancelButton -> setLegend (gui_msg_CANCEL);
1022 AptDialogCancelButton -> setCallback (AptDialog_Cancel);
1024 AptDialogResetButton = new puOneShot (240, 10, 300, 50);
1025 AptDialogResetButton -> setLegend (gui_msg_RESET);
1026 AptDialogResetButton -> setCallback (AptDialog_Reset);
1028 cout << "NewAirportInit " << NewAirportId << endl;
1029 FG_FINALIZE_PUI_DIALOG( AptDialog );
1032 /// The beginnings of networking :-)
1033 // Needs cleaning up but works
1034 // These statics should disapear when this is a class
1035 static puDialogBox *NetIdDialog = 0;
1036 static puFrame *NetIdDialogFrame = 0;
1037 static puText *NetIdDialogMessage = 0;
1038 static puInput *NetIdDialogInput = 0;
1040 static char NewNetId[16];
1041 static char NewNetIdLabel[] = "Enter New Callsign";
1043 static puOneShot *NetIdDialogOkButton = 0;
1044 static puOneShot *NetIdDialogCancelButton = 0;
1046 void NetIdDialog_Cancel(puObject *)
1048 FG_POP_PUI_DIALOG( NetIdDialog );
1051 void NetIdDialog_OK (puObject *)
1055 FGTime *t = FGTime::cur_time_params;
1056 int PauseMode = t->getPause();
1058 t->togglePauseMode();
1060 The following needs some cleanup because
1061 "string options.NetId" and "char *net_callsign"
1063 NetIdDialogInput->getValue(&net_callsign);
1064 NetId = net_callsign;
1066 NetIdDialog_Cancel( NULL );
1067 current_options.set_net_id( NetId.c_str() );
1068 /* Entering a callsign indicates : user wants Net HUD Info */
1069 net_hud_display = 1;
1071 if( PauseMode != t->getPause() )
1072 t->togglePauseMode();
1075 void NewCallSign(puObject *cb)
1077 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1078 NetIdDialogInput->setValue( NewNetId );
1080 FG_PUSH_PUI_DIALOG( NetIdDialog );
1083 static void NewNetIdInit(void)
1085 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1086 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1087 NewNetIdLabel ) / 2;
1089 NetIdDialog = new puDialogBox (150, 50);
1091 NetIdDialogFrame = new puFrame (0,0,350, 150);
1092 NetIdDialogMessage = new puText (len, 110);
1093 NetIdDialogMessage -> setLabel (NewNetIdLabel);
1095 NetIdDialogInput = new puInput (50, 70, 300, 100);
1096 NetIdDialogInput -> setValue (NewNetId);
1097 NetIdDialogInput -> acceptInput();
1099 NetIdDialogOkButton = new puOneShot (50, 10, 110, 50);
1100 NetIdDialogOkButton -> setLegend (gui_msg_OK);
1101 NetIdDialogOkButton -> setCallback (NetIdDialog_OK);
1102 NetIdDialogOkButton -> makeReturnDefault(TRUE);
1104 NetIdDialogCancelButton = new puOneShot (240, 10, 300, 50);
1105 NetIdDialogCancelButton -> setLegend (gui_msg_CANCEL);
1106 NetIdDialogCancelButton -> setCallback (NetIdDialog_Cancel);
1109 FG_FINALIZE_PUI_DIALOG( NetIdDialog );
1112 static void net_display_toggle( puObject *cb)
1114 net_hud_display = (net_hud_display) ? 0 : 1;
1115 printf("Toggle net_hud_display : %d\n", net_hud_display);
1118 static void net_blaster_toggle( puObject *cb)
1120 net_blast_toggle = (net_blast_toggle) ? 0 : -1;
1121 printf("Toggle net_blast : %d\n", net_blast_toggle);
1124 /*************** End Networking **************/
1128 /* -----------------------------------------------------------------------
1130 ---------------------------------------------------------------------*/
1131 char *fileSubmenu [] = {
1132 "Exit", /* "Close", "---------", */
1133 #if defined( WIN32 ) && !defined( __CYGWIN__)
1137 /* "---------", "Save", */
1140 puCallback fileSubmenuCb [] = {
1141 MayBeGoodBye, /* hideMenuCb, NULL, */
1142 #if defined( WIN32 ) && !defined( __CYGWIN__)
1151 char *editSubmenu [] = {
1154 puCallback editSubmenuCb [] = {
1159 char *viewSubmenu [] = {
1160 /* "Cockpit View > ", "View >","------------", */ "Toggle Panel...", NULL
1162 puCallback viewSubmenuCb [] = {
1163 /* notCb, notCb, NULL, guiTogglePanel, */ NULL
1166 char *aircraftSubmenu [] = {
1167 "Autopilot", "Heading", "Altitude", "Navigation", "Airport",
1168 /* "Communication", */ NULL
1170 puCallback aircraftSubmenuCb [] = {
1171 fgAPAdjust, NewHeading, NewAltitude, fgLatLonFormatToggle, NewTgtAirport,
1175 char *environmentSubmenu [] = {
1176 "Airport", /* "Terrain", "Weather", */ NULL
1178 puCallback environmentSubmenuCb [] = {
1179 NewAirport, /* notCb, notCb, */ NULL
1183 char *optionsSubmenu [] = {
1184 "Preferences", "Realism & Reliablity...", NULL
1186 puCallback optionsSubmenuCb [] = {
1191 #ifdef FG_NETWORK_OLK
1192 char *networkSubmenu [] = {
1193 /* "Unregister from FGD ", "Send MSG to All", "Send MSG", "Show Pilots", */
1194 /* "Register to FGD", */
1195 /* "Scan for Deamons", */ "Enter Callsign", /* "Display Netinfos", */
1199 puCallback networkSubmenuCb [] = {
1200 /* notCb, notCb, notCb, notCb, notCb, notCb, */ NewCallSign, /* notCb, */
1201 net_display_toggle, net_blaster_toggle, NULL
1205 char *helpSubmenu [] = {
1206 /* "About...", */ "Help", NULL
1208 puCallback helpSubmenuCb [] = {
1209 /* notCb, */ helpCb, NULL
1213 /* -------------------------------------------------------------------------
1215 _____________________________________________________________________*/
1220 char *mesa_win_state;
1224 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
1225 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
1227 // Initialize our GLOBAL GUI STRINGS
1228 gui_msg_OK = msg_OK; // "OK"
1229 gui_msg_NO = msg_NO; // "NO"
1230 gui_msg_YES = msg_YES; // "YES"
1231 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
1232 gui_msg_RESET = msg_RESET; // "RESET"
1234 // Next check home directory
1236 char* envp = ::getenv( "FG_FONTS" );
1237 if ( envp != NULL ) {
1238 fntpath.set( envp );
1240 fntpath.set( current_options.get_fg_root() );
1241 fntpath.append( "Fonts" );
1244 // Install our fast fonts
1245 fntpath.append( "typewriter.txf" );
1246 guiFntHandle = new fntTexFont ;
1247 guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
1248 puFont GuiFont ( guiFntHandle, 15 ) ;
1249 puSetDefaultFonts( GuiFont, GuiFont ) ;
1250 guiFnt = puGetDefaultLabelFont();
1252 if ( current_options.get_mouse_pointer() == 0 ) {
1253 // no preference specified for mouse pointer, attempt to autodetect...
1254 // Determine if we need to render the cursor, or if the windowing
1255 // system will do it. First test if we are rendering with glide.
1256 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1257 // Test for the MESA_GLX_FX env variable
1258 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1259 // test if we are fullscreen mesa/glide
1260 if ( (mesa_win_state[0] == 'f') ||
1261 (mesa_win_state[0] == 'F') ) {
1266 mouse_active = ~mouse_active;
1267 } else if ( current_options.get_mouse_pointer() == 1 ) {
1268 // don't show pointer
1269 } else if ( current_options.get_mouse_pointer() == 2 ) {
1270 // force showing pointer
1272 mouse_active = ~mouse_active;
1275 // MOUSE_VIEW mode stuff
1276 trackball(_quat0, 0.0, 0.0, 0.0, 0.0);
1278 build_rotmatrix(quat_mat, curquat);
1280 // Set up our Dialog Boxes
1281 ConfirmExitDialogInit();
1283 #ifdef FG_NETWORK_OLK
1288 // Make the menu bar
1289 mainMenuBar = new puMenuBar ();
1290 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
1291 // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
1292 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
1293 mainMenuBar -> add_submenu ("Aircraft", aircraftSubmenu, aircraftSubmenuCb);
1294 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
1295 // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
1296 #ifdef FG_NETWORK_OLK
1297 mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
1299 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
1300 mainMenuBar-> close ();
1301 // Set up menu bar toggle
1310 * Implementation of a virtual trackball.
1311 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
1312 * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
1314 * Vector manip code:
1316 * Original code from:
1317 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
1319 * Much mucking with by:
1322 #if defined(_WIN32) && !defined( __CYGWIN32__ )
1323 #pragma warning (disable:4244) /* disable bogus conversion warnings */
1327 //#include "trackball.h"
1330 * This size should really be based on the distance from the center of
1331 * rotation to the point on the object underneath the mouse. That
1332 * point would then track the mouse as closely as possible. This is a
1333 * simple example, though, so that is left as an Exercise for the
1336 #define TRACKBALLSIZE (0.8f)
1337 #define SQRT(x) sqrt(x)
1340 * Local function prototypes (not defined in trackball.h)
1342 static float tb_project_to_sphere(float, float, float);
1343 static void normalize_quat(float [4]);
1354 vset(float *v, float x, float y, float z)
1362 vsub(const float *src1, const float *src2, float *dst)
1364 dst[0] = src1[0] - src2[0];
1365 dst[1] = src1[1] - src2[1];
1366 dst[2] = src1[2] - src2[2];
1370 vcopy(const float *v1, float *v2)
1373 for (i = 0 ; i < 3 ; i++)
1378 vcross(const float *v1, const float *v2, float *cross)
1382 temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
1383 temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
1384 temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
1389 vlength(const float *v)
1391 float tmp = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1396 vscale(float *v, float div)
1406 vscale(v,1.0/vlength(v));
1410 vdot(const float *v1, const float *v2)
1412 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
1416 vadd(const float *src1, const float *src2, float *dst)
1418 dst[0] = src1[0] + src2[0];
1419 dst[1] = src1[1] + src2[1];
1420 dst[2] = src1[2] + src2[2];
1424 * Given an axis and angle, compute quaternion.
1427 axis_to_quat(float a[3], float phi, float q[4])
1429 double sinphi2, cosphi2;
1430 double phi2 = phi/2.0;
1431 sinphi2 = sin(phi2);
1432 cosphi2 = cos(phi2);
1440 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
1441 * if we are away from the center of the sphere.
1444 tb_project_to_sphere(float r, float x, float y)
1450 if (d < r * 0.70710678118654752440) { /* Inside sphere */
1453 } else { /* On hyperbola */
1454 t = r / 1.41421356237309504880;
1461 * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
1462 * If they don't add up to 1.0, dividing by their magnitued will
1465 * Note: See the following for more information on quaternions:
1467 * - Shoemake, K., Animating rotation with quaternion curves, Computer
1468 * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
1469 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
1470 * graphics, The Visual Computer 5, 2-13, 1989.
1473 normalize_quat(float q[4])
1478 tmp = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
1479 mag = 1.0 / SQRT(tmp);
1480 for (i = 0; i < 4; i++)
1485 * Ok, simulate a track-ball. Project the points onto the virtual
1486 * trackball, then figure out the axis of rotation, which is the cross
1487 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
1488 * Note: This is a deformed trackball-- is a trackball in the center,
1489 * but is deformed into a hyperbolic sheet of rotation away from the
1490 * center. This particular function was chosen after trying out
1491 * several variations.
1493 * It is assumed that the arguments to this routine are in the range
1497 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
1499 float a[3]; /* Axis of rotation */
1500 float phi; /* how much to rotate about axis */
1501 float p1[3], p2[3], d[3];
1504 if (p1x == p2x && p1y == p2y) {
1512 * First, figure out z-coordinates for projection of P1 and P2 to
1515 vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
1516 vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
1519 * Now, we want the cross product of P1 and P2
1524 * Figure out how much to rotate around that axis.
1527 t = vlength(d) / (2.0*TRACKBALLSIZE);
1530 * Avoid problems with out-of-control values...
1532 if (t > 1.0) t = 1.0;
1533 if (t < -1.0) t = -1.0;
1534 phi = 2.0 * asin(t);
1536 axis_to_quat(a,phi,q);
1540 * Given two rotations, e1 and e2, expressed as quaternion rotations,
1541 * figure out the equivalent single rotation and stuff it into dest.
1543 * This routine also normalizes the result every RENORMCOUNT times it is
1544 * called, to keep error from creeping in.
1546 * NOTE: This routine is written so that q1 or q2 may be the same
1547 * as dest (or each other).
1550 #define RENORMCOUNT 97
1553 add_quats(float q1[4], float q2[4], float dest[4])
1556 float t1[4], t2[4], t3[4];
1560 printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]);
1561 printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]);
1573 tf[3] = q1[3] * q2[3] - vdot(q1,q2);
1576 printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
1584 if (++count > RENORMCOUNT) {
1586 normalize_quat(dest);
1591 * Build a rotation matrix, given a quaternion rotation.
1595 build_rotmatrix(float m[4][4], float q[4])
1597 //#define TRANSPOSED_QUAT
1598 #ifndef TRANSPOSED_QUAT
1599 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1600 m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1601 m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1604 m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1605 m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1606 m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1609 m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1610 m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1611 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
1618 #else // TRANSPOSED_QUAT
1619 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1620 m[0][1] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1621 m[0][2] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1624 m[1][0] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1625 m[1][1] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1626 m[1][2] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1629 m[2][0] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1630 m[2][1] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1631 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);