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