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