]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGBinding.cxx
0c3f7feb4eb384090366c135b8028594c9c854db
[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     return;
46   }
47
48   _arg = const_cast<SGPropertyNode*>(node);
49   _setting = 0;
50 }
51
52 void
53 SGBinding::fire () const
54 {
55   if (test()) {
56     if (_command == 0)
57       _command = SGCommandMgr::instance()->getCommand(_command_name);
58     if (_command == 0) {
59       SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding");
60     } else if (!(*_command)(_arg)) {
61       SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command "
62              << _command_name);
63     }
64   }
65 }
66
67 void
68 SGBinding::fire (double offset, double max) const
69 {
70   if (test()) {
71     _arg->setDoubleValue("offset", offset/max);
72     fire();
73   }
74 }
75
76 void
77 SGBinding::fire (double setting) const
78 {
79   if (test()) {
80                                 // A value is automatically added to
81                                 // the args
82     if (_setting == 0)          // save the setting node for efficiency
83       _setting = _arg->getChild("setting", 0, true);
84     _setting->setDoubleValue(setting);
85     fire();
86   }
87 }