]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
860f9a1f9c9d54f1fdfcf8bcde3bfb069f095dca
[flightgear.git] / src / Main / fg_commands.cxx
1 // fg_commands.cxx - internal FGFS commands.
2
3 #include "fg_commands.hxx"
4
5 #include <simgear/debug/logstream.hxx>
6 #include <simgear/misc/commands.hxx>
7 #include <simgear/misc/props.hxx>
8
9 #include <GUI/gui.h>
10
11 #include "fg_props.hxx"
12 #include "fg_io.hxx"
13 #include "globals.hxx"
14
15
16 /**
17  * Built-in command: do nothing.
18  */
19 static bool
20 do_null (const SGPropertyNode * arg)
21 {
22   return true;
23 }
24
25
26 /**
27  * Built-in command: exit FlightGear.
28  *
29  * TODO: show a confirm dialog.
30  */
31 static bool
32 do_exit (const SGPropertyNode * arg)
33 {
34   fgIOShutdownAll();
35   exit(0);
36   return true;
37 }
38
39
40 /**
41  * Built-in command: cycle view.
42  */
43 static bool
44 do_view_cycle (const SGPropertyNode * arg)
45 {
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"));
49   return true;
50 }
51
52
53 /**
54  * Built-in command: capture screen.
55  */
56 static bool
57 do_screen_capture (const SGPropertyNode * arg)
58 {
59   fgDumpSnapShot();
60   return true;
61 }
62
63
64 /**
65  * Built-in command: toggle a bool property value.
66  *
67  * property: The name of the property to toggle.
68  */
69 static bool
70 do_property_toggle (const SGPropertyNode * arg)
71 {
72   const string & propname = arg->getStringValue("property", "");
73   if (propname == "")
74     return false;
75
76   SGPropertyNode * node = fgGetNode(propname);
77   return node->setBoolValue(!node->getBoolValue());
78 }
79
80
81 /**
82  * Built-in command: assign a value to a property.
83  *
84  * property: the name of the property to assign.
85  * value: the value to assign.
86  */
87 static bool
88 do_property_assign (const SGPropertyNode * arg)
89 {
90   const string & propname = arg->getStringValue("property", "");
91   if (propname == "")
92     return false;
93
94   SGPropertyNode * node = fgGetNode(propname, true);
95
96   switch (node->getType()) {
97   case SGValue::BOOL:
98     return node->setBoolValue(arg->getBoolValue("value"));
99   case SGValue::INT:
100     return node->setIntValue(arg->getIntValue("value"));
101   case SGValue::LONG:
102     return node->setLongValue(arg->getLongValue("value"));
103   case SGValue::FLOAT:
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"));
109   default:
110     return node->setUnknownValue(arg->getStringValue("value"));
111   }
112 }
113
114
115 /**
116  * Built-in command: increment or decrement a property value.
117  *
118  * property: the name of the property to increment or decrement.
119  * step: the amount of the increment or decrement.
120  */
121 static bool
122 do_property_adjust (const SGPropertyNode * arg)
123 {
124   const string & propname = arg->getStringValue("property", "");
125   if (propname == "")
126     return false;
127
128   SGPropertyNode * node = fgGetNode(propname, true);
129
130   switch (node->getType()) {
131   case SGValue::BOOL:
132     if (arg->getBoolValue("step"))
133       return node->setBoolValue(!node->getBoolValue());
134     else
135       return true;
136   case SGValue::INT:
137     return node->setIntValue(node->getIntValue()
138                              + arg->getIntValue("step"));
139   case SGValue::LONG:
140     return node->setLongValue(node->getLongValue()
141                               + arg->getLongValue("step"));
142   case SGValue::FLOAT:
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
149     return false;
150   }
151 }
152
153
154 /**
155  * Built-in command: swap two property values.
156  *
157  * property[0]: the name of the first property.
158  * property[1]: the name of the second property.
159  */
160 static bool
161 do_property_swap (const SGPropertyNode * arg)
162 {
163   const string &propname1 = arg->getStringValue("property[0]", "");
164   const string &propname2 = arg->getStringValue("property[1]", "");
165   if (propname1 == "" || propname2 == "")
166     return false;
167
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));
173 }
174
175
176 /**
177  * Set a property to an axis or other moving input.
178  *
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).
183  */
184 static bool
185 do_property_scale (const SGPropertyNode * arg)
186 {
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);
192 }
193
194
195 \f
196 /**
197  * Table of built-in commands.
198  *
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(...).
201  */
202 static struct {
203   const char * name;
204   SGCommandMgr::command_t command;
205 } built_ins [] = {
206   "null", do_null,
207   "exit", do_exit,
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
216 };
217
218
219 /**
220  * Initialize the default built-in commands.
221  *
222  * Other commands may be added by other parts of the application.
223  */
224 void
225 fgInitCommands ()
226 {
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);
232   }
233 }
234
235 // end of fg_commands.hxx