]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.cxx
allow to trigger widgets via accelerator key, which is defined via "keynum"
[flightgear.git] / src / Main / globals.cxx
1 // globals.cxx -- Global state that needs to be shared among the sim modules
2 //
3 // Written by Curtis Olson, started July 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - http://www.flightgear.org/~curt
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 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/sound/soundmgr_openal.hxx>
28 #include <simgear/structure/commands.hxx>
29 #include <simgear/misc/sg_path.hxx>
30
31 #include "globals.hxx"
32 #include "renderer.hxx"
33 #include "viewmgr.hxx"
34
35 #include "fg_props.hxx"
36 #include "fg_io.hxx"
37
38 \f
39 ////////////////////////////////////////////////////////////////////////
40 // Implementation of FGGlobals.
41 ////////////////////////////////////////////////////////////////////////
42
43 // global global :-)
44 FGGlobals *globals;
45
46
47 // Constructor
48 FGGlobals::FGGlobals() :
49     renderer( new FGRenderer ),
50     subsystem_mgr( new SGSubsystemMgr ),
51     event_mgr( new SGEventMgr ),
52     sim_time_sec( 0.0 ),
53     fg_root( "" ),
54 #if defined(FX) && defined(XMESA)
55     fullscreen( true ),
56 #endif
57     warp( 0 ),
58     warp_delta( 0 ),
59     time_params( NULL ),
60     ephem( NULL ),
61     mag( NULL ),
62     route_mgr( NULL ),
63     current_panel( NULL ),
64     soundmgr( NULL ),
65     airports( NULL ),
66     ATC_mgr( NULL ),
67     ATC_display( NULL ),
68     AI_mgr( NULL ),
69     controls( NULL ),
70     viewmgr( NULL ),
71     props( new SGPropertyNode ),
72     initial_state( NULL ),
73     locale( NULL ),
74     commands( new SGCommandMgr ),
75     model_lib( NULL ),
76     acmodel( NULL ),
77     model_mgr( NULL ),
78     channel_options_list( NULL ),
79     initial_waypoints( NULL ),
80     scenery( NULL ),
81     tile_mgr( NULL ),
82     io( new FGIO ),
83     navlist( NULL ),
84     loclist( NULL ),
85     gslist( NULL ),
86     dmelist( NULL ),
87     mkrlist( NULL ),
88     tacanlist( NULL ),
89     carrierlist( NULL ), 
90     fixlist( NULL )
91 {
92 }
93
94
95 // Destructor
96 FGGlobals::~FGGlobals() 
97 {
98     delete soundmgr;
99     delete subsystem_mgr;
100     delete event_mgr;
101     delete initial_state;
102     delete props;
103     delete commands;
104     delete io;
105     delete renderer;
106     delete initial_waypoints;
107 }
108
109
110 // set the fg_root path
111 void FGGlobals::set_fg_root (const string &root) {
112     fg_root = root;
113     
114     // append /data to root if it exists
115     SGPath tmp( fg_root );
116     tmp.append( "data" );
117     tmp.append( "version" );
118     if ( ulFileExists( tmp.c_str() ) ) {
119         fg_root += "/data";
120         }
121 }
122
123 void FGGlobals::set_fg_scenery (const string &scenery) {
124     SGPath s;
125     if (scenery.empty()) {
126         s.set( fg_root );
127         s.append( "Scenery" );
128     } else
129         s.set( scenery );
130
131     string_list path_list = sgPathSplit( s.str() );
132     fg_scenery.clear();
133
134     for (unsigned i = 0; i < path_list.size(); i++) {
135
136         ulDir *d = ulOpenDir( path_list[i].c_str() );
137         if (d == NULL)
138             continue;
139         ulCloseDir( d );
140
141         SGPath pt( path_list[i] ), po( path_list[i] );
142         pt.append("Terrain");
143         po.append("Objects");
144
145         ulDir *td = ulOpenDir( pt.c_str() );
146         ulDir *od = ulOpenDir( po.c_str() );
147
148         if (td == NULL && od == NULL)
149             fg_scenery.push_back( path_list[i] );
150         else {
151             if (td != NULL) {
152                 fg_scenery.push_back( pt.str() );
153                 ulCloseDir( td );
154             }
155             if (od != NULL) {
156                 fg_scenery.push_back( po.str() );
157                 ulCloseDir( od );
158             }
159         }
160     }
161 }
162
163
164 FGRenderer *
165 FGGlobals::get_renderer () const
166 {
167    return renderer;
168 }
169
170 SGSubsystemMgr *
171 FGGlobals::get_subsystem_mgr () const
172 {
173     return subsystem_mgr;
174 }
175
176 SGSubsystem *
177 FGGlobals::get_subsystem (const char * name)
178 {
179     return subsystem_mgr->get_subsystem(name);
180 }
181
182 void
183 FGGlobals::add_subsystem (const char * name,
184                           SGSubsystem * subsystem,
185                           SGSubsystemMgr::GroupType type,
186                           double min_time_sec)
187 {
188     subsystem_mgr->add(name, subsystem, type, min_time_sec);
189 }
190
191
192 SGEventMgr *
193 FGGlobals::get_event_mgr () const
194 {
195     return event_mgr;
196 }
197
198
199 // Save the current state as the initial state.
200 void
201 FGGlobals::saveInitialState ()
202 {
203   delete initial_state;
204   initial_state = new SGPropertyNode();
205
206   if (!copyProperties(props, initial_state))
207     SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
208 }
209
210
211 // Restore the saved initial state, if any
212 void
213 FGGlobals::restoreInitialState ()
214 {
215     if ( initial_state == 0 ) {
216         SG_LOG(SG_GENERAL, SG_ALERT,
217                "No initial state available to restore!!!");
218         return;
219     }
220
221     SGPropertyNode *currentPresets = new SGPropertyNode;
222     SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
223
224     // stash the /sim/presets tree
225     if ( !copyProperties(targetNode, currentPresets) ) {
226         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
227     }
228     
229     if ( copyProperties(initial_state, props) ) {
230         SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
231     } else {
232         SG_LOG( SG_GENERAL, SG_INFO,
233                 "Some errors restoring initial state (read-only props?)" );
234     }
235
236     // recover the /sim/presets tree
237     if ( !copyProperties(currentPresets, targetNode) ) {
238         SG_LOG( SG_GENERAL, SG_ALERT,
239                 "Failed to restore /sim/presets subtree" );
240     }
241
242    delete currentPresets;
243 }
244
245 FGViewer *
246 FGGlobals::get_current_view () const
247 {
248   return viewmgr->get_current_view();
249 }
250
251 // end of globals.cxx