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