]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
- added load and save commands
[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
16 #include "fg_commands.hxx"
17
18 SG_USING_STD(string);
19 SG_USING_STD(ifstream);
20 SG_USING_STD(ofstream);
21
22 #include "fg_props.hxx"
23 #include "fg_io.hxx"
24 #include "globals.hxx"
25
26
27 /**
28  * Built-in command: do nothing.
29  */
30 static bool
31 do_null (const SGPropertyNode * arg)
32 {
33   return true;
34 }
35
36
37 /**
38  * Built-in command: exit FlightGear.
39  *
40  * TODO: show a confirm dialog.
41  */
42 static bool
43 do_exit (const SGPropertyNode * arg)
44 {
45   fgIOShutdownAll();
46   exit(0);
47   return true;
48 }
49
50
51 /**
52  * Built-in command: load flight.
53  *
54  * file (optional): the name of the file to load (relative to current
55  * directory).  Defaults to "fgfs.sav".
56  */
57 static bool
58 do_load (const SGPropertyNode * arg)
59 {
60   const string &file = arg->getStringValue("file", "fgfs.sav");
61   ifstream input(file.c_str());
62   if (input.good() && fgLoadFlight(input)) {
63     input.close();
64     SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
65     return true;
66   } else {
67     SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
68     return false;
69   }
70 }
71
72
73 /**
74  * Built-in command: save flight.
75  *
76  * file (optional): the name of the file to save (relative to the
77  * current directory).  Defaults to "fgfs.sav".
78  */
79 static bool
80 do_save (const SGPropertyNode * arg)
81 {
82   const string &file = arg->getStringValue("file", "fgfs.sav");
83   SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
84   ofstream output(file.c_str());
85   if (output.good() && fgSaveFlight(output)) {
86     output.close();
87     SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
88     return true;
89   } else {
90     SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
91     return false;
92   }
93 }
94
95
96 /**
97  * Built-in command: (re)load the panel.
98  *
99  * path (optional): the file name to load the panel from 
100  * (relative to FG_ROOT).  Defaults to the value of /sim/panel/path,
101  * and if that's unspecified, to "Panels/Default/default.xml".
102  */
103 static bool
104 do_panel_load (const SGPropertyNode * arg)
105 {
106   string panel_path =
107     arg->getStringValue("path",
108                         fgGetString("/sim/panel/path",
109                                     "Panels/Default/default.xml"));
110   FGPanel * new_panel = fgReadPanel(panel_path);
111   if (new_panel == 0) {
112     SG_LOG(SG_INPUT, SG_ALERT,
113            "Error reading new panel from " << panel_path);
114     return false;
115   }
116   SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
117   current_panel->unbind();
118   delete current_panel;
119   current_panel = new_panel;
120   current_panel->bind();
121   return true;
122 }
123
124
125 /**
126  * Built-in command: (re)load preferences.
127  *
128  * path (optional): the file name to load the panel from (relative
129  * to FG_ROOT). Defaults to "preferences.xml".
130  */
131 static bool
132 do_preferences_load (const SGPropertyNode * arg)
133 {
134   const string &path = arg->getStringValue("path", "preferences.xml");
135   SGPath props_path(globals->get_fg_root());
136   props_path.append(path);
137   SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
138          << props_path.str());
139   if (!readProperties(props_path.str(), globals->get_props())) {
140     SG_LOG(SG_INPUT, SG_ALERT, "Failed to reread global preferences");
141     return false;
142   } else {
143     SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
144     return true;
145   }
146 }
147
148
149 /**
150  * Built-in command: cycle view.
151  */
152 static bool
153 do_view_cycle (const SGPropertyNode * arg)
154 {
155   globals->get_current_view()->set_view_offset(0.0);
156   globals->set_current_view(globals->get_viewmgr()->next_view());
157 //   fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
158   return true;
159 }
160
161
162 /**
163  * Built-in command: capture screen.
164  */
165 static bool
166 do_screen_capture (const SGPropertyNode * arg)
167 {
168   fgDumpSnapShot();
169   return true;
170 }
171
172
173 /**
174  * Built-in command: toggle a bool property value.
175  *
176  * property: The name of the property to toggle.
177  */
178 static bool
179 do_property_toggle (const SGPropertyNode * arg)
180 {
181   const string & propname = arg->getStringValue("property", "");
182   if (propname == "")
183     return false;
184
185   SGPropertyNode * node = fgGetNode(propname);
186   return node->setBoolValue(!node->getBoolValue());
187 }
188
189
190 /**
191  * Built-in command: assign a value to a property.
192  *
193  * property: the name of the property to assign.
194  * value: the value to assign.
195  */
196 static bool
197 do_property_assign (const SGPropertyNode * arg)
198 {
199   const string & propname = arg->getStringValue("property", "");
200   if (propname == "")
201     return false;
202
203   SGPropertyNode * node = fgGetNode(propname, true);
204
205   switch (node->getType()) {
206   case SGValue::BOOL:
207     return node->setBoolValue(arg->getBoolValue("value"));
208   case SGValue::INT:
209     return node->setIntValue(arg->getIntValue("value"));
210   case SGValue::LONG:
211     return node->setLongValue(arg->getLongValue("value"));
212   case SGValue::FLOAT:
213     return node->setFloatValue(arg->getFloatValue("value"));
214   case SGValue::DOUBLE:
215     return node->setDoubleValue(arg->getDoubleValue("value"));
216   case SGValue::STRING:
217     return node->setStringValue(arg->getStringValue("value"));
218   default:
219     return node->setUnknownValue(arg->getStringValue("value"));
220   }
221 }
222
223
224 /**
225  * Built-in command: increment or decrement a property value.
226  *
227  * property: the name of the property to increment or decrement.
228  * step: the amount of the increment or decrement.
229  */
230 static bool
231 do_property_adjust (const SGPropertyNode * arg)
232 {
233   const string & propname = arg->getStringValue("property", "");
234   if (propname == "")
235     return false;
236
237   SGPropertyNode * node = fgGetNode(propname, true);
238
239   switch (node->getType()) {
240   case SGValue::BOOL:
241     if (arg->getBoolValue("step"))
242       return node->setBoolValue(!node->getBoolValue());
243     else
244       return true;
245   case SGValue::INT:
246     return node->setIntValue(node->getIntValue()
247                              + arg->getIntValue("step"));
248   case SGValue::LONG:
249     return node->setLongValue(node->getLongValue()
250                               + arg->getLongValue("step"));
251   case SGValue::FLOAT:
252     return node->setFloatValue(node->getFloatValue()
253                                + arg->getFloatValue("step"));
254   case SGValue::DOUBLE:
255     return node->setDoubleValue(node->getDoubleValue()
256                                 + arg->getDoubleValue("step"));
257   default:                      // doesn't make sense with strings
258     return false;
259   }
260 }
261
262
263 /**
264  * Built-in command: swap two property values.
265  *
266  * property[0]: the name of the first property.
267  * property[1]: the name of the second property.
268  */
269 static bool
270 do_property_swap (const SGPropertyNode * arg)
271 {
272   const string &propname1 = arg->getStringValue("property[0]", "");
273   const string &propname2 = arg->getStringValue("property[1]", "");
274   if (propname1 == "" || propname2 == "")
275     return false;
276
277   SGPropertyNode * node1 = fgGetNode(propname1, true);
278   SGPropertyNode * node2 = fgGetNode(propname2, true);
279   const string & tmp = node1->getStringValue();
280   return (node1->setUnknownValue(node2->getStringValue()) &&
281           node2->setUnknownValue(tmp));
282 }
283
284
285 /**
286  * Set a property to an axis or other moving input.
287  *
288  * property: the name of the property to set.
289  * setting: the current input setting, usually between -1.0 and 1.0.
290  * offset: the offset to shift by, before applying the factor.
291  * factor: the factor to multiply by (use negative to reverse).
292  */
293 static bool
294 do_property_scale (const SGPropertyNode * arg)
295 {
296   const string &propname = arg->getStringValue("property");
297   double setting = arg->getDoubleValue("setting", 0.0);
298   double offset = arg->getDoubleValue("offset", 0.0);
299   double factor = arg->getDoubleValue("factor", 1.0);
300   return fgSetDouble(propname, (setting + offset) * factor);
301 }
302
303
304 \f
305 /**
306  * Table of built-in commands.
307  *
308  * New commands do not have to be added here; any module in the application
309  * can add a new command using globals->get_commands()->addCommand(...).
310  */
311 static struct {
312   const char * name;
313   SGCommandMgr::command_t command;
314 } built_ins [] = {
315   "null", do_null,
316   "exit", do_exit,
317   "load", do_load,
318   "save", do_save,
319   "panel-load", do_panel_load,
320   "preferences-load", do_preferences_load,
321   "view-cycle", do_view_cycle,
322   "screen-capture", do_screen_capture,
323   "property-toggle", do_property_toggle,
324   "property-assign", do_property_assign,
325   "property-adjust", do_property_adjust,
326   "property-swap", do_property_swap,
327   "property-scale", do_property_scale,
328   0, 0                          // zero-terminated
329 };
330
331
332 /**
333  * Initialize the default built-in commands.
334  *
335  * Other commands may be added by other parts of the application.
336  */
337 void
338 fgInitCommands ()
339 {
340   SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
341   for (int i = 0; built_ins[i].name != 0; i++) {
342     SG_LOG(SG_GENERAL, SG_INFO, "  " << built_ins[i].name);
343     globals->get_commands()->addCommand(built_ins[i].name,
344                                         built_ins[i].command);
345   }
346 }
347
348 // end of fg_commands.hxx