OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PropertyManager.h
Go to the documentation of this file.
1 /* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield
2  *
3  * This software is open source and may be redistributed and/or modified under
4  * the terms of the GNU General Public License (GPL) version 2.0.
5  * The full license is in LICENSE.txt file included with this distribution,.
6  *
7  * This software is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * include LICENSE.txt for more details.
11 */
12 
13 #ifndef PROPERTYMANAGER
14 #define PROPERTYMANAGER 1
15 
16 #include <osg/UserDataContainer>
17 #include <osg/ValueObject>
18 #include <osg/ImageSequence>
19 #include <osgGA/GUIEventHandler>
20 
21 #include <osgPresentation/Export>
22 
23 #include <sstream>
24 
25 namespace osgPresentation
26 {
27 
28 class PropertyManager : protected osg::Object
29 {
30 public:
31 
34  osg::Object(pm,copyop) {}
35 
37 
38 
40  template<typename T>
41  bool getProperty(const std::string& name, T& value) const
42  {
44  return getUserValue(name, value);
45  }
46 
50  template<typename T>
51  void setProperty(const std::string& name, const T& value)
52  {
54  return setUserValue(name, value);
55  }
56 
57  int ref() const { return osg::Referenced::ref(); }
58  int unref() const { return osg::Referenced::unref(); }
59 
60 protected:
61 
63 
64 };
65 
66 extern OSGPRESENTATION_EXPORT const osg::Object* getUserObject(const osg::NodePath& nodepath, const std::string& name);
67 
68 template<typename T>
69 bool getUserValue(const osg::NodePath& nodepath, const std::string& name, T& value)
70 {
71  typedef osg::TemplateValueObject<T> UserValueObject;
72  const osg::Object* object = getUserObject(nodepath, name);
73  const UserValueObject* uvo = dynamic_cast<const UserValueObject*>(object);
74 
75  if (uvo)
76  {
77  value = uvo->getValue();
78  return true;
79  }
80  else
81  {
82  return false;
83  }
84 }
85 
86 extern OSGPRESENTATION_EXPORT bool containsPropertyReference(const std::string& str);
87 
89 {
90  PropertyReader(const osg::NodePath& nodePath, const std::string& str):
91  _errorGenerated(false),
92  _nodePath(nodePath),
93  _sstream(str) {}
94 
95  template<typename T>
96  bool read(T& value)
97  {
98  // skip white space.
99  while(!_sstream.fail() && _sstream.peek()==' ') _sstream.ignore();
100 
101  // check to see if a &propertyName is used.
102  if (_sstream.peek()=='$')
103  {
104  std::string propertyName;
105  _sstream.ignore(1);
106  _sstream >> propertyName;
107  OSG_NOTICE<<"Reading propertyName="<<propertyName<<std::endl;
108  if (!_sstream.fail() && !propertyName.empty()) return getUserValue(_nodePath, propertyName, value);
109  else return false;
110  }
111  else
112  {
113  _sstream >> value;
114  OSG_NOTICE<<"Reading value="<<value<<std::endl;
115  return !_sstream.fail();
116  }
117  }
118 
119  template<typename T>
120  PropertyReader& operator>>( T& value ) { if (!read(value)) _errorGenerated=true; return *this; }
121 
122  bool ok() { return !_sstream.fail() && !_errorGenerated; }
123  bool fail() { return _sstream.fail() || _errorGenerated; }
124 
127  std::istringstream _sstream;
128 };
129 
130 
132 {
133 public:
135  _firstTime(DBL_MAX),
136  _latestTime(0.0),
137  _pause(false),
138  _pauseTime(0.0) {}
139 
140  void setPropertyManager(PropertyManager* pm) { _pm = pm; }
141  PropertyManager* getPropertyManager() const { return _pm.get(); }
142 
143  typedef std::map<double, osg::ref_ptr<osg::UserDataContainer> > KeyFrameMap;
144 
145  KeyFrameMap& getKeyFrameMap() { return _keyFrameMap; }
146  const KeyFrameMap& getKeyFrameMap() const { return _keyFrameMap; }
147 
148  void addKeyFrame(double time, osg::UserDataContainer* udc)
149  {
150  _keyFrameMap[time] = udc;
151  }
152 
153  virtual void reset();
154 
155  void setPause(bool pause);
156  bool getPause() const { return _pause; }
157 
158  double getAnimationTime() const;
159 
160  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
161 
162  virtual void update(osg::Node& node);
163 
164 
165 protected:
166 
168 
169  void assign(osg::UserDataContainer* destination, osg::UserDataContainer* source);
170  void assign(osg::UserDataContainer* udc, osg::Object* obj);
171 
172  KeyFrameMap _keyFrameMap;
173 
174  double _firstTime;
175  double _latestTime;
176  bool _pause;
177  double _pauseTime;
178 
179 };
180 
181 
182 
184 {
185  ImageSequenceUpdateCallback(osg::ImageSequence* is, PropertyManager* pm, const std::string& propertyName):
186  _imageSequence(is),
187  _propertyManager(pm),
188  _propertyName(propertyName) {}
189 
190  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
191 
194  std::string _propertyName;
195 };
196 
198 {
200  _propertyManager(pm) {}
201 
202  virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
203 
205 };
206 
207 }
208 
209 #endif
bool getUserValue(const osg::NodePath &nodepath, const std::string &name, T &value)
#define OSG_NOTICE
Definition: Notify.h:86
This class provides an object-oriented thread mutex interface.
Definition: Mutex.h:31
void setProperty(const std::string &name, const T &value)
int unref() const
Definition: Referenced.h:175
void setUserValue(const std::string &name, const T &value)
Definition: ValueObject.h:209
void addKeyFrame(double time, osg::UserDataContainer *udc)
int ref() const
Definition: Referenced.h:158
PropertyManager(const PropertyManager &pm, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
#define OSGPRESENTATION_EXPORT
Definition: Export.h:38
PropertyReader(const osg::NodePath &nodePath, const std::string &str)
osg::ref_ptr< osg::ImageSequence > _imageSequence
const KeyFrameMap & getKeyFrameMap() const
osg::ref_ptr< PropertyManager > _propertyManager
#define META_Object(library, name)
Definition: Object.h:42
void setPropertyManager(PropertyManager *pm)
Object()
Definition: Object.h:65
osg::ref_ptr< PropertyManager > _propertyManager
osg::ref_ptr< PropertyManager > _pm
std::map< double, osg::ref_ptr< osg::UserDataContainer > > KeyFrameMap
PropertyReader & operator>>(T &value)
Definition: Node.h:71
Definition: AlphaFunc.h:19
OSGPRESENTATION_EXPORT bool containsPropertyReference(const std::string &str)
OSGPRESENTATION_EXPORT const osg::Object * getUserObject(const osg::NodePath &nodepath, const std::string &name)
ImageSequenceUpdateCallback(osg::ImageSequence *is, PropertyManager *pm, const std::string &propertyName)
PropertyManager * getPropertyManager() const
std::vector< Node * > NodePath
Definition: Node.h:44
bool getUserValue(const std::string &name, T &value) const