]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/component.cxx
Merge branch 'next' of http://git.gitorious.org/fg/flightgear into next
[flightgear.git] / src / Autopilot / component.cxx
1 // component.cxx - Base class for autopilot components
2 //
3 // Written by Torsten Dreyer
4 // Based heavily on work created by Curtis Olson, started January 2004.
5 //
6 // Copyright (C) 2004  Curtis L. Olson  - http://www.flightgear.org/~curt
7 // Copyright (C) 2010  Torsten Dreyer - Torsten (at) t3r (dot) de
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23 #include "component.hxx"
24 #include <Main/fg_props.hxx>
25 #include <simgear/structure/exception.hxx>
26
27 using namespace FGXMLAutopilot;
28
29 Component::Component() :
30   _enable_value(NULL),
31   _enabled(false),
32   _debug(false),
33   _honor_passive(false)
34 {
35 }
36
37 Component::~Component()
38 {
39   delete _enable_value;
40 }
41
42 bool Component::configure( SGPropertyNode_ptr configNode )
43 {
44   for (int i = 0; i < configNode->nChildren(); ++i ) {
45     SGPropertyNode_ptr prop;
46
47     SGPropertyNode_ptr child = configNode->getChild(i);
48     string cname(child->getName());
49
50     if( configure( cname, child ) )
51       continue;
52
53   } // for configNode->nChildren()
54
55   return true;
56 }
57
58 bool Component::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
59 {
60   SG_LOG( SG_AUTOPILOT, SG_BULK, "Component::configure(" << nodeName << ")" << endl );
61
62   if ( nodeName == "name" ) {
63     _name = configNode->getStringValue();
64     return true;
65   } 
66
67   if ( nodeName == "debug" ) {
68     _debug = configNode->getBoolValue();
69     return true;
70   }
71
72   if ( nodeName == "enable" ) {
73     SGPropertyNode_ptr prop;
74
75     if( (prop = configNode->getChild("condition")) != NULL ) {
76       _condition = sgReadCondition(fgGetNode("/"), prop);
77       return true;
78     } 
79     if ( (prop = configNode->getChild( "property" )) != NULL ) {
80       _enable_prop = fgGetNode( prop->getStringValue(), true );
81     }
82        
83     if ( (prop = configNode->getChild( "prop" )) != NULL ) {
84       _enable_prop = fgGetNode( prop->getStringValue(), true );
85     }
86
87     if ( (prop = configNode->getChild( "value" )) != NULL ) {
88       delete _enable_value;
89       _enable_value = new string(prop->getStringValue());
90     }
91
92     if ( (prop = configNode->getChild( "honor-passive" )) != NULL ) {
93       _honor_passive = prop->getBoolValue();
94     }
95
96     return true;
97   } // enable
98
99   SG_LOG( SG_AUTOPILOT, SG_BULK, "Component::configure(" << nodeName << ") [unhandled]" << endl );
100   return false;
101 }
102
103 bool Component::isPropertyEnabled()
104 {
105     if( _condition )
106         return _condition->test();
107
108     if( _enable_prop ) {
109         if( _enable_value ) {
110             return *_enable_value == _enable_prop->getStringValue();
111         } else {
112             return _enable_prop->getBoolValue();
113         }
114     }
115     return true;
116 }
117
118 void Component::update( double dt )
119 {
120   bool firstTime = false;
121   if( isPropertyEnabled() ) {
122     firstTime = !_enabled;
123     _enabled = true;
124   } else {
125     _enabled = false;
126   }
127
128   if( _enabled ) update( firstTime, dt );
129   else disabled( dt );
130 }