PropertyManager
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2018 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library 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
* OpenSceneGraph Public License for more details.
*/
#ifndef PROPERTYMANAGER
#define PROPERTYMANAGER 1
#include <osg/UserDataContainer>
#include <osg/ValueObject>
#include <osg/ImageSequence>
#include <osgGA/GUIEventHandler>
#include <osgPresentation/Export>
#include <sstream>
namespace osgPresentation
{
class PropertyManager : protected osg::Object
{
public:
PropertyManager() {}
PropertyManager(const PropertyManager& pm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
osg::Object(pm,copyop) {}
META_Object(osgPresentation, PropertyManager)
template<typename T>
bool getProperty(const std::string& name, T& value) const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
return getUserValue(name, value);
}
template<typename T>
void setProperty(const std::string& name, const T& value)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
return setUserValue(name, value);
}
int ref() const { return osg::Referenced::ref(); }
int unref() const { return osg::Referenced::unref(); }
protected:
mutable OpenThreads::Mutex _mutex;
};
extern OSGPRESENTATION_EXPORT const osg::Object* getUserObject(const osg::NodePath& nodepath, const std::string& name);
template<typename T>
bool getUserValue(const osg::NodePath& nodepath, const std::string& name, T& value)
{
typedef osg::TemplateValueObject<T> UserValueObject;
const osg::Object* object = getUserObject(nodepath, name);
const UserValueObject* uvo = dynamic_cast<const UserValueObject*>(object);
if (uvo)
{
value = uvo->getValue();
return true;
}
else
{
return false;
}
}
extern OSGPRESENTATION_EXPORT bool containsPropertyReference(const std::string& str);
struct PropertyReader
{
PropertyReader(const osg::NodePath& nodePath, const std::string& str):
_errorGenerated(false),
_nodePath(nodePath),
_sstream(str) {}
template<typename T>
bool read(T& value)
{
// skip white space.
while(!_sstream.fail() && _sstream.peek()==' ') _sstream.ignore();
// check to see if a &propertyName is used.
if (_sstream.peek()=='$')
{
std::string propertyName;
_sstream.ignore(1);
_sstream >> propertyName;
OSG_NOTICE<<"Reading propertyName="<<propertyName<<std::endl;
if (!_sstream.fail() && !propertyName.empty()) return getUserValue(_nodePath, propertyName, value);
else return false;
}
else
{
_sstream >> value;
OSG_NOTICE<<"Reading value="<<value<<std::endl;
return !_sstream.fail();
}
}
template<typename T>
PropertyReader& operator>>( T& value ) { if (!read(value)) _errorGenerated=true; return *this; }
bool ok() { return !_sstream.fail() && !_errorGenerated; }
bool fail() { return _sstream.fail() || _errorGenerated; }
bool _errorGenerated;
osg::NodePath _nodePath;
std::istringstream _sstream;
};
class OSGPRESENTATION_EXPORT PropertyAnimation : public osg::NodeCallback
{
public:
PropertyAnimation():
_firstTime(DBL_MAX),
_latestTime(0.0),
_pause(false),
_pauseTime(0.0) {}
void setPropertyManager(PropertyManager* pm) { _pm = pm; }
PropertyManager* getPropertyManager() const { return _pm.get(); }
typedef std::map<double, osg::ref_ptr<osg::UserDataContainer> > KeyFrameMap;
KeyFrameMap& getKeyFrameMap() { return _keyFrameMap; }
const KeyFrameMap& getKeyFrameMap() const { return _keyFrameMap; }
void addKeyFrame(double time, osg::UserDataContainer* udc)
{
_keyFrameMap[time] = udc;
}
virtual void reset();
void setPause(bool pause);
bool getPause() const { return _pause; }
double getAnimationTime() const;
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
virtual void update(osg::Node& node);
protected:
osg::ref_ptr<PropertyManager> _pm;
void assign(osg::UserDataContainer* destination, osg::UserDataContainer* source);
void assign(osg::UserDataContainer* udc, osg::Object* obj);
KeyFrameMap _keyFrameMap;
double _firstTime;
double _latestTime;
bool _pause;
double _pauseTime;
};
struct OSGPRESENTATION_EXPORT ImageSequenceUpdateCallback : public osg::NodeCallback
{
ImageSequenceUpdateCallback(osg::ImageSequence* is, PropertyManager* pm, const std::string& propertyName):
_imageSequence(is),
_propertyManager(pm),
_propertyName(propertyName) {}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
osg::ref_ptr<osg::ImageSequence> _imageSequence;
osg::ref_ptr<PropertyManager> _propertyManager;
std::string _propertyName;
};
struct OSGPRESENTATION_EXPORT PropertyEventCallback : public osgGA::GUIEventHandler
{
PropertyEventCallback(PropertyManager* pm):
_propertyManager(pm) {}
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
osg::ref_ptr<PropertyManager> _propertyManager;
};
}
#endif
GUIEventHandler
ImageSequence
OSG_NOTICE
#define OSG_NOTICE
Definition Notify:86
UserDataContainer
ValueObject
OpenThreads::Mutex
This class provides an object-oriented thread mutex interface.
Definition Mutex:31
OpenThreads::ScopedLock
Definition ScopedLock:25
osgGA::GUIActionAdapter
Definition GUIActionAdapter:62
osgGA::GUIEventAdapter
Definition GUIEventAdapter:82
osgGA::GUIEventHandler
Definition GUIEventHandler:50
osgPresentation::PropertyAnimation::_firstTime
double _firstTime
Definition PropertyManager:175
osgPresentation::PropertyAnimation::getPropertyManager
PropertyManager * getPropertyManager() const
Definition PropertyManager:142
osgPresentation::PropertyAnimation::_latestTime
double _latestTime
Definition PropertyManager:176
osgPresentation::PropertyAnimation::assign
void assign(osg::UserDataContainer *destination, osg::UserDataContainer *source)
osgPresentation::PropertyAnimation::getKeyFrameMap
const KeyFrameMap & getKeyFrameMap() const
Definition PropertyManager:147
osgPresentation::PropertyAnimation::update
virtual void update(osg::Node &node)
osgPresentation::PropertyAnimation::getKeyFrameMap
KeyFrameMap & getKeyFrameMap()
Definition PropertyManager:146
osgPresentation::PropertyAnimation::_keyFrameMap
KeyFrameMap _keyFrameMap
Definition PropertyManager:173
osgPresentation::PropertyAnimation::setPropertyManager
void setPropertyManager(PropertyManager *pm)
Definition PropertyManager:141
osgPresentation::PropertyAnimation::getPause
bool getPause() const
Definition PropertyManager:157
osgPresentation::PropertyAnimation::PropertyAnimation
PropertyAnimation()
Definition PropertyManager:135
osgPresentation::PropertyAnimation::_pauseTime
double _pauseTime
Definition PropertyManager:178
osgPresentation::PropertyAnimation::_pause
bool _pause
Definition PropertyManager:177
osgPresentation::PropertyAnimation::operator()
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
osgPresentation::PropertyAnimation::_pm
osg::ref_ptr< PropertyManager > _pm
Definition PropertyManager:168
osgPresentation::PropertyAnimation::setPause
void setPause(bool pause)
osgPresentation::PropertyAnimation::reset
virtual void reset()
osgPresentation::PropertyAnimation::KeyFrameMap
std::map< double, osg::ref_ptr< osg::UserDataContainer > > KeyFrameMap
Definition PropertyManager:144
osgPresentation::PropertyAnimation::addKeyFrame
void addKeyFrame(double time, osg::UserDataContainer *udc)
Definition PropertyManager:149
osgPresentation::PropertyAnimation::assign
void assign(osg::UserDataContainer *udc, osg::Object *obj)
osgPresentation::PropertyAnimation::getAnimationTime
double getAnimationTime() const
osgPresentation::PropertyManager
Definition PropertyManager:30
osgPresentation::PropertyManager::unref
int unref() const
Definition PropertyManager:59
osgPresentation::PropertyManager::META_Object
META_Object(osgPresentation, PropertyManager) template< typename T > bool getProperty(const std
Definition PropertyManager:37
osgPresentation::PropertyManager::PropertyManager
PropertyManager(const PropertyManager &pm, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
Definition PropertyManager:34
osgPresentation::PropertyManager::_mutex
OpenThreads::Mutex _mutex
Definition PropertyManager:63
osgPresentation::PropertyManager::PropertyManager
PropertyManager()
Definition PropertyManager:33
osgPresentation::PropertyManager::ref
int ref() const
Definition PropertyManager:58
osg::CopyOp
Definition CopyOp:41
osg::CopyOp::SHALLOW_COPY
@ SHALLOW_COPY
Definition CopyOp:47
osg::ImageSequence
Definition ImageSequence:29
osg::NodeCallback
Definition Callback:215
osg::NodeVisitor
Definition NodeVisitor:82
osg::Node
Definition Node:72
osg::Object
Definition Object:61
osg::Object::setUserValue
void setUserValue(const std::string &name, const T &value)
Definition ValueObject:377
osg::Object::Object
Object()
Definition Object:69
osg::Object::getUserValue
bool getUserValue(const std::string &name, T &value) const
osg::Referenced::unref
int unref() const
Definition Referenced:178
osg::Referenced::ref
int ref() const
Definition Referenced:161
osg::TemplateValueObject
Definition ValueObject:283
osg::UserDataContainer
Definition UserDataContainer:26
osg::ref_ptr
Definition ref_ptr:32
osgPresentation
Definition AnimationMaterial:26
osgPresentation::getUserValue
bool getUserValue(const osg::NodePath &nodepath, const std::string &name, T &value)
Definition PropertyManager:70
osgPresentation::getUserObject
OSGPRESENTATION_EXPORT const osg::Object * getUserObject(const osg::NodePath &nodepath, const std::string &name)
osgPresentation::containsPropertyReference
OSGPRESENTATION_EXPORT bool containsPropertyReference(const std::string &str)
osg
Definition AlphaFunc:19
osg::NodePath
std::vector< Node * > NodePath
Definition Node:47
Export
OSGPRESENTATION_EXPORT
#define OSGPRESENTATION_EXPORT
Definition osgPresentation/Export:38
osgPresentation::ImageSequenceUpdateCallback::ImageSequenceUpdateCallback
ImageSequenceUpdateCallback(osg::ImageSequence *is, PropertyManager *pm, const std::string &propertyName)
Definition PropertyManager:186
osgPresentation::ImageSequenceUpdateCallback::_imageSequence
osg::ref_ptr< osg::ImageSequence > _imageSequence
Definition PropertyManager:193
osgPresentation::ImageSequenceUpdateCallback::operator()
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
osgPresentation::ImageSequenceUpdateCallback::_propertyName
std::string _propertyName
Definition PropertyManager:195
osgPresentation::ImageSequenceUpdateCallback::_propertyManager
osg::ref_ptr< PropertyManager > _propertyManager
Definition PropertyManager:194
osgPresentation::PropertyEventCallback::handle
virtual bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &)
osgPresentation::PropertyEventCallback::_propertyManager
osg::ref_ptr< PropertyManager > _propertyManager
Definition PropertyManager:205
osgPresentation::PropertyEventCallback::PropertyEventCallback
PropertyEventCallback(PropertyManager *pm)
Definition PropertyManager:200
osgPresentation::PropertyReader::operator>>
PropertyReader & operator>>(T &value)
Definition PropertyManager:121
osgPresentation::PropertyReader::_nodePath
osg::NodePath _nodePath
Definition PropertyManager:127
osgPresentation::PropertyReader::PropertyReader
PropertyReader(const osg::NodePath &nodePath, const std::string &str)
Definition PropertyManager:91
osgPresentation::PropertyReader::ok
bool ok()
Definition PropertyManager:123
osgPresentation::PropertyReader::read
bool read(T &value)
Definition PropertyManager:97
osgPresentation::PropertyReader::_sstream
std::istringstream _sstream
Definition PropertyManager:128
osgPresentation::PropertyReader::fail
bool fail()
Definition PropertyManager:124
osgPresentation::PropertyReader::_errorGenerated
bool _errorGenerated
Definition PropertyManager:126