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