]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
9ae0129f5faeca87c3eb54ba4793bf47d4bc3741
[flightgear.git] / src / Main / fg_commands.cxx
1 // fg_commands.cxx - internal FGFS commands.
2
3 #include <simgear/compiler.h>
4
5 #include STL_STRING
6 #include STL_FSTREAM
7
8 #include <simgear/debug/logstream.hxx>
9 #include <simgear/misc/commands.hxx>
10 #include <simgear/misc/props.hxx>
11
12 #include <GUI/gui.h>
13 #include <Cockpit/panel.hxx>
14 #include <Cockpit/panel_io.hxx>
15 #include <Scenery/tilemgr.hxx>
16 #include <Time/tmp.hxx>
17
18 #include "fg_commands.hxx"
19
20 SG_USING_STD(string);
21 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
22 SG_USING_STD(ifstream);
23 SG_USING_STD(ofstream);
24 #endif
25
26 #include "fg_props.hxx"
27 #include "fg_io.hxx"
28 #include "globals.hxx"
29
30
31 /**
32  * Built-in command: do nothing.
33  */
34 static bool
35 do_null (const SGPropertyNode * arg)
36 {
37   return true;
38 }
39
40
41 /**
42  * Built-in command: exit FlightGear.
43  *
44  * TODO: show a confirm dialog.
45  */
46 static bool
47 do_exit (const SGPropertyNode * arg)
48 {
49   SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
50   ConfirmExitDialog();
51   return true;
52 }
53
54
55 /**
56  * Built-in command: load flight.
57  *
58  * file (optional): the name of the file to load (relative to current
59  * directory).  Defaults to "fgfs.sav".
60  */
61 static bool
62 do_load (const SGPropertyNode * arg)
63 {
64   const string &file = arg->getStringValue("file", "fgfs.sav");
65   ifstream input(file.c_str());
66   if (input.good() && fgLoadFlight(input)) {
67     input.close();
68     SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
69     return true;
70   } else {
71     SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
72     return false;
73   }
74 }
75
76
77 /**
78  * Built-in command: save flight.
79  *
80  * file (optional): the name of the file to save (relative to the
81  * current directory).  Defaults to "fgfs.sav".
82  */
83 static bool
84 do_save (const SGPropertyNode * arg)
85 {
86   const string &file = arg->getStringValue("file", "fgfs.sav");
87   SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
88   ofstream output(file.c_str());
89   if (output.good() && fgSaveFlight(output)) {
90     output.close();
91     SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
92     return true;
93   } else {
94     SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
95     return false;
96   }
97 }
98
99
100 /**
101  * Built-in command: (re)load the panel.
102  *
103  * path (optional): the file name to load the panel from 
104  * (relative to FG_ROOT).  Defaults to the value of /sim/panel/path,
105  * and if that's unspecified, to "Panels/Default/default.xml".
106  */
107 static bool
108 do_panel_load (const SGPropertyNode * arg)
109 {
110   string panel_path =
111     arg->getStringValue("path",
112                         fgGetString("/sim/panel/path",
113                                     "Panels/Default/default.xml"));
114   FGPanel * new_panel = fgReadPanel(panel_path);
115   if (new_panel == 0) {
116     SG_LOG(SG_INPUT, SG_ALERT,
117            "Error reading new panel from " << panel_path);
118     return false;
119   }
120   SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
121   current_panel->unbind();
122   delete current_panel;
123   current_panel = new_panel;
124   current_panel->bind();
125   return true;
126 }
127
128
129 /**
130  * Built-in command: (re)load preferences.
131  *
132  * path (optional): the file name to load the panel from (relative
133  * to FG_ROOT). Defaults to "preferences.xml".
134  */
135 static bool
136 do_preferences_load (const SGPropertyNode * arg)
137 {
138   const string &path = arg->getStringValue("path", "preferences.xml");
139   SGPath props_path(globals->get_fg_root());
140   props_path.append(path);
141   SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
142          << props_path.str());
143   if (!readProperties(props_path.str(), globals->get_props())) {
144     SG_LOG(SG_INPUT, SG_ALERT, "Failed to reread global preferences");
145     return false;
146   } else {
147     SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
148     return true;
149   }
150 }
151
152
153 /**
154  * Built-in command: cycle view.
155  */
156 static bool
157 do_view_cycle (const SGPropertyNode * arg)
158 {
159   globals->get_current_view()->set_view_offset(0.0);
160   globals->set_current_view(globals->get_viewmgr()->next_view());
161 //   fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
162   return true;
163 }
164
165
166 /**
167  * Built-in command: capture screen.
168  */
169 static bool
170 do_screen_capture (const SGPropertyNode * arg)
171 {
172   fgDumpSnapShot();
173   return true;
174 }
175
176
177 /**
178  * Reload the tile cache.
179  */
180 static bool
181 do_tile_cache_reload (const SGPropertyNode * arg)
182 {
183   bool freeze = globals->get_freeze();
184   SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
185   if ( !freeze ) 
186     globals->set_freeze( true );
187   BusyCursor(0);
188   if ( global_tile_mgr.init() ) {
189     // Load the local scenery data
190     global_tile_mgr.update(fgGetDouble("/position/longitude"),
191                            fgGetDouble("/position/latitude"));
192   } else {
193     SG_LOG( SG_GENERAL, SG_ALERT, 
194             "Error in Tile Manager initialization!" );
195     exit(-1);
196   }
197   BusyCursor(1);
198   if ( !freeze )
199     globals->set_freeze( false );
200   return true;
201 }
202
203
204 /**
205  * Update the lighting manually.
206  */
207 static bool
208 do_lighting_update (const SGPropertyNode * arg)
209 {
210   fgUpdateSkyAndLightingParams();
211   return true;
212 }
213
214
215 /**
216  * Built-in command: toggle a bool property value.
217  *
218  * property: The name of the property to toggle.
219  */
220 static bool
221 do_property_toggle (const SGPropertyNode * arg)
222 {
223   const string & propname = arg->getStringValue("property", "");
224   if (propname == "")
225     return false;
226
227   SGPropertyNode * node = fgGetNode(propname);
228   return node->setBoolValue(!node->getBoolValue());
229 }
230
231
232 /**
233  * Built-in command: assign a value to a property.
234  *
235  * property: the name of the property to assign.
236  * value: the value to assign.
237  */
238 static bool
239 do_property_assign (const SGPropertyNode * arg)
240 {
241   const string & propname = arg->getStringValue("property", "");
242   if (propname == "")
243     return false;
244
245   SGPropertyNode * node = fgGetNode(propname, true);
246
247   switch (node->getType()) {
248   case SGPropertyNode::BOOL:
249     return node->setBoolValue(arg->getBoolValue("value"));
250   case SGPropertyNode::INT:
251     return node->setIntValue(arg->getIntValue("value"));
252   case SGPropertyNode::LONG:
253     return node->setLongValue(arg->getLongValue("value"));
254   case SGPropertyNode::FLOAT:
255     return node->setFloatValue(arg->getFloatValue("value"));
256   case SGPropertyNode::DOUBLE:
257     return node->setDoubleValue(arg->getDoubleValue("value"));
258   case SGPropertyNode::STRING:
259     return node->setStringValue(arg->getStringValue("value"));
260   default:
261     return node->setUnknownValue(arg->getStringValue("value"));
262   }
263 }
264
265
266 /**
267  * Built-in command: increment or decrement a property value.
268  *
269  * property: the name of the property to increment or decrement.
270  * step: the amount of the increment or decrement.
271  */
272 static bool
273 do_property_adjust (const SGPropertyNode * arg)
274 {
275   const string & propname = arg->getStringValue("property", "");
276   if (propname == "")
277     return false;
278
279   SGPropertyNode * node = fgGetNode(propname, true);
280
281   switch (node->getType()) {
282   case SGPropertyNode::BOOL:
283     if (arg->getBoolValue("step"))
284       return node->setBoolValue(!node->getBoolValue());
285     else
286       return true;
287   case SGPropertyNode::INT:
288     return node->setIntValue(node->getIntValue()
289                              + arg->getIntValue("step"));
290   case SGPropertyNode::LONG:
291     return node->setLongValue(node->getLongValue()
292                               + arg->getLongValue("step"));
293   case SGPropertyNode::FLOAT:
294     return node->setFloatValue(node->getFloatValue()
295                                + arg->getFloatValue("step"));
296   case SGPropertyNode::DOUBLE:
297   case SGPropertyNode::UNKNOWN:
298     return node->setDoubleValue(node->getDoubleValue()
299                                 + arg->getDoubleValue("step"));
300   default:                      // doesn't make sense with strings
301     return false;
302   }
303 }
304
305
306 /**
307  * Built-in command: multiply a property value.
308  *
309  * property: the name of the property to multiply.
310  * factor: the amount by which to multiply.
311  */
312 static bool
313 do_property_multiply (const SGPropertyNode * arg)
314 {
315   const string & propname = arg->getStringValue("property", "");
316   if (propname == "")
317     return false;
318
319   SGPropertyNode * node = fgGetNode(propname, true);
320
321   switch (node->getType()) {
322   case SGPropertyNode::BOOL:
323     return node->setBoolValue(node->getBoolValue() &&
324                               arg->getBoolValue("factor"));
325   case SGPropertyNode::INT:
326     return node->setIntValue(int(node->getIntValue()
327                                  * arg->getDoubleValue("factor")));
328   case SGPropertyNode::LONG:
329     return node->setLongValue(long(node->getLongValue()
330                                    * arg->getDoubleValue("factor")));
331   case SGPropertyNode::FLOAT:
332     return node->setFloatValue(float(node->getFloatValue()
333                                      * arg->getDoubleValue("factor")));
334   case SGPropertyNode::DOUBLE:
335   case SGPropertyNode::UNKNOWN:
336     return node->setDoubleValue(node->getDoubleValue()
337                                 * arg->getDoubleValue("factor"));
338   default:                      // doesn't make sense with strings
339     return false;
340   }
341 }
342
343
344 /**
345  * Built-in command: swap two property values.
346  *
347  * property[0]: the name of the first property.
348  * property[1]: the name of the second property.
349  */
350 static bool
351 do_property_swap (const SGPropertyNode * arg)
352 {
353   const string &propname1 = arg->getStringValue("property[0]", "");
354   const string &propname2 = arg->getStringValue("property[1]", "");
355   if (propname1 == "" || propname2 == "")
356     return false;
357
358   SGPropertyNode * node1 = fgGetNode(propname1, true);
359   SGPropertyNode * node2 = fgGetNode(propname2, true);
360   const string & tmp = node1->getStringValue();
361   return (node1->setUnknownValue(node2->getStringValue()) &&
362           node2->setUnknownValue(tmp));
363 }
364
365
366 /**
367  * Set a property to an axis or other moving input.
368  *
369  * property: the name of the property to set.
370  * setting: the current input setting, usually between -1.0 and 1.0.
371  * offset: the offset to shift by, before applying the factor.
372  * factor: the factor to multiply by (use negative to reverse).
373  */
374 static bool
375 do_property_scale (const SGPropertyNode * arg)
376 {
377   const string &propname = arg->getStringValue("property");
378   double setting = arg->getDoubleValue("setting", 0.0);
379   double offset = arg->getDoubleValue("offset", 0.0);
380   double factor = arg->getDoubleValue("factor", 1.0);
381   return fgSetDouble(propname, (setting + offset) * factor);
382 }
383
384
385 \f
386 /**
387  * Table of built-in commands.
388  *
389  * New commands do not have to be added here; any module in the application
390  * can add a new command using globals->get_commands()->addCommand(...).
391  */
392 static struct {
393   const char * name;
394   SGCommandMgr::command_t command;
395 } built_ins [] = {
396     { "null", do_null },
397     { "exit", do_exit },
398     { "load", do_load },
399     { "save", do_save },
400     { "panel-load", do_panel_load },
401     { "preferences-load", do_preferences_load },
402     { "view-cycle", do_view_cycle },
403     { "screen-capture", do_screen_capture },
404     { "tile-cache-reload", do_tile_cache_reload },
405     { "lighting-update", do_lighting_update },
406     { "property-toggle", do_property_toggle },
407     { "property-assign", do_property_assign },
408     { "property-adjust", do_property_adjust },
409     { "property-multiply", do_property_multiply },
410     { "property-swap", do_property_swap },
411     { "property-scale", do_property_scale },
412     { 0, 0 }                    // zero-terminated
413 };
414
415
416 /**
417  * Initialize the default built-in commands.
418  *
419  * Other commands may be added by other parts of the application.
420  */
421 void
422 fgInitCommands ()
423 {
424   SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
425   for (int i = 0; built_ins[i].name != 0; i++) {
426     SG_LOG(SG_GENERAL, SG_INFO, "  " << built_ins[i].name);
427     globals->get_commands()->addCommand(built_ins[i].name,
428                                         built_ins[i].command);
429   }
430 }
431
432 // end of fg_commands.hxx