1
0
Fork 0

Refactor PropertyBasedElement and Manager

This commit is contained in:
Thomas Geymayer 2012-10-29 16:15:10 +01:00
parent 7c8ad455a3
commit 4e75587332
14 changed files with 19 additions and 351 deletions

View file

@ -11,8 +11,6 @@ set(SOURCES
elements/CanvasImage.cxx
gui_mgr.cxx
placement.cxx
property_based_element.cxx
property_based_mgr.cxx
property_helper.cxx
window.cxx
)
@ -28,8 +26,6 @@ set(HEADERS
elements/CanvasImage.hxx
gui_mgr.hxx
placement.hxx
property_based_element.hxx
property_based_mgr.hxx
property_helper.hxx
window.hxx
)

View file

@ -20,11 +20,11 @@
#define CANVAS_HXX_
#include "placement.hxx"
#include "property_based_element.hxx"
#include <Canvas/canvas_fwd.hpp>
#include <Cockpit/od_gauge.hxx>
#include <simgear/props/PropertyBasedElement.hxx>
#include <simgear/props/propertyObject.hxx>
#include <osg/NodeCallback>
#include <osg/observer_ptr>
@ -33,7 +33,7 @@
#include <string>
class Canvas:
public PropertyBasedElement
public simgear::PropertyBasedElement
{
public:

View file

@ -30,10 +30,6 @@ class Canvas;
typedef boost::shared_ptr<Canvas> CanvasPtr;
typedef boost::weak_ptr<Canvas> CanvasWeakPtr;
class PropertyBasedElement;
typedef boost::shared_ptr<PropertyBasedElement> PropertyBasedElementPtr;
typedef boost::weak_ptr<PropertyBasedElement> PropertyBasedElementWeakPtr;
namespace canvas
{
const std::string VG_INIT_SIGNAL = "/sim/signals/vg-initialized-frame";

View file

@ -19,6 +19,8 @@
#include "canvas_mgr.hxx"
#include "canvas.hxx"
#include <Main/fg_props.hxx>
#include <boost/bind.hpp>
typedef boost::shared_ptr<Canvas> CanvasPtr;
@ -30,7 +32,9 @@ CanvasPtr canvasFactory(SGPropertyNode* node)
//------------------------------------------------------------------------------
CanvasMgr::CanvasMgr():
PropertyBasedMgr("/canvas/by-index", "texture", &canvasFactory)
PropertyBasedMgr( fgGetNode("/canvas/by-index", true),
"texture",
&canvasFactory )
{
Canvas::addPlacementFactory
(

View file

@ -20,10 +20,10 @@
#define CANVAS_MGR_H_
#include "canvas_fwd.hpp"
#include "property_based_mgr.hxx"
#include <simgear/props/PropertyBasedMgr.hxx>
class CanvasMgr:
public PropertyBasedMgr
public simgear::PropertyBasedMgr
{
public:
CanvasMgr();

View file

@ -20,6 +20,7 @@
#include <Canvas/window.hxx>
#include <Canvas/canvas.hxx>
#include <Main/fg_props.hxx>
#include <Main/globals.hxx>
#include <Viewer/CameraGroup.hxx>
#include <Viewer/renderer.hxx>
@ -105,7 +106,9 @@ WindowPtr windowFactory(SGPropertyNode* node)
//------------------------------------------------------------------------------
GUIMgr::GUIMgr():
PropertyBasedMgr("/sim/gui/canvas", "window", &windowFactory),
PropertyBasedMgr( fgGetNode("/sim/gui/canvas", true),
"window",
&windowFactory ),
_event_handler( new GUIEventHandler(this) ),
_transform( new osg::MatrixTransform ),
_width(_props, "size[0]"),
@ -164,7 +167,7 @@ void GUIMgr::shutdown()
}
//------------------------------------------------------------------------------
void GUIMgr::elementCreated(PropertyBasedElementPtr element)
void GUIMgr::elementCreated(simgear::PropertyBasedElementPtr element)
{
canvas::WindowPtr window =
boost::static_pointer_cast<canvas::Window>(element);

View file

@ -19,10 +19,10 @@
#ifndef CANVAS_GUI_MGR_HXX_
#define CANVAS_GUI_MGR_HXX_
#include "property_based_mgr.hxx"
#include <Canvas/canvas_fwd.hpp>
#include <Canvas/placement.hxx>
#include <simgear/props/PropertyBasedMgr.hxx>
#include <simgear/props/propertyObject.hxx>
#include <osg/ref_ptr>
@ -36,7 +36,7 @@ namespace osgGA
class GUIEventHandler;
class GUIMgr:
public PropertyBasedMgr
public simgear::PropertyBasedMgr
{
public:
GUIMgr();
@ -44,7 +44,7 @@ class GUIMgr:
virtual void init();
virtual void shutdown();
virtual void elementCreated(PropertyBasedElementPtr element);
virtual void elementCreated(simgear::PropertyBasedElementPtr element);
bool handleEvent(const osgGA::GUIEventAdapter& ea);

View file

@ -1,44 +0,0 @@
// Base class for elements of property controlled subsystems
//
// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "property_based_element.hxx"
//------------------------------------------------------------------------------
PropertyBasedElement::PropertyBasedElement(SGPropertyNode* node):
_node(node)
{
_node->addChangeListener(this);
}
//------------------------------------------------------------------------------
PropertyBasedElement::~PropertyBasedElement()
{
_node->removeChangeListener(this);
}
//------------------------------------------------------------------------------
SGConstPropertyNode_ptr PropertyBasedElement::getProps() const
{
return _node;
}
//------------------------------------------------------------------------------
SGPropertyNode_ptr PropertyBasedElement::getProps()
{
return _node;
}

View file

@ -1,49 +0,0 @@
// Base class for elements of property controlled subsystems
//
// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef PROPERTY_BASED_ELEMENT_HXX_
#define PROPERTY_BASED_ELEMENT_HXX_
#include <Canvas/canvas_fwd.hpp>
#include <simgear/props/props.hxx>
/**
* Base class for a property controlled element
*/
class PropertyBasedElement:
public SGPropertyChangeListener
{
public:
PropertyBasedElement(SGPropertyNode* node);
virtual ~PropertyBasedElement();
virtual void update(double delta_time_sec) = 0;
SGConstPropertyNode_ptr getProps() const;
SGPropertyNode_ptr getProps();
protected:
friend class PropertyBasedMgr;
SGPropertyNode_ptr _node;
PropertyBasedElementWeakPtr _self;
};
#endif /* PROPERTY_BASED_ELEMENT_HXX_ */

View file

@ -1,126 +0,0 @@
// Base class for all property controlled subsystems
//
// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "property_based_mgr.hxx"
#include "property_helper.hxx"
#include <Main/fg_props.hxx>
#include <stdexcept>
#include <string>
//------------------------------------------------------------------------------
void PropertyBasedMgr::init()
{
_props->addChangeListener(this);
canvas::triggerChangeRecursive(_props);
}
//------------------------------------------------------------------------------
void PropertyBasedMgr::shutdown()
{
_props->removeChangeListener(this);
}
//------------------------------------------------------------------------------
void PropertyBasedMgr::update(double delta_time_sec)
{
for( size_t i = 0; i < _elements.size(); ++i )
if( _elements[i] )
_elements[i]->update(delta_time_sec);
}
//------------------------------------------------------------------------------
void PropertyBasedMgr::childAdded( SGPropertyNode * parent,
SGPropertyNode * child )
{
if( parent != _props || child->getNameString() != _name_elements )
return;
size_t index = child->getIndex();
if( index >= _elements.size() )
{
if( index > _elements.size() )
SG_LOG
(
SG_GENERAL,
SG_WARN,
"Skipping unused " << _name_elements << " slot(s)!"
);
_elements.resize(index + 1);
}
else if( _elements[index] )
SG_LOG
(
SG_GENERAL,
SG_WARN,
_name_elements << "[" << index << "] already exists!"
);
PropertyBasedElementPtr el = _element_factory(child);
el->_self = el;
_elements[index] = el;
elementCreated( el );
}
//------------------------------------------------------------------------------
void PropertyBasedMgr::childRemoved( SGPropertyNode * parent,
SGPropertyNode * child )
{
if( parent != _props )
return canvas::triggerRemoveRecursive(child);
else if( child->getNameString() != _name_elements )
return;
size_t index = child->getIndex();
if( index >= _elements.size() )
SG_LOG
(
SG_GENERAL,
SG_WARN,
"can't removed unknown " << _name_elements << "[" << index << "]!"
);
else
// remove the element...
_elements[index].reset();
}
//------------------------------------------------------------------------------
const SGPropertyNode* PropertyBasedMgr::getPropertyRoot() const
{
return _props;
}
//------------------------------------------------------------------------------
PropertyBasedMgr::PropertyBasedMgr( const std::string& path_root,
const std::string& name_elements,
ElementFactory element_factory ):
_props( fgGetNode(path_root, true) ),
_name_elements( name_elements ),
_element_factory( element_factory )
{
}
//------------------------------------------------------------------------------
PropertyBasedMgr::~PropertyBasedMgr()
{
}

View file

@ -1,77 +0,0 @@
// Base class for all property controlled subsystems
//
// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef PROPERTY_BASED_MGR_HXX_
#define PROPERTY_BASED_MGR_HXX_
#include "property_based_element.hxx"
#include <simgear/structure/subsystem_mgr.hxx>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <vector>
class PropertyBasedMgr:
public SGSubsystem,
public SGPropertyChangeListener
{
public:
virtual void init();
virtual void shutdown();
virtual void update (double delta_time_sec);
virtual void childAdded( SGPropertyNode * parent,
SGPropertyNode * child );
virtual void childRemoved( SGPropertyNode * parent,
SGPropertyNode * child );
virtual void elementCreated(PropertyBasedElementPtr element) {}
virtual const SGPropertyNode* getPropertyRoot() const;
protected:
typedef boost::function<PropertyBasedElementPtr(SGPropertyNode*)>
ElementFactory;
/** Branch in the property tree for this property managed subsystem */
SGPropertyNode* _props;
/** Property name of managed elements */
const std::string _name_elements;
/** The actually managed elements */
std::vector<PropertyBasedElementPtr> _elements;
/** Function object which creates a new element */
ElementFactory _element_factory;
/**
* @param path_root Path to property branch used for controlling this
* subsystem
* @param name_elements The name of the nodes for the managed elements
*/
PropertyBasedMgr( const std::string& path_root,
const std::string& name_elements,
ElementFactory element_factory );
virtual ~PropertyBasedMgr() = 0;
};
#endif /* PROPERTY_BASED_MGR_HXX_ */

View file

@ -132,27 +132,4 @@ namespace canvas
for( size_t i = 0; i < num_channels; ++i )
nodes.push_back( getChildDefault(color, channels[i], def[i]) );
}
//----------------------------------------------------------------------------
void triggerChangeRecursive(SGPropertyNode* node)
{
node->getParent()->fireChildAdded(node);
if( node->nChildren() == 0 && node->getType() != simgear::props::NONE )
return node->fireValueChanged();
for( int i = 0; i < node->nChildren(); ++i )
triggerChangeRecursive( node->getChild(i) );
}
//----------------------------------------------------------------------------
void triggerRemoveRecursive(SGPropertyNode* node)
{
for( int i = 0; i < node->nChildren(); ++i )
{
SGPropertyNode* child = node->getChild(i);
node->fireChildRemoved( child );
triggerRemoveRecursive( child );
}
}
}

View file

@ -92,18 +92,6 @@ namespace canvas
std::vector<SGPropertyNode_ptr>& nodes,
const osg::Vec4& def = osg::Vec4(0,0,0,1) );
/**
* Trigger a childAdded and valueChanged event for every child of node
* (Unlimited depth) and node itself.
*/
void triggerChangeRecursive(SGPropertyNode* node);
/**
* Trigger a childRemoved event for every child of node (Unlimited depth) and
* node itself.
*/
void triggerRemoveRecursive(SGPropertyNode* node);
} // namespace canvas
#endif /* PROPERTY_HELPER_HXX_ */

View file

@ -19,10 +19,10 @@
#ifndef CANVAS_WINDOW_HXX_
#define CANVAS_WINDOW_HXX_
#include "property_based_element.hxx"
#include <Canvas/elements/CanvasImage.hxx>
#include <Canvas/MouseEvent.hxx>
#include <simgear/props/PropertyBasedElement.hxx>
#include <simgear/props/propertyObject.hxx>
#include <osg/Geode>
@ -31,7 +31,7 @@
namespace canvas
{
class Window:
public PropertyBasedElement
public simgear::PropertyBasedElement
{
public:
Window(SGPropertyNode* node);