]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_commands.cxx
changed SGValue::Type references to SGPropertyNode::Type
[flightgear.git] / src / Main / fg_commands.cxx
index b544f79cb2eb03a2bfd19afcacc450009bb5736f..9303b17727f14e55cdef5540dd2a5334cbffd0f3 100644 (file)
@@ -1,18 +1,41 @@
 // fg_commands.cxx - internal FGFS commands.
 
-#include "fg_commands.hxx"
+#include <simgear/compiler.h>
+
+#include STL_STRING
+#include STL_FSTREAM
 
 #include <simgear/debug/logstream.hxx>
 #include <simgear/misc/commands.hxx>
 #include <simgear/misc/props.hxx>
 
 #include <GUI/gui.h>
+#include <Cockpit/panel.hxx>
+#include <Cockpit/panel_io.hxx>
+
+#include "fg_commands.hxx"
+
+SG_USING_STD(string);
+#if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
+SG_USING_STD(ifstream);
+SG_USING_STD(ofstream);
+#endif
 
 #include "fg_props.hxx"
 #include "fg_io.hxx"
 #include "globals.hxx"
 
 
+/**
+ * Built-in command: do nothing.
+ */
+static bool
+do_null (const SGPropertyNode * arg)
+{
+  return true;
+}
+
+
 /**
  * Built-in command: exit FlightGear.
  *
@@ -27,6 +50,104 @@ do_exit (const SGPropertyNode * arg)
 }
 
 
+/**
+ * Built-in command: load flight.
+ *
+ * file (optional): the name of the file to load (relative to current
+ * directory).  Defaults to "fgfs.sav".
+ */
+static bool
+do_load (const SGPropertyNode * arg)
+{
+  const string &file = arg->getStringValue("file", "fgfs.sav");
+  ifstream input(file.c_str());
+  if (input.good() && fgLoadFlight(input)) {
+    input.close();
+    SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
+    return true;
+  } else {
+    SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
+    return false;
+  }
+}
+
+
+/**
+ * Built-in command: save flight.
+ *
+ * file (optional): the name of the file to save (relative to the
+ * current directory).  Defaults to "fgfs.sav".
+ */
+static bool
+do_save (const SGPropertyNode * arg)
+{
+  const string &file = arg->getStringValue("file", "fgfs.sav");
+  SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
+  ofstream output(file.c_str());
+  if (output.good() && fgSaveFlight(output)) {
+    output.close();
+    SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
+    return true;
+  } else {
+    SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
+    return false;
+  }
+}
+
+
+/**
+ * Built-in command: (re)load the panel.
+ *
+ * path (optional): the file name to load the panel from 
+ * (relative to FG_ROOT).  Defaults to the value of /sim/panel/path,
+ * and if that's unspecified, to "Panels/Default/default.xml".
+ */
+static bool
+do_panel_load (const SGPropertyNode * arg)
+{
+  string panel_path =
+    arg->getStringValue("path",
+                       fgGetString("/sim/panel/path",
+                                   "Panels/Default/default.xml"));
+  FGPanel * new_panel = fgReadPanel(panel_path);
+  if (new_panel == 0) {
+    SG_LOG(SG_INPUT, SG_ALERT,
+          "Error reading new panel from " << panel_path);
+    return false;
+  }
+  SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
+  current_panel->unbind();
+  delete current_panel;
+  current_panel = new_panel;
+  current_panel->bind();
+  return true;
+}
+
+
+/**
+ * Built-in command: (re)load preferences.
+ *
+ * path (optional): the file name to load the panel from (relative
+ * to FG_ROOT). Defaults to "preferences.xml".
+ */
+static bool
+do_preferences_load (const SGPropertyNode * arg)
+{
+  const string &path = arg->getStringValue("path", "preferences.xml");
+  SGPath props_path(globals->get_fg_root());
+  props_path.append(path);
+  SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
+        << props_path.str());
+  if (!readProperties(props_path.str(), globals->get_props())) {
+    SG_LOG(SG_INPUT, SG_ALERT, "Failed to reread global preferences");
+    return false;
+  } else {
+    SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
+    return true;
+  }
+}
+
+
 /**
  * Built-in command: cycle view.
  */
@@ -59,7 +180,7 @@ do_screen_capture (const SGPropertyNode * arg)
 static bool
 do_property_toggle (const SGPropertyNode * arg)
 {
-  string propname = arg->getStringValue("property", "");
+  const string & propname = arg->getStringValue("property", "");
   if (propname == "")
     return false;
 
@@ -77,24 +198,24 @@ do_property_toggle (const SGPropertyNode * arg)
 static bool
 do_property_assign (const SGPropertyNode * arg)
 {
-  string propname = arg->getStringValue("property", "");
+  const string & propname = arg->getStringValue("property", "");
   if (propname == "")
     return false;
 
   SGPropertyNode * node = fgGetNode(propname, true);
 
   switch (node->getType()) {
-  case SGValue::BOOL:
+  case SGPropertyNode::BOOL:
     return node->setBoolValue(arg->getBoolValue("value"));
-  case SGValue::INT:
+  case SGPropertyNode::INT:
     return node->setIntValue(arg->getIntValue("value"));
-  case SGValue::LONG:
+  case SGPropertyNode::LONG:
     return node->setLongValue(arg->getLongValue("value"));
-  case SGValue::FLOAT:
+  case SGPropertyNode::FLOAT:
     return node->setFloatValue(arg->getFloatValue("value"));
-  case SGValue::DOUBLE:
+  case SGPropertyNode::DOUBLE:
     return node->setDoubleValue(arg->getDoubleValue("value"));
-  case SGValue::STRING:
+  case SGPropertyNode::STRING:
     return node->setStringValue(arg->getStringValue("value"));
   default:
     return node->setUnknownValue(arg->getStringValue("value"));
@@ -111,28 +232,28 @@ do_property_assign (const SGPropertyNode * arg)
 static bool
 do_property_adjust (const SGPropertyNode * arg)
 {
-  string propname = arg->getStringValue("property", "");
+  const string & propname = arg->getStringValue("property", "");
   if (propname == "")
     return false;
 
   SGPropertyNode * node = fgGetNode(propname, true);
 
   switch (node->getType()) {
-  case SGValue::BOOL:
+  case SGPropertyNode::BOOL:
     if (arg->getBoolValue("step"))
       return node->setBoolValue(!node->getBoolValue());
     else
       return true;
-  case SGValue::INT:
+  case SGPropertyNode::INT:
     return node->setIntValue(node->getIntValue()
                             + arg->getIntValue("step"));
-  case SGValue::LONG:
+  case SGPropertyNode::LONG:
     return node->setLongValue(node->getLongValue()
                              + arg->getLongValue("step"));
-  case SGValue::FLOAT:
+  case SGPropertyNode::FLOAT:
     return node->setFloatValue(node->getFloatValue()
                               + arg->getFloatValue("step"));
-  case SGValue::DOUBLE:
+  case SGPropertyNode::DOUBLE:
     return node->setDoubleValue(node->getDoubleValue()
                                + arg->getDoubleValue("step"));
   default:                     // doesn't make sense with strings
@@ -150,19 +271,38 @@ do_property_adjust (const SGPropertyNode * arg)
 static bool
 do_property_swap (const SGPropertyNode * arg)
 {
-  string propname1 = arg->getStringValue("property[0]", "");
-  string propname2 = arg->getStringValue("property[1]", "");
+  const string &propname1 = arg->getStringValue("property[0]", "");
+  const string &propname2 = arg->getStringValue("property[1]", "");
   if (propname1 == "" || propname2 == "")
     return false;
 
   SGPropertyNode * node1 = fgGetNode(propname1, true);
   SGPropertyNode * node2 = fgGetNode(propname2, true);
-  string tmp = node1->getStringValue();
+  const string & tmp = node1->getStringValue();
   return (node1->setUnknownValue(node2->getStringValue()) &&
          node2->setUnknownValue(tmp));
 }
 
 
+/**
+ * Set a property to an axis or other moving input.
+ *
+ * property: the name of the property to set.
+ * setting: the current input setting, usually between -1.0 and 1.0.
+ * offset: the offset to shift by, before applying the factor.
+ * factor: the factor to multiply by (use negative to reverse).
+ */
+static bool
+do_property_scale (const SGPropertyNode * arg)
+{
+  const string &propname = arg->getStringValue("property");
+  double setting = arg->getDoubleValue("setting", 0.0);
+  double offset = arg->getDoubleValue("offset", 0.0);
+  double factor = arg->getDoubleValue("factor", 1.0);
+  return fgSetDouble(propname, (setting + offset) * factor);
+}
+
+
 \f
 /**
  * Table of built-in commands.
@@ -174,13 +314,19 @@ static struct {
   const char * name;
   SGCommandMgr::command_t command;
 } built_ins [] = {
+  "null", do_null,
   "exit", do_exit,
+  "load", do_load,
+  "save", do_save,
+  "panel-load", do_panel_load,
+  "preferences-load", do_preferences_load,
   "view-cycle", do_view_cycle,
   "screen-capture", do_screen_capture,
   "property-toggle", do_property_toggle,
   "property-assign", do_property_assign,
   "property-adjust", do_property_adjust,
   "property-swap", do_property_swap,
+  "property-scale", do_property_scale,
   0, 0                         // zero-terminated
 };