]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
Started new, XML-configurable GUI subsystem.
[flightgear.git] / src / Main / fg_commands.cxx
1 // fg_commands.cxx - internal FGFS commands.
2
3 #include <string.h>             // strcmp()
4
5 #include <simgear/compiler.h>
6 #include <simgear/misc/exception.hxx>
7
8 #include STL_STRING
9 #include STL_FSTREAM
10
11 #include <simgear/debug/logstream.hxx>
12 #include <simgear/misc/commands.hxx>
13 #include <simgear/misc/props.hxx>
14
15 #include <GUI/gui.h>
16 #include <GUI/new_gui.hxx>
17 #include <Cockpit/panel.hxx>
18 #include <Cockpit/panel_io.hxx>
19 #include <Scenery/tilemgr.hxx>
20 #include <Time/tmp.hxx>
21
22 #include "fg_commands.hxx"
23
24 SG_USING_STD(string);
25 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
26 SG_USING_STD(ifstream);
27 SG_USING_STD(ofstream);
28 #endif
29
30 #include "fg_props.hxx"
31 #include "fg_io.hxx"
32 #include "globals.hxx"
33 #include "viewmgr.hxx"
34
35
36 \f
37 ////////////////////////////////////////////////////////////////////////
38 // Static helper functions.
39 ////////////////////////////////////////////////////////////////////////
40
41
42 /**
43  * Template function to wrap a value.
44  */
45 template <class T>
46 static inline void
47 do_wrap (T * value, T min, T max)
48 {
49     if (min >= max) {           // basic sanity check
50         *value = min;
51     } else {
52         T range = max - min;
53         while (*value < min)
54             *value += range;
55         while (*value > max)
56             *value -= range;
57     }
58 }
59
60 static inline SGPropertyNode *
61 get_prop (const SGPropertyNode * arg)
62 {
63     return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
64 }
65
66 static inline SGPropertyNode *
67 get_prop2 (const SGPropertyNode * arg)
68 {
69     return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
70 }
71
72
73 \f
74 ////////////////////////////////////////////////////////////////////////
75 // Command implementations.
76 ////////////////////////////////////////////////////////////////////////
77
78
79 /**
80  * Built-in command: do nothing.
81  */
82 static bool
83 do_null (const SGPropertyNode * arg)
84 {
85   return true;
86 }
87
88
89 /**
90  * Built-in command: exit FlightGear.
91  *
92  * TODO: show a confirm dialog.
93  */
94 static bool
95 do_exit (const SGPropertyNode * arg)
96 {
97   SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
98   ConfirmExitDialog();
99   return true;
100 }
101
102
103 /**
104  * Built-in command: load flight.
105  *
106  * file (optional): the name of the file to load (relative to current
107  * directory).  Defaults to "fgfs.sav".
108  */
109 static bool
110 do_load (const SGPropertyNode * arg)
111 {
112   const string &file = arg->getStringValue("file", "fgfs.sav");
113   ifstream input(file.c_str());
114   if (input.good() && fgLoadFlight(input)) {
115     input.close();
116     SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
117     return true;
118   } else {
119     SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
120     return false;
121   }
122 }
123
124
125 /**
126  * Built-in command: save flight.
127  *
128  * file (optional): the name of the file to save (relative to the
129  * current directory).  Defaults to "fgfs.sav".
130  */
131 static bool
132 do_save (const SGPropertyNode * arg)
133 {
134   const string &file = arg->getStringValue("file", "fgfs.sav");
135   bool write_all = arg->getBoolValue("write-all", false);
136   SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
137   ofstream output(file.c_str());
138   if (output.good() && fgSaveFlight(output, write_all)) {
139     output.close();
140     SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
141     return true;
142   } else {
143     SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
144     return false;
145   }
146 }
147
148
149 /**
150  * Built-in command: (re)load the panel.
151  *
152  * path (optional): the file name to load the panel from 
153  * (relative to FG_ROOT).  Defaults to the value of /sim/panel/path,
154  * and if that's unspecified, to "Panels/Default/default.xml".
155  */
156 static bool
157 do_panel_load (const SGPropertyNode * arg)
158 {
159   string panel_path =
160     arg->getStringValue("path",
161                         fgGetString("/sim/panel/path",
162                                     "Panels/Default/default.xml"));
163   FGPanel * new_panel = fgReadPanel(panel_path);
164   if (new_panel == 0) {
165     SG_LOG(SG_INPUT, SG_ALERT,
166            "Error reading new panel from " << panel_path);
167     return false;
168   }
169   SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
170   current_panel->unbind();
171   delete current_panel;
172   current_panel = new_panel;
173   current_panel->bind();
174   return true;
175 }
176
177
178 /**
179  * Built-in command: pass a mouse click to the panel.
180  *
181  * button: the mouse button number, zero-based.
182  * is-down: true if the button is down, false if it is up.
183  * x-pos: the x position of the mouse click.
184  * y-pos: the y position of the mouse click.
185  */
186 static bool
187 do_panel_mouse_click (const SGPropertyNode * arg)
188 {
189   if (current_panel != 0)
190     return current_panel
191       ->doMouseAction(arg->getIntValue("button"),
192                       arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
193                       arg->getIntValue("x-pos"),
194                       arg->getIntValue("y-pos"));
195   else
196     return false;
197 }
198
199
200 /**
201  * Built-in command: (re)load preferences.
202  *
203  * path (optional): the file name to load the panel from (relative
204  * to FG_ROOT). Defaults to "preferences.xml".
205  */
206 static bool
207 do_preferences_load (const SGPropertyNode * arg)
208 {
209   try {
210     fgLoadProps(arg->getStringValue("path", "preferences.xml"),
211                 globals->get_props());
212   } catch (const sg_exception &e) {
213     guiErrorMessage("Error reading global preferences: ", e);
214     return false;
215   }
216   SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
217   return true;
218 }
219
220
221 static void
222 fix_hud_visibility()
223 {
224   if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
225       globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
226       if ( globals->get_viewmgr()->get_current() == 1 ) {
227           globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
228       }
229   }
230 }
231
232 void
233 do_view_next( bool )
234 {
235     globals->get_current_view()->setHeadingOffset_deg(0.0);
236     globals->get_viewmgr()->next_view();
237     fix_hud_visibility();
238   global_tile_mgr.refresh_view_timestamps();
239 }
240
241 void
242 do_view_prev( bool )
243 {
244     globals->get_current_view()->setHeadingOffset_deg(0.0);
245     globals->get_viewmgr()->prev_view();
246     fix_hud_visibility();
247   global_tile_mgr.refresh_view_timestamps();
248 }
249
250 /**
251  * Built-in command: cycle view.
252  */
253 static bool
254 do_view_cycle (const SGPropertyNode * arg)
255 {
256   globals->get_current_view()->setHeadingOffset_deg(0.0);
257   globals->get_viewmgr()->next_view();
258   fix_hud_visibility();
259   global_tile_mgr.refresh_view_timestamps();
260 //   fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
261   return true;
262 }
263
264 /**
265  * Built-in command: capture screen.
266  */
267 static bool
268 do_screen_capture (const SGPropertyNode * arg)
269 {
270   fgDumpSnapShot();
271   return true;
272 }
273
274
275 /**
276  * Reload the tile cache.
277  */
278 static bool
279 do_tile_cache_reload (const SGPropertyNode * arg)
280 {
281     static const SGPropertyNode *master_freeze
282         = fgGetNode("/sim/freeze/master");
283     bool freeze = master_freeze->getBoolValue();
284     SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
285     if ( !freeze ) {
286         fgSetBool("/sim/freeze/master", true);
287     }
288     // BusyCursor(0);
289     if ( global_tile_mgr.init() ) {
290         // Load the local scenery data
291         double visibility_meters = fgGetDouble("/environment/visibility-m");
292         global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
293                                fgGetDouble("/position/latitude-deg"),
294                                visibility_meters);
295     } else {
296         SG_LOG( SG_GENERAL, SG_ALERT, 
297                 "Error in Tile Manager initialization!" );
298         exit(-1);
299     }
300     // BusyCursor(1);
301     if ( !freeze ) {
302         fgSetBool("/sim/freeze/master", false);
303     }
304     return true;
305 }
306
307
308 /**
309  * Update the lighting manually.
310  */
311 static bool
312 do_lighting_update (const SGPropertyNode * arg)
313 {
314   fgUpdateSkyAndLightingParams();
315   return true;
316 }
317
318
319 /**
320  * Built-in command: toggle a bool property value.
321  *
322  * property: The name of the property to toggle.
323  */
324 static bool
325 do_property_toggle (const SGPropertyNode * arg)
326 {
327   SGPropertyNode * prop = get_prop(arg);
328   return prop->setBoolValue(!prop->getBoolValue());
329 }
330
331
332 /**
333  * Built-in command: assign a value to a property.
334  *
335  * property: the name of the property to assign.
336  * value: the value to assign.
337  */
338 static bool
339 do_property_assign (const SGPropertyNode * arg)
340 {
341   SGPropertyNode * prop = get_prop(arg);
342   const SGPropertyNode * value = arg->getNode("value");
343
344   switch (prop->getType()) {
345
346   case SGPropertyNode::BOOL:
347     return prop->setBoolValue(value->getBoolValue());
348
349   case SGPropertyNode::INT:
350     return prop->setIntValue(value->getIntValue());
351
352   case SGPropertyNode::LONG:
353     return prop->setLongValue(value->getLongValue());
354
355   case SGPropertyNode::FLOAT:
356     return prop->setFloatValue(value->getFloatValue());
357
358   case SGPropertyNode::DOUBLE:
359     return prop->setDoubleValue(value->getDoubleValue());
360
361   case SGPropertyNode::STRING:
362     return prop->setStringValue(value->getStringValue());
363
364   default:
365     return prop->setUnspecifiedValue(value->getStringValue());
366
367   }
368 }
369
370
371 /**
372  * Built-in command: increment or decrement a property value.
373  *
374  * property: the name of the property to increment or decrement.
375  * step: the amount of the increment or decrement (default: 0).
376  * offset: a normalized amount to offset by (if step is not present).
377  * factor: the amount by which to multiply the offset (if step is not present).
378  * min: the minimum allowed value (default: no minimum).
379  * max: the maximum allowed value (default: no maximum).
380  * wrap: true if the value should be wrapped when it passes min or max;
381  *       both min and max must be present for this to work (default:
382  *       false).
383  */
384 static bool
385 do_property_adjust (const SGPropertyNode * arg)
386 {
387   SGPropertyNode * prop = get_prop(arg);
388   const SGPropertyNode * step = arg->getChild("step");
389   const SGPropertyNode * offset = arg->getChild("offset");
390   const SGPropertyNode * factor = arg->getChild("factor");
391   const SGPropertyNode * min = arg->getChild("min");
392   const SGPropertyNode * max = arg->getChild("max");
393   bool wrap = arg->getBoolValue("wrap");
394
395   if (min == 0 || max == 0)
396       wrap = false;
397
398   double amount = 0;
399   if (step == 0)
400     amount = offset->getDoubleValue() * factor->getDoubleValue();
401
402   switch (prop->getType()) {
403
404   case SGPropertyNode::BOOL: {
405     bool value;
406     if (step != 0)
407       value = step->getBoolValue();
408     else
409       value = (0.0 != amount);
410     if (value)
411       return prop->setBoolValue(!prop->getBoolValue());
412     else
413       return true;
414   }
415
416   case SGPropertyNode::INT: {
417     int value;
418     if (step != 0)
419       value = prop->getIntValue() + step->getIntValue();
420     else
421       value = prop->getIntValue() + int(amount);
422     if (wrap) {
423         do_wrap(&value, min->getIntValue(), max->getIntValue());
424     } else {
425         if (min != 0 && value < min->getIntValue())
426             value = min->getIntValue();
427         if (max != 0 && value > max->getIntValue())
428             value = max->getIntValue();
429     }
430     return prop->setIntValue(value);
431   }
432
433   case SGPropertyNode::LONG: {
434     long value;
435     if (step != 0)
436       value = prop->getLongValue() + step->getLongValue();
437     else
438       value = prop->getLongValue() + long(amount);
439     if (wrap) {
440         do_wrap(&value, min->getLongValue(), max->getLongValue());
441     } else {
442         if (min != 0 && value < min->getLongValue())
443             value = min->getLongValue();
444         if (max != 0 && value > max->getLongValue())
445             value = max->getLongValue();
446     }
447     return prop->setLongValue(value);
448   }
449
450   case SGPropertyNode::FLOAT: {
451     float value;
452     if (step != 0)
453       value = prop->getFloatValue() + step->getFloatValue();
454     else
455       value = prop->getFloatValue() + float(amount);
456     if (wrap) {
457         do_wrap(&value, min->getFloatValue(), max->getFloatValue());
458     } else {
459         if (min != 0 && value < min->getFloatValue())
460             value = min->getFloatValue();
461         if (max != 0 && value > max->getFloatValue())
462             value = max->getFloatValue();
463     }
464     return prop->setFloatValue(value);
465   }
466
467   case SGPropertyNode::DOUBLE:
468   case SGPropertyNode::UNSPECIFIED:
469   case SGPropertyNode::NONE: {
470     double value;
471     if (step != 0)
472       value = prop->getDoubleValue() + step->getDoubleValue();
473     else
474       value = prop->getDoubleValue() + amount;
475     if (wrap) {
476         do_wrap(&value, min->getDoubleValue(), max->getDoubleValue());
477     } else {
478         if (min != 0 && value < min->getDoubleValue())
479             value = min->getDoubleValue();
480         if (max != 0 && value > max->getDoubleValue())
481             value = max->getDoubleValue();
482     }
483     return prop->setDoubleValue(value);
484   }
485
486   case SGPropertyNode::STRING: // doesn't make sense with strings
487     SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
488     return false;
489
490   default:
491     SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
492     return false;
493
494   }
495 }
496
497
498 /**
499  * Built-in command: multiply a property value.
500  *
501  * property: the name of the property to multiply.
502  * factor: the amount by which to multiply.
503  */
504 static bool
505 do_property_multiply (const SGPropertyNode * arg)
506 {
507   SGPropertyNode * prop = get_prop(arg);
508   const SGPropertyNode * factor = arg->getChild("factor");
509
510   if (factor == 0)
511       return true;
512
513   switch (prop->getType()) {
514
515   case SGPropertyNode::BOOL:
516     return prop->setBoolValue(prop->getBoolValue() && factor->getBoolValue());
517
518   case SGPropertyNode::INT:
519     return prop->setIntValue(int(prop->getIntValue()
520                                  * factor->getDoubleValue()));
521
522   case SGPropertyNode::LONG:
523     return prop->setLongValue(long(prop->getLongValue()
524                                    * factor->getDoubleValue()));
525
526   case SGPropertyNode::FLOAT:
527     return prop->setFloatValue(float(prop->getFloatValue()
528                                      * factor->getDoubleValue()));
529
530   case SGPropertyNode::DOUBLE:
531   case SGPropertyNode::UNSPECIFIED:
532   case SGPropertyNode::NONE:
533     return prop->setDoubleValue(prop->getDoubleValue()
534                                 * factor->getDoubleValue());
535
536   default:                      // doesn't make sense with strings
537     return false;
538   }
539 }
540
541
542 /**
543  * Built-in command: swap two property values.
544  *
545  * property[0]: the name of the first property.
546  * property[1]: the name of the second property.
547  */
548 static bool
549 do_property_swap (const SGPropertyNode * arg)
550 {
551   SGPropertyNode * prop1 = get_prop(arg);
552   SGPropertyNode * prop2 = get_prop2(arg);
553
554                                 // FIXME: inefficient
555   const string & tmp = prop1->getStringValue();
556   return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
557           prop2->setUnspecifiedValue(tmp.c_str()));
558 }
559
560
561 /**
562  * Set a property to an axis or other moving input.
563  *
564  * property: the name of the property to set.
565  * setting: the current input setting, usually between -1.0 and 1.0.
566  * offset: the offset to shift by, before applying the factor.
567  * factor: the factor to multiply by (use negative to reverse).
568  */
569 static bool
570 do_property_scale (const SGPropertyNode * arg)
571 {
572   SGPropertyNode * prop = get_prop(arg);
573   double setting = arg->getDoubleValue("setting");
574   double offset = arg->getDoubleValue("offset", 0.0);
575   double factor = arg->getDoubleValue("factor", 1.0);
576   bool squared = arg->getBoolValue("squared", false);
577
578   if (squared)
579     setting = (setting < 0 ? -1 : 1) * setting * setting;
580
581   return prop->setDoubleValue((setting + offset) * factor);
582 }
583
584 static bool
585 do_gui (const SGPropertyNode * arg)
586 {
587     NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
588     gui->display(arg->getStringValue("name"));
589     return true;
590 }
591
592
593 \f
594 ////////////////////////////////////////////////////////////////////////
595 // Command setup.
596 ////////////////////////////////////////////////////////////////////////
597
598
599 /**
600  * Table of built-in commands.
601  *
602  * New commands do not have to be added here; any module in the application
603  * can add a new command using globals->get_commands()->addCommand(...).
604  */
605 static struct {
606   const char * name;
607   SGCommandMgr::command_t command;
608 } built_ins [] = {
609     { "null", do_null },
610     { "exit", do_exit },
611     { "load", do_load },
612     { "save", do_save },
613     { "panel-load", do_panel_load },
614     { "panel-mouse-click", do_panel_mouse_click },
615     { "preferences-load", do_preferences_load },
616     { "view-cycle", do_view_cycle },
617     { "screen-capture", do_screen_capture },
618     { "tile-cache-reload", do_tile_cache_reload },
619     { "lighting-update", do_lighting_update },
620     { "property-toggle", do_property_toggle },
621     { "property-assign", do_property_assign },
622     { "property-adjust", do_property_adjust },
623     { "property-multiply", do_property_multiply },
624     { "property-swap", do_property_swap },
625     { "property-scale", do_property_scale },
626     { "gui", do_gui },
627     { 0, 0 }                    // zero-terminated
628 };
629
630
631 /**
632  * Initialize the default built-in commands.
633  *
634  * Other commands may be added by other parts of the application.
635  */
636 void
637 fgInitCommands ()
638 {
639   SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
640   for (int i = 0; built_ins[i].name != 0; i++) {
641     SG_LOG(SG_GENERAL, SG_INFO, "  " << built_ins[i].name);
642     globals->get_commands()->addCommand(built_ins[i].name,
643                                         built_ins[i].command);
644   }
645
646   typedef bool (*dummy)();
647   fgTie( "/command/view/next", dummy(0), do_view_next );
648   fgTie( "/command/view/prev", dummy(0), do_view_prev );
649 }
650
651 // end of fg_commands.cxx