1 // Base class for all property controlled subsystems
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "property_based_mgr.hxx"
20 #include "property_helper.hxx"
21 #include <Main/fg_props.hxx>
26 //------------------------------------------------------------------------------
27 void PropertyBasedMgr::init()
29 _props->addChangeListener(this);
30 canvas::triggerChangeRecursive(_props);
33 //------------------------------------------------------------------------------
34 void PropertyBasedMgr::shutdown()
36 _props->removeChangeListener(this);
39 //------------------------------------------------------------------------------
40 void PropertyBasedMgr::update(double delta_time_sec)
42 for( size_t i = 0; i < _elements.size(); ++i )
44 _elements[i]->update(delta_time_sec);
47 //------------------------------------------------------------------------------
48 void PropertyBasedMgr::childAdded( SGPropertyNode * parent,
49 SGPropertyNode * child )
51 if( parent != _props || child->getNameString() != _name_elements )
54 size_t index = child->getIndex();
56 if( index >= _elements.size() )
58 if( index > _elements.size() )
63 "Skipping unused " << _name_elements << " slot(s)!"
66 _elements.resize(index + 1);
68 else if( _elements[index] )
73 _name_elements << "[" << index << "] already exists!"
76 PropertyBasedElementPtr el = _element_factory(child);
78 _elements[index] = el;
82 //------------------------------------------------------------------------------
83 void PropertyBasedMgr::childRemoved( SGPropertyNode * parent,
84 SGPropertyNode * child )
86 if( parent != _props || child->getNameString() != _name_elements )
89 size_t index = child->getIndex();
91 if( index >= _elements.size() )
96 "can't removed unknown " << _name_elements << "[" << index << "]!"
99 // remove the element...
100 _elements[index].reset();
103 //------------------------------------------------------------------------------
104 const SGPropertyNode* PropertyBasedMgr::getPropertyRoot() const
109 //------------------------------------------------------------------------------
110 PropertyBasedMgr::PropertyBasedMgr( const std::string& path_root,
111 const std::string& name_elements,
112 ElementFactory element_factory ):
113 _props( fgGetNode(path_root, true) ),
114 _name_elements( name_elements ),
115 _element_factory( element_factory )
120 //------------------------------------------------------------------------------
121 PropertyBasedMgr::~PropertyBasedMgr()