]> git.mxchange.org Git - flightgear.git/blob - src/GUI/mouse.cxx
- Some refactoring of the configure.ac script.
[flightgear.git] / src / GUI / mouse.cxx
1 /**************************************************************************
2  * gui.cxx
3  *
4  * Written 1998 by Durk Talsma, started Juni, 1998.  For the flight gear
5  * project.
6  *
7  * Additional mouse supported added by David Megginson, 1999.
8  *
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.
13  *
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.
18  *
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.
22  *
23  * $Id$
24  **************************************************************************/
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #ifdef SG_MATH_EXCEPTION_CLASH
34 #  include <math.h>
35 #endif
36
37 #ifdef HAVE_WINDOWS_H
38 #  include <windows.h>
39 #endif
40
41 #include GLUT_H
42
43 #if defined(FX) && defined(XMESA)
44 #  include <GL/xmesa.h>
45 #endif
46
47 #include STL_STRING
48
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include <simgear/constants.h>
53 #include <simgear/debug/logstream.hxx>
54 #include <simgear/misc/sg_path.hxx>
55 #include <simgear/screen/screen-dump.hxx>
56
57 #include <Include/general.hxx>
58 //#include <Include/fg_memory.h>
59 #include <Aircraft/aircraft.hxx>
60 #include <Airports/simple.hxx>
61 //#include <Autopilot/auto_gui.hxx>
62 #include <Autopilot/newauto.hxx>
63 #include <Cockpit/panel.hxx>
64 #include <Controls/controls.hxx>
65 #include <FDM/flight.hxx>
66 #include <Main/fg_init.hxx>
67 #include <Main/fg_props.hxx>
68 #include <Main/viewmgr.hxx>
69
70 #include "gui.h"
71 #include "gui_local.hxx"
72
73 SG_USING_STD(string);
74
75 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
76 SG_USING_STD(cout);
77 #endif
78
79 /* --------------------------------------------------------------------
80 Mouse stuff
81 ---------------------------------------------------------------------*/
82
83 static int _mX = 0;
84 static int _mY = 0;
85 static int _savedX = 0;
86 static int _savedY = 0;
87 static int last_buttons = 0 ;
88 static int mouse_active = 0;
89 static int mouse_joystick_control = 0;
90
91 //static time_t mouse_off_time;
92 //static int mouse_timed_out;
93
94 // to allow returning to previous view
95 // on second left click in MOUSE_VIEW mode
96 // This has file scope so that it can be reset
97 // if the little rodent is moved  NHV
98 static  int _mVtoggle = 0;
99
100 // we break up the glutGetModifiers return mask
101 // once per loop and stash what we need in these
102 static int glut_active_shift = 0;
103 static int glut_active_ctrl = 0;
104 static int glut_active_alt = 0;
105
106 static int MOUSE_XSIZE = 0;
107 static int MOUSE_YSIZE = 0;
108
109 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
110 // else smooth out the view panning to .01 radian per frame
111 // see view_offset smoothing mechanism in main.cxx
112 #define NO_SMOOTH_MOUSE_VIEW
113
114 // uncomment following to
115 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
116
117 /* --------------------------------------------------------------------
118 Support for mouse as control yoke (david@megginson.com)
119
120 - right button toggles between pointer and yoke
121 - horizontal drag with no buttons moves ailerons
122 - vertical drag with no buttons moves elevators
123 - horizontal drag with left button moves brakes (left=on)
124 - vertical drag with left button moves throttle (up=more)
125 - horizontal drag with middle button moves rudder
126 - vertical drag with middle button moves trim
127
128 For the *_sensitivity variables, a lower number means more sensitive.
129
130 TODO: figure out how to keep pointer from leaving window in yoke mode.
131 TODO: add thresholds and null zones
132 TODO: sensitivity should be configurable at user option.
133 TODO: allow differential braking (this will be useful if FlightGear
134       ever supports tail-draggers like the DC-3)
135 ---------------------------------------------------------------------*/
136
137 MouseMode mouse_mode = MOUSE_POINTER;
138
139 static double aileron_sensitivity = 1.0/500.0;
140 static double elevator_sensitivity = 1.0/500.0;
141 static double brake_sensitivity = 1.0/250.0;
142 static double throttle_sensitivity = 1.0/250.0;
143 static double rudder_sensitivity = 1.0/500.0;
144 static double trim_sensitivity = 1.0/1000.0;
145
146 void guiInitMouse(int width, int height)
147 {
148         MOUSE_XSIZE = width;
149         MOUSE_YSIZE = height;
150 }
151
152 static inline int guiGetMouseButton(void)
153 {
154         return last_buttons;
155 }
156
157 static inline void guiGetMouse(int *x, int *y)
158 {
159         *x = _mX;
160         *y = _mY;
161 };
162
163 static inline int left_button( void ) {
164     return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
165 }
166
167 static inline int middle_button( void ) {
168     return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
169 }
170
171 static inline int right_button( void ) {
172     return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
173 }
174
175 static inline void set_goal_view_offset( float offset )
176 {
177         globals->get_current_view()->setGoalHeadingOffset_deg(offset * SGD_RADIANS_TO_DEGREES);
178 }
179
180 static inline void set_view_offset( float offset )
181 {
182         globals->get_current_view()->setHeadingOffset_deg(offset * SGD_RADIANS_TO_DEGREES);
183 }
184
185 static inline void set_goal_view_tilt( float tilt )
186 {
187         globals->get_current_view()->setGoalPitchOffset_deg(tilt);
188 }
189
190 static inline void set_view_tilt( float tilt )
191 {
192         globals->get_current_view()->setPitchOffset_deg(tilt);
193 }
194
195 static inline float get_view_offset() {
196         return globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS;
197 }
198
199 static inline float get_goal_view_offset() {
200         return globals->get_current_view()->getGoalHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS;
201 }
202
203 static inline void move_brake(float offset) {
204         globals->get_controls()->move_brake(FGControls::ALL_WHEELS, offset);
205 }
206
207 static inline void move_throttle(float offset) {
208         globals->get_controls()->move_throttle(FGControls::ALL_ENGINES, offset);
209 }
210
211 static inline void move_rudder(float offset) {
212         globals->get_controls()->move_rudder(offset);
213 }
214
215 static inline void move_elevator_trim(float offset) {
216         globals->get_controls()->move_elevator_trim(offset);
217 }
218
219 static inline void move_aileron(float offset) {
220         globals->get_controls()->move_aileron(offset);
221 }
222
223 static inline void move_elevator(float offset) {
224         globals->get_controls()->move_elevator(offset);
225 }
226
227 static inline float get_aileron() {
228         return globals->get_controls()->get_aileron();
229 }
230
231 static inline float get_elevator() {
232         return globals->get_controls()->get_elevator();
233 }
234
235 static inline bool AP_HeadingEnabled() {
236         return globals->get_autopilot()->get_HeadingEnabled();
237 }
238
239 static inline bool AP_AltitudeEnabled() {
240         return globals->get_autopilot()->get_AltitudeEnabled();
241 }
242
243 void TurnCursorOn( void )
244 {
245     mouse_active = ~0;
246 #if defined(WIN32)
247     switch (mouse_mode) {
248         case MOUSE_POINTER:
249             glutSetCursor(GLUT_CURSOR_INHERIT);
250             break;
251         case MOUSE_YOKE:
252             glutSetCursor(GLUT_CURSOR_CROSSHAIR);
253             break;
254         case MOUSE_VIEW:
255             glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
256             break;
257     }
258 #endif
259 #if defined(X_CURSOR_TWEAKS)
260     glutWarpPointer( MOUSE_XSIZE/2,
261                      MOUSE_YSIZE/2);
262 #endif
263 }
264
265 void TurnCursorOff( void )
266 {
267     mouse_active = 0;
268 #if defined(WIN32_CURSOR_TWEAKS)
269     glutSetCursor(GLUT_CURSOR_NONE);
270 #elif defined(X_CURSOR_TWEAKS)
271     glutWarpPointer( MOUSE_XSIZE,
272                      MOUSE_YSIZE);
273 #endif
274 }
275
276 void maybeToggleMouse( void )
277 {
278 #if defined(WIN32_CURSOR_TWEAKS_OFF)
279     static int first_time = ~0;
280     static int mouse_changed = 0;
281
282     if ( first_time ) {
283         if(!mouse_active) {
284             mouse_changed = ~mouse_changed;
285             TurnCursorOn();
286         }
287     } else {
288         if( mouse_mode != MOUSE_POINTER )
289             return;
290         if( mouse_changed ) {
291             mouse_changed = ~mouse_changed;
292             if(mouse_active) {
293                 TurnCursorOff();
294             }
295         }
296     }
297     first_time = ~first_time;
298 #endif // #ifdef WIN32
299 }
300
301 // Call with FALSE to init and TRUE to restore
302 void BusyCursor( int restore )
303 {
304     static GLenum cursor = (GLenum) 0;
305     if( restore ) {
306         glutSetCursor(cursor);
307     } else {
308         cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
309 #if defined(WIN32_CURSOR_TWEAKS)
310         TurnCursorOn();
311 #endif
312         glutSetCursor( GLUT_CURSOR_WAIT );
313     }
314 }
315
316
317 // Center the view offsets
318 void CenterView( void ) {
319     if( mouse_mode == MOUSE_VIEW ) {
320         mouse_mode = MOUSE_POINTER;
321         _savedX = MOUSE_XSIZE/2;
322         _savedY = MOUSE_YSIZE/2;
323         _mVtoggle = 0;
324         Quat0();
325         build_rotmatrix(GuiQuat_mat, curGuiQuat);
326         glutSetCursor(GLUT_CURSOR_INHERIT);
327
328         // Is this necessary ??
329         if( !gui_menu_on )   TurnCursorOff();
330
331         glutWarpPointer( _savedX, _savedY );
332     }
333     set_goal_view_offset(0.0);
334     set_view_offset(0.0);
335 }
336
337
338 //#define TRANSLATE_HUD
339 // temporary hack until pitch_offset is added to view pipeline
340 void fgTranslateHud( void ) {
341 #ifdef TRANSLATE_HUD
342     if(mouse_mode == MOUSE_VIEW) {
343
344         int ww = MOUSE_XSIZE;
345         int wh = MOUSE_YSIZE;
346
347         float y = 4*(_mY-(wh/2));// * ((wh/SGD_PI)*SG_RADIANS_TO_DEGREES);
348         
349         float x =  get_view_offset() * SG_RADIANS_TO_DEGREES;
350
351         if( x < -180 )      x += 360;
352         else if( x > 180 )      x -= 360;
353
354         x *= ww/90.0;
355         //      x *= ww/180.0;
356         //      x *= ww/360.0;
357
358         //      glTranslatef( x*ww/640, y*wh/480, 0 );
359         glTranslatef( x*640/ww, y*480/wh, 0 );
360     }
361 #endif // TRANSLATE_HUD
362 }
363
364 void guiMotionFunc ( int x, int y )
365 {
366     int ww, wh, need_warp = 0;
367     float W, H;
368     double offset;
369
370     ww = MOUSE_XSIZE;
371     wh = MOUSE_YSIZE;
372
373     if (mouse_mode == MOUSE_POINTER) {
374         // TURN MENU ON IF MOUSE AT TOP
375         if( y < 1 ) {
376             if( !gui_menu_on )
377                 guiToggleMenu();                        
378         }
379         // TURN MENU OFF IF MOUSE AT BOTTOM
380         else if( y > wh-2 ) {
381             if( gui_menu_on )
382                 guiToggleMenu();                        
383         }
384         puMouse ( x, y ) ;
385         glutPostRedisplay () ;
386     } else {
387         if( x == _mX && y == _mY)
388             return;
389         
390         // reset left click MOUSE_VIEW toggle feature
391         _mVtoggle = 0;
392         
393         switch (mouse_mode) {
394             case MOUSE_YOKE:
395                 if( !mouse_joystick_control ) {
396                     mouse_joystick_control = 1;
397                     fgSetString("/sim/control-mode", "mouse");
398                 } else {
399                     if ( left_button() ) {
400                         move_brake(   (_mX - x) * brake_sensitivity);
401                         move_throttle((_mY - y) * throttle_sensitivity);
402                     } else if ( right_button() ) {
403                         if( ! AP_HeadingEnabled() ) {
404                             move_rudder((x - _mX) * rudder_sensitivity);
405                         }
406                         if( ! AP_AltitudeEnabled() ) {
407                             move_elevator_trim((_mY - y) * trim_sensitivity);
408                         }
409                     } else {
410                         if( ! AP_HeadingEnabled() ) {
411                             move_aileron((x - _mX) * aileron_sensitivity);
412                         }
413                         if( ! AP_AltitudeEnabled() ) {
414                             move_elevator((_mY - y) * elevator_sensitivity);
415                         }
416                     }
417                 }
418                 // Keep the mouse in the window.
419                 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
420                     x = ww / 2;
421                     y = wh / 2;
422                     need_warp = 1;
423                 }
424                 break;
425                 
426             case MOUSE_VIEW:
427                 if( y <= 0 ) {
428 #define CONTRAINED_MOUSE_VIEW_Y
429 #ifdef CONTRAINED_MOUSE_VIEW_Y
430                     y = 1;
431 #else
432                     y = wh-2;
433 #endif // CONTRAINED_MOUSE_VIEW_Y
434                     need_warp = 1;
435                 } else if( y >= wh-1) {
436 #ifdef CONTRAINED_MOUSE_VIEW_Y
437                     y = wh-2;
438 #else
439                     y = 1;
440 #endif // CONTRAINED_MOUSE_VIEW_Y
441                     need_warp = 1;
442                 }
443                 // wrap MOUSE_VIEW mode cursor x position
444                 if ( x <= 0 ) {
445                     need_warp = 1;
446                     x = ww-2;
447                 } else if ( x >= ww-1 ) {
448                     need_warp = 1;
449                     x = 1;
450                 }
451                 // try to get SGD_PI movement in each half of screen
452                 // do spherical pan
453                 W = ww;
454                 H = wh;
455                 if( middle_button() ) {
456                     trackball(lastGuiQuat,
457                               (2.0f * _mX - W) / W,
458                               0, //(H - 2.0f * y) / H,         // 3
459                               (2.0f * x - W) / W,
460                               0 //(H - 2.0f * _mY) / H       // 1
461                              );
462                     x = _mX;
463                     y = _mY;
464                     need_warp = 1;
465                 } else {
466                     trackball(lastGuiQuat,
467                               0, //(2.0f * _mX - W) / W,  // 0
468                               (H - 2.0f * y) / H,         // 3
469                               0, //(2.0f * x - W) / W,    // 2
470                               (H - 2.0f * _mY) / H        // 1 
471                              );
472                 }
473                 add_quats(lastGuiQuat, curGuiQuat, curGuiQuat);
474                 build_rotmatrix(GuiQuat_mat, curGuiQuat);
475                 
476                 // do horizontal pan
477                 // this could be done in above quat
478                 // but requires redoing view pipeline
479                 offset = get_goal_view_offset();
480                 offset += ((_mX - x) * SGD_2PI / W );
481                 while (offset < 0.0) {
482                     offset += SGD_2PI;
483                 }
484                 while (offset > SGD_2PI) {
485                     offset -= SGD_2PI;
486                 }
487                 set_goal_view_offset(offset);
488                 set_goal_view_tilt(asin( GuiQuat_mat[1][2]) * SGD_RADIANS_TO_DEGREES );
489 #ifdef NO_SMOOTH_MOUSE_VIEW
490                 set_view_offset(offset);
491                 set_view_tilt(asin( GuiQuat_mat[1][2]) * SGD_RADIANS_TO_DEGREES );
492 #endif
493                 break;
494             
495             default:
496                 break;
497         }
498     }
499     if( need_warp)
500         glutWarpPointer(x, y);
501     
502     // Record the new mouse position.
503     _mX = x;
504     _mY = y;
505 }
506
507
508 void guiMouseFunc(int button, int updown, int x, int y)
509 {
510     int glutModifiers;
511
512     // private MOUSE_VIEW state variables
513     // to allow alternate left clicks in MOUSE_VIEW mode
514     // to toggle between current offsets and straight ahead
515     // uses _mVtoggle
516     static int _mVx, _mVy, _Vx, _Vy;
517     static float _quat[4];
518     static double _view_offset;
519     
520     glutModifiers = glutGetModifiers();
521     glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
522     glut_active_ctrl  = glutModifiers & GLUT_ACTIVE_CTRL; 
523     glut_active_alt   = glutModifiers & GLUT_ACTIVE_ALT;
524     
525     // Was the left button pressed?
526     if (updown == GLUT_DOWN ) {
527         if( button == GLUT_LEFT_BUTTON)
528         {
529             switch (mouse_mode) {
530                 case MOUSE_POINTER:
531                     break;
532                 case MOUSE_YOKE:
533                     break;
534                 case MOUSE_VIEW:
535                     if(_mVtoggle) {
536                         // resume previous view offsets
537                         _mX = _mVx;
538                         _mY = _mVy;
539                         x = _Vx;
540                         y = _Vy;
541                         sgCopyVec4(curGuiQuat, _quat);
542                         set_goal_view_offset(_view_offset);
543                         set_goal_view_tilt(0.0);
544 #ifdef NO_SMOOTH_MOUSE_VIEW
545                         set_view_offset(_view_offset);
546 #endif
547                     } else {
548                         // center view
549                         _mVx = _mX;
550                         _mVy = _mY;
551                         _Vx = x;
552                         _Vy = y;
553                         sgCopyVec4(_quat,curGuiQuat);
554                         x = MOUSE_XSIZE/2;
555                         y = MOUSE_YSIZE/2;
556                         Quat0();
557                         _view_offset = get_goal_view_offset();
558                         set_goal_view_offset(0.0);
559                         set_goal_view_tilt(0.0);
560 #ifdef NO_SMOOTH_MOUSE_VIEW
561                         set_view_offset(0.0);
562                         set_view_tilt(0.0);
563 #endif
564                     }
565                     glutWarpPointer( x , y);
566                     build_rotmatrix(GuiQuat_mat, curGuiQuat);
567                     _mVtoggle = ~_mVtoggle;
568                     break;
569             }
570         } else if ( button == GLUT_RIGHT_BUTTON) {
571             switch (mouse_mode) {
572                                 
573                 case MOUSE_POINTER:
574                     SG_LOG( SG_INPUT, SG_INFO, "Mouse in yoke mode" );
575                                         
576                     mouse_mode = MOUSE_YOKE;
577                     mouse_joystick_control = 0;
578                     _savedX = x;
579                     _savedY = y;
580                     // start with zero point in center of screen
581                     _mX = MOUSE_XSIZE/2;
582                     _mY = MOUSE_YSIZE/2;
583                     
584                     // try to have the MOUSE_YOKE position
585                     // reflect the current stick position
586                     x = _mX - (int)(get_aileron() * aileron_sensitivity);
587                     y = _mY - (int)(get_elevator() * elevator_sensitivity);
588                     
589                     glutSetCursor(GLUT_CURSOR_CROSSHAIR);
590                     break;
591                     
592                 case MOUSE_YOKE:
593                     SG_LOG( SG_INPUT, SG_INFO, "Mouse in view mode" );
594                                         
595                     mouse_mode = MOUSE_VIEW;
596                     fgSetString("/sim/control-mode", "joystick");
597                                         
598                                         // recenter cursor and reset 
599                     x = MOUSE_XSIZE/2;
600                     y = MOUSE_YSIZE/2;
601                     _mVtoggle = 0;
602 // #ifndef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
603                     Quat0();
604                     build_rotmatrix(GuiQuat_mat, curGuiQuat);
605 // #endif
606                     glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
607                     break;
608                     
609                 case MOUSE_VIEW:
610                     SG_LOG( SG_INPUT, SG_INFO, "Mouse in pointer mode" );
611                                         
612                     mouse_mode = MOUSE_POINTER;
613                     x = _savedX;
614                     y = _savedY;
615 #ifdef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
616                     Quat0();
617                     build_rotmatrix(GuiQuat_mat, curGuiQuat);
618                     set_goal_view_offset(0.0);
619                     set_goal_view_tilt(0.0);
620 #ifdef NO_SMOOTH_MOUSE_VIEW
621                     set_view_offset(0.0);
622                     set_view_tilt(0.0);
623 #endif // NO_SMOOTH_MOUSE_VIEW
624 #endif // RESET_VIEW_ON_LEAVING_MOUSE_VIEW
625                     glutSetCursor(GLUT_CURSOR_INHERIT);
626                     
627 #if defined(WIN32_CURSOR_TWEAKS_OFF)
628                     if(!gui_menu_on)
629                         TurnCursorOff();
630 #endif // WIN32_CURSOR_TWEAKS_OFF
631                     break;
632             } // end switch (mouse_mode)
633             glutWarpPointer( x, y );
634         } // END RIGHT BUTTON
635     } // END UPDOWN == GLUT_DOWN
636     
637     // Note which button is pressed.
638     if ( updown == GLUT_DOWN ) {
639         last_buttons |=  ( 1 << button ) ;
640     } else {
641         last_buttons &= ~( 1 << button ) ;
642     }
643     
644     // If we're in pointer mode, let PUI
645     // know what's going on.
646     if (mouse_mode == MOUSE_POINTER) {
647       if (!puMouse (button, updown, x,y)) {
648         if ( current_panel != NULL ) {
649           current_panel->doMouseAction(button, updown, x, y);
650         }
651       }
652     }
653     
654     // Register the new position (if it
655     // hasn't been registered already).
656     _mX = x;
657     _mY = y;
658     
659     glutPostRedisplay ();
660 }
661
662
663
664