1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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.
18#include <osg/UserDataContainer>
21namespace osgGA { class EventHandler; }
28class StateAttributeCallback;
30class DrawableUpdateCallback;
31class DrawableEventCallback;
32class DrawableCullCallback;
34class OSG_EXPORT Callback : public virtual Object {
40 Callback(const Callback& cb,const CopyOp& copyop):
41 osg::Object(cb, copyop),
42 _nestedCallback(cb._nestedCallback) {}
44 META_Object(osg, Callback);
46 virtual Callback* asCallback() { return this; }
47 virtual const Callback* asCallback() const { return this; }
49 virtual CallbackObject* asCallbackObject() { return 0; }
50 virtual const CallbackObject* asCallbackObject() const { return 0; }
52 virtual NodeCallback* asNodeCallback() { return 0; }
53 virtual const NodeCallback* asNodeCallback() const { return 0; }
55 virtual StateAttributeCallback* asStateAttributeCallback() { return 0; }
56 virtual const StateAttributeCallback* asStateAttributeCallback() const { return 0; }
58 virtual UniformCallback* asUniformCallback() { return 0; }
59 virtual const UniformCallback* asUniformCallback() const { return 0; }
61 virtual DrawableUpdateCallback* asDrawableUpdateCallback() { return 0; }
62 virtual const DrawableUpdateCallback* asDrawableUpdateCallback() const { return 0; }
64 virtual DrawableEventCallback* asDrawableEventCallback() { return 0; }
65 virtual const DrawableEventCallback* asDrawableEventCallback() const { return 0; }
67 virtual DrawableCullCallback* asDrawableCullCallback() { return 0; }
68 virtual const DrawableCullCallback* asDrawableCullCallback() const { return 0; }
70 virtual osgGA::EventHandler* asEventHandler() { return 0; }
71 virtual const osgGA::EventHandler* asEventHandler() const { return 0; }
73 /** Invoke the callback, first parameter is the Object that the callback is attached to,
74 * the second parameter, the data, is typically the NodeVisitor that is invoking the callback.
75 * The run(..) method may be overridden by users directly, or if the user is using one of the old
76 * style callbacks such as NodeCallback or Drawable::UpdateCallback then you can just override
77 * the appropriate callback method on those callback subclasses.
78 * If you are implementing your own callback then one should call traverse() to make sure nested callbacks
79 * and visitor traversal() is completed. */
80 virtual bool run(osg::Object* object, osg::Object* data)
82 return traverse(object, data);
85 /** traverse the nested callbacks or call NodeVisitor::traverse() if the object is Node, and data is NodeVisitor.*/
86 bool traverse(osg::Object* object, osg::Object* data);
88 void setNestedCallback(osg::Callback* cb) { _nestedCallback = cb; }
89 osg::Callback* getNestedCallback() { return _nestedCallback.get(); }
90 const osg::Callback* getNestedCallback() const { return _nestedCallback.get(); }
92 inline void addNestedCallback(osg::Callback* nc)
96 if (_nestedCallback.valid())
98 _nestedCallback->addNestedCallback(nc);
102 _nestedCallback = nc;
107 inline void removeNestedCallback(osg::Callback* nc)
111 if (_nestedCallback==nc)
113 ref_ptr<osg::Callback> new_nested_callback = _nestedCallback->getNestedCallback();
114 _nestedCallback->setNestedCallback(0);
115 _nestedCallback = new_nested_callback;
117 else if (_nestedCallback.valid())
119 _nestedCallback->removeNestedCallback(nc);
126 virtual ~Callback() {}
127 ref_ptr<Callback> _nestedCallback;
130typedef std::vector< osg::ref_ptr<osg::Object> > Parameters;
132/** Callback for attaching a script to a Node's via there UserDataContainer for the purpose of overriding class methods within scripts.*/
133class OSG_EXPORT CallbackObject : public virtual osg::Callback
137 CallbackObject(const std::string& name) { setName(name); }
138 CallbackObject(const CallbackObject& co, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY):
139 osg::Object(co, copyop),
140 osg::Callback(co,copyop) {}
142 META_Object(osg, CallbackObject);
144 virtual CallbackObject* asCallbackObject() { return this; }
145 virtual const CallbackObject* asCallbackObject() const { return this; }
147 /** override Callback::run() entry point to adapt to CallbackObject::run(..) method.*/
148 bool run(osg::Object* object, osg::Object* data);
150 inline bool run(osg::Object* object) const
152 osg::Parameters inputParameters;
153 osg::Parameters outputParameters;
154 return run(object, inputParameters, outputParameters);
157 virtual bool run(osg::Object* object, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const;
161/** Convenience function for getting the CallbackObject associated with specified name from an Object's UserDataContainer.*/
162inline CallbackObject* getCallbackObject(osg::Object* object, const std::string& name)
164 osg::UserDataContainer* udc = object->getUserDataContainer();
167 osg::Object* obj = udc->getUserObject(name);
170 return obj->asCallbackObject();
174/** Convenience function for getting the CallbackObject associated with specified name from an Object's UserDataContainer.*/
175inline const CallbackObject* getCallbackObject(const osg::Object* object, const std::string& name)
177 const osg::UserDataContainer* udc = object->getUserDataContainer();
180 const osg::Object* obj = udc->getUserObject(name);
183 return obj->asCallbackObject();
186/** Call run(..) on named CallbackObjects attached to specified Object. Return true if at least one CallbackObject has been successfully invoked.*/
187inline bool runNamedCallbackObjects(osg::Object* object, const std::string& name, osg::Parameters& inputParameters, osg::Parameters& outputParameters)
190 osg::UserDataContainer* udc = object->getUserDataContainer();
193 for(unsigned int i = 0; i<udc->getNumUserObjects(); ++i)
195 osg::Object* obj = udc->getUserObject(i);
196 if (obj && obj->getName()==name)
198 osg::CallbackObject* co = obj->asCallbackObject();
199 if (co) result = co->run(object, inputParameters, outputParameters) | result;
215class OSG_EXPORT NodeCallback : public virtual Callback {
221 NodeCallback(const NodeCallback& nc,const CopyOp& copyop):
223 Callback(nc, copyop) {}
225 META_Object(osg,NodeCallback);
227 virtual NodeCallback* asNodeCallback() { return this; }
228 virtual const NodeCallback* asNodeCallback() const { return this; }
230 /** NodeCallback overrides the Callback::run() method to adapt it the old style NodeCallback::operator()(Node* node, NodeVisitor* nv) method.*/
231 virtual bool run(osg::Object* object, osg::Object* data);
233 /** Callback method called by the NodeVisitor when visiting a node.*/
234 virtual void operator()(Node* node, NodeVisitor* nv);
238 virtual ~NodeCallback() {}
245class OSG_EXPORT StateAttributeCallback : public virtual osg::Callback
248 StateAttributeCallback() {}
250 StateAttributeCallback(const StateAttributeCallback& org,const CopyOp& copyop) :
252 Callback(org, copyop) {}
254 META_Object(osg,StateAttributeCallback);
256 virtual StateAttributeCallback* asStateAttributeCallback() { return this; }
257 virtual const StateAttributeCallback* asStateAttributeCallback() const { return this; }
259 /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
260 virtual bool run(osg::Object* object, osg::Object* data);
262 /** do customized update code.*/
263 virtual void operator () (StateAttribute*, NodeVisitor*) {}
270class OSG_EXPORT UniformCallback : public virtual osg::Callback
275 UniformCallback(const UniformCallback& org, const CopyOp& copyop) :
277 Callback(org, copyop) {}
279 META_Object(osg, UniformCallback);
281 virtual UniformCallback* asUniformCallback() { return this; }
282 virtual const UniformCallback* asUniformCallback() const { return this; }
284 /** override Callback::run() entry point to adapt to UniformCallback::run(..) method.*/
285 virtual bool run(osg::Object* object, osg::Object* data);
287 /** do customized update code.*/
288 virtual void operator () (Uniform*, NodeVisitor*) {}
297class OSG_EXPORT DrawableUpdateCallback : public virtual Callback
300 DrawableUpdateCallback() {}
302 DrawableUpdateCallback(const DrawableUpdateCallback& org,const CopyOp& copyop):
304 Callback(org, copyop) {}
306 META_Object(osg,DrawableUpdateCallback);
308 virtual DrawableUpdateCallback* asDrawableUpdateCallback() { return this; }
309 virtual const DrawableUpdateCallback* asDrawableUpdateCallback() const { return this; }
311 /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
312 virtual bool run(osg::Object* object, osg::Object* data);
314 /** do customized update code.*/
315 virtual void update(osg::NodeVisitor*, osg::Drawable*) {}
319class OSG_EXPORT DrawableEventCallback : public virtual Callback
322 DrawableEventCallback() {}
324 DrawableEventCallback(const DrawableEventCallback& org, const CopyOp& copyop) :
326 Callback(org, copyop) {}
328 META_Object(osg,DrawableEventCallback);
330 virtual DrawableEventCallback* asDrawableEventCallback() { return this; }
331 virtual const DrawableEventCallback* asDrawableEventCallback() const { return this; }
333 /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
334 virtual bool run(osg::Object* object, osg::Object* data);
336 /** do customized Event code. */
337 virtual void event(osg::NodeVisitor*, osg::Drawable*) {}
340class OSG_EXPORT DrawableCullCallback : public virtual Callback
343 DrawableCullCallback() {}
345 DrawableCullCallback(const DrawableCullCallback& org, const CopyOp& copyop) :
347 Callback(org, copyop) {}
349 META_Object(osg,DrawableCullCallback);
351 virtual DrawableCullCallback* asDrawableCullCallback() { return this; }
352 virtual const DrawableCullCallback* asDrawableCullCallback() const { return this; }
354 // just use the standard run implementation to passes run onto any nested callbacks.
358 virtual bool cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const { return false; }
360 /** do customized cull code, return true if drawable should be culled.*/
361 virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const;