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 <simgear/compiler.h>
15 #include "SGBinding.hxx"
17 #include <simgear/math/SGMath.hxx>
19 #include <simgear/structure/exception.hxx>
21 SGBinding::SGBinding()
23 _arg(new SGPropertyNode),
28 SGBinding::SGBinding(const SGPropertyNode* node, SGPropertyNode* root)
36 SGBinding::~SGBinding()
38 if(_arg && _arg->getParent())
39 _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex(), false);
43 SGBinding::read(const SGPropertyNode* node, SGPropertyNode* root)
45 const SGPropertyNode * conditionNode = node->getChild("condition");
46 if (conditionNode != 0)
47 setCondition(sgReadCondition(root, conditionNode));
49 _command_name = node->getStringValue("command", "");
50 if (_command_name.empty()) {
51 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
55 _arg = const_cast<SGPropertyNode*>(node);
60 SGBinding::fire () const
64 _command = SGCommandMgr::instance()->getCommand(_command_name);
66 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
70 if (!(*_command)(_arg)) {
71 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
74 } catch (sg_exception& e) {
75 SG_LOG(SG_GENERAL, SG_ALERT, "command '" << _command_name << "' failed with exception\n"
76 << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")");
83 SGBinding::fire (double offset, double max) const
86 _arg->setDoubleValue("offset", offset/max);
92 SGBinding::fire (double setting) const
95 // A value is automatically added to
97 if (_setting == 0) // save the setting node for efficiency
98 _setting = _arg->getChild("setting", 0, true);
99 _setting->setDoubleValue(setting);