1 // fg_commands.cxx - internal FGFS commands.
3 #include "fg_commands.hxx"
5 #include <simgear/debug/logstream.hxx>
6 #include <simgear/misc/commands.hxx>
7 #include <simgear/misc/props.hxx>
11 #include "fg_props.hxx"
13 #include "globals.hxx"
17 * Built-in command: do nothing.
20 do_null (const SGPropertyNode * arg)
27 * Built-in command: exit FlightGear.
29 * TODO: show a confirm dialog.
32 do_exit (const SGPropertyNode * arg)
41 * Built-in command: cycle view.
44 do_view_cycle (const SGPropertyNode * arg)
46 globals->get_current_view()->set_view_offset(0.0);
47 globals->set_current_view(globals->get_viewmgr()->next_view());
48 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
54 * Built-in command: capture screen.
57 do_screen_capture (const SGPropertyNode * arg)
65 * Built-in command: toggle a bool property value.
67 * property: The name of the property to toggle.
70 do_property_toggle (const SGPropertyNode * arg)
72 const string & propname = arg->getStringValue("property", "");
76 SGPropertyNode * node = fgGetNode(propname);
77 return node->setBoolValue(!node->getBoolValue());
82 * Built-in command: assign a value to a property.
84 * property: the name of the property to assign.
85 * value: the value to assign.
88 do_property_assign (const SGPropertyNode * arg)
90 const string & propname = arg->getStringValue("property", "");
94 SGPropertyNode * node = fgGetNode(propname, true);
96 switch (node->getType()) {
98 return node->setBoolValue(arg->getBoolValue("value"));
100 return node->setIntValue(arg->getIntValue("value"));
102 return node->setLongValue(arg->getLongValue("value"));
104 return node->setFloatValue(arg->getFloatValue("value"));
105 case SGValue::DOUBLE:
106 return node->setDoubleValue(arg->getDoubleValue("value"));
107 case SGValue::STRING:
108 return node->setStringValue(arg->getStringValue("value"));
110 return node->setUnknownValue(arg->getStringValue("value"));
116 * Built-in command: increment or decrement a property value.
118 * property: the name of the property to increment or decrement.
119 * step: the amount of the increment or decrement.
122 do_property_adjust (const SGPropertyNode * arg)
124 const string & propname = arg->getStringValue("property", "");
128 SGPropertyNode * node = fgGetNode(propname, true);
130 switch (node->getType()) {
132 if (arg->getBoolValue("step"))
133 return node->setBoolValue(!node->getBoolValue());
137 return node->setIntValue(node->getIntValue()
138 + arg->getIntValue("step"));
140 return node->setLongValue(node->getLongValue()
141 + arg->getLongValue("step"));
143 return node->setFloatValue(node->getFloatValue()
144 + arg->getFloatValue("step"));
145 case SGValue::DOUBLE:
146 return node->setDoubleValue(node->getDoubleValue()
147 + arg->getDoubleValue("step"));
148 default: // doesn't make sense with strings
155 * Built-in command: swap two property values.
157 * property[0]: the name of the first property.
158 * property[1]: the name of the second property.
161 do_property_swap (const SGPropertyNode * arg)
163 const string &propname1 = arg->getStringValue("property[0]", "");
164 const string &propname2 = arg->getStringValue("property[1]", "");
165 if (propname1 == "" || propname2 == "")
168 SGPropertyNode * node1 = fgGetNode(propname1, true);
169 SGPropertyNode * node2 = fgGetNode(propname2, true);
170 const string & tmp = node1->getStringValue();
171 return (node1->setUnknownValue(node2->getStringValue()) &&
172 node2->setUnknownValue(tmp));
177 * Set a property to an axis or other moving input.
179 * property: the name of the property to set.
180 * setting: the current input setting, usually between -1.0 and 1.0.
181 * offset: the offset to shift by, before applying the factor.
182 * factor: the factor to multiply by (use negative to reverse).
185 do_property_scale (const SGPropertyNode * arg)
187 const string &propname = arg->getStringValue("property");
188 double setting = arg->getDoubleValue("setting", 0.0);
189 double offset = arg->getDoubleValue("offset", 0.0);
190 double factor = arg->getDoubleValue("factor", 1.0);
191 return fgSetDouble(propname, (setting + offset) * factor);
197 * Table of built-in commands.
199 * New commands do not have to be added here; any module in the application
200 * can add a new command using globals->get_commands()->addCommand(...).
204 SGCommandMgr::command_t command;
208 "view-cycle", do_view_cycle,
209 "screen-capture", do_screen_capture,
210 "property-toggle", do_property_toggle,
211 "property-assign", do_property_assign,
212 "property-adjust", do_property_adjust,
213 "property-swap", do_property_swap,
214 "property-scale", do_property_scale,
215 0, 0 // zero-terminated
220 * Initialize the default built-in commands.
222 * Other commands may be added by other parts of the application.
227 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
228 for (int i = 0; built_ins[i].name != 0; i++) {
229 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
230 globals->get_commands()->addCommand(built_ins[i].name,
231 built_ins[i].command);
235 // end of fg_commands.hxx