]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGBinding.cxx
Working 'noshadow' animation
[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/structure/exception.hxx>
18
19 SGBinding::SGBinding()
20   : _command(0),
21     _arg(new SGPropertyNode),
22     _setting(0)
23 {
24 }
25
26 SGBinding::SGBinding(const SGPropertyNode* node, SGPropertyNode* root)
27   : _command(0),
28     _arg(0),
29     _setting(0)
30 {
31   read(node, root);
32 }
33
34 SGBinding::~SGBinding()
35 {
36   if(_arg && _arg->getParent())
37     _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex(), false);
38 }
39
40 void
41 SGBinding::read(const SGPropertyNode* node, SGPropertyNode* root)
42 {
43   const SGPropertyNode * conditionNode = node->getChild("condition");
44   if (conditionNode != 0)
45     setCondition(sgReadCondition(root, conditionNode));
46
47   _command_name = node->getStringValue("command", "");
48   if (_command_name.empty()) {
49     SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
50     _command = 0;
51   }
52
53   _arg = const_cast<SGPropertyNode*>(node);
54   _setting = 0;
55 }
56
57 void
58 SGBinding::fire () const
59 {
60   if (test()) {
61     if (_command == 0)
62       _command = SGCommandMgr::instance()->getCommand(_command_name);
63     if (_command == 0) {
64       SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
65     } else
66     {
67         try {
68             if (!(*_command)(_arg)) {
69                   SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
70                          << _command_name);
71             }
72         } catch (sg_exception& e) {
73           SG_LOG(SG_GENERAL, SG_ALERT, "command '" << _command_name << "' failed with exception\n"
74             << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")");
75         }
76     }
77   }
78 }
79
80 void
81 SGBinding::fire (double offset, double max) const
82 {
83   if (test()) {
84     _arg->setDoubleValue("offset", offset/max);
85     fire();
86   }
87 }
88
89 void
90 SGBinding::fire (double setting) const
91 {
92   if (test()) {
93                                 // A value is automatically added to
94                                 // the args
95     if (_setting == 0)          // save the setting node for efficiency
96       _setting = _arg->getChild("setting", 0, true);
97     _setting->setDoubleValue(setting);
98     fire();
99   }
100 }