]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_commands.cxx
Added FGScript.{cpp,h}
[flightgear.git] / src / Main / fg_commands.cxx
index ac2ef8b164f61c911aa819e1e26de6be9ede3caa..77b9aedf76b49f01f8d9381ef071aaf98c82a8c0 100644 (file)
@@ -1,6 +1,7 @@
 // fg_commands.cxx - internal FGFS commands.
 
 #include <simgear/compiler.h>
+#include <simgear/misc/exception.hxx>
 
 #include STL_STRING
 #include STL_FSTREAM
@@ -26,6 +27,85 @@ SG_USING_STD(ofstream);
 #include "fg_props.hxx"
 #include "fg_io.hxx"
 #include "globals.hxx"
+#include "viewmgr.hxx"
+
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Saved command states.
+////////////////////////////////////////////////////////////////////////
+
+
+/**
+ * Base saved state for property commands.
+ *
+ * Since this class isn't publicly visible, it is simply an aggregate
+ * of all the stuff any property command needs.
+ */
+class PropertyCommandState : public SGCommandState
+{
+public:
+  PropertyCommandState (const SGPropertyNode * arg);
+  virtual SGPropertyNode * getProp () const { return _prop; }
+  virtual SGPropertyNode * getProp2 () const { return _prop2; }
+  virtual const SGPropertyNode * getValue () const
+    { return _value ? _value : &_dummy_0; }
+  virtual const SGPropertyNode * getStep () const 
+    { return _step ? _step : &_dummy_0; }
+  virtual const SGPropertyNode * getMin () const { return _min; }
+  virtual const SGPropertyNode * getMax () const { return _max; }
+  virtual const SGPropertyNode * getWrap () const
+    { return _wrap ? _wrap : &_dummy_0; }
+  virtual const SGPropertyNode * getFactor () const 
+    { return _factor ? _factor : &_dummy_1; }
+  virtual const SGPropertyNode * getSetting () const 
+    { return _setting ? _setting : &_dummy_0; }
+  virtual const SGPropertyNode * getOffset () const
+    { return _offset ? _offset : &_dummy_0; }
+private:
+  static SGPropertyNode _dummy_0;
+  static SGPropertyNode _dummy_1;
+  mutable SGPropertyNode * _prop;
+  mutable SGPropertyNode * _prop2;
+  const SGPropertyNode * _value;
+  const SGPropertyNode * _step;
+  const SGPropertyNode * _min;
+  const SGPropertyNode * _max;
+  const SGPropertyNode * _wrap;
+  const SGPropertyNode * _factor;
+  const SGPropertyNode * _setting;
+  const SGPropertyNode * _offset;
+};
+
+SGPropertyNode PropertyCommandState::_dummy_0;
+SGPropertyNode PropertyCommandState::_dummy_1;
+
+PropertyCommandState::PropertyCommandState (const SGPropertyNode * arg)
+  : SGCommandState(arg),
+    _prop(fgGetNode(arg->getStringValue("property[0]", "/null"), true)),
+    _prop2(fgGetNode(arg->getStringValue("property[1]", "/null"), true)),
+    _value(arg->getNode("value")),
+    _step(arg->getNode("step")),
+    _min(arg->getNode("min")),
+    _max(arg->getNode("max")),
+    _wrap(arg->getNode("wrap")),
+    _factor(arg->getNode("factor")),
+    _setting(arg->getNode("setting")),
+    _offset(arg->getNode("offset"))
+{
+                               // It would be better not to do this
+                               // every time, but it's not that big
+                               // a deal.  I don't know enough about
+                               // C++ static initialization to fix it.
+  _dummy_1.setDoubleValue(1.0);
+}
+
+
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Command implementations.
+////////////////////////////////////////////////////////////////////////
 
 
 /**
@@ -140,13 +220,14 @@ do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
   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");
+  try {
+    readProperties(props_path.str(), globals->get_props());
+  } catch (const sg_exception &e) {
+    guiErrorMessage("Error reading global preferences: ", e);
     return false;
-  } else {
-    SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
-    return true;
   }
+  SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
+  return true;
 }
 
 
@@ -158,6 +239,12 @@ do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
 {
   globals->get_current_view()->set_view_offset(0.0);
   globals->set_current_view(globals->get_viewmgr()->next_view());
+  if ( fgGetString("/sim/flight-model") == "ada" ) {
+      globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
+      if ( globals->get_viewmgr()->get_current() == 1 ) {
+          globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
+      }
+  }
 //   fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
   return true;
 }
@@ -184,7 +271,7 @@ do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
   SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
   if ( !freeze ) 
     globals->set_freeze( true );
-  BusyCursor(0);
+  // BusyCursor(0);
   if ( global_tile_mgr.init() ) {
     // Load the local scenery data
     global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
@@ -194,7 +281,7 @@ do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
            "Error in Tile Manager initialization!" );
     exit(-1);
   }
-  BusyCursor(1);
+  // BusyCursor(1);
   if ( !freeze )
     globals->set_freeze( false );
   return true;
@@ -220,12 +307,10 @@ do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
 static bool
 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
 {
-  const string & propname = arg->getStringValue("property", "");
-  if (propname == "")
-    return false;
-
-  SGPropertyNode * node = fgGetNode(propname);
-  return node->setBoolValue(!node->getBoolValue());
+  if (*state == 0)
+    *state = new PropertyCommandState(arg);
+  SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
+  return prop->setBoolValue(!prop->getBoolValue());
 }
 
 
@@ -238,27 +323,27 @@ do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
 static bool
 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
 {
-  const string & propname = arg->getStringValue("property", "");
-  if (propname == "")
-    return false;
-
-  SGPropertyNode * node = fgGetNode(propname, true);
+  if (*state == 0)
+    *state = new PropertyCommandState(arg);
+  SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
+  const SGPropertyNode * value =
+    ((PropertyCommandState *)(*state))->getValue();
 
-  switch (node->getType()) {
+  switch (prop->getType()) {
   case SGPropertyNode::BOOL:
-    return node->setBoolValue(arg->getBoolValue("value"));
+    return prop->setBoolValue(value->getBoolValue());
   case SGPropertyNode::INT:
-    return node->setIntValue(arg->getIntValue("value"));
+    return prop->setIntValue(value->getIntValue());
   case SGPropertyNode::LONG:
-    return node->setLongValue(arg->getLongValue("value"));
+    return prop->setLongValue(value->getLongValue());
   case SGPropertyNode::FLOAT:
-    return node->setFloatValue(arg->getFloatValue("value"));
+    return prop->setFloatValue(value->getFloatValue());
   case SGPropertyNode::DOUBLE:
-    return node->setDoubleValue(arg->getDoubleValue("value"));
+    return prop->setDoubleValue(value->getDoubleValue());
   case SGPropertyNode::STRING:
-    return node->setStringValue(arg->getStringValue("value"));
+    return prop->setStringValue(value->getStringValue());
   default:
-    return node->setUnspecifiedValue(arg->getStringValue("value"));
+    return prop->setUnspecifiedValue(value->getStringValue());
   }
 }
 
@@ -267,114 +352,139 @@ do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
  * Built-in command: increment or decrement a property value.
  *
  * property: the name of the property to increment or decrement.
- * step: the amount of the increment or decrement.
+ * step: the amount of the increment or decrement (default: 0).
+ * min: the minimum allowed value (default: no minimum).
+ * max: the maximum allowed value (default: no maximum).
+ * wrap: true if the value should be wrapped when it passes min or max;
+ *       both min and max must be present for this to work (default:
+ *       false).
  */
 static bool
 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
 {
-  const string & propname = arg->getStringValue("property", "");
-  if (propname == "")
-    return false;
-
-  SGPropertyNode * node = fgGetNode(propname, true);
-
-  switch (node->getType()) {
+  if (*state == 0)
+    *state = new PropertyCommandState(arg);
+  SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
+  const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
+  const SGPropertyNode * min = ((PropertyCommandState *)(*state))->getMin();
+  const SGPropertyNode * max = ((PropertyCommandState *)(*state))->getMax();
+  bool wrap = ((PropertyCommandState *)(*state))->getWrap()->getBoolValue();
+
+  switch (prop->getType()) {
   case SGPropertyNode::BOOL:
-    if (arg->getBoolValue("step"))
-      return node->setBoolValue(!node->getBoolValue());
+    if (step->getBoolValue())
+      return prop->setBoolValue(!prop->getBoolValue());
     else
       return true;
-  case SGPropertyNode::INT:
-    return node->setIntValue(node->getIntValue()
-                            + arg->getIntValue("step"));
-  case SGPropertyNode::LONG:
-    return node->setLongValue(node->getLongValue()
-                             + arg->getLongValue("step"));
-  case SGPropertyNode::FLOAT:
-    return node->setFloatValue(node->getFloatValue()
-                              + arg->getFloatValue("step"));
+  case SGPropertyNode::INT: {
+    int value = prop->getIntValue() + step->getIntValue();
+    if (min && (value < min->getIntValue())) {
+      if (wrap && max)
+       value = max->getIntValue();
+      else
+       value = min->getIntValue();
+    }
+    if (max && value > max->getIntValue()) {
+      if (wrap && min)
+       value = min->getIntValue();
+      else
+       value = max->getIntValue();
+    }
+    return prop->setIntValue(value);
+  }
+  case SGPropertyNode::LONG: {
+    long value = prop->getLongValue() + step->getLongValue();
+    if (min && (value < min->getLongValue())) {
+      if (wrap && max)
+       value = max->getLongValue();
+      else
+       value = min->getLongValue();
+    }
+    if (max && value > max->getLongValue()) {
+      if (wrap && min)
+       value = min->getLongValue();
+      else
+       value = max->getLongValue();
+    }
+    return prop->setLongValue(value);
+  }
+  case SGPropertyNode::FLOAT: {
+    float value = prop->getFloatValue() + step->getFloatValue();
+    if (min && (value < min->getFloatValue())) {
+      if (wrap && max)
+       value = max->getFloatValue();
+      else
+       value = min->getFloatValue();
+    }
+    if (max && value > max->getFloatValue()) {
+      if (wrap && min)
+       value = min->getFloatValue();
+      else
+       value = max->getFloatValue();
+    }
+    return prop->setFloatValue(value);
+  }
   case SGPropertyNode::DOUBLE:
   case SGPropertyNode::UNSPECIFIED:
-    return node->setDoubleValue(node->getDoubleValue()
-                               + arg->getDoubleValue("step"));
-  default:                     // doesn't make sense with strings
-    return false;
+  case SGPropertyNode::NONE: {
+    double value = prop->getDoubleValue() + step->getDoubleValue();
+    if (min && (value < min->getDoubleValue())) {
+      if (wrap && max)
+       value = max->getDoubleValue();
+      else
+       value = min->getDoubleValue();
+    }
+    if (max && value > max->getDoubleValue()) {
+      if (wrap && min)
+       value = min->getDoubleValue();
+      else
+       value = max->getDoubleValue();
+    }
+    return prop->setDoubleValue(value);
   }
-}
-
-
-/**
-<<<<<<< fg_commands.cxx
- * Built-in command: multiply a property value.
- *
- * property: the name of the property to multiply.
- * factor: the amount by which to multiply.
- */
-static bool
-do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
-{
-  const string & propname = arg->getStringValue("property", "");
-  if (propname == "")
+  case SGPropertyNode::STRING: // doesn't make sense with strings
+    SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
     return false;
-
-  SGPropertyNode * node = fgGetNode(propname, true);
-
-  switch (node->getType()) {
-  case SGPropertyNode::BOOL:
-    return node->setBoolValue(node->getBoolValue() &&
-                             arg->getBoolValue("factor"));
-  case SGPropertyNode::INT:
-    return node->setIntValue(int(node->getIntValue()
-                                * arg->getDoubleValue("factor")));
-  case SGPropertyNode::LONG:
-    return node->setLongValue(long(node->getLongValue()
-                                  * arg->getDoubleValue("factor")));
-  case SGPropertyNode::FLOAT:
-    return node->setFloatValue(float(node->getFloatValue()
-                                    * arg->getDoubleValue("factor")));
-  case SGPropertyNode::DOUBLE:
-  case SGPropertyNode::UNSPECIFIED:
-    return node->setDoubleValue(node->getDoubleValue()
-                               * arg->getDoubleValue("factor"));
-  default:                     // doesn't make sense with strings
+  default:
+    SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
     return false;
   }
 }
 
 
 /**
-=======
  * Built-in command: multiply a property value.
  *
  * property: the name of the property to multiply.
  * factor: the amount by which to multiply.
  */
 static bool
-do_property_multiply (const SGPropertyNode * arg)
+do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
 {
-  const string & propname = arg->getStringValue("property", "");
-  if (propname == "")
-    return false;
-
-  SGPropertyNode * node = fgGetNode(propname, true);
+  if (*state == 0)
+    *state = new PropertyCommandState(arg);
+  SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
+  const SGPropertyNode * factor =
+    ((PropertyCommandState *)(*state))->getFactor();
 
-  switch (node->getType()) {
+  switch (prop->getType()) {
   case SGPropertyNode::BOOL:
-    return node->setBoolValue(node->getBoolValue() &&
-                             arg->getBoolValue("factor"));
+    return prop->setBoolValue(prop->getBoolValue() &&
+                             factor->getBoolValue());
   case SGPropertyNode::INT:
-    return node->setIntValue(int(node->getIntValue()
-                                * arg->getDoubleValue("factor")));
+    return prop->setIntValue(int(prop->getIntValue()
+                                * factor->getDoubleValue()));
   case SGPropertyNode::LONG:
-    return node->setLongValue(long(node->getLongValue()
-                                  * arg->getDoubleValue("factor")));
+    return prop->setLongValue(long(prop->getLongValue()
+                                  * factor->getDoubleValue()));
   case SGPropertyNode::FLOAT:
-    return node->setFloatValue(float(node->getFloatValue()
-                                    * arg->getDoubleValue("factor")));
+    return prop->setFloatValue(float(prop->getFloatValue()
+                                    * factor->getDoubleValue()));
   case SGPropertyNode::DOUBLE:
   case SGPropertyNode::UNSPECIFIED:
-    return node->setDoubleValue(node->getDoubleValue()
-                               * arg->getDoubleValue("factor"));
+  case SGPropertyNode::NONE:
+    return prop->setDoubleValue(prop->getDoubleValue()
+                               * factor->getDoubleValue());
   default:                     // doesn't make sense with strings
     return false;
   }
@@ -390,16 +500,15 @@ do_property_multiply (const SGPropertyNode * arg)
 static bool
 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
 {
-  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);
-  const string & tmp = node1->getStringValue();
-  return (node1->setUnspecifiedValue(node2->getStringValue()) &&
-         node2->setUnspecifiedValue(tmp));
+  if (*state == 0)
+    *state = new PropertyCommandState(arg);
+  SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
+  SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
+
+                               // FIXME: inefficient
+  const string & tmp = prop1->getStringValue();
+  return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
+         prop2->setUnspecifiedValue(tmp));
 }
 
 
@@ -414,15 +523,26 @@ do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
 static bool
 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
 {
-  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);
+  if (*state == 0)
+    *state = new PropertyCommandState(arg);
+  SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
+  double setting =
+    ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
+  double offset =
+    ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
+  double factor =
+    ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
+
+  return prop->setDoubleValue((setting + offset) * factor);
 }
 
 
 \f
+////////////////////////////////////////////////////////////////////////
+// Command setup.
+////////////////////////////////////////////////////////////////////////
+
+
 /**
  * Table of built-in commands.
  *