]> git.mxchange.org Git - simgear.git/blobdiff - simgear/structure/SGBinding.cxx
Some Linux platforms need <cstdio> for snprintf.
[simgear.git] / simgear / structure / SGBinding.cxx
index 8ca19034daeddff8737c3c48b53609034c64affd..62d1b3c5d96c1c5eafdad0b2a00422838d8d3dfe 100644 (file)
 #  include <simgear_config.h>
 #endif
 
+#include <boost/foreach.hpp>
 #include <simgear/compiler.h>
 #include "SGBinding.hxx"
 
+#include <simgear/props/props_io.hxx>
 #include <simgear/structure/exception.hxx>
 
 SGBinding::SGBinding()
@@ -23,6 +25,14 @@ SGBinding::SGBinding()
 {
 }
 
+SGBinding::SGBinding(const std::string& commandName)
+    : _command(0),
+    _arg(0),
+    _setting(0)
+{
+    _command_name = commandName;
+}
+
 SGBinding::SGBinding(const SGPropertyNode* node, SGPropertyNode* root)
   : _command(0),
     _arg(0),
@@ -37,6 +47,14 @@ SGBinding::~SGBinding()
     _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex(), false);
 }
 
+void
+SGBinding::clear()
+{
+    _command = NULL;
+    _arg.clear();
+    _setting.clear();
+}
+
 void
 SGBinding::read(const SGPropertyNode* node, SGPropertyNode* root)
 {
@@ -55,25 +73,42 @@ SGBinding::read(const SGPropertyNode* node, SGPropertyNode* root)
 }
 
 void
-SGBinding::fire () const
+SGBinding::fire() const
 {
   if (test()) {
-    if (_command == 0)
-      _command = SGCommandMgr::instance()->getCommand(_command_name);
-    if (_command == 0) {
-      SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
-    } else
-    {
-        try {
-            if (!(*_command)(_arg)) {
-                  SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
-                         << _command_name);
-            }
-        } catch (sg_exception& e) {
-          SG_LOG(SG_GENERAL, SG_ALERT, "command '" << _command_name << "' failed with exception\n"
-            << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")");
-        }
+    innerFire();
+  }
+}
+
+void
+SGBinding::innerFire () const
+{
+  if (_command == 0)
+    _command = SGCommandMgr::instance()->getCommand(_command_name);
+  if (_command == 0) {
+    SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding:" << _command_name);
+  } else {
+      try {
+          if (!(*_command)(_arg)) {
+                SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
+                       << _command_name);
+          }
+      } catch (sg_exception& e) {
+        SG_LOG(SG_GENERAL, SG_ALERT, "command '" << _command_name << "' failed with exception\n"
+          << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")");
+      }
+  }
+}
+
+void
+SGBinding::fire (SGPropertyNode* params) const
+{
+  if (test()) {
+    if (params != NULL) {
+      copyProperties(params, _arg);
     }
+    
+    innerFire();
   }
 }
 
@@ -82,7 +117,7 @@ SGBinding::fire (double offset, double max) const
 {
   if (test()) {
     _arg->setDoubleValue("offset", offset/max);
-    fire();
+    innerFire();
   }
 }
 
@@ -95,6 +130,38 @@ SGBinding::fire (double setting) const
     if (_setting == 0)          // save the setting node for efficiency
       _setting = _arg->getChild("setting", 0, true);
     _setting->setDoubleValue(setting);
-    fire();
+    innerFire();
   }
 }
+
+void fireBindingList(const SGBindingList& aBindings, SGPropertyNode* params)
+{
+    BOOST_FOREACH(SGBinding_ptr b, aBindings) {
+        b->fire(params);
+    }
+}
+
+void fireBindingListWithOffset(const SGBindingList& aBindings, double offset, double max)
+{
+    BOOST_FOREACH(SGBinding_ptr b, aBindings) {
+        b->fire(offset, max);
+    }
+}
+
+SGBindingList readBindingList(const simgear::PropertyList& aNodes, SGPropertyNode* aRoot)
+{
+    SGBindingList result;
+    BOOST_FOREACH(SGPropertyNode* node, aNodes) {
+        result.push_back(new SGBinding(node, aRoot));
+    }
+    
+    return result;
+}
+
+void clearBindingList(const SGBindingList& aBindings)
+{
+    BOOST_FOREACH(SGBinding_ptr b, aBindings) {
+        b->clear();
+    }
+}
+