]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui_funcs.cxx
4a3bd8793eb9a1d48fb5298add0c3ebe742a0870
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 <sstream>
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 <Cockpit/panel.hxx>
75 #include <FDM/flight.hxx>
76 #include <Main/main.hxx>
77 #include <Main/fg_init.hxx>
78 #include <Main/fg_io.hxx>
79 #include <Main/globals.hxx>
80 #include <Main/fg_props.hxx>
81 #include <Main/renderer.hxx>
82 #include <Main/viewmgr.hxx>
83 #include <GUI/new_gui.hxx>
84
85 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
86 #  include <simgear/screen/win32-printer.h>
87 #  include <simgear/screen/GlBitmaps.h>
88 #endif
89
90 #include "gui.h"
91 #include "gui_local.hxx"
92 #include "sgVec3Slider.hxx"
93
94 SG_USING_STD(string);
95 SG_USING_STD(cout);
96
97
98 #if defined( TR_HIRES_SNAP)
99 #include <simgear/screen/tr.h>
100 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
101                          GLfloat x_end, GLfloat y_end );
102 #endif
103
104
105 const __fg_gui_fn_t __fg_gui_fn[] = {
106
107         // File
108         {"reInit", reInit},
109 #ifdef TR_HIRES_SNAP
110         {"dumpHiResSnapShot", fgHiResDumpWrapper},
111 #endif
112         {"dumpSnapShot", fgDumpSnapShotWrapper},
113 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
114         {"printScreen", printScreen},
115 #endif
116         // Help
117         {"helpCb", helpCb},
118
119         // Structure termination
120         {"", NULL}
121 };
122
123
124 /* ================ General Purpose Functions ================ */
125
126 // General Purpose Message Box. Makes sure no more than 5 different
127 // messages are displayed at the same time, and none of them are
128 // duplicates. (5 is a *lot*, but this will hardly ever be reached
129 // and we don't want to miss any, either.)
130 void mkDialog (const char *txt)
131 {
132     NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
133     if (!gui)
134         return;
135     SGPropertyNode *master = gui->getDialogProperties("message");
136     if (!master)
137         return;
138
139     const int maxdialogs = 5;
140     string name;
141     SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
142     int i;
143     for (i = 0; i < maxdialogs; i++) {
144         std::ostringstream s;
145         s << "message-" << i;
146         name = s.str();
147
148         if (!msg->getNode(name.c_str(), false))
149             break;
150
151         if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
152             SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
153             return;
154         }
155     }
156     if (i == maxdialogs)
157         return;
158     msg = msg->getNode(name.c_str(), true);
159     msg->setStringValue("message", txt);
160     msg = msg->getNode("dialog", true);
161     copyProperties(master, msg);
162     msg->setStringValue("name", name.c_str());
163     gui->newDialog(msg);
164     gui->showDialog(name.c_str());
165 }
166
167 // Message Box to report an error.
168 void guiErrorMessage (const char *txt)
169 {
170     SG_LOG(SG_GENERAL, SG_ALERT, txt);
171     mkDialog(txt);
172 }
173
174 // Message Box to report a throwable (usually an exception).
175 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
176 {
177     string msg = txt;
178     msg += '\n';
179     msg += throwable.getFormattedMessage();
180     if (!throwable.getOrigin().empty()) {
181         msg += "\n (reported by ";
182         msg += throwable.getOrigin();
183         msg += ')';
184     }
185     SG_LOG(SG_GENERAL, SG_ALERT, msg);
186     mkDialog(msg.c_str());
187 }
188
189
190
191 /* -----------------------------------------------------------------------
192 the Gui callback functions 
193 ____________________________________________________________________*/
194
195
196 // Hier Neu :-) This is my newly added code
197 // Added by David Findlay <nedz@bigpond.com>
198 // on Sunday 3rd of December
199
200
201 void helpCb (puObject *)
202 {
203     string command;
204         
205 #if defined(FX) && !defined(WIN32)
206 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
207     if ( globals->get_fullscreen() ) {
208         globals->set_fullscreen(false);
209         XMesaSetFXmode( XMESA_FX_WINDOW );
210     }
211 #  endif
212 #endif
213         
214     SGPath path( globals->get_fg_root() );
215     path.append( "Docs/index.html" );
216         
217 #if !defined(WIN32)
218
219     string help_app = fgGetString("/sim/startup/browser-app");
220
221     if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
222         command = help_app + " -remote \"openURL(" + path.str() + ")\"";
223     } else {
224         command = help_app + " " + path.str();
225     }
226     command += " &";
227     system( command.c_str() );
228
229 #else // WIN32
230
231     // Look for favorite browser
232     char Dummy[1024], ExecName[1024], browserParameter[1024];
233     char win32_name[1024];
234 # ifdef __CYGWIN__
235     cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
236 # else
237     strncpy(win32_name,path.c_str(), 1024);
238 # endif
239     Dummy[0] = 0;
240     FindExecutable(win32_name, Dummy, ExecName);
241     snprintf(browserParameter, 1024, "file:///%s", win32_name);
242     ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
243                    SW_SHOWNORMAL ) ;
244
245 #endif
246         
247     mkDialog ("Help started in your web browser window.");
248 }
249
250 #if defined( TR_HIRES_SNAP)
251 void fgHiResDump()
252 {
253     FILE *f;
254     string message;
255     bool show_pu_cursor = false;
256     bool menu_status = fgGetBool("/sim/menubar/visibility");
257     char *filename = new char [24];
258     static int count = 1;
259
260     static const SGPropertyNode *master_freeze
261         = fgGetNode("/sim/freeze/master");
262
263     bool freeze = master_freeze->getBoolValue();
264     if ( !freeze ) {
265         fgSetBool("/sim/freeze/master", true);
266     }
267
268     fgSetBool("/sim/menubar/visibility", false);
269     TurnCursorOff();
270     if ( !puCursorIsHidden() ) {
271         show_pu_cursor = true;
272         puHideCursor();
273     }
274
275     FGRenderer *renderer = globals->get_renderer();
276 //     renderer->init();
277     renderer->resize( fgGetInt("/sim/startup/xsize"),
278                       fgGetInt("/sim/startup/ysize") );
279
280     // we need two render frames here to clear the menu and cursor
281     // ... not sure why but doing an extra fgRenderFrame() shouldn't
282     // hurt anything
283     //renderer->update( true );
284     //renderer->update( true );
285
286     // This ImageSize stuff is a temporary hack
287     // should probably use 128x128 tile size and
288     // support any image size
289
290     // This should be a requester to get multiplier from user
291     int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
292     int width = fgGetInt("/sim/startup/xsize");
293     int height = fgGetInt("/sim/startup/ysize");
294         
295     /* allocate buffer large enough to store one tile */
296     GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
297     if (!tile) {
298         printf("Malloc of tile buffer failed!\n");
299         return;
300     }
301
302     int imageWidth  = multiplier*width;
303     int imageHeight = multiplier*height;
304
305     /* allocate buffer to hold a row of tiles */
306     GLubyte *buffer
307         = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
308     if (!buffer) {
309         free(tile);
310         printf("Malloc of tile row buffer failed!\n");
311         return;
312     }
313     TRcontext *tr = trNew();
314     trTileSize(tr, width, height, 0);
315     trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
316     trImageSize(tr, imageWidth, imageHeight);
317     trRowOrder(tr, TR_TOP_TO_BOTTOM);
318     // OSGFIXME
319 //     sgFrustum *frustum = ssgGetFrustum();
320 //     trFrustum(tr,
321 //               frustum->getLeft(), frustum->getRight(),
322 //               frustum->getBot(),  frustum->getTop(), 
323 //               frustum->getNear(), frustum->getFar());
324         
325     /* Prepare ppm output file */
326     while (count < 1000) {
327         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
328         if ( (f = fopen(filename, "r")) == NULL )
329             break;
330         fclose(f);
331     }
332     f = fopen(filename, "wb");
333     if (!f) {
334         printf("Couldn't open image file: %s\n", filename);
335         free(buffer);
336         free(tile);
337         return;
338     }
339     fprintf(f,"P6\n");
340     fprintf(f,"# ppm-file created by %s\n", "trdemo2");
341     fprintf(f,"%i %i\n", imageWidth, imageHeight);
342     fprintf(f,"255\n");
343
344     /* just to be safe... */
345     glPixelStorei(GL_PACK_ALIGNMENT, 1);
346
347     /* Because the HUD and Panel change the ViewPort we will
348      * need to handle some lowlevel stuff ourselves */
349     int ncols = trGet(tr, TR_COLUMNS);
350     int nrows = trGet(tr, TR_ROWS);
351
352     bool do_hud = fgGetBool("/sim/hud/visibility");
353     GLfloat hud_col_step = 640.0 / ncols;
354     GLfloat hud_row_step = 480.0 / nrows;
355         
356     bool do_panel = fgPanelVisible();
357     GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
358     GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
359
360     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
361     glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
362     glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
363     glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
364     glHint(GL_FOG_HINT, GL_NICEST);
365         
366     /* Draw tiles */
367     int more = 1;
368     while (more) {
369         trBeginTile(tr);
370         int curColumn = trGet(tr, TR_CURRENT_COLUMN);
371         int curRow =  trGet(tr, TR_CURRENT_ROW);
372
373         renderer->update( false );
374         // OSGFIXME
375 //         if ( do_hud )
376 //             fgUpdateHUD( curColumn*hud_col_step,      curRow*hud_row_step,
377 //                          (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
378         // OSGFIXME
379 //         if (do_panel)
380 //             globals->get_current_panel()->update(
381 //                                    curColumn*panel_col_step, panel_col_step,
382 //                                    curRow*panel_row_step,    panel_row_step );
383         more = trEndTile(tr);
384
385         /* save tile into tile row buffer*/
386         int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
387         int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
388         int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
389         int xOffset = curColumn * bytesPerTileRow;
390         int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
391         int i;
392         for (i=0;i<height;i++) {
393             memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
394                    tile + i*bytesPerTileRow,              /* Src */
395                    bytesPerCurrentTileRow);               /* Byte count*/
396         }
397
398         if (curColumn == trGet(tr, TR_COLUMNS)-1) {
399             /* write this buffered row of tiles to the file */
400             int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
401             int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
402             int i;
403             for (i=0;i<curTileHeight;i++) {
404                 /* Remember, OpenGL images are bottom to top.  Have to reverse. */
405                 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
406                 fwrite(rowPtr, 1, imageWidth*3, f);
407             }
408         }
409
410     }
411
412     renderer->resize( width, height );
413
414     trDelete(tr);
415
416     glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
417     glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
418     glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
419     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
420     if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
421          (!fgGetBool("/sim/rendering/shading"))) {
422         // if fastest fog requested, or if flat shading force fastest
423         glHint ( GL_FOG_HINT, GL_FASTEST );
424     } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
425         glHint ( GL_FOG_HINT, GL_DONT_CARE );
426     }
427
428     fclose(f);
429
430     message = "Snapshot saved to \"";
431     message += filename;
432     message += "\".";
433     mkDialog (message.c_str());
434
435     free(tile);
436     free(buffer);
437
438     delete [] filename;
439
440     if ( show_pu_cursor ) {
441         puShowCursor();
442     }
443
444     TurnCursorOn();
445     fgSetBool("/sim/menubar/visibility", menu_status);
446
447     if ( !freeze ) {
448         fgSetBool("/sim/freeze/master", false);
449     }
450 }
451 #endif // #if defined( TR_HIRES_SNAP)
452
453
454 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
455
456 void rotateView( double roll, double pitch, double yaw )
457 {
458         // rotate view
459 }
460
461 GlBitmap *b1 = NULL;
462 GLubyte *hiResScreenCapture( int multiplier )
463 {
464     float oldfov = fgGetDouble("/sim/current-view/field-of-view");
465     float fov = oldfov / multiplier;
466     FGViewer *v = globals->get_current_view();
467     fgSetDouble("/sim/current-view/field-of-view", fov);
468 //     globals->get_renderer()->init();
469     int cur_width = fgGetInt("/sim/startup/xsize");
470     int cur_height = fgGetInt("/sim/startup/ysize");
471     delete( b1 );
472     // New empty (mostly) bitmap
473     b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
474     int x,y;
475     for ( y = 0; y < multiplier; y++ ) {
476         for ( x = 0; x < multiplier; x++ ) {
477             globals->get_renderer()->resize( cur_width, cur_height );
478             // pan to tile
479             rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
480             globals->get_renderer()->update( false );
481             // restore view
482             GlBitmap b2;
483             b1->copyBitmap( &b2, cur_width*x, cur_height*y );
484         }
485     }
486     fgSetDouble("/sim/current-view/field-of-view", oldfov);
487     return b1->getBitmap();
488 }
489 #endif
490
491 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
492 // win32 print screen function
493 void printScreen ( puObject *obj ) {
494     bool show_pu_cursor = false;
495     TurnCursorOff();
496     if ( !puCursorIsHidden() ) {
497         show_pu_cursor = true;
498         puHideCursor();
499     }
500
501     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
502     int cur_width = fgGetInt("/sim/startup/xsize");
503     int cur_height = fgGetInt("/sim/startup/ysize");
504     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
505     p.End( hiResScreenCapture(3) );
506
507     if ( show_pu_cursor ) {
508         puShowCursor();
509     }
510     TurnCursorOn();
511 }
512 #endif // #ifdef WIN32
513
514
515 void fgDumpSnapShotWrapper ( puObject *obj ) {
516     fgDumpSnapShot();
517 }
518
519
520 void fgHiResDumpWrapper ( puObject *obj ) {
521     fgHiResDump();
522 }
523
524
525 // do a screen snap shot
526 bool fgDumpSnapShot () {
527     bool show_pu_cursor = false;
528     static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
529
530     bool freeze = master_freeze->getBoolValue();
531     if ( !freeze ) {
532         fgSetBool("/sim/freeze/master", true);
533     }
534
535     TurnCursorOff();
536     if ( !puCursorIsHidden() ) {
537         show_pu_cursor = true;
538         puHideCursor();
539     }
540     fgSetBool("/sim/signals/screenshot", true);
541
542     FGRenderer *renderer = globals->get_renderer();
543     renderer->resize( fgGetInt("/sim/startup/xsize"),
544                       fgGetInt("/sim/startup/ysize") );
545
546     // we need two render frames here to clear the menu and cursor
547     // ... not sure why but doing an extra fgRenderFrame() shouldn't
548     // hurt anything
549     renderer->update( true );
550     renderer->update( true );
551
552     string dir = fgGetString("/sim/paths/screenshot-dir", fgGetString("/sim/fg-current"));
553     SGPath path(dir + '/');
554     if (path.create_dir( 0755 )) {
555         SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
556                 << dir << "'. Trying home directory.");
557         dir = fgGetString("/sim/fg-home");
558     }
559
560     char filename[24];
561     static int count = 1;
562     while (count < 1000) {
563         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
564
565         SGPath p(dir);
566         p.append(filename);
567         if (!p.exists()) {
568             path.set(p.str());
569             break;
570         }
571     }
572
573     int result = sg_glDumpWindow(path.c_str(),
574                                  fgGetInt("/sim/startup/xsize"),
575                                  fgGetInt("/sim/startup/ysize"));
576
577     fgSetString("/sim/paths/screenshot-last", path.c_str());
578     fgSetBool("/sim/signals/screenshot", false);
579
580     if ( show_pu_cursor ) {
581         puShowCursor();
582     }
583
584     TurnCursorOn();
585
586     if ( !freeze ) {
587         fgSetBool("/sim/freeze/master", false);
588     }
589     return result != 0;
590 }
591
592 // do an entire scenegraph dump
593 void fgDumpSceneGraph()
594 {
595     char *filename = new char [24];
596     string message;
597     static int count = 1;
598
599     FGRenderer *renderer = globals->get_renderer();
600
601     static const SGPropertyNode *master_freeze
602         = fgGetNode("/sim/freeze/master");
603
604     bool freeze = master_freeze->getBoolValue();
605     if ( !freeze ) {
606         fgSetBool("/sim/freeze/master", true);
607     }
608
609     while (count < 1000) {
610         FILE *fp;
611         snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
612         if ( (fp = fopen(filename, "r")) == NULL )
613             break;
614         fclose(fp);
615     }
616
617     if ( fgDumpSceneGraphToFile(filename)) {
618         message = "Entire scene graph saved to \"";
619         message += filename;
620         message += "\".";
621     } else {
622         message = "Failed to save to \"";
623         message += filename;
624         message += "\".";
625     }
626
627     mkDialog (message.c_str());
628
629     delete [] filename;
630
631     if ( !freeze ) {
632         fgSetBool("/sim/freeze/master", false);
633     }
634 }
635
636     
637 // do an terrain branch dump
638 void fgDumpTerrainBranch()
639 {
640     char *filename = new char [24];
641     string message;
642     static int count = 1;
643
644     FGRenderer *renderer = globals->get_renderer();
645
646     static const SGPropertyNode *master_freeze
647         = fgGetNode("/sim/freeze/master");
648
649     bool freeze = master_freeze->getBoolValue();
650     if ( !freeze ) {
651         fgSetBool("/sim/freeze/master", true);
652     }
653
654     while (count < 1000) {
655         FILE *fp;
656         snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
657         if ( (fp = fopen(filename, "r")) == NULL )
658             break;
659         fclose(fp);
660     }
661
662     if ( fgDumpTerrainBranchToFile(filename)) {
663         message = "Terrain graph saved to \"";
664         message += filename;
665         message += "\".";
666     } else {
667         message = "Failed to save to \"";
668         message += filename;
669         message += "\".";
670     }
671
672     mkDialog (message.c_str());
673
674     delete [] filename;
675
676     if ( !freeze ) {
677         fgSetBool("/sim/freeze/master", false);
678     }
679 }
680
681     
682