]> git.mxchange.org Git - flightgear.git/blob - src/Main/keyboard.cxx
Norman's autopilot updates, now serving great circle routes direct to
[flightgear.git] / src / Main / keyboard.cxx
1 // keyboard.cxx -- handle GLUT keyboard events
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997 - 1999  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>                     
30 #endif
31
32 #include <GL/glut.h>
33 #include <XGL/xgl.h>
34
35 #if defined(FX) && defined(XMESA)
36 #include <GL/xmesa.h>
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41
42 #include <pu.h>                 // plib include
43
44 #include <Debug/logstream.hxx>
45 #include <Aircraft/aircraft.hxx>
46 #include <Astro/solarsystem.hxx>
47 #include <Astro/sky.hxx>
48 #include <Autopilot/autopilot.hxx>
49 #include <Cockpit/hud.hxx>
50 #include <GUI/gui.h>
51 #include <Include/fg_constants.h>
52 #include <Scenery/tilemgr.hxx>
53 #include <Objects/materialmgr.hxx>
54 #include <Time/fg_time.hxx>
55 #include <Time/light.hxx>
56
57 #ifdef FG_NEW_WEATHER
58 #  include <WeatherCM/FGLocalWeatherDatabase.h>
59 #else
60 #  include <Weather/weather.hxx>
61 #endif
62
63 #include "keyboard.hxx"
64 #include "options.hxx"
65 #include "views.hxx"
66
67 extern void NewAltitude( puObject *cb );
68 extern void NewHeading( puObject *cb );
69
70 // Force an update of the sky and lighting parameters
71 static void local_update_sky_and_lighting_params( void ) {
72     // fgSunInit();
73     SolarSystem::theSolarSystem->rebuild();
74     cur_light_params.Update();
75     fgSkyColorsInit();
76 }
77
78
79 // Handle keyboard events
80 void GLUTkey(unsigned char k, int x, int y) {
81     FGInterface *f;
82     FGTime *t;
83     FGView *v;
84     float fov, tmp;
85     static bool winding_ccw = true;
86     int speed;
87
88     f = current_aircraft.fdm_state;
89     t = FGTime::cur_time_params;
90     v = &current_view;
91
92     FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
93     if ( puKeyboard(k, PU_DOWN) ) {
94         return;
95     }
96
97     if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
98         FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
99         switch (k) {
100         case 1: // Ctrl-A key
101             fgAPToggleAltitude();
102             return;
103         case 8: // Ctrl-H key
104             fgAPToggleHeading();
105             return;
106         case 18: // Ctrl-R key
107             // temporary
108             winding_ccw = !winding_ccw;
109             if ( winding_ccw ) {
110                 glFrontFace ( GL_CCW );
111             } else {
112                 glFrontFace ( GL_CW );
113             }
114             return;
115         case 19: // Ctrl-S key
116             fgAPToggleAutoThrottle();
117             return;
118         case 20: // Ctrl-T key
119             fgAPToggleTerrainFollow();
120             return;
121         case 49: // numeric keypad 1
122             v->set_goal_view_offset( FG_PI * 0.75 );
123             return;
124         case 50: // numeric keypad 2
125             v->set_goal_view_offset( FG_PI );
126             return;
127         case 51: // numeric keypad 3
128             v->set_goal_view_offset( FG_PI * 1.25 );
129             return;
130         case 52: // numeric keypad 4
131             v->set_goal_view_offset( FG_PI * 0.50 );
132             return;
133         case 54: // numeric keypad 6
134             v->set_goal_view_offset( FG_PI * 1.50 );
135             return;
136         case 55: // numeric keypad 7
137             v->set_goal_view_offset( FG_PI * 0.25 );
138             return;
139         case 56: // numeric keypad 8
140             v->set_goal_view_offset( 0.00 );
141             return;
142         case 57: // numeric keypad 9
143             v->set_goal_view_offset( FG_PI * 1.75 );
144             return;
145         case 65: // A key
146             speed = current_options.get_speed_up();
147             speed--;
148             if ( speed < 1 ) {
149                 speed = 1;
150             }
151             current_options.set_speed_up( speed );
152             return;
153         case 72: // H key
154             // status = current_options.get_hud_status();
155             // current_options.set_hud_status(!status);
156             HUD_brightkey( true );
157             return;
158         case 73: // I key
159             // Minimal Hud
160             fgHUDInit2(&current_aircraft);
161             return;
162         case 77: // M key
163             t->adjust_warp(-60);
164             local_update_sky_and_lighting_params();
165             return;
166         case 80: // P key
167             current_options.toggle_panel();
168             break;
169         case 84: // T key
170             t->adjust_warp_delta(-30);
171             local_update_sky_and_lighting_params();
172             return;
173         case 87: // W key
174 #if defined(FX) && !defined(WIN32)
175             global_fullscreen = ( !global_fullscreen );
176 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
177             XMesaSetFXmode( global_fullscreen ? 
178                             XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
179 #  endif
180 #endif
181             return;
182         case 88: // X key
183             fov = current_options.get_fov();
184             fov *= 1.05;
185             if ( fov > FG_FOV_MAX ) {
186                 fov = FG_FOV_MAX;
187             }
188             current_options.set_fov(fov);
189             v->force_update_fov_math();
190             return;
191         case 90: // Z key
192 #ifdef FG_NEW_WEATHER
193             tmp = WeatherDatabase->getWeatherVisibility();
194             tmp /= 1.10;
195             WeatherDatabase->setWeatherVisibility( tmp );
196 #else
197             tmp = current_weather.get_visibility();   // in meters
198             tmp /= 1.10;
199             current_weather.set_visibility( tmp );
200 #endif
201             return;
202         }
203     } else {
204         FG_LOG( FG_INPUT, FG_DEBUG, "" );
205         switch (k) {
206         case 50: // numeric keypad 2
207             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
208                 fgAPAltitudeAdjust( 100 );
209             } else {
210                 controls.move_elevator(-0.05);
211             }
212             return;
213         case 56: // numeric keypad 8
214             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
215                 fgAPAltitudeAdjust( -100 );
216             } else {
217                 controls.move_elevator(0.05);
218             }
219             return;
220         case 49: // numeric keypad 1
221             controls.move_elevator_trim(-0.001);
222             return;
223         case 55: // numeric keypad 7
224             controls.move_elevator_trim(0.001);
225             return;
226         case 52: // numeric keypad 4
227             controls.move_aileron(-0.05);
228             return;
229         case 54: // numeric keypad 6
230             controls.move_aileron(0.05);
231             return;
232         case 48: // numeric keypad Ins
233             if( fgAPHeadingEnabled() ) {
234                 fgAPHeadingAdjust( -1 );
235             } else {
236                 controls.move_rudder(-0.05);
237             }
238             return;
239         case 13: // numeric keypad Enter
240             if( fgAPHeadingEnabled() ) {
241                 fgAPHeadingAdjust( 1 );
242             } else {
243                 controls.move_rudder(0.05);
244             }
245             return;
246         case 53: // numeric keypad 5
247             controls.set_aileron(0.0);
248             controls.set_elevator(0.0);
249             controls.set_rudder(0.0);
250             return;
251         case 57: // numeric keypad 9 (Pg Up)
252             if( fgAPAutoThrottleEnabled() ) {
253                 fgAPAutoThrottleAdjust( 5 );
254             } else {
255                 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
256             }
257             return;
258         case 51: // numeric keypad 3 (Pg Dn)
259             if( fgAPAutoThrottleEnabled() ) {
260                 fgAPAutoThrottleAdjust( -5 );
261             } else {
262                 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
263             }
264             return;
265         case 97: // a key
266             speed = current_options.get_speed_up();
267             speed++;
268             current_options.set_speed_up( speed );
269             return;
270         case 98: // b key
271             int b_ret;
272             double b_set;
273             b_ret = int( controls.get_brake( 0 ) );
274             b_set = double(!b_ret);
275             controls.set_brake( FGControls::ALL_WHEELS, b_set);
276             return;
277         case 104: // h key
278             HUD_brightkey( false );
279             return;
280         case 105: // i key
281             fgHUDInit(&current_aircraft);  // normal HUD
282             return;
283         case 109: // m key
284             t->adjust_warp (+60);
285             local_update_sky_and_lighting_params();
286             return;
287         case 112: // p key
288             t->togglePauseMode();
289             // printf position and attitude information
290             FG_LOG( FG_INPUT, FG_INFO,
291                     "Lon = " << f->get_Longitude() * RAD_TO_DEG
292                     << "  Lat = " << f->get_Latitude() * RAD_TO_DEG
293                     << "  Altitude = " << f->get_Altitude() * FEET_TO_METER );
294             FG_LOG( FG_INPUT, FG_INFO,
295                     "Heading = " << f->get_Psi() * RAD_TO_DEG 
296                     << "  Roll = " << f->get_Phi() * RAD_TO_DEG
297                     << "  Pitch = " << f->get_Theta() * RAD_TO_DEG );
298             return;
299         case 116: // t key
300             t->adjust_warp_delta (+30);
301             local_update_sky_and_lighting_params();
302             return;
303         case 118: // v key
304             current_options.cycle_view_mode();
305             return;
306         case 120: // x key
307             fov = current_options.get_fov();
308             fov /= 1.05;
309             if ( fov < FG_FOV_MIN ) {
310                 fov = FG_FOV_MIN;
311             }
312             current_options.set_fov(fov);
313             v->force_update_fov_math();
314             return;
315         case 122: // z key
316 #ifdef FG_NEW_WEATHER
317             tmp = WeatherDatabase->getWeatherVisibility();
318             tmp *= 1.10;
319             WeatherDatabase->setWeatherVisibility( tmp );
320 #else
321             tmp = current_weather.get_visibility();   // in meters
322             tmp *= 1.10;
323             current_weather.set_visibility( tmp );
324 #endif
325             return;
326         case 27: // ESC
327             // if( fg_DebugOutput ) {
328             //   fclose( fg_DebugOutput );
329             // }
330             FG_LOG( FG_INPUT, FG_ALERT, 
331                     "Program exiting normally at user request." );
332             ConfirmExitDialog();
333             return;
334         }
335     }
336 }
337
338
339 // Handle "special" keyboard events
340 void GLUTspecialkey(int k, int x, int y) {
341     FGView *v;
342
343     v = &current_view;
344
345     FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
346
347     if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
348         return;
349     }
350
351     if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
352         FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
353         switch (k) {
354         case GLUT_KEY_END: // numeric keypad 1
355             v->set_goal_view_offset( FG_PI * 0.75 );
356             return;
357         case GLUT_KEY_DOWN: // numeric keypad 2
358             v->set_goal_view_offset( FG_PI );
359             return;
360         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
361             v->set_goal_view_offset( FG_PI * 1.25 );
362             return;
363         case GLUT_KEY_LEFT: // numeric keypad 4
364             v->set_goal_view_offset( FG_PI * 0.50 );
365             return;
366         case GLUT_KEY_RIGHT: // numeric keypad 6
367             v->set_goal_view_offset( FG_PI * 1.50 );
368             return;
369         case GLUT_KEY_HOME: // numeric keypad 7
370             v->set_goal_view_offset( FG_PI * 0.25 );
371             return;
372         case GLUT_KEY_UP: // numeric keypad 8
373             v->set_goal_view_offset( 0.00 );
374             return;
375         case GLUT_KEY_PAGE_UP: // numeric keypad 9
376             v->set_goal_view_offset( FG_PI * 1.75 );
377             return;
378         }
379     } else {
380         FG_LOG( FG_INPUT, FG_DEBUG, "" );
381         switch (k) {
382         case GLUT_KEY_F2: // F2 Reload Tile Cache...
383             {
384                 int toggle_pause;
385                 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
386                 FGTime *t = FGTime::cur_time_params;
387                 if( (toggle_pause = !t->getPause()) )
388                     t->togglePauseMode();
389                 BusyCursor(0);
390                 if( global_tile_mgr.init() ) {
391                     // Load the local scenery data
392                     global_tile_mgr.update();
393                 } else {
394                     FG_LOG( FG_GENERAL, FG_ALERT, 
395                             "Error in Tile Manager initialization!" );
396                     exit(-1);
397                 }
398                 BusyCursor(1);
399                 if(toggle_pause)
400                     t->togglePauseMode();
401                 return;
402             }
403         case GLUT_KEY_F6: // F6 toggles Autopilot target location
404                 fgAPToggleWayPoint();
405                 return;
406         case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
407             current_options.cycle_fog();
408         
409             if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
410                 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
411             } else if ( current_options.get_fog() == 
412                         fgOPTIONS::FG_FOG_FASTEST )
413             {
414                 FG_LOG( FG_INPUT, FG_INFO, 
415                         "Fog enabled, hint set to fastest" );
416             } else if ( current_options.get_fog() ==
417                         fgOPTIONS::FG_FOG_NICEST )
418             {
419                 FG_LOG( FG_INPUT, FG_INFO,
420                         "Fog enabled, hint set to nicest" );
421             }
422
423             return;
424         case GLUT_KEY_F9: // F9 toggles textures on and off...
425             if ( material_mgr.loaded() ) {
426                 if (current_options.get_textures()) {
427                     current_options.set_textures(false);
428                     glDisable(GL_TEXTURE_2D);
429                     ssgOverrideTexture(true);
430                 } else {
431                     current_options.set_textures(true);
432                     glEnable(GL_TEXTURE_2D);
433                     ssgOverrideTexture(false);
434                 }
435                 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
436             } else {
437                 FG_LOG( FG_INPUT, FG_INFO, 
438                         "No textures loaded, cannot toggle" );
439             }
440             return;
441         case GLUT_KEY_F10: // F10 toggles menu on and off...
442             FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
443             guiToggleMenu();
444             return;
445         case GLUT_KEY_F11: // F11 Altitude Dialog.
446             FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
447             NewAltitude( NULL );
448             return;
449         case GLUT_KEY_F12: // F12 Heading Dialog...
450             FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
451             NewHeading( NULL );
452             return;
453         case GLUT_KEY_UP:
454             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
455                 fgAPAltitudeAdjust( -100 );
456             } else {
457                 controls.move_elevator(0.05);
458             }
459             return;
460         case GLUT_KEY_DOWN:
461             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
462                 fgAPAltitudeAdjust( 100 );
463             } else {
464                 controls.move_elevator(-0.05);
465             }
466             return;
467         case GLUT_KEY_LEFT:
468             controls.move_aileron(-0.05);
469             return;
470         case GLUT_KEY_RIGHT:
471             controls.move_aileron(0.05);
472             return;
473         case GLUT_KEY_HOME: // numeric keypad 1
474             controls.move_elevator_trim(0.001);
475             return;
476         case GLUT_KEY_END: // numeric keypad 7
477             controls.move_elevator_trim(-0.001);
478             return;
479         case GLUT_KEY_INSERT: // numeric keypad Ins
480             if( fgAPHeadingEnabled() ) {
481                 fgAPHeadingAdjust( -1 );
482             } else {
483                 controls.move_rudder(-0.05);
484             }
485             return;
486         case 13: // numeric keypad Enter
487             if( fgAPHeadingEnabled() ) {
488                 fgAPHeadingAdjust( 1 );
489             } else {
490                 controls.move_rudder(0.05);
491             }
492             return;
493         case 53: // numeric keypad 5
494             controls.set_aileron(0.0);
495             controls.set_elevator(0.0);
496             controls.set_rudder(0.0);
497             return;
498         case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
499             if( fgAPAutoThrottleEnabled() ) {
500                 fgAPAutoThrottleAdjust( 5 );
501             } else {
502                 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
503             }
504             return;
505         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
506             if( fgAPAutoThrottleEnabled() ) {
507                 fgAPAutoThrottleAdjust( -5 );
508             } else {
509                 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
510             }
511             return;
512         }
513     }
514 }
515
516