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