]> git.mxchange.org Git - flightgear.git/blob - src/Main/keyboard.cxx
Updated to support new weather subsystem (visibility variable determins
[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 // #include <Weather/weather.hxx>
57 #include <WeatherCM/FGLocalWeatherDatabase.h>
58
59 #include "keyboard.hxx"
60 #include "options.hxx"
61 #include "views.hxx"
62
63 extern void NewAltitude( puObject *cb );
64 extern void NewHeading( puObject *cb );
65
66 // Force an update of the sky and lighting parameters
67 static void local_update_sky_and_lighting_params( void ) {
68     // fgSunInit();
69     SolarSystem::theSolarSystem->rebuild();
70     cur_light_params.Update();
71     fgSkyColorsInit();
72 }
73
74
75 // Handle keyboard events
76 void GLUTkey(unsigned char k, int x, int y) {
77     FGInterface *f;
78     FGTime *t;
79     FGView *v;
80     // FGWeather *w;
81     float fov, tmp;
82     static bool winding_ccw = true;
83     int speed;
84
85     f = current_aircraft.fdm_state;
86     t = FGTime::cur_time_params;
87     v = &current_view;
88     // w = &current_weather;
89
90     FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
91     if ( puKeyboard(k, PU_DOWN) ) {
92         return;
93     }
94
95     if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
96         FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
97         switch (k) {
98         case 1: // Ctrl-A key
99             fgAPToggleAltitude();
100             return;
101         case 8: // Ctrl-H key
102             fgAPToggleHeading();
103             return;
104         case 18: // Ctrl-R key
105             // temporary
106             winding_ccw = !winding_ccw;
107             if ( winding_ccw ) {
108                 glFrontFace ( GL_CCW );
109             } else {
110                 glFrontFace ( GL_CW );
111             }
112             return;
113         case 19: // Ctrl-S key
114             fgAPToggleAutoThrottle();
115             return;
116         case 20: // Ctrl-T key
117             fgAPToggleTerrainFollow();
118             return;
119         case 49: // numeric keypad 1
120             v->set_goal_view_offset( FG_PI * 0.75 );
121             return;
122         case 50: // numeric keypad 2
123             v->set_goal_view_offset( FG_PI );
124             return;
125         case 51: // numeric keypad 3
126             v->set_goal_view_offset( FG_PI * 1.25 );
127             return;
128         case 52: // numeric keypad 4
129             v->set_goal_view_offset( FG_PI * 0.50 );
130             return;
131         case 54: // numeric keypad 6
132             v->set_goal_view_offset( FG_PI * 1.50 );
133             return;
134         case 55: // numeric keypad 7
135             v->set_goal_view_offset( FG_PI * 0.25 );
136             return;
137         case 56: // numeric keypad 8
138             v->set_goal_view_offset( 0.00 );
139             return;
140         case 57: // numeric keypad 9
141             v->set_goal_view_offset( FG_PI * 1.75 );
142             return;
143         case 65: // A key
144             speed = current_options.get_speed_up();
145             speed--;
146             if ( speed < 1 ) {
147                 speed = 1;
148             }
149             current_options.set_speed_up( speed );
150             return;
151         case 72: // H key
152             // status = current_options.get_hud_status();
153             // current_options.set_hud_status(!status);
154             HUD_brightkey( true );
155             return;
156         case 73: // I key
157             // Minimal Hud
158             fgHUDInit2(&current_aircraft);
159             return;
160         case 77: // M key
161             t->adjust_warp(-60);
162             local_update_sky_and_lighting_params();
163             return;
164         case 80: // P key
165             current_options.toggle_panel();
166             break;
167         case 84: // T key
168             t->adjust_warp_delta(-30);
169             local_update_sky_and_lighting_params();
170             return;
171         case 87: // W key
172 #if defined(FX) && !defined(WIN32)
173             global_fullscreen = ( !global_fullscreen );
174 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
175             XMesaSetFXmode( global_fullscreen ? 
176                             XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
177 #  endif
178 #endif
179             return;
180         case 88: // X key
181             fov = current_options.get_fov();
182             fov *= 1.05;
183             if ( fov > FG_FOV_MAX ) {
184                 fov = FG_FOV_MAX;
185             }
186             current_options.set_fov(fov);
187             v->force_update_fov_math();
188             return;
189         case 90: // Z key
190             // tmp = w->get_visibility();   // in meters
191             // tmp /= 1.10;
192             // w->set_visibility( tmp );
193             tmp = WeatherDatabase->getWeatherVisibility();
194             tmp /= 1.10;
195             WeatherDatabase->setWeatherVisibility( tmp );
196             return;
197         }
198     } else {
199         FG_LOG( FG_INPUT, FG_DEBUG, "" );
200         switch (k) {
201         case 50: // numeric keypad 2
202             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
203                 fgAPAltitudeAdjust( 100 );
204             } else {
205                 controls.move_elevator(-0.05);
206             }
207             return;
208         case 56: // numeric keypad 8
209             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
210                 fgAPAltitudeAdjust( -100 );
211             } else {
212                 controls.move_elevator(0.05);
213             }
214             return;
215         case 49: // numeric keypad 1
216             controls.move_elevator_trim(-0.001);
217             return;
218         case 55: // numeric keypad 7
219             controls.move_elevator_trim(0.001);
220             return;
221         case 52: // numeric keypad 4
222             controls.move_aileron(-0.05);
223             return;
224         case 54: // numeric keypad 6
225             controls.move_aileron(0.05);
226             return;
227         case 48: // numeric keypad Ins
228             if( fgAPHeadingEnabled() ) {
229                 fgAPHeadingAdjust( -1 );
230             } else {
231                 controls.move_rudder(-0.05);
232             }
233             return;
234         case 13: // numeric keypad Enter
235             if( fgAPHeadingEnabled() ) {
236                 fgAPHeadingAdjust( 1 );
237             } else {
238                 controls.move_rudder(0.05);
239             }
240             return;
241         case 53: // numeric keypad 5
242             controls.set_aileron(0.0);
243             controls.set_elevator(0.0);
244             controls.set_rudder(0.0);
245             return;
246         case 57: // numeric keypad 9 (Pg Up)
247             if( fgAPAutoThrottleEnabled() ) {
248                 fgAPAutoThrottleAdjust( 5 );
249             } else {
250                 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
251             }
252             return;
253         case 51: // numeric keypad 3 (Pg Dn)
254             if( fgAPAutoThrottleEnabled() ) {
255                 fgAPAutoThrottleAdjust( -5 );
256             } else {
257                 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
258             }
259             return;
260         case 97: // a key
261             speed = current_options.get_speed_up();
262             speed++;
263             current_options.set_speed_up( speed );
264             return;
265         case 98: // b key
266             int b_ret;
267             double b_set;
268             b_ret = int( controls.get_brake( 0 ) );
269             b_set = double(!b_ret);
270             controls.set_brake( FGControls::ALL_WHEELS, b_set);
271             return;
272         case 104: // h key
273             HUD_brightkey( false );
274             return;
275         case 105: // i key
276             fgHUDInit(&current_aircraft);  // normal HUD
277             return;
278         case 109: // m key
279             t->adjust_warp (+60);
280             local_update_sky_and_lighting_params();
281             return;
282         case 112: // p key
283             t->togglePauseMode();
284             // printf position and attitude information
285             FG_LOG( FG_INPUT, FG_INFO,
286                     "Lon = " << f->get_Longitude() * RAD_TO_DEG
287                     << "  Lat = " << f->get_Latitude() * RAD_TO_DEG
288                     << "  Altitude = " << f->get_Altitude() * FEET_TO_METER );
289             FG_LOG( FG_INPUT, FG_INFO,
290                     "Heading = " << f->get_Psi() * RAD_TO_DEG 
291                     << "  Roll = " << f->get_Phi() * RAD_TO_DEG
292                     << "  Pitch = " << f->get_Theta() * RAD_TO_DEG );
293             return;
294         case 116: // t key
295             t->adjust_warp_delta (+30);
296             local_update_sky_and_lighting_params();
297             return;
298         case 118: // v key
299             v->cycle_view_mode();
300             return;
301         case 120: // x key
302             fov = current_options.get_fov();
303             fov /= 1.05;
304             if ( fov < FG_FOV_MIN ) {
305                 fov = FG_FOV_MIN;
306             }
307             current_options.set_fov(fov);
308             v->force_update_fov_math();
309             return;
310         case 122: // z key
311             // tmp = w->get_visibility();   // in meters
312             // tmp *= 1.10;
313             // w->set_visibility( tmp );
314             tmp = WeatherDatabase->getWeatherVisibility();
315             tmp *= 1.10;
316             WeatherDatabase->setWeatherVisibility( tmp );
317             return;
318         case 27: // ESC
319             // if( fg_DebugOutput ) {
320             //   fclose( fg_DebugOutput );
321             // }
322             FG_LOG( FG_INPUT, FG_ALERT, 
323                     "Program exiting normally at user request." );
324             ConfirmExitDialog();
325             return;
326         }
327     }
328 }
329
330
331 // Handle "special" keyboard events
332 void GLUTspecialkey(int k, int x, int y) {
333     FGView *v;
334
335     v = &current_view;
336
337     FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
338
339     if ( puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN) ) {
340         return;
341     }
342
343     if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
344         FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
345         switch (k) {
346         case GLUT_KEY_END: // numeric keypad 1
347             v->set_goal_view_offset( FG_PI * 0.75 );
348             return;
349         case GLUT_KEY_DOWN: // numeric keypad 2
350             v->set_goal_view_offset( FG_PI );
351             return;
352         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
353             v->set_goal_view_offset( FG_PI * 1.25 );
354             return;
355         case GLUT_KEY_LEFT: // numeric keypad 4
356             v->set_goal_view_offset( FG_PI * 0.50 );
357             return;
358         case GLUT_KEY_RIGHT: // numeric keypad 6
359             v->set_goal_view_offset( FG_PI * 1.50 );
360             return;
361         case GLUT_KEY_HOME: // numeric keypad 7
362             v->set_goal_view_offset( FG_PI * 0.25 );
363             return;
364         case GLUT_KEY_UP: // numeric keypad 8
365             v->set_goal_view_offset( 0.00 );
366             return;
367         case GLUT_KEY_PAGE_UP: // numeric keypad 9
368             v->set_goal_view_offset( FG_PI * 1.75 );
369             return;
370         }
371     } else {
372         FG_LOG( FG_INPUT, FG_DEBUG, "" );
373         switch (k) {
374         case GLUT_KEY_F2: // F2 Reload Tile Cache...
375             {
376                 int toggle_pause;
377                 FG_LOG(FG_INPUT, FG_INFO, "ReIniting TileCache");
378                 FGTime *t = FGTime::cur_time_params;
379                 if( (toggle_pause = !t->getPause()) )
380                     t->togglePauseMode();
381                 BusyCursor(0);
382                 if( global_tile_mgr.init() ) {
383                     // Load the local scenery data
384                     global_tile_mgr.update();
385                 } else {
386                     FG_LOG( FG_GENERAL, FG_ALERT, 
387                             "Error in Tile Manager initialization!" );
388                     exit(-1);
389                 }
390                 BusyCursor(1);
391                 if(toggle_pause)
392                     t->togglePauseMode();
393                 return;
394             }
395         case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
396             current_options.cycle_fog();
397         
398             if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
399                 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
400             } else if ( current_options.get_fog() == 
401                         fgOPTIONS::FG_FOG_FASTEST )
402             {
403                 FG_LOG( FG_INPUT, FG_INFO, 
404                         "Fog enabled, hint set to fastest" );
405             } else if ( current_options.get_fog() ==
406                         fgOPTIONS::FG_FOG_NICEST )
407             {
408                 FG_LOG( FG_INPUT, FG_INFO,
409                         "Fog enabled, hint set to nicest" );
410             }
411
412             return;
413         case GLUT_KEY_F9: // F9 toggles textures on and off...
414             if ( material_mgr.loaded() ) {
415                 current_options.get_textures() ?
416                     current_options.set_textures(false) :
417                     current_options.set_textures(true);
418                 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
419             } else {
420                 FG_LOG( FG_INPUT, FG_INFO, 
421                         "No textures loaded, cannot toggle" );
422             }
423             return;
424         case GLUT_KEY_F10: // F10 toggles menu on and off...
425             FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
426             guiToggleMenu();
427             return;
428         case GLUT_KEY_F11: // F11 Altitude Dialog.
429             FG_LOG(FG_INPUT, FG_INFO, "Invoking Altitude call back function");
430             NewAltitude( NULL );
431             return;
432         case GLUT_KEY_F12: // F12 Heading Dialog...
433             FG_LOG(FG_INPUT, FG_INFO, "Invoking Heading call back function");
434             NewHeading( NULL );
435             return;
436         case GLUT_KEY_UP:
437             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
438                 fgAPAltitudeAdjust( -100 );
439             } else {
440                 controls.move_elevator(0.05);
441             }
442             return;
443         case GLUT_KEY_DOWN:
444             if( fgAPAltitudeEnabled() || fgAPTerrainFollowEnabled() ) {
445                 fgAPAltitudeAdjust( 100 );
446             } else {
447                 controls.move_elevator(-0.05);
448             }
449             return;
450         case GLUT_KEY_LEFT:
451             controls.move_aileron(-0.05);
452             return;
453         case GLUT_KEY_RIGHT:
454             controls.move_aileron(0.05);
455             return;
456         case GLUT_KEY_HOME: // numeric keypad 1
457             controls.move_elevator_trim(0.001);
458             return;
459         case GLUT_KEY_END: // numeric keypad 7
460             controls.move_elevator_trim(-0.001);
461             return;
462         case GLUT_KEY_INSERT: // numeric keypad Ins
463             if( fgAPHeadingEnabled() ) {
464                 fgAPHeadingAdjust( -1 );
465             } else {
466                 controls.move_rudder(-0.05);
467             }
468             return;
469         case 13: // numeric keypad Enter
470             if( fgAPHeadingEnabled() ) {
471                 fgAPHeadingAdjust( 1 );
472             } else {
473                 controls.move_rudder(0.05);
474             }
475             return;
476         case 53: // numeric keypad 5
477             controls.set_aileron(0.0);
478             controls.set_elevator(0.0);
479             controls.set_rudder(0.0);
480             return;
481         case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
482             if( fgAPAutoThrottleEnabled() ) {
483                 fgAPAutoThrottleAdjust( 5 );
484             } else {
485                 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
486             }
487             return;
488         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
489             if( fgAPAutoThrottleEnabled() ) {
490                 fgAPAutoThrottleAdjust( -5 );
491             } else {
492                 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
493             }
494             return;
495         }
496     }
497 }
498
499