]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
cccaf0f7ce0b551bd046bae0ca57d3680fb38e8c
[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, SGCommandState ** state)
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, SGCommandState ** state)
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, SGCommandState ** state)
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, SGCommandState ** state)
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, SGCommandState ** state)
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, SGCommandState ** state)
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, SGCommandState ** state)
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, SGCommandState ** state)
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, SGCommandState ** state)
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, SGCommandState ** state)
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, SGCommandState ** state)
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, SGCommandState ** state)
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->setUnspecifiedValue(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, SGCommandState ** state)
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::UNSPECIFIED:
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 <<<<<<< fg_commands.cxx
308  * Built-in command: multiply a property value.
309  *
310  * property: the name of the property to multiply.
311  * factor: the amount by which to multiply.
312  */
313 static bool
314 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
315 {
316   const string & propname = arg->getStringValue("property", "");
317   if (propname == "")
318     return false;
319
320   SGPropertyNode * node = fgGetNode(propname, true);
321
322   switch (node->getType()) {
323   case SGPropertyNode::BOOL:
324     return node->setBoolValue(node->getBoolValue() &&
325                               arg->getBoolValue("factor"));
326   case SGPropertyNode::INT:
327     return node->setIntValue(int(node->getIntValue()
328                                  * arg->getDoubleValue("factor")));
329   case SGPropertyNode::LONG:
330     return node->setLongValue(long(node->getLongValue()
331                                    * arg->getDoubleValue("factor")));
332   case SGPropertyNode::FLOAT:
333     return node->setFloatValue(float(node->getFloatValue()
334                                      * arg->getDoubleValue("factor")));
335   case SGPropertyNode::DOUBLE:
336   case SGPropertyNode::UNSPECIFIED:
337     return node->setDoubleValue(node->getDoubleValue()
338                                 * arg->getDoubleValue("factor"));
339   default:                      // doesn't make sense with strings
340     return false;
341   }
342 }
343
344
345 /**
346 =======
347  * Built-in command: multiply a property value.
348  *
349  * property: the name of the property to multiply.
350  * factor: the amount by which to multiply.
351  */
352 static bool
353 do_property_multiply (const SGPropertyNode * arg)
354 {
355   const string & propname = arg->getStringValue("property", "");
356   if (propname == "")
357     return false;
358
359   SGPropertyNode * node = fgGetNode(propname, true);
360
361   switch (node->getType()) {
362   case SGPropertyNode::BOOL:
363     return node->setBoolValue(node->getBoolValue() &&
364                               arg->getBoolValue("factor"));
365   case SGPropertyNode::INT:
366     return node->setIntValue(int(node->getIntValue()
367                                  * arg->getDoubleValue("factor")));
368   case SGPropertyNode::LONG:
369     return node->setLongValue(long(node->getLongValue()
370                                    * arg->getDoubleValue("factor")));
371   case SGPropertyNode::FLOAT:
372     return node->setFloatValue(float(node->getFloatValue()
373                                      * arg->getDoubleValue("factor")));
374   case SGPropertyNode::DOUBLE:
375   case SGPropertyNode::UNSPECIFIED:
376     return node->setDoubleValue(node->getDoubleValue()
377                                 * arg->getDoubleValue("factor"));
378   default:                      // doesn't make sense with strings
379     return false;
380   }
381 }
382
383
384 /**
385  * Built-in command: swap two property values.
386  *
387  * property[0]: the name of the first property.
388  * property[1]: the name of the second property.
389  */
390 static bool
391 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
392 {
393   const string &propname1 = arg->getStringValue("property[0]", "");
394   const string &propname2 = arg->getStringValue("property[1]", "");
395   if (propname1 == "" || propname2 == "")
396     return false;
397
398   SGPropertyNode * node1 = fgGetNode(propname1, true);
399   SGPropertyNode * node2 = fgGetNode(propname2, true);
400   const string & tmp = node1->getStringValue();
401   return (node1->setUnspecifiedValue(node2->getStringValue()) &&
402           node2->setUnspecifiedValue(tmp));
403 }
404
405
406 /**
407  * Set a property to an axis or other moving input.
408  *
409  * property: the name of the property to set.
410  * setting: the current input setting, usually between -1.0 and 1.0.
411  * offset: the offset to shift by, before applying the factor.
412  * factor: the factor to multiply by (use negative to reverse).
413  */
414 static bool
415 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
416 {
417   const string &propname = arg->getStringValue("property");
418   double setting = arg->getDoubleValue("setting", 0.0);
419   double offset = arg->getDoubleValue("offset", 0.0);
420   double factor = arg->getDoubleValue("factor", 1.0);
421   return fgSetDouble(propname, (setting + offset) * factor);
422 }
423
424
425 \f
426 /**
427  * Table of built-in commands.
428  *
429  * New commands do not have to be added here; any module in the application
430  * can add a new command using globals->get_commands()->addCommand(...).
431  */
432 static struct {
433   const char * name;
434   SGCommandMgr::command_t command;
435 } built_ins [] = {
436     { "null", do_null },
437     { "exit", do_exit },
438     { "load", do_load },
439     { "save", do_save },
440     { "panel-load", do_panel_load },
441     { "preferences-load", do_preferences_load },
442     { "view-cycle", do_view_cycle },
443     { "screen-capture", do_screen_capture },
444     { "tile-cache-reload", do_tile_cache_reload },
445     { "lighting-update", do_lighting_update },
446     { "property-toggle", do_property_toggle },
447     { "property-assign", do_property_assign },
448     { "property-adjust", do_property_adjust },
449     { "property-multiply", do_property_multiply },
450     { "property-swap", do_property_swap },
451     { "property-scale", do_property_scale },
452     { 0, 0 }                    // zero-terminated
453 };
454
455
456 /**
457  * Initialize the default built-in commands.
458  *
459  * Other commands may be added by other parts of the application.
460  */
461 void
462 fgInitCommands ()
463 {
464   SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
465   for (int i = 0; built_ins[i].name != 0; i++) {
466     SG_LOG(SG_GENERAL, SG_INFO, "  " << built_ins[i].name);
467     globals->get_commands()->addCommand(built_ins[i].name,
468                                         built_ins[i].command);
469   }
470 }
471
472 // end of fg_commands.hxx