3 * Interface definition for encapsulated commands.
4 * Started Spring 2001 by David Megginson, david@megginson.com
5 * This code is released into the Public Domain.
11 # include <simgear_config.h>
14 #include <boost/foreach.hpp>
15 #include <simgear/compiler.h>
16 #include "SGBinding.hxx"
18 #include <simgear/structure/exception.hxx>
20 SGBinding::SGBinding()
22 _arg(new SGPropertyNode),
27 SGBinding::SGBinding(const std::string& commandName)
32 _command_name = commandName;
35 SGBinding::SGBinding(const SGPropertyNode* node, SGPropertyNode* root)
43 SGBinding::~SGBinding()
45 if(_arg && _arg->getParent())
46 _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex(), false);
50 SGBinding::read(const SGPropertyNode* node, SGPropertyNode* root)
52 const SGPropertyNode * conditionNode = node->getChild("condition");
53 if (conditionNode != 0)
54 setCondition(sgReadCondition(root, conditionNode));
56 _command_name = node->getStringValue("command", "");
57 if (_command_name.empty()) {
58 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
62 _arg = const_cast<SGPropertyNode*>(node);
67 SGBinding::fire () const
71 _command = SGCommandMgr::instance()->getCommand(_command_name);
73 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
77 if (!(*_command)(_arg)) {
78 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
81 } catch (sg_exception& e) {
82 SG_LOG(SG_GENERAL, SG_ALERT, "command '" << _command_name << "' failed with exception\n"
83 << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")");
90 SGBinding::fire (double offset, double max) const
93 _arg->setDoubleValue("offset", offset/max);
99 SGBinding::fire (double setting) const
102 // A value is automatically added to
104 if (_setting == 0) // save the setting node for efficiency
105 _setting = _arg->getChild("setting", 0, true);
106 _setting->setDoubleValue(setting);
111 void fireBindingList(const SGBindingList& aBindings)
113 BOOST_FOREACH(SGBinding_ptr b, aBindings) {
118 void fireBindingListWithOffset(const SGBindingList& aBindings, double offset, double max)
120 BOOST_FOREACH(SGBinding_ptr b, aBindings) {
121 b->fire(offset, max);
125 SGBindingList readBindingList(const simgear::PropertyList& aNodes, SGPropertyNode* aRoot)
127 SGBindingList result;
128 BOOST_FOREACH(SGPropertyNode* node, aNodes) {
129 result.push_back(new SGBinding(node, aRoot));