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