]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui_funcs.cxx
"nine" (via IRC):
[flightgear.git] / src / GUI / gui_funcs.cxx
1 /**************************************************************************
2  * gui_funcs.cxx
3  *
4  * Based on gui.cxx and renamed on 2002/08/13 by Erik Hofman.
5  *
6  * Written 1998 by Durk Talsma, started Juni, 1998.  For the flight gear
7  * project.
8  *
9  * Additional mouse supported added by David Megginson, 1999.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of the
14  * License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * $Id$
26  **************************************************************************/
27
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #include <simgear/compiler.h>
34
35 #ifdef SG_MATH_EXCEPTION_CLASH
36 #  include <math.h>
37 #endif
38
39 #ifdef HAVE_WINDOWS_H
40 #  include <windows.h>
41 #endif
42
43 #include SG_GL_H
44
45 #if defined(FX) && defined(XMESA)
46 #  include <GL/xmesa.h>
47 #endif
48
49 #include STL_FSTREAM
50 #include STL_STRING
51
52 #include <stdlib.h>
53 #include <string.h>
54
55 // for help call back
56 #ifdef WIN32
57 # include <shellapi.h>
58 # ifdef __CYGWIN__
59 #  include <sys/cygwin.h>
60 # endif
61 #endif
62
63 #include <plib/ssg.h>
64
65 #include <simgear/constants.h>
66 #include <simgear/debug/logstream.hxx>
67 #include <simgear/misc/sg_path.hxx>
68 #include <simgear/screen/screen-dump.hxx>
69
70 #include <Include/general.hxx>
71 #include <Aircraft/aircraft.hxx>
72 #include <Aircraft/controls.hxx>
73 #include <Airports/simple.hxx>
74 #include <Autopilot/auto_gui.hxx>
75 #include <Cockpit/panel.hxx>
76 #include <FDM/flight.hxx>
77 #include <Main/main.hxx>
78 #include <Main/fg_init.hxx>
79 #include <Main/fg_io.hxx>
80 #include <Main/globals.hxx>
81 #include <Main/fg_props.hxx>
82 #include <Main/renderer.hxx>
83 #include <Main/viewmgr.hxx>
84 #include <GUI/new_gui.hxx>
85
86 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
87 #  include <simgear/screen/win32-printer.h>
88 #  include <simgear/screen/GlBitmaps.h>
89 #endif
90
91 #include "gui.h"
92 #include "gui_local.hxx"
93 #include "preset_dlg.hxx"
94 #include "prop_picker.hxx"
95 #include "sgVec3Slider.hxx"
96
97 SG_USING_STD(string);
98 SG_USING_STD(cout);
99
100 // from cockpit.cxx
101 extern void fgLatLonFormatToggle( puObject *);
102
103 #if defined( TR_HIRES_SNAP)
104 #include <simgear/screen/tr.h>
105 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
106                          GLfloat x_end, GLfloat y_end );
107 #endif
108
109
110 // TODO: remove after the last hardcoded dialog has died
111 char *gui_msg_OK     = "OK";
112 char *gui_msg_NO     = "NO";
113 char *gui_msg_YES    = "YES";
114 char *gui_msg_CANCEL = "CANCEL";
115 char *gui_msg_RESET  = "RESET";
116
117
118 const __fg_gui_fn_t __fg_gui_fn[] = {
119
120         // File
121         {"reInit", reInit},
122 #ifdef TR_HIRES_SNAP
123         {"dumpHiResSnapShot", fgHiResDumpWrapper},
124 #endif
125         {"dumpSnapShot", fgDumpSnapShotWrapper},
126 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
127         {"printScreen", printScreen},
128 #endif
129
130         //View
131         {"guiTogglePanel", guiTogglePanel},
132         {"prop_pickerView", prop_pickerView},
133
134         // Environment
135         {"fgPresetAirport", fgPresetAirport},
136         {"fgPresetRunway", fgPresetRunway},
137         {"fgPresetOffsetDistance", fgPresetOffsetDistance},
138         {"fgPresetAltitude", fgPresetAltitude},
139         {"fgPresetGlideslope", fgPresetGlideslope},
140         {"fgPresetAirspeed", fgPresetAirspeed},
141         {"fgPresetCommit", fgPresetCommit},
142
143         // Autopilot
144         {"NewAltitude", NewAltitude},
145         {"NewHeading", NewHeading},
146         {"AddWayPoint", AddWayPoint},
147         {"PopWayPoint", PopWayPoint},
148         {"ClearRoute", ClearRoute},
149         {"fgLatLonFormatToggle", fgLatLonFormatToggle},
150
151         // Help
152         {"helpCb", helpCb},
153
154         // Structure termination
155         {"", NULL}
156 };
157
158
159 /* ================ General Purpose Functions ================ */
160
161 // General Purpose Message Box
162 void mkDialog (const char *txt)
163 {
164     NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
165     if (!gui)
166         return;
167     SGPropertyNode_ptr dlg = gui->getDialog("message");
168     if (!dlg)
169         return;
170
171     dlg->setStringValue("text/label", txt);
172     dlg->setStringValue("button/legend", "OK");
173     gui->showDialog("message");
174 }
175
176 // Message Box to report an error.
177 void guiErrorMessage (const char *txt)
178 {
179     SG_LOG(SG_GENERAL, SG_ALERT, txt);
180     mkDialog(txt);
181 }
182
183 // Message Box to report a throwable (usually an exception).
184 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
185 {
186     string msg = txt;
187     msg += '\n';
188     msg += throwable.getFormattedMessage();
189     if (!throwable.getOrigin().empty()) {
190       msg += "\n (reported by ";
191       msg += throwable.getOrigin();
192       msg += ')';
193     }
194     SG_LOG(SG_GENERAL, SG_ALERT, msg);
195     mkDialog(msg.c_str());
196 }
197
198
199
200 /* -----------------------------------------------------------------------
201 the Gui callback functions 
202 ____________________________________________________________________*/
203
204
205 // Hier Neu :-) This is my newly added code
206 // Added by David Findlay <nedz@bigpond.com>
207 // on Sunday 3rd of December
208
209
210 // This is the accessor function
211 void guiTogglePanel(puObject *cb)
212 {
213   if (fgGetBool("/sim/panel/visibility"))
214     fgSetBool("/sim/panel/visibility", false);
215   else
216     fgSetBool("/sim/panel/visibility", true);
217
218   globals->get_renderer()->resize(fgGetInt("/sim/startup/xsize"),
219                                   fgGetInt("/sim/startup/ysize"));
220 }
221
222 void helpCb (puObject *)
223 {
224     string command;
225         
226 #if defined(FX) && !defined(WIN32)
227 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
228     if ( globals->get_fullscreen() ) {
229         globals->set_fullscreen(false);
230         XMesaSetFXmode( XMESA_FX_WINDOW );
231     }
232 #  endif
233 #endif
234         
235     SGPath path( globals->get_fg_root() );
236     path.append( "Docs/index.html" );
237         
238 #if !defined(WIN32)
239
240     string help_app = fgGetString("/sim/startup/browser-app");
241
242     if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
243         command = help_app + " -remote \"openURL(" + path.str() + ")\"";
244     } else {
245         command = help_app + " " + path.str();
246     }
247     command += " &";
248     system( command.c_str() );
249
250 #else // WIN32
251
252     // Look for favorite browser
253     char Dummy[1024], ExecName[1024], browserParameter[1024];
254     char win32_name[1024];
255 # ifdef __CYGWIN__
256     cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
257 # else
258     strcpy(win32_name,path.c_str());
259 # endif
260     Dummy[0] = 0;
261     FindExecutable(win32_name, Dummy, ExecName);
262     sprintf(browserParameter, "file:///%s", win32_name);
263     ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
264                    SW_SHOWNORMAL ) ;
265
266 #endif
267         
268     mkDialog ("Help started in your web browser window.");
269 }
270
271 #if defined( TR_HIRES_SNAP)
272 void fgHiResDump()
273 {
274     FILE *f;
275     string message;
276     bool show_pu_cursor = false;
277     char *filename = new char [24];
278     static int count = 1;
279
280     static const SGPropertyNode *master_freeze
281         = fgGetNode("/sim/freeze/master");
282
283     bool freeze = master_freeze->getBoolValue();
284     if ( !freeze ) {
285         fgSetBool("/sim/freeze/master", true);
286     }
287
288     TurnCursorOff();
289     if ( !puCursorIsHidden() ) {
290         show_pu_cursor = true;
291         puHideCursor();
292     }
293
294     FGRenderer *renderer = globals->get_renderer();
295     renderer->init();
296     renderer->resize( fgGetInt("/sim/startup/xsize"),
297                       fgGetInt("/sim/startup/ysize") );
298
299     // we need two render frames here to clear the menu and cursor
300     // ... not sure why but doing an extra fgRenderFrame() shouldn't
301     // hurt anything
302     renderer->update( true );
303     renderer->update( true );
304
305     // This ImageSize stuff is a temporary hack
306     // should probably use 128x128 tile size and
307     // support any image size
308
309     // This should be a requester to get multiplier from user
310     int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
311     int width = fgGetInt("/sim/startup/xsize");
312     int height = fgGetInt("/sim/startup/ysize");
313         
314     /* allocate buffer large enough to store one tile */
315     GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
316     if (!tile) {
317         printf("Malloc of tile buffer failed!\n");
318         return;
319     }
320
321     int imageWidth  = multiplier*width;
322     int imageHeight = multiplier*height;
323
324     /* allocate buffer to hold a row of tiles */
325     GLubyte *buffer
326         = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
327     if (!buffer) {
328         free(tile);
329         printf("Malloc of tile row buffer failed!\n");
330         return;
331     }
332     TRcontext *tr = trNew();
333     trTileSize(tr, width, height, 0);
334     trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
335     trImageSize(tr, imageWidth, imageHeight);
336     trRowOrder(tr, TR_TOP_TO_BOTTOM);
337     sgFrustum *frustum = ssgGetFrustum();
338     trFrustum(tr,
339               frustum->getLeft(), frustum->getRight(),
340               frustum->getBot(),  frustum->getTop(), 
341               frustum->getNear(), frustum->getFar());
342         
343     /* Prepare ppm output file */
344     while (count < 1000) {
345         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
346         if ( (f = fopen(filename, "r")) == NULL )
347             break;
348         fclose(f);
349     }
350     f = fopen(filename, "wb");
351     if (!f) {
352         printf("Couldn't open image file: %s\n", filename);
353         free(buffer);
354         free(tile);
355         return;
356     }
357     fprintf(f,"P6\n");
358     fprintf(f,"# ppm-file created by %s\n", "trdemo2");
359     fprintf(f,"%i %i\n", imageWidth, imageHeight);
360     fprintf(f,"255\n");
361
362     /* just to be safe... */
363     glPixelStorei(GL_PACK_ALIGNMENT, 1);
364
365     /* Because the HUD and Panel change the ViewPort we will
366      * need to handle some lowlevel stuff ourselves */
367     int ncols = trGet(tr, TR_COLUMNS);
368     int nrows = trGet(tr, TR_ROWS);
369
370     bool do_hud = fgGetBool("/sim/hud/visibility");
371     GLfloat hud_col_step = 640.0 / ncols;
372     GLfloat hud_row_step = 480.0 / nrows;
373         
374     bool do_panel = fgPanelVisible();
375     GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
376     GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
377         
378     /* Draw tiles */
379     int more = 1;
380     while (more) {
381         trBeginTile(tr);
382         int curColumn = trGet(tr, TR_CURRENT_COLUMN);
383         int curRow =  trGet(tr, TR_CURRENT_ROW);
384         renderer->update( false );
385         if ( do_hud )
386             fgUpdateHUD( curColumn*hud_col_step,      curRow*hud_row_step,
387                          (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
388         if (do_panel)
389             globals->get_current_panel()->update(
390                                    curColumn*panel_col_step, panel_col_step,
391                                    curRow*panel_row_step,    panel_row_step );
392         more = trEndTile(tr);
393
394         /* save tile into tile row buffer*/
395         int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
396         int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
397         int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
398         int xOffset = curColumn * bytesPerTileRow;
399         int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
400         int i;
401         for (i=0;i<height;i++) {
402             memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
403                    tile + i*bytesPerTileRow,              /* Src */
404                    bytesPerCurrentTileRow);               /* Byte count*/
405         }
406
407         if (curColumn == trGet(tr, TR_COLUMNS)-1) {
408             /* write this buffered row of tiles to the file */
409             int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
410             int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
411             int i;
412             for (i=0;i<curTileHeight;i++) {
413                 /* Remember, OpenGL images are bottom to top.  Have to reverse. */
414                 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
415                 fwrite(rowPtr, 1, imageWidth*3, f);
416             }
417         }
418
419     }
420
421     renderer->resize( width, height );
422
423     trDelete(tr);
424
425     fclose(f);
426
427     message = "Snapshot saved to \"";
428     message += filename;
429     message += "\".";
430     mkDialog (message.c_str());
431
432     free(tile);
433     free(buffer);
434
435     delete [] filename;
436
437     if ( show_pu_cursor ) {
438         puShowCursor();
439     }
440
441     TurnCursorOn();
442
443     if ( !freeze ) {
444         fgSetBool("/sim/freeze/master", false);
445     }
446 }
447 #endif // #if defined( TR_HIRES_SNAP)
448
449
450 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
451
452 void rotateView( double roll, double pitch, double yaw )
453 {
454         // rotate view
455 }
456
457 GlBitmap *b1 = NULL;
458 GLubyte *hiResScreenCapture( int multiplier )
459 {
460     float oldfov = fgGetDouble("/sim/current-view/field-of-view");
461     float fov = oldfov / multiplier;
462     FGViewer *v = globals->get_current_view();
463     fgSetDouble("/sim/current-view/field-of-view", fov);
464     globals->get_renderer()->init();
465     int cur_width = fgGetInt("/sim/startup/xsize");
466     int cur_height = fgGetInt("/sim/startup/ysize");
467     delete( b1 );
468     // New empty (mostly) bitmap
469     b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
470     int x,y;
471     for ( y = 0; y < multiplier; y++ ) {
472         for ( x = 0; x < multiplier; x++ ) {
473             globals->get_renderer()->resize( cur_width, cur_height );
474             // pan to tile
475             rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
476             globals->get_renderer()->update( false );
477             // restore view
478             GlBitmap b2;
479             b1->copyBitmap( &b2, cur_width*x, cur_height*y );
480         }
481     }
482     fgSetDouble("/sim/current-view/field-of-view", oldfov);
483     return b1->getBitmap();
484 }
485 #endif
486
487 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
488 // win32 print screen function
489 void printScreen ( puObject *obj ) {
490     bool show_pu_cursor = false;
491     TurnCursorOff();
492     if ( !puCursorIsHidden() ) {
493         show_pu_cursor = true;
494         puHideCursor();
495     }
496     // BusyCursor( 0 );
497
498     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
499     int cur_width = fgGetInt("/sim/startup/xsize");
500     int cur_height = fgGetInt("/sim/startup/ysize");
501     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
502     p.End( hiResScreenCapture(3) );
503
504     // BusyCursor(1);
505     if ( show_pu_cursor ) {
506         puShowCursor();
507     }
508     TurnCursorOn();
509 }
510 #endif // #ifdef WIN32
511
512
513 void fgDumpSnapShotWrapper ( puObject *obj ) {
514     fgDumpSnapShot();
515 }
516
517
518 void fgHiResDumpWrapper ( puObject *obj ) {
519     fgHiResDump();
520 }
521
522
523 // do a screen snap shot
524 void fgDumpSnapShot () {
525     bool show_pu_cursor = false;
526     char *filename = new char [24];
527     string message;
528     static int count = 1;
529
530     static const SGPropertyNode *master_freeze
531         = fgGetNode("/sim/freeze/master");
532
533     bool freeze = master_freeze->getBoolValue();
534     if ( !freeze ) {
535         fgSetBool("/sim/freeze/master", true);
536     }
537
538     TurnCursorOff();
539     if ( !puCursorIsHidden() ) {
540         show_pu_cursor = true;
541         puHideCursor();
542     }
543
544     FGRenderer *renderer = globals->get_renderer();
545     renderer->init();
546     renderer->resize( fgGetInt("/sim/startup/xsize"),
547                         fgGetInt("/sim/startup/ysize") );
548
549     // we need two render frames here to clear the menu and cursor
550     // ... not sure why but doing an extra fgRenderFrame() shouldn't
551     // hurt anything
552     renderer->update( true );
553     renderer->update( true );
554
555     while (count < 1000) {
556         FILE *fp;
557         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
558         if ( (fp = fopen(filename, "r")) == NULL )
559             break;
560         fclose(fp);
561     }
562
563     if ( sg_glDumpWindow( filename,
564                           fgGetInt("/sim/startup/xsize"), 
565                           fgGetInt("/sim/startup/ysize")) ) {
566         message = "Snapshot saved to \"";
567         message += filename;
568         message += "\".";
569     } else {
570         message = "Failed to save to \"";
571         message += filename;
572         message += "\".";
573     }
574
575     mkDialog (message.c_str());
576
577     delete [] filename;
578
579     if ( show_pu_cursor ) {
580         puShowCursor();
581     }
582
583     TurnCursorOn();
584
585     if ( !freeze ) {
586         fgSetBool("/sim/freeze/master", false);
587     }
588 }
589