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