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;
864 CGlPrinter p( CGlPrinter::READ_BITMAP );
865 int cur_width = current_view.get_winWidth( );
866 int cur_height = current_view.get_winHeight( );
867 p.Begin( "FlightGear" );
869 fgReshape( cur_width, cur_height );
870 //fgReshape( p.GetHorzRes(), p.GetVertRes() );
876 mainMenuBar->reveal();
879 if ( show_pu_cursor ) {
884 #endif // #ifdef WIN32
887 void dumpSnapShot ( puObject *obj ) {
892 // do a screen snap shot
893 void fgDumpSnapShot () {
894 bool show_pu_cursor = false;
898 if ( !puCursorIsHidden() ) {
899 show_pu_cursor = true;
904 fgReshape( current_options.get_xsize(), current_options.get_ysize() );
906 // we need two render frames here to clear the menu and cursor
907 // ... not sure why but doing an extra fgFenderFrame() shoulnd't
912 my_glDumpWindow( "fgfs-screen.ppm",
913 current_options.get_xsize(),
914 current_options.get_ysize() );
916 mkDialog ("Snap shot saved to fgfs-screen.ppm");
918 if ( show_pu_cursor ) {
924 mainMenuBar->reveal();
930 /// The beginnings of teleportation :-)
931 // Needs cleaning up but works
932 // These statics should disapear when this is a class
933 static puDialogBox *AptDialog = 0;
934 static puFrame *AptDialogFrame = 0;
935 static puText *AptDialogMessage = 0;
936 static puInput *AptDialogInput = 0;
938 static char NewAirportId[16];
939 static char NewAirportLabel[] = "Enter New Airport ID";
941 static puOneShot *AptDialogOkButton = 0;
942 static puOneShot *AptDialogCancelButton = 0;
943 static puOneShot *AptDialogResetButton = 0;
945 void AptDialog_Cancel(puObject *)
947 FG_POP_PUI_DIALOG( AptDialog );
950 void AptDialog_OK (puObject *)
955 FGTime *t = FGTime::cur_time_params;
956 int PauseMode = t->getPause();
958 t->togglePauseMode();
961 AptDialogInput->getValue(&s);
964 cout << "AptDialog_OK " << AptId << " " << AptId.length() << endl;
966 AptDialog_Cancel( NULL );
968 if ( AptId.length() ) {
969 // set initial position from airport id
970 FG_LOG( FG_GENERAL, FG_INFO,
971 "Attempting to set starting position from airport code "
974 airports.load("apt_simple");
975 if ( airports.search( AptId, &a ) )
977 current_options.set_airport_id( AptId.c_str() );
979 fgReInitSubsystems();
982 AptId += " not in database.";
983 mkDialog(AptId.c_str());
986 if( PauseMode != t->getPause() )
987 t->togglePauseMode();
990 void AptDialog_Reset(puObject *)
992 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
993 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
994 AptDialogInput->setValue ( NewAirportId );
995 AptDialogInput->setCursor( 0 ) ;
998 void NewAirport(puObject *cb)
1000 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1001 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1002 // cout << "NewAirport " << NewAirportId << endl;
1003 AptDialogInput->setValue( NewAirportId );
1005 FG_PUSH_PUI_DIALOG( AptDialog );
1008 static void NewAirportInit(void)
1010 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1011 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1012 NewAirportLabel ) / 2;
1014 AptDialog = new puDialogBox (150, 50);
1016 AptDialogFrame = new puFrame (0,0,350, 150);
1017 AptDialogMessage = new puText (len, 110);
1018 AptDialogMessage -> setLabel (NewAirportLabel);
1020 AptDialogInput = new puInput (50, 70, 300, 100);
1021 AptDialogInput -> setValue (NewAirportId);
1022 AptDialogInput -> acceptInput();
1024 AptDialogOkButton = new puOneShot (50, 10, 110, 50);
1025 AptDialogOkButton -> setLegend (gui_msg_OK);
1026 AptDialogOkButton -> setCallback (AptDialog_OK);
1027 AptDialogOkButton -> makeReturnDefault(TRUE);
1029 AptDialogCancelButton = new puOneShot (140, 10, 210, 50);
1030 AptDialogCancelButton -> setLegend (gui_msg_CANCEL);
1031 AptDialogCancelButton -> setCallback (AptDialog_Cancel);
1033 AptDialogResetButton = new puOneShot (240, 10, 300, 50);
1034 AptDialogResetButton -> setLegend (gui_msg_RESET);
1035 AptDialogResetButton -> setCallback (AptDialog_Reset);
1037 cout << "NewAirportInit " << NewAirportId << endl;
1038 FG_FINALIZE_PUI_DIALOG( AptDialog );
1041 /// The beginnings of networking :-)
1042 // Needs cleaning up but works
1043 // These statics should disapear when this is a class
1044 static puDialogBox *NetIdDialog = 0;
1045 static puFrame *NetIdDialogFrame = 0;
1046 static puText *NetIdDialogMessage = 0;
1047 static puInput *NetIdDialogInput = 0;
1049 static char NewNetId[16];
1050 static char NewNetIdLabel[] = "Enter New Callsign";
1052 static puOneShot *NetIdDialogOkButton = 0;
1053 static puOneShot *NetIdDialogCancelButton = 0;
1055 void NetIdDialog_Cancel(puObject *)
1057 FG_POP_PUI_DIALOG( NetIdDialog );
1060 void NetIdDialog_OK (puObject *)
1064 FGTime *t = FGTime::cur_time_params;
1065 int PauseMode = t->getPause();
1067 t->togglePauseMode();
1069 The following needs some cleanup because
1070 "string options.NetId" and "char *net_callsign"
1072 NetIdDialogInput->getValue(&net_callsign);
1073 NetId = net_callsign;
1075 NetIdDialog_Cancel( NULL );
1076 current_options.set_net_id( NetId.c_str() );
1077 /* Entering a callsign indicates : user wants Net HUD Info */
1078 net_hud_display = 1;
1080 if( PauseMode != t->getPause() )
1081 t->togglePauseMode();
1084 void NewCallSign(puObject *cb)
1086 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1087 NetIdDialogInput->setValue( NewNetId );
1089 FG_PUSH_PUI_DIALOG( NetIdDialog );
1092 static void NewNetIdInit(void)
1094 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1095 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1096 NewNetIdLabel ) / 2;
1098 NetIdDialog = new puDialogBox (150, 50);
1100 NetIdDialogFrame = new puFrame (0,0,350, 150);
1101 NetIdDialogMessage = new puText (len, 110);
1102 NetIdDialogMessage -> setLabel (NewNetIdLabel);
1104 NetIdDialogInput = new puInput (50, 70, 300, 100);
1105 NetIdDialogInput -> setValue (NewNetId);
1106 NetIdDialogInput -> acceptInput();
1108 NetIdDialogOkButton = new puOneShot (50, 10, 110, 50);
1109 NetIdDialogOkButton -> setLegend (gui_msg_OK);
1110 NetIdDialogOkButton -> setCallback (NetIdDialog_OK);
1111 NetIdDialogOkButton -> makeReturnDefault(TRUE);
1113 NetIdDialogCancelButton = new puOneShot (240, 10, 300, 50);
1114 NetIdDialogCancelButton -> setLegend (gui_msg_CANCEL);
1115 NetIdDialogCancelButton -> setCallback (NetIdDialog_Cancel);
1118 FG_FINALIZE_PUI_DIALOG( NetIdDialog );
1121 static void net_display_toggle( puObject *cb)
1123 net_hud_display = (net_hud_display) ? 0 : 1;
1124 printf("Toggle net_hud_display : %d\n", net_hud_display);
1127 static void net_blaster_toggle( puObject *cb)
1129 net_blast_toggle = (net_blast_toggle) ? 0 : -1;
1130 printf("Toggle net_blast : %d\n", net_blast_toggle);
1133 /*************** End Networking **************/
1137 /* -----------------------------------------------------------------------
1139 ---------------------------------------------------------------------*/
1140 char *fileSubmenu [] = {
1141 "Exit", /* "Close", "---------", */
1142 #if defined( WIN32 ) && !defined( __CYGWIN__)
1146 /* "---------", "Save", */
1149 puCallback fileSubmenuCb [] = {
1150 MayBeGoodBye, /* hideMenuCb, NULL, */
1151 #if defined( WIN32 ) && !defined( __CYGWIN__)
1160 char *editSubmenu [] = {
1163 puCallback editSubmenuCb [] = {
1168 char *viewSubmenu [] = {
1169 /* "Cockpit View > ", "View >","------------", */ "Toggle Panel...", NULL
1171 puCallback viewSubmenuCb [] = {
1172 /* notCb, notCb, NULL, guiTogglePanel, */ NULL
1175 char *aircraftSubmenu [] = {
1176 "Autopilot", "Heading", "Altitude", "Navigation", "Airport",
1177 /* "Communication", */ NULL
1179 puCallback aircraftSubmenuCb [] = {
1180 fgAPAdjust, NewHeading, NewAltitude, fgLatLonFormatToggle, NewTgtAirport,
1184 char *environmentSubmenu [] = {
1185 "Airport", /* "Terrain", "Weather", */ NULL
1187 puCallback environmentSubmenuCb [] = {
1188 NewAirport, /* notCb, notCb, */ NULL
1192 char *optionsSubmenu [] = {
1193 "Preferences", "Realism & Reliablity...", NULL
1195 puCallback optionsSubmenuCb [] = {
1200 #ifdef FG_NETWORK_OLK
1201 char *networkSubmenu [] = {
1202 /* "Unregister from FGD ", "Send MSG to All", "Send MSG", "Show Pilots", */
1203 /* "Register to FGD", */
1204 /* "Scan for Deamons", */ "Enter Callsign", /* "Display Netinfos", */
1208 puCallback networkSubmenuCb [] = {
1209 /* notCb, notCb, notCb, notCb, notCb, notCb, */ NewCallSign, /* notCb, */
1210 net_display_toggle, net_blaster_toggle, NULL
1214 char *helpSubmenu [] = {
1215 /* "About...", */ "Help", NULL
1217 puCallback helpSubmenuCb [] = {
1218 /* notCb, */ helpCb, NULL
1222 /* -------------------------------------------------------------------------
1224 _____________________________________________________________________*/
1229 char *mesa_win_state;
1233 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
1234 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
1236 // Initialize our GLOBAL GUI STRINGS
1237 gui_msg_OK = msg_OK; // "OK"
1238 gui_msg_NO = msg_NO; // "NO"
1239 gui_msg_YES = msg_YES; // "YES"
1240 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
1241 gui_msg_RESET = msg_RESET; // "RESET"
1243 // Next check home directory
1245 char* envp = ::getenv( "FG_FONTS" );
1246 if ( envp != NULL ) {
1247 fntpath.set( envp );
1249 fntpath.set( current_options.get_fg_root() );
1250 fntpath.append( "Fonts" );
1253 // Install our fast fonts
1254 fntpath.append( "typewriter.txf" );
1255 guiFntHandle = new fntTexFont ;
1256 guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
1257 puFont GuiFont ( guiFntHandle, 15 ) ;
1258 puSetDefaultFonts( GuiFont, GuiFont ) ;
1259 guiFnt = puGetDefaultLabelFont();
1261 if ( current_options.get_mouse_pointer() == 0 ) {
1262 // no preference specified for mouse pointer, attempt to autodetect...
1263 // Determine if we need to render the cursor, or if the windowing
1264 // system will do it. First test if we are rendering with glide.
1265 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1266 // Test for the MESA_GLX_FX env variable
1267 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1268 // test if we are fullscreen mesa/glide
1269 if ( (mesa_win_state[0] == 'f') ||
1270 (mesa_win_state[0] == 'F') ) {
1275 mouse_active = ~mouse_active;
1276 } else if ( current_options.get_mouse_pointer() == 1 ) {
1277 // don't show pointer
1278 } else if ( current_options.get_mouse_pointer() == 2 ) {
1279 // force showing pointer
1281 mouse_active = ~mouse_active;
1284 // MOUSE_VIEW mode stuff
1285 trackball(_quat0, 0.0, 0.0, 0.0, 0.0);
1287 build_rotmatrix(quat_mat, curquat);
1289 // Set up our Dialog Boxes
1290 ConfirmExitDialogInit();
1292 #ifdef FG_NETWORK_OLK
1297 // Make the menu bar
1298 mainMenuBar = new puMenuBar ();
1299 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
1300 // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
1301 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
1302 mainMenuBar -> add_submenu ("Aircraft", aircraftSubmenu, aircraftSubmenuCb);
1303 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
1304 // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
1305 #ifdef FG_NETWORK_OLK
1306 mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
1308 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
1309 mainMenuBar-> close ();
1310 // Set up menu bar toggle
1319 * Implementation of a virtual trackball.
1320 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
1321 * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
1323 * Vector manip code:
1325 * Original code from:
1326 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
1328 * Much mucking with by:
1331 #if defined(_WIN32) && !defined( __CYGWIN32__ )
1332 #pragma warning (disable:4244) /* disable bogus conversion warnings */
1336 //#include "trackball.h"
1339 * This size should really be based on the distance from the center of
1340 * rotation to the point on the object underneath the mouse. That
1341 * point would then track the mouse as closely as possible. This is a
1342 * simple example, though, so that is left as an Exercise for the
1345 #define TRACKBALLSIZE (0.8f)
1346 #define SQRT(x) sqrt(x)
1349 * Local function prototypes (not defined in trackball.h)
1351 static float tb_project_to_sphere(float, float, float);
1352 static void normalize_quat(float [4]);
1363 vset(float *v, float x, float y, float z)
1371 vsub(const float *src1, const float *src2, float *dst)
1373 dst[0] = src1[0] - src2[0];
1374 dst[1] = src1[1] - src2[1];
1375 dst[2] = src1[2] - src2[2];
1379 vcopy(const float *v1, float *v2)
1382 for (i = 0 ; i < 3 ; i++)
1387 vcross(const float *v1, const float *v2, float *cross)
1391 temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
1392 temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
1393 temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
1398 vlength(const float *v)
1400 float tmp = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1405 vscale(float *v, float div)
1415 vscale(v,1.0/vlength(v));
1419 vdot(const float *v1, const float *v2)
1421 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
1425 vadd(const float *src1, const float *src2, float *dst)
1427 dst[0] = src1[0] + src2[0];
1428 dst[1] = src1[1] + src2[1];
1429 dst[2] = src1[2] + src2[2];
1433 * Given an axis and angle, compute quaternion.
1436 axis_to_quat(float a[3], float phi, float q[4])
1438 double sinphi2, cosphi2;
1439 double phi2 = phi/2.0;
1440 sinphi2 = sin(phi2);
1441 cosphi2 = cos(phi2);
1449 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
1450 * if we are away from the center of the sphere.
1453 tb_project_to_sphere(float r, float x, float y)
1459 if (d < r * 0.70710678118654752440) { /* Inside sphere */
1462 } else { /* On hyperbola */
1463 t = r / 1.41421356237309504880;
1470 * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
1471 * If they don't add up to 1.0, dividing by their magnitued will
1474 * Note: See the following for more information on quaternions:
1476 * - Shoemake, K., Animating rotation with quaternion curves, Computer
1477 * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
1478 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
1479 * graphics, The Visual Computer 5, 2-13, 1989.
1482 normalize_quat(float q[4])
1487 tmp = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
1488 mag = 1.0 / SQRT(tmp);
1489 for (i = 0; i < 4; i++)
1494 * Ok, simulate a track-ball. Project the points onto the virtual
1495 * trackball, then figure out the axis of rotation, which is the cross
1496 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
1497 * Note: This is a deformed trackball-- is a trackball in the center,
1498 * but is deformed into a hyperbolic sheet of rotation away from the
1499 * center. This particular function was chosen after trying out
1500 * several variations.
1502 * It is assumed that the arguments to this routine are in the range
1506 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
1508 float a[3]; /* Axis of rotation */
1509 float phi; /* how much to rotate about axis */
1510 float p1[3], p2[3], d[3];
1513 if (p1x == p2x && p1y == p2y) {
1521 * First, figure out z-coordinates for projection of P1 and P2 to
1524 vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
1525 vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
1528 * Now, we want the cross product of P1 and P2
1533 * Figure out how much to rotate around that axis.
1536 t = vlength(d) / (2.0*TRACKBALLSIZE);
1539 * Avoid problems with out-of-control values...
1541 if (t > 1.0) t = 1.0;
1542 if (t < -1.0) t = -1.0;
1543 phi = 2.0 * asin(t);
1545 axis_to_quat(a,phi,q);
1549 * Given two rotations, e1 and e2, expressed as quaternion rotations,
1550 * figure out the equivalent single rotation and stuff it into dest.
1552 * This routine also normalizes the result every RENORMCOUNT times it is
1553 * called, to keep error from creeping in.
1555 * NOTE: This routine is written so that q1 or q2 may be the same
1556 * as dest (or each other).
1559 #define RENORMCOUNT 97
1562 add_quats(float q1[4], float q2[4], float dest[4])
1565 float t1[4], t2[4], t3[4];
1569 printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]);
1570 printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]);
1582 tf[3] = q1[3] * q2[3] - vdot(q1,q2);
1585 printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
1593 if (++count > RENORMCOUNT) {
1595 normalize_quat(dest);
1600 * Build a rotation matrix, given a quaternion rotation.
1604 build_rotmatrix(float m[4][4], float q[4])
1606 //#define TRANSPOSED_QUAT
1607 #ifndef TRANSPOSED_QUAT
1608 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1609 m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1610 m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1613 m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1614 m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1615 m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1618 m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1619 m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1620 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
1627 #else // TRANSPOSED_QUAT
1628 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1629 m[0][1] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1630 m[0][2] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1633 m[1][0] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1634 m[1][1] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1635 m[1][2] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1638 m[2][0] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1639 m[2][1] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1640 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);