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