]> git.mxchange.org Git - flightgear.git/blob - src/GUI/preset_dlg.cxx
Massaging the FGTileMgr->update() interface towards using FGLocation.
[flightgear.git] / src / GUI / preset_dlg.cxx
1 // preset_dlg.cxx -- Preset dialogs and funcitons
2 //
3 // Written by Curtis Olson, started November 2002.
4 //
5 // Copyright (C) 2002  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #ifdef HAVE_WINDOWS_H
31 #  include <windows.h>
32 #endif
33
34 // #include <stdlib.h>
35
36 // #include <simgear/constants.h>
37 // #include <simgear/debug/logstream.hxx>
38 #include <simgear/misc/sg_path.hxx>
39
40 // #include <Include/general.hxx>
41
42 // #include <GL/glut.h>
43 // #include <GL/gl.h>
44
45 #include <Airports/simple.hxx>
46 #include <FDM/flight.hxx>
47 #include <Main/fg_init.hxx>
48 #include <Main/fg_props.hxx>
49 #include <Scenery/tilemgr.hxx>
50
51 #include "gui.h"
52 #include "preset_dlg.hxx"
53
54
55 static puDialogBox *PresetDialog = 0;
56 static puFrame     *PresetDialogFrame = 0;
57 static puText      *PresetDialogMessage = 0;
58 static puInput     *PresetDialogInput = 0;
59
60 static const int MAX_VALUE = 16;
61 static char PresetValue[MAX_VALUE];
62 static char PresetSavedValue[MAX_VALUE];
63 static char PresetLabel[] = "Enter New Airport ID"; 
64 static string PresetProperty = "";
65
66 static puOneShot *PresetDialogOkButton = 0;
67 static puOneShot *PresetDialogCancelButton = 0;
68 static puOneShot *PresetDialogResetButton = 0;
69
70
71 static void PresetDialog_OK(puObject *)
72 {
73     char *value;
74     PresetDialogInput->getValue(&value);
75     cout << "setting " << PresetProperty << " = " << value << endl;
76     fgSetString( PresetProperty.c_str(), value );
77     FG_POP_PUI_DIALOG( PresetDialog );
78
79     // consistancy handling for some specialized cases
80     if ( PresetProperty == "/sim/presets/airport-id" ) {
81         fgSetDouble("/sim/presets/longitude-deg", -9999.0 );
82         fgSetDouble("/sim/presets/latitude-deg", -9999.0 );
83     } else if ( PresetProperty == "/sim/presets/runway" ) {
84         fgSetDouble("/sim/presets/longitude-deg", -9999.0 );
85         fgSetDouble("/sim/presets/latitude-deg", -9999.0 );
86     } else if ( PresetProperty == "/sim/presets/offset-distance" ) {
87         if ( fabs(fgGetDouble("/sim/presets/altitude-ft")) > 0.000001
88              && fabs(fgGetDouble("/sim/presets/glideslope-deg")) > 0.000001 ) {
89             fgSetDouble("/sim/presets/altitude-ft", -9999.0);
90             cout << "nuking altitude" << endl;
91         }
92     } else if ( PresetProperty == "/sim/presets/altitude-ft" ) {
93         if ( fabs(fgGetDouble("/sim/presets/offset-distance")) > 0.000001
94              && fabs(fgGetDouble("/sim/presets/glideslope-deg")) > 0.000001 ) {
95             fgSetDouble("/sim/presets/offset-distance", 0.0);
96             cout << "nuking offset distance" << endl;
97         }
98     } else if ( PresetProperty == "/sim/presets/glideslope-deg" ) {
99         if ( fabs(fgGetDouble("/sim/presets/offset-distance")) > 0.000001
100              && fabs(fgGetDouble("/sim/presets/altitude-ft")) > 0.000001 ) {
101             fgSetDouble("/sim/presets/altitude-ft", -9999.0);
102             cout << "nuking altitude" << endl;
103         }
104     }
105 }
106
107
108 static void PresetDialog_Cancel(puObject *)
109 {
110     FG_POP_PUI_DIALOG( PresetDialog );
111 }
112
113
114 static void PresetDialog_Reset(puObject *)
115 {
116     PresetDialogInput->setValue( PresetSavedValue );
117     PresetDialogInput->setCursor( 0 ) ;
118 }
119
120
121 // Initialize the preset dialog box
122 void fgPresetInit()
123 {
124     sprintf( PresetValue, "%s", fgGetString("/sim/presets/airport-id") );
125     int len = 150
126         - puGetDefaultLabelFont().getStringWidth( PresetLabel ) / 2;
127
128     PresetDialog = new puDialogBox (150, 50);
129     {
130         PresetDialogFrame = new puFrame           (0,0,350, 150);
131         PresetDialogMessage = new puText            (len, 110);
132         PresetDialogMessage -> setLabel          ("");
133
134         PresetDialogInput = new puInput           (50, 70, 300, 100);
135         PresetDialogInput -> setValue          ("");
136         PresetDialogInput -> acceptInput();
137
138         PresetDialogOkButton = new puOneShot   (50, 10, 110, 50);
139         PresetDialogOkButton -> setLegend(gui_msg_OK);
140         PresetDialogOkButton -> setCallback (PresetDialog_OK);
141         PresetDialogOkButton -> makeReturnDefault(TRUE);
142
143         PresetDialogCancelButton =  new puOneShot (140, 10, 210, 50);
144         PresetDialogCancelButton -> setLegend (gui_msg_CANCEL);
145         PresetDialogCancelButton -> setCallback (PresetDialog_Cancel);
146
147         PresetDialogResetButton =  new puOneShot (240, 10, 300, 50);
148         PresetDialogResetButton -> setLegend (gui_msg_RESET);
149         PresetDialogResetButton -> setCallback (PresetDialog_Reset);
150     }
151     cout << "PresetInit " << PresetValue << endl;
152     FG_FINALIZE_PUI_DIALOG( PresetDialog );
153 }
154
155
156 void fgPresetAirport(puObject *cb)
157 {
158     PresetDialogMessage -> setLabel( "Enter Airport ID:" );
159     PresetProperty = "/sim/presets/airport-id";
160     snprintf( PresetValue, MAX_VALUE, "%s",
161               fgGetString(PresetProperty.c_str()) );
162     snprintf( PresetSavedValue, MAX_VALUE, "%s",
163               fgGetString(PresetProperty.c_str()) );
164     PresetDialogInput->setValue( PresetValue );
165
166     FG_PUSH_PUI_DIALOG( PresetDialog );
167 }
168
169
170 void fgPresetRunway(puObject *cb)
171 {
172     PresetDialogMessage -> setLabel( "Enter Runway Number:" );
173     PresetProperty = "/sim/presets/runway";
174     snprintf( PresetValue, MAX_VALUE, "%s",
175               fgGetString(PresetProperty.c_str()) );
176     snprintf( PresetSavedValue, MAX_VALUE, "%s",
177               fgGetString(PresetProperty.c_str()) );
178     PresetDialogInput->setValue( PresetValue );
179
180     FG_PUSH_PUI_DIALOG( PresetDialog );
181 }
182
183
184 void fgPresetOffsetDistance(puObject *cb)
185 {
186     PresetDialogMessage -> setLabel( "Enter Offset Distance (miles):" );
187     PresetProperty = "/sim/presets/offset-distance";
188     snprintf( PresetValue, MAX_VALUE, "%s",
189               fgGetString(PresetProperty.c_str()) );
190     snprintf( PresetSavedValue, MAX_VALUE, "%s",
191               fgGetString(PresetProperty.c_str()) );
192     PresetDialogInput->setValue( PresetValue );
193
194     FG_PUSH_PUI_DIALOG( PresetDialog );
195 }
196
197
198 void fgPresetAltitude(puObject *cb)
199 {
200     PresetDialogMessage -> setLabel( "Enter Altitude (feet):" );
201     PresetProperty = "/sim/presets/altitude-ft";
202     snprintf( PresetValue, MAX_VALUE, "%s",
203               fgGetString(PresetProperty.c_str()) );
204     snprintf( PresetSavedValue, MAX_VALUE, "%s",
205               fgGetString(PresetProperty.c_str()) );
206     PresetDialogInput->setValue( PresetValue );
207
208     FG_PUSH_PUI_DIALOG( PresetDialog );
209 }
210
211
212 void fgPresetGlideslope(puObject *cb)
213 {
214     PresetDialogMessage -> setLabel( "Enter Glideslope (deg):" );
215     PresetProperty = "/sim/presets/glideslope-deg";
216     snprintf( PresetValue, MAX_VALUE, "%s",
217               fgGetString(PresetProperty.c_str()) );
218     snprintf( PresetSavedValue, MAX_VALUE, "%s",
219               fgGetString(PresetProperty.c_str()) );
220     PresetDialogInput->setValue( PresetValue );
221
222     FG_PUSH_PUI_DIALOG( PresetDialog );
223 }
224
225
226 void fgPresetAirspeed(puObject *cb)
227 {
228     PresetDialogMessage -> setLabel( "Enter Airspeed (kts):" );
229     PresetProperty = "/sim/presets/airspeed-kt";
230     snprintf( PresetValue, MAX_VALUE, "%s",
231               fgGetString(PresetProperty.c_str()) );
232     snprintf( PresetSavedValue, MAX_VALUE, "%s",
233               fgGetString(PresetProperty.c_str()) );
234     PresetDialogInput->setValue( PresetValue );
235
236     FG_PUSH_PUI_DIALOG( PresetDialog );
237 }
238
239
240 void fgPresetCommit(puObject *)
241 {
242     static const SGPropertyNode *longitude
243         = fgGetNode("/sim/presets/longitude-deg");
244     static const SGPropertyNode *latitude
245         = fgGetNode("/sim/presets/latitude-deg");
246     static const SGPropertyNode *master_freeze
247         = fgGetNode("/sim/freeze/master");
248
249     SGPath path( globals->get_fg_root() );
250     path.append( "Airports" );
251     path.append( "simple.mk4" );
252     FGAirports airports( path.c_str() );
253
254     FGAirport a;
255
256     bool freeze = master_freeze->getBoolValue();
257     if ( !freeze ) {
258         fgSetBool("/sim/freeze/master", true);
259     }
260
261     // unbind the current fdm state so property changes
262     // don't get lost when we subsequently delete this fdm
263     // and create a new one.
264     cur_fdm_state->unbind();
265         
266     // invalidate lon/lat if an airport is specified in the presets
267     string apt = fgGetString("/sim/presets/airport-id");
268     if ( !apt.empty() ) {
269         fgSetDouble("/sim/presets/longitude-deg", -9999.0 );
270         fgSetDouble("/sim/presets/latitude-deg", -9999.0 );
271     }
272
273     // set position from presets
274     fgInitPosition();
275
276     // BusyCursor(0);
277     fgReInitSubsystems();
278
279     cout << "before tile_mgr init " << longitude->getDoubleValue() << " "
280          << latitude->getDoubleValue() << endl;
281
282     double visibility_meters = fgGetDouble("/environment/visibility-m");
283     global_tile_mgr.update( visibility_meters );
284     // BusyCursor(1);
285
286     if ( !freeze ) {
287         fgSetBool("/sim/freeze/master", false);
288     }
289 }
290
291