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