00001
00010 #ifdef HAVE_CONFIG_H
00011 # include <simgear_config.h>
00012 #endif
00013
00014 #include <simgear/compiler.h>
00015 #include "SGBinding.hxx"
00016
00017 #include <simgear/math/SGMath.hxx>
00018
00019 SGBinding::SGBinding()
00020 : _command(0),
00021 _arg(new SGPropertyNode),
00022 _setting(0)
00023 {
00024 }
00025
00026 SGBinding::SGBinding(const SGPropertyNode* node, SGPropertyNode* root)
00027 : _command(0),
00028 _arg(0),
00029 _setting(0)
00030 {
00031 read(node, root);
00032 }
00033
00034 SGBinding::~SGBinding()
00035 {
00036 if(_arg && _arg->getParent())
00037 _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex(), false);
00038 }
00039
00040 void
00041 SGBinding::read(const SGPropertyNode* node, SGPropertyNode* root)
00042 {
00043 const SGPropertyNode * conditionNode = node->getChild("condition");
00044 if (conditionNode != 0)
00045 setCondition(sgReadCondition(root, conditionNode));
00046
00047 _command_name = node->getStringValue("command", "");
00048 if (_command_name.empty()) {
00049 SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
00050 _command = 0;
00051 }
00052
00053 _arg = const_cast<SGPropertyNode*>(node);
00054 _setting = 0;
00055 }
00056
00057 void
00058 SGBinding::fire () const
00059 {
00060 if (test()) {
00061 if (_command == 0)
00062 _command = SGCommandMgr::instance()->getCommand(_command_name);
00063 if (_command == 0) {
00064 SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
00065 } else if (!(*_command)(_arg)) {
00066 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
00067 << _command_name);
00068 }
00069 }
00070 }
00071
00072 void
00073 SGBinding::fire (double offset, double max) const
00074 {
00075 if (test()) {
00076 _arg->setDoubleValue("offset", offset/max);
00077 fire();
00078 }
00079 }
00080
00081 void
00082 SGBinding::fire (double setting) const
00083 {
00084 if (test()) {
00085
00086
00087 if (_setting == 0)
00088 _setting = _arg->getChild("setting", 0, true);
00089 _setting->setDoubleValue(setting);
00090 fire();
00091 }
00092 }