1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2018 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef PROPERTYMANAGER
15#define PROPERTYMANAGER 1
17#include <osg/UserDataContainer>
18#include <osg/ValueObject>
19#include <osg/ImageSequence>
20#include <osgGA/GUIEventHandler>
22#include <osgPresentation/Export>
26namespace osgPresentation
29class PropertyManager : protected osg::Object
34 PropertyManager(const PropertyManager& pm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
35 osg::Object(pm,copyop) {}
37 META_Object(osgPresentation, PropertyManager)
39 /** Convenience method that casts the named UserObject to osg::TemplateValueObject<T> and gets the value.
40 * To use this template method you need to include the osg/ValueObject header.*/
42 bool getProperty(const std::string& name, T& value) const
44 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
45 return getUserValue(name, value);
48 /** Convenience method that creates the osg::TemplateValueObject<T> to store the
49 * specified value and adds it as a named UserObject.
50 * To use this template method you need to include the osg/ValueObject header. */
52 void setProperty(const std::string& name, const T& value)
54 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
55 return setUserValue(name, value);
58 int ref() const { return osg::Referenced::ref(); }
59 int unref() const { return osg::Referenced::unref(); }
63 mutable OpenThreads::Mutex _mutex;
67extern OSGPRESENTATION_EXPORT const osg::Object* getUserObject(const osg::NodePath& nodepath, const std::string& name);
70bool getUserValue(const osg::NodePath& nodepath, const std::string& name, T& value)
72 typedef osg::TemplateValueObject<T> UserValueObject;
73 const osg::Object* object = getUserObject(nodepath, name);
74 const UserValueObject* uvo = dynamic_cast<const UserValueObject*>(object);
78 value = uvo->getValue();
87extern OSGPRESENTATION_EXPORT bool containsPropertyReference(const std::string& str);
91 PropertyReader(const osg::NodePath& nodePath, const std::string& str):
92 _errorGenerated(false),
100 while(!_sstream.fail() && _sstream.peek()==' ') _sstream.ignore();
102 // check to see if a &propertyName is used.
103 if (_sstream.peek()=='$')
105 std::string propertyName;
107 _sstream >> propertyName;
108 OSG_NOTICE<<"Reading propertyName="<<propertyName<<std::endl;
109 if (!_sstream.fail() && !propertyName.empty()) return getUserValue(_nodePath, propertyName, value);
115 OSG_NOTICE<<"Reading value="<<value<<std::endl;
116 return !_sstream.fail();
121 PropertyReader& operator>>( T& value ) { if (!read(value)) _errorGenerated=true; return *this; }
123 bool ok() { return !_sstream.fail() && !_errorGenerated; }
124 bool fail() { return _sstream.fail() || _errorGenerated; }
126 bool _errorGenerated;
127 osg::NodePath _nodePath;
128 std::istringstream _sstream;
132class OSGPRESENTATION_EXPORT PropertyAnimation : public osg::NodeCallback
141 void setPropertyManager(PropertyManager* pm) { _pm = pm; }
142 PropertyManager* getPropertyManager() const { return _pm.get(); }
144 typedef std::map<double, osg::ref_ptr<osg::UserDataContainer> > KeyFrameMap;
146 KeyFrameMap& getKeyFrameMap() { return _keyFrameMap; }
147 const KeyFrameMap& getKeyFrameMap() const { return _keyFrameMap; }
149 void addKeyFrame(double time, osg::UserDataContainer* udc)
151 _keyFrameMap[time] = udc;
154 virtual void reset();
156 void setPause(bool pause);
157 bool getPause() const { return _pause; }
159 double getAnimationTime() const;
161 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
163 virtual void update(osg::Node& node);
168 osg::ref_ptr<PropertyManager> _pm;
170 void assign(osg::UserDataContainer* destination, osg::UserDataContainer* source);
171 void assign(osg::UserDataContainer* udc, osg::Object* obj);
173 KeyFrameMap _keyFrameMap;
184struct OSGPRESENTATION_EXPORT ImageSequenceUpdateCallback : public osg::NodeCallback
186 ImageSequenceUpdateCallback(osg::ImageSequence* is, PropertyManager* pm, const std::string& propertyName):
188 _propertyManager(pm),
189 _propertyName(propertyName) {}
191 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
193 osg::ref_ptr<osg::ImageSequence> _imageSequence;
194 osg::ref_ptr<PropertyManager> _propertyManager;
195 std::string _propertyName;
198struct OSGPRESENTATION_EXPORT PropertyEventCallback : public osgGA::GUIEventHandler
200 PropertyEventCallback(PropertyManager* pm):
201 _propertyManager(pm) {}
203 virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
205 osg::ref_ptr<PropertyManager> _propertyManager;