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/props/props_io.hxx>
19 #include <simgear/structure/exception.hxx>
21 SGBinding::SGBinding()
23 _arg(new SGPropertyNode),
28 SGBinding::SGBinding(const std::string& commandName)
33 _command_name = commandName;
36 SGBinding::SGBinding(const SGPropertyNode* node, SGPropertyNode* root)
44 SGBinding::~SGBinding()
46 if(_arg && _arg->getParent())
47 _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex());
59 SGBinding::read(const SGPropertyNode* node, SGPropertyNode* root)
61 const SGPropertyNode * conditionNode = node->getChild("condition");
62 if (conditionNode != 0)
63 setCondition(sgReadCondition(root, conditionNode));
65 _command_name = node->getStringValue("command", "");
66 if (_command_name.empty()) {
67 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
71 _arg = const_cast<SGPropertyNode*>(node);
76 SGBinding::fire() const
84 SGBinding::innerFire () const
87 _command = SGCommandMgr::instance()->getCommand(_command_name);
89 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding:" << _command_name);
92 if (!(*_command)(_arg)) {
93 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
96 } catch (sg_exception& e) {
97 SG_LOG(SG_GENERAL, SG_ALERT, "command '" << _command_name << "' failed with exception\n"
98 << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")");
104 SGBinding::fire (SGPropertyNode* params) const
107 if (params != NULL) {
108 copyProperties(params, _arg);
116 SGBinding::fire (double offset, double max) const
119 _arg->setDoubleValue("offset", offset/max);
125 SGBinding::fire (double setting) const
128 // A value is automatically added to
130 if (_setting == 0) // save the setting node for efficiency
131 _setting = _arg->getChild("setting", 0, true);
132 _setting->setDoubleValue(setting);
137 void fireBindingList(const SGBindingList& aBindings, SGPropertyNode* params)
139 BOOST_FOREACH(SGBinding_ptr b, aBindings) {
144 void fireBindingListWithOffset(const SGBindingList& aBindings, double offset, double max)
146 BOOST_FOREACH(SGBinding_ptr b, aBindings) {
147 b->fire(offset, max);
151 SGBindingList readBindingList(const simgear::PropertyList& aNodes, SGPropertyNode* aRoot)
153 SGBindingList result;
154 BOOST_FOREACH(SGPropertyNode* node, aNodes) {
155 result.push_back(new SGBinding(node, aRoot));
161 void clearBindingList(const SGBindingList& aBindings)
163 BOOST_FOREACH(SGBinding_ptr b, aBindings) {