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