]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGBinding.cxx
Work in progress for Technique validation
[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 #include <simgear/compiler.h>
11 #include "SGBinding.hxx"
12
13 SGBinding::SGBinding()
14   : _command(0),
15     _arg(new SGPropertyNode),
16     _setting(0)
17 {
18 }
19
20 SGBinding::SGBinding(const SGPropertyNode* node, SGPropertyNode* root)
21   : _command(0),
22     _arg(0),
23     _setting(0)
24 {
25   read(node, root);
26 }
27
28 SGBinding::~SGBinding()
29 {
30   if(_arg && _arg->getParent())
31     _arg->getParent()->removeChild(_arg->getName(), _arg->getIndex(), false);
32 }
33
34 void
35 SGBinding::read(const SGPropertyNode* node, SGPropertyNode* root)
36 {
37   const SGPropertyNode * conditionNode = node->getChild("condition");
38   if (conditionNode != 0)
39     setCondition(sgReadCondition(root, conditionNode));
40
41   _command_name = node->getStringValue("command", "");
42   if (_command_name.empty()) {
43     SG_LOG(SG_INPUT, SG_WARN, "No command supplied for binding.");
44     _command = 0;
45   }
46
47   _arg = const_cast<SGPropertyNode*>(node);
48   _setting = 0;
49 }
50
51 void
52 SGBinding::fire () const
53 {
54   if (test()) {
55     if (_command == 0)
56       _command = SGCommandMgr::instance()->getCommand(_command_name);
57     if (_command == 0) {
58       SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
59     } else if (!(*_command)(_arg)) {
60       SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
61              << _command_name);
62     }
63   }
64 }
65
66 void
67 SGBinding::fire (double offset, double max) const
68 {
69   if (test()) {
70     _arg->setDoubleValue("offset", offset/max);
71     fire();
72   }
73 }
74
75 void
76 SGBinding::fire (double setting) const
77 {
78   if (test()) {
79                                 // A value is automatically added to
80                                 // the args
81     if (_setting == 0)          // save the setting node for efficiency
82       _setting = _arg->getChild("setting", 0, true);
83     _setting->setDoubleValue(setting);
84     fire();
85   }
86 }