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