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