]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/component.cxx
Autopilot: add interface properties and property-root.
[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 //------------------------------------------------------------------------------
44 bool Component::configure( SGPropertyNode& prop_root,
45                            SGPropertyNode& cfg )
46 {
47   for( int i = 0; i < cfg.nChildren(); ++i )
48   {
49     SGPropertyNode_ptr child = cfg.getChild(i);
50     std::string cname(child->getName());
51
52     if( !configure(*child, cname, prop_root) )
53       SG_LOG
54       (
55         SG_AUTOPILOT,
56         SG_INFO,
57         "Component::configure: unknown node: " << cname
58       );
59   }
60
61   return true;
62 }
63
64 //------------------------------------------------------------------------------
65 bool Component::configure( SGPropertyNode& cfg_node,
66                            const std::string& cfg_name,
67                            SGPropertyNode& prop_root )
68 {
69   SG_LOG
70   (
71     SG_AUTOPILOT,
72     SG_BULK,
73     "Component::configure(" << cfg_name << ")"
74   );
75
76   if ( cfg_name == "name" ) {
77     _name = cfg_node.getStringValue();
78     return true;
79   } 
80
81   if ( cfg_name == "debug" ) {
82     _debug = cfg_node.getBoolValue();
83     return true;
84   }
85
86   if ( cfg_name == "enable" ) {
87     SGPropertyNode_ptr prop;
88
89     if( (prop = cfg_node.getChild("condition")) != NULL ) {
90       _condition = sgReadCondition(fgGetNode("/"), prop);
91       return true;
92     } 
93     if ( (prop = cfg_node.getChild( "property" )) != NULL ) {
94       _enable_prop = fgGetNode( prop->getStringValue(), true );
95     }
96        
97     if ( (prop = cfg_node.getChild( "prop" )) != NULL ) {
98       _enable_prop = fgGetNode( prop->getStringValue(), true );
99     }
100
101     if ( (prop = cfg_node.getChild( "value" )) != NULL ) {
102       delete _enable_value;
103       _enable_value = new std::string(prop->getStringValue());
104     }
105
106     if ( (prop = cfg_node.getChild( "honor-passive" )) != NULL ) {
107       _honor_passive = prop->getBoolValue();
108     }
109
110     return true;
111   } // enable
112
113   SG_LOG
114   (
115     SG_AUTOPILOT,
116     SG_BULK,
117     "Component::configure(" << cfg_name << ") [unhandled]"
118   );
119   return false;
120 }
121
122 bool Component::isPropertyEnabled()
123 {
124     if( _condition )
125         return _condition->test();
126
127     if( _enable_prop ) {
128         if( _enable_value ) {
129             return *_enable_value == _enable_prop->getStringValue();
130         } else {
131             return _enable_prop->getBoolValue();
132         }
133     }
134     return true;
135 }
136
137 void Component::update( double dt )
138 {
139   bool firstTime = false;
140   if( isPropertyEnabled() ) {
141     firstTime = !_enabled;
142     _enabled = true;
143   } else {
144     _enabled = false;
145   }
146
147   if( _enabled ) update( firstTime, dt );
148   else disabled( dt );
149 }