]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGBinding.cxx
Boolean uniforms are now updatable by properties
[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 <simgear/compiler.h>
15 #include "SGBinding.hxx"
16
17 #include <simgear/math/SGMath.hxx>
18
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 SGPropertyNode* node, SGPropertyNode* root)
29   : _command(0),
30     _arg(0),
31     _setting(0)
32 {
33   read(node, root);
34 }
35
36 SGBinding::~SGBinding()
37 {
38   if(_arg && _arg->getParent())
39     _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex(), false);
40 }
41
42 void
43 SGBinding::read(const SGPropertyNode* node, SGPropertyNode* root)
44 {
45   const SGPropertyNode * conditionNode = node->getChild("condition");
46   if (conditionNode != 0)
47     setCondition(sgReadCondition(root, conditionNode));
48
49   _command_name = node->getStringValue("command", "");
50   if (_command_name.empty()) {
51     SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
52     _command = 0;
53   }
54
55   _arg = const_cast<SGPropertyNode*>(node);
56   _setting = 0;
57 }
58
59 void
60 SGBinding::fire () const
61 {
62   if (test()) {
63     if (_command == 0)
64       _command = SGCommandMgr::instance()->getCommand(_command_name);
65     if (_command == 0) {
66       SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
67     } else
68     {
69         try {
70             if (!(*_command)(_arg)) {
71                   SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
72                          << _command_name);
73             }
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() << ")");
77         }
78     }
79   }
80 }
81
82 void
83 SGBinding::fire (double offset, double max) const
84 {
85   if (test()) {
86     _arg->setDoubleValue("offset", offset/max);
87     fire();
88   }
89 }
90
91 void
92 SGBinding::fire (double setting) const
93 {
94   if (test()) {
95                                 // A value is automatically added to
96                                 // the args
97     if (_setting == 0)          // save the setting node for efficiency
98       _setting = _arg->getChild("setting", 0, true);
99     _setting->setDoubleValue(setting);
100     fire();
101   }
102 }