]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/component.cxx
Autopilot: Optionally write (internal) state to property tree
[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 #include <simgear/props/condition.hxx>
27
28 using namespace FGXMLAutopilot;
29
30 Component::Component() :
31   _enable_value(NULL),
32   _enabled(false),
33   _debug(false),
34   _honor_passive(false)
35 {
36 }
37
38 Component::~Component()
39 {
40   delete _enable_value;
41 }
42
43 bool Component::configure( SGPropertyNode_ptr configNode )
44 {
45   for (int i = 0; i < configNode->nChildren(); ++i ) {
46     SGPropertyNode_ptr prop;
47
48     SGPropertyNode_ptr child = configNode->getChild(i);
49     std::string cname(child->getName());
50
51     if( configure( cname, child ) )
52       continue;
53
54   } // for configNode->nChildren()
55
56   return true;
57 }
58
59 bool Component::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
60 {
61   SG_LOG( SG_AUTOPILOT, SG_BULK, "Component::configure(" << nodeName << ")" << std::endl );
62
63   if ( nodeName == "name" ) {
64     _name = configNode->getStringValue();
65     return true;
66   } 
67
68   if ( nodeName == "debug" ) {
69     _debug = configNode->getBoolValue();
70     return true;
71   }
72
73   if ( nodeName == "debug-node" ) {
74     _debug_node = fgGetNode( configNode->getStringValue(), true );
75     return true;
76   }
77
78   if ( nodeName == "enable" ) {
79     SGPropertyNode_ptr prop;
80
81     if( (prop = configNode->getChild("condition")) != NULL ) {
82       _condition = sgReadCondition(fgGetNode("/"), prop);
83       return true;
84     } 
85     if ( (prop = configNode->getChild( "property" )) != NULL ) {
86       _enable_prop = fgGetNode( prop->getStringValue(), true );
87     }
88        
89     if ( (prop = configNode->getChild( "prop" )) != NULL ) {
90       _enable_prop = fgGetNode( prop->getStringValue(), true );
91     }
92
93     if ( (prop = configNode->getChild( "value" )) != NULL ) {
94       delete _enable_value;
95       _enable_value = new std::string(prop->getStringValue());
96     }
97
98     if ( (prop = configNode->getChild( "honor-passive" )) != NULL ) {
99       _honor_passive = prop->getBoolValue();
100     }
101
102     return true;
103   } // enable
104
105   SG_LOG( SG_AUTOPILOT, SG_BULK, "Component::configure(" << nodeName << ") [unhandled]" << std::endl );
106   return false;
107 }
108
109 bool Component::isPropertyEnabled()
110 {
111     if( _condition )
112         return _condition->test();
113
114     if( _enable_prop ) {
115         if( _enable_value ) {
116             return *_enable_value == _enable_prop->getStringValue();
117         } else {
118             return _enable_prop->getBoolValue();
119         }
120     }
121     return true;
122 }
123
124 void Component::update( double dt )
125 {
126   bool firstTime = false;
127   if( isPropertyEnabled() ) {
128     firstTime = !_enabled;
129     _enabled = true;
130   } else {
131     _enabled = false;
132   }
133
134   if( _enabled ) update( firstTime, dt );
135   else disabled( dt );
136 }