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