]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGBinding.cxx
Support arbitrary parameters to bindings.
[simgear.git] / simgear / structure / SGBinding.cxx
1 /**
2  * \file commands.hxx
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.
6  *
7  * $Id$
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #  include <simgear_config.h>
12 #endif
13
14 #include <boost/foreach.hpp>
15 #include <simgear/compiler.h>
16 #include "SGBinding.hxx"
17
18 #include <simgear/props/props_io.hxx>
19 #include <simgear/structure/exception.hxx>
20
21 SGBinding::SGBinding()
22   : _command(0),
23     _arg(new SGPropertyNode),
24     _setting(0)
25 {
26 }
27
28 SGBinding::SGBinding(const std::string& commandName)
29     : _command(0),
30     _arg(0),
31     _setting(0)
32 {
33     _command_name = commandName;
34 }
35
36 SGBinding::SGBinding(const SGPropertyNode* node, SGPropertyNode* root)
37   : _command(0),
38     _arg(0),
39     _setting(0)
40 {
41   read(node, root);
42 }
43
44 SGBinding::~SGBinding()
45 {
46   if(_arg && _arg->getParent())
47     _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex(), false);
48 }
49
50 void
51 SGBinding::read(const SGPropertyNode* node, SGPropertyNode* root)
52 {
53   const SGPropertyNode * conditionNode = node->getChild("condition");
54   if (conditionNode != 0)
55     setCondition(sgReadCondition(root, conditionNode));
56
57   _command_name = node->getStringValue("command", "");
58   if (_command_name.empty()) {
59     SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
60     _command = 0;
61   }
62
63   _arg = const_cast<SGPropertyNode*>(node);
64   _setting = 0;
65 }
66
67 void
68 SGBinding::fire() const
69 {
70   if (test()) {
71     innerFire();
72   }
73 }
74
75 void
76 SGBinding::innerFire () const
77 {
78   if (_command == 0)
79     _command = SGCommandMgr::instance()->getCommand(_command_name);
80   if (_command == 0) {
81     SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding:" << _command_name);
82   } else {
83       try {
84           if (!(*_command)(_arg)) {
85                 SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
86                        << _command_name);
87           }
88       } catch (sg_exception& e) {
89         SG_LOG(SG_GENERAL, SG_ALERT, "command '" << _command_name << "' failed with exception\n"
90           << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")");
91       }
92   }
93 }
94
95 void
96 SGBinding::fire (SGPropertyNode* params) const
97 {
98   if (test()) {
99     if (params != NULL) {
100       copyProperties(params, _arg);
101     }
102     
103     innerFire();
104   }
105 }
106
107 void
108 SGBinding::fire (double offset, double max) const
109 {
110   if (test()) {
111     _arg->setDoubleValue("offset", offset/max);
112     innerFire();
113   }
114 }
115
116 void
117 SGBinding::fire (double setting) const
118 {
119   if (test()) {
120                                 // A value is automatically added to
121                                 // the args
122     if (_setting == 0)          // save the setting node for efficiency
123       _setting = _arg->getChild("setting", 0, true);
124     _setting->setDoubleValue(setting);
125     innerFire();
126   }
127 }
128
129 void fireBindingList(const SGBindingList& aBindings, SGPropertyNode* params)
130 {
131     BOOST_FOREACH(SGBinding_ptr b, aBindings) {
132         b->fire(params);
133     }
134 }
135
136 void fireBindingListWithOffset(const SGBindingList& aBindings, double offset, double max)
137 {
138     BOOST_FOREACH(SGBinding_ptr b, aBindings) {
139         b->fire(offset, max);
140     }
141 }
142
143 SGBindingList readBindingList(const simgear::PropertyList& aNodes, SGPropertyNode* aRoot)
144 {
145     SGBindingList result;
146     BOOST_FOREACH(SGPropertyNode* node, aNodes) {
147         result.push_back(new SGBinding(node, aRoot));
148     }
149     
150     return result;
151 }