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