]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
- make repeatable for keys and joystick buttons default to false
[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: exit FlightGear.
18  *
19  * TODO: show a confirm dialog.
20  */
21 static bool
22 do_exit (const SGPropertyNode * arg)
23 {
24   fgIOShutdownAll();
25   exit(0);
26   return true;
27 }
28
29
30 /**
31  * Built-in command: cycle view.
32  */
33 static bool
34 do_view_cycle (const SGPropertyNode * arg)
35 {
36   globals->get_current_view()->set_view_offset(0.0);
37   globals->set_current_view(globals->get_viewmgr()->next_view());
38 //   fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
39   return true;
40 }
41
42
43 /**
44  * Built-in command: capture screen.
45  */
46 static bool
47 do_screen_capture (const SGPropertyNode * arg)
48 {
49   fgDumpSnapShot();
50   return true;
51 }
52
53
54 /**
55  * Built-in command: toggle a bool property value.
56  *
57  * property: The name of the property to toggle.
58  */
59 static bool
60 do_property_toggle (const SGPropertyNode * arg)
61 {
62   string propname = arg->getStringValue("property", "");
63   if (propname == "")
64     return false;
65
66   SGPropertyNode * node = fgGetNode(propname);
67   return node->setBoolValue(!node->getBoolValue());
68 }
69
70
71 /**
72  * Built-in command: assign a value to a property.
73  *
74  * property: the name of the property to assign.
75  * value: the value to assign.
76  */
77 static bool
78 do_property_assign (const SGPropertyNode * arg)
79 {
80   string propname = arg->getStringValue("property", "");
81   if (propname == "")
82     return false;
83
84   SGPropertyNode * node = fgGetNode(propname, true);
85
86   switch (node->getType()) {
87   case SGValue::BOOL:
88     return node->setBoolValue(arg->getBoolValue("value"));
89   case SGValue::INT:
90     return node->setIntValue(arg->getIntValue("value"));
91   case SGValue::LONG:
92     return node->setLongValue(arg->getLongValue("value"));
93   case SGValue::FLOAT:
94     return node->setFloatValue(arg->getFloatValue("value"));
95   case SGValue::DOUBLE:
96     return node->setDoubleValue(arg->getDoubleValue("value"));
97   case SGValue::STRING:
98     return node->setStringValue(arg->getStringValue("value"));
99   default:
100     return node->setUnknownValue(arg->getStringValue("value"));
101   }
102 }
103
104
105 /**
106  * Built-in command: increment or decrement a property value.
107  *
108  * property: the name of the property to increment or decrement.
109  * step: the amount of the increment or decrement.
110  */
111 static bool
112 do_property_adjust (const SGPropertyNode * arg)
113 {
114   string propname = arg->getStringValue("property", "");
115   if (propname == "")
116     return false;
117
118   SGPropertyNode * node = fgGetNode(propname, true);
119
120   switch (node->getType()) {
121   case SGValue::BOOL:
122     if (arg->getBoolValue("step"))
123       return node->setBoolValue(!node->getBoolValue());
124     else
125       return true;
126   case SGValue::INT:
127     return node->setIntValue(node->getIntValue()
128                              + arg->getIntValue("step"));
129   case SGValue::LONG:
130     return node->setLongValue(node->getLongValue()
131                               + arg->getLongValue("step"));
132   case SGValue::FLOAT:
133     return node->setFloatValue(node->getFloatValue()
134                                + arg->getFloatValue("step"));
135   case SGValue::DOUBLE:
136     return node->setDoubleValue(node->getDoubleValue()
137                                 + arg->getDoubleValue("step"));
138   default:                      // doesn't make sense with strings
139     return false;
140   }
141 }
142
143
144 /**
145  * Built-in command: swap two property values.
146  *
147  * property[0]: the name of the first property.
148  * property[1]: the name of the second property.
149  */
150 static bool
151 do_property_swap (const SGPropertyNode * arg)
152 {
153   string propname1 = arg->getStringValue("property[0]", "");
154   string propname2 = arg->getStringValue("property[1]", "");
155   if (propname1 == "" || propname2 == "")
156     return false;
157
158   SGPropertyNode * node1 = fgGetNode(propname1, true);
159   SGPropertyNode * node2 = fgGetNode(propname2, true);
160   string tmp = node1->getStringValue();
161   return (node1->setUnknownValue(node2->getStringValue()) &&
162           node2->setUnknownValue(tmp));
163 }
164
165
166 \f
167 /**
168  * Table of built-in commands.
169  *
170  * New commands do not have to be added here; any module in the application
171  * can add a new command using globals->get_commands()->addCommand(...).
172  */
173 static struct {
174   const char * name;
175   SGCommandMgr::command_t command;
176 } built_ins [] = {
177   "exit", do_exit,
178   "view-cycle", do_view_cycle,
179   "screen-capture", do_screen_capture,
180   "property-toggle", do_property_toggle,
181   "property-assign", do_property_assign,
182   "property-adjust", do_property_adjust,
183   "property-swap", do_property_swap,
184   0, 0                          // zero-terminated
185 };
186
187
188 /**
189  * Initialize the default built-in commands.
190  *
191  * Other commands may be added by other parts of the application.
192  */
193 void
194 fgInitCommands ()
195 {
196   SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
197   for (int i = 0; built_ins[i].name != 0; i++) {
198     SG_LOG(SG_GENERAL, SG_INFO, "  " << built_ins[i].name);
199     globals->get_commands()->addCommand(built_ins[i].name,
200                                         built_ins[i].command);
201   }
202 }
203
204 // end of fg_commands.hxx