Callback
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 OSG_CALLBACK
#define OSG_CALLBACK 1
#include <osg/Object>
#include <osg/UserDataContainer>
// forward declare
namespace osgGA { class EventHandler; }
namespace osg {
// forward declare
class CallbackObject;
class NodeCallback;
class StateAttributeCallback;
class UniformCallback;
class DrawableUpdateCallback;
class DrawableEventCallback;
class DrawableCullCallback;
class OSG_EXPORT Callback : public virtual Object {
public :
Callback(){}
Callback(const Callback& cb,const CopyOp& copyop):
osg::Object(cb, copyop),
_nestedCallback(cb._nestedCallback) {}
META_Object(osg, Callback);
virtual Callback* asCallback() { return this; }
virtual const Callback* asCallback() const { return this; }
virtual CallbackObject* asCallbackObject() { return 0; }
virtual const CallbackObject* asCallbackObject() const { return 0; }
virtual NodeCallback* asNodeCallback() { return 0; }
virtual const NodeCallback* asNodeCallback() const { return 0; }
virtual StateAttributeCallback* asStateAttributeCallback() { return 0; }
virtual const StateAttributeCallback* asStateAttributeCallback() const { return 0; }
virtual UniformCallback* asUniformCallback() { return 0; }
virtual const UniformCallback* asUniformCallback() const { return 0; }
virtual DrawableUpdateCallback* asDrawableUpdateCallback() { return 0; }
virtual const DrawableUpdateCallback* asDrawableUpdateCallback() const { return 0; }
virtual DrawableEventCallback* asDrawableEventCallback() { return 0; }
virtual const DrawableEventCallback* asDrawableEventCallback() const { return 0; }
virtual DrawableCullCallback* asDrawableCullCallback() { return 0; }
virtual const DrawableCullCallback* asDrawableCullCallback() const { return 0; }
virtual osgGA::EventHandler* asEventHandler() { return 0; }
virtual const osgGA::EventHandler* asEventHandler() const { return 0; }
virtual bool run(osg::Object* object, osg::Object* data)
{
return traverse(object, data);
}
bool traverse(osg::Object* object, osg::Object* data);
void setNestedCallback(osg::Callback* cb) { _nestedCallback = cb; }
osg::Callback* getNestedCallback() { return _nestedCallback.get(); }
const osg::Callback* getNestedCallback() const { return _nestedCallback.get(); }
inline void addNestedCallback(osg::Callback* nc)
{
if (nc)
{
if (_nestedCallback.valid())
{
_nestedCallback->addNestedCallback(nc);
}
else
{
_nestedCallback = nc;
}
}
}
inline void removeNestedCallback(osg::Callback* nc)
{
if (nc)
{
if (_nestedCallback==nc)
{
ref_ptr<osg::Callback> new_nested_callback = _nestedCallback->getNestedCallback();
_nestedCallback->setNestedCallback(0);
_nestedCallback = new_nested_callback;
}
else if (_nestedCallback.valid())
{
_nestedCallback->removeNestedCallback(nc);
}
}
}
protected:
virtual ~Callback() {}
ref_ptr<Callback> _nestedCallback;
};
typedef std::vector< osg::ref_ptr<osg::Object> > Parameters;
class OSG_EXPORT CallbackObject : public virtual osg::Callback
{
public:
CallbackObject() {}
CallbackObject(const std::string& name) { setName(name); }
CallbackObject(const CallbackObject& co, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY):
osg::Object(co, copyop),
osg::Callback(co,copyop) {}
META_Object(osg, CallbackObject);
virtual CallbackObject* asCallbackObject() { return this; }
virtual const CallbackObject* asCallbackObject() const { return this; }
bool run(osg::Object* object, osg::Object* data);
inline bool run(osg::Object* object) const
{
osg::Parameters inputParameters;
osg::Parameters outputParameters;
return run(object, inputParameters, outputParameters);
}
virtual bool run(osg::Object* object, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const;
};
inline CallbackObject* getCallbackObject(osg::Object* object, const std::string& name)
{
osg::UserDataContainer* udc = object->getUserDataContainer();
if (!udc) return 0;
osg::Object* obj = udc->getUserObject(name);
if (!obj) return 0;
return obj->asCallbackObject();
}
inline const CallbackObject* getCallbackObject(const osg::Object* object, const std::string& name)
{
const osg::UserDataContainer* udc = object->getUserDataContainer();
if (!udc) return 0;
const osg::Object* obj = udc->getUserObject(name);
if (!obj) return 0;
return obj->asCallbackObject();
}
inline bool runNamedCallbackObjects(osg::Object* object, const std::string& name, osg::Parameters& inputParameters, osg::Parameters& outputParameters)
{
bool result = false;
osg::UserDataContainer* udc = object->getUserDataContainer();
if (udc)
{
for(unsigned int i = 0; i<udc->getNumUserObjects(); ++i)
{
osg::Object* obj = udc->getUserObject(i);
if (obj && obj->getName()==name)
{
osg::CallbackObject* co = obj->asCallbackObject();
if (co) result = co->run(object, inputParameters, outputParameters) | result;
}
}
}
return result;
}
// forward declare
class Node;
class NodeVisitor;
class OSG_EXPORT NodeCallback : public virtual Callback {
public :
NodeCallback(){}
NodeCallback(const NodeCallback& nc,const CopyOp& copyop):
Object(nc, copyop),
Callback(nc, copyop) {}
META_Object(osg,NodeCallback);
virtual NodeCallback* asNodeCallback() { return this; }
virtual const NodeCallback* asNodeCallback() const { return this; }
virtual bool run(osg::Object* object, osg::Object* data);
virtual void operator()(Node* node, NodeVisitor* nv);
protected:
virtual ~NodeCallback() {}
};
// forward declare
class StateAttribute;
class OSG_EXPORT StateAttributeCallback : public virtual osg::Callback
{
public:
StateAttributeCallback() {}
StateAttributeCallback(const StateAttributeCallback& org,const CopyOp& copyop) :
Object(org, copyop),
Callback(org, copyop) {}
META_Object(osg,StateAttributeCallback);
virtual StateAttributeCallback* asStateAttributeCallback() { return this; }
virtual const StateAttributeCallback* asStateAttributeCallback() const { return this; }
virtual bool run(osg::Object* object, osg::Object* data);
virtual void operator () (StateAttribute*, NodeVisitor*) {}
};
// forward declare
class Uniform;
class OSG_EXPORT UniformCallback : public virtual osg::Callback
{
public:
UniformCallback() {}
UniformCallback(const UniformCallback& org, const CopyOp& copyop) :
Object(org, copyop),
Callback(org, copyop) {}
META_Object(osg, UniformCallback);
virtual UniformCallback* asUniformCallback() { return this; }
virtual const UniformCallback* asUniformCallback() const { return this; }
virtual bool run(osg::Object* object, osg::Object* data);
virtual void operator () (Uniform*, NodeVisitor*) {}
};
// forward declare
class Drawable;
class State;
class RenderInfo;
class OSG_EXPORT DrawableUpdateCallback : public virtual Callback
{
public:
DrawableUpdateCallback() {}
DrawableUpdateCallback(const DrawableUpdateCallback& org,const CopyOp& copyop):
Object(org, copyop),
Callback(org, copyop) {}
META_Object(osg,DrawableUpdateCallback);
virtual DrawableUpdateCallback* asDrawableUpdateCallback() { return this; }
virtual const DrawableUpdateCallback* asDrawableUpdateCallback() const { return this; }
virtual bool run(osg::Object* object, osg::Object* data);
virtual void update(osg::NodeVisitor*, osg::Drawable*) {}
};
class OSG_EXPORT DrawableEventCallback : public virtual Callback
{
public:
DrawableEventCallback() {}
DrawableEventCallback(const DrawableEventCallback& org, const CopyOp& copyop) :
Object(org, copyop),
Callback(org, copyop) {}
META_Object(osg,DrawableEventCallback);
virtual DrawableEventCallback* asDrawableEventCallback() { return this; }
virtual const DrawableEventCallback* asDrawableEventCallback() const { return this; }
virtual bool run(osg::Object* object, osg::Object* data);
virtual void event(osg::NodeVisitor*, osg::Drawable*) {}
};
class OSG_EXPORT DrawableCullCallback : public virtual Callback
{
public:
DrawableCullCallback() {}
DrawableCullCallback(const DrawableCullCallback& org, const CopyOp& copyop) :
Object(org, copyop),
Callback(org, copyop) {}
META_Object(osg,DrawableCullCallback);
virtual DrawableCullCallback* asDrawableCullCallback() { return this; }
virtual const DrawableCullCallback* asDrawableCullCallback() const { return this; }
// just use the standard run implementation to passes run onto any nested callbacks.
using Callback::run;
virtual bool cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const { return false; }
virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const;
};
} // namespace
#endif
Object
UserDataContainer
osgGA::EventHandler
Definition EventHandler:34
osg::CallbackObject
Definition Callback:134
osg::CallbackObject::CallbackObject
CallbackObject()
Definition Callback:136
osg::CallbackObject::asCallbackObject
virtual CallbackObject * asCallbackObject()
Definition Callback:144
osg::CallbackObject::run
bool run(osg::Object *object, osg::Object *data)
osg::CallbackObject::asCallbackObject
virtual const CallbackObject * asCallbackObject() const
Definition Callback:145
osg::CallbackObject::META_Object
META_Object(osg, CallbackObject)
osg::CallbackObject::CallbackObject
CallbackObject(const std::string &name)
Definition Callback:137
osg::CallbackObject::run
bool run(osg::Object *object) const
Definition Callback:150
osg::CallbackObject::CallbackObject
CallbackObject(const CallbackObject &co, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY)
Definition Callback:138
osg::CallbackObject::run
virtual bool run(osg::Object *object, osg::Parameters &inputParameters, osg::Parameters &outputParameters) const
osg::Callback
Definition Callback:34
osg::Callback::asDrawableCullCallback
virtual DrawableCullCallback * asDrawableCullCallback()
Definition Callback:67
osg::Callback::asUniformCallback
virtual UniformCallback * asUniformCallback()
Definition Callback:58
osg::Callback::asCallbackObject
virtual CallbackObject * asCallbackObject()
Definition Callback:49
osg::Callback::asCallbackObject
virtual const CallbackObject * asCallbackObject() const
Definition Callback:50
osg::Callback::asCallback
virtual const Callback * asCallback() const
Definition Callback:47
osg::Callback::getNestedCallback
osg::Callback * getNestedCallback()
Definition Callback:89
osg::Callback::asEventHandler
virtual osgGA::EventHandler * asEventHandler()
Definition Callback:70
osg::Callback::asDrawableCullCallback
virtual const DrawableCullCallback * asDrawableCullCallback() const
Definition Callback:68
osg::Callback::addNestedCallback
void addNestedCallback(osg::Callback *nc)
Definition Callback:92
osg::Callback::traverse
bool traverse(osg::Object *object, osg::Object *data)
osg::Callback::asStateAttributeCallback
virtual const StateAttributeCallback * asStateAttributeCallback() const
Definition Callback:56
osg::Callback::_nestedCallback
ref_ptr< Callback > _nestedCallback
Definition Callback:127
osg::Callback::Callback
Callback()
Definition Callback:38
osg::Callback::asCallback
virtual Callback * asCallback()
Definition Callback:46
osg::Callback::asDrawableEventCallback
virtual DrawableEventCallback * asDrawableEventCallback()
Definition Callback:64
osg::Callback::asDrawableUpdateCallback
virtual const DrawableUpdateCallback * asDrawableUpdateCallback() const
Definition Callback:62
osg::Callback::asStateAttributeCallback
virtual StateAttributeCallback * asStateAttributeCallback()
Definition Callback:55
osg::Callback::run
virtual bool run(osg::Object *object, osg::Object *data)
Definition Callback:80
osg::Callback::Callback
Callback(const Callback &cb, const CopyOp &copyop)
Definition Callback:40
osg::Callback::getNestedCallback
const osg::Callback * getNestedCallback() const
Definition Callback:90
osg::Callback::asNodeCallback
virtual const NodeCallback * asNodeCallback() const
Definition Callback:53
osg::Callback::asDrawableUpdateCallback
virtual DrawableUpdateCallback * asDrawableUpdateCallback()
Definition Callback:61
osg::Callback::asNodeCallback
virtual NodeCallback * asNodeCallback()
Definition Callback:52
osg::Callback::asDrawableEventCallback
virtual const DrawableEventCallback * asDrawableEventCallback() const
Definition Callback:65
osg::Callback::asUniformCallback
virtual const UniformCallback * asUniformCallback() const
Definition Callback:59
osg::Callback::META_Object
META_Object(osg, Callback)
osg::Callback::~Callback
virtual ~Callback()
Definition Callback:126
osg::Callback::setNestedCallback
void setNestedCallback(osg::Callback *cb)
Definition Callback:88
osg::Callback::asEventHandler
virtual const osgGA::EventHandler * asEventHandler() const
Definition Callback:71
osg::Callback::removeNestedCallback
void removeNestedCallback(osg::Callback *nc)
Definition Callback:107
osg::CopyOp
Definition CopyOp:41
osg::CopyOp::SHALLOW_COPY
@ SHALLOW_COPY
Definition CopyOp:47
osg::DrawableCullCallback
Definition Callback:341
osg::DrawableCullCallback::asDrawableCullCallback
virtual const DrawableCullCallback * asDrawableCullCallback() const
Definition Callback:352
osg::DrawableCullCallback::META_Object
META_Object(osg, DrawableCullCallback)
osg::DrawableCullCallback::cull
virtual bool cull(osg::NodeVisitor *, osg::Drawable *, osg::State *) const
Definition Callback:358
osg::DrawableCullCallback::cull
virtual bool cull(osg::NodeVisitor *nv, osg::Drawable *drawable, osg::RenderInfo *renderInfo) const
osg::DrawableCullCallback::asDrawableCullCallback
virtual DrawableCullCallback * asDrawableCullCallback()
Definition Callback:351
osg::DrawableCullCallback::DrawableCullCallback
DrawableCullCallback()
Definition Callback:343
osg::DrawableCullCallback::DrawableCullCallback
DrawableCullCallback(const DrawableCullCallback &org, const CopyOp &copyop)
Definition Callback:345
osg::DrawableEventCallback
Definition Callback:320
osg::DrawableEventCallback::asDrawableEventCallback
virtual DrawableEventCallback * asDrawableEventCallback()
Definition Callback:330
osg::DrawableEventCallback::asDrawableEventCallback
virtual const DrawableEventCallback * asDrawableEventCallback() const
Definition Callback:331
osg::DrawableEventCallback::DrawableEventCallback
DrawableEventCallback(const DrawableEventCallback &org, const CopyOp &copyop)
Definition Callback:324
osg::DrawableEventCallback::event
virtual void event(osg::NodeVisitor *, osg::Drawable *)
Definition Callback:337
osg::DrawableEventCallback::META_Object
META_Object(osg, DrawableEventCallback)
osg::DrawableEventCallback::DrawableEventCallback
DrawableEventCallback()
Definition Callback:322
osg::DrawableEventCallback::run
virtual bool run(osg::Object *object, osg::Object *data)
osg::DrawableUpdateCallback
Definition Callback:298
osg::DrawableUpdateCallback::META_Object
META_Object(osg, DrawableUpdateCallback)
osg::DrawableUpdateCallback::DrawableUpdateCallback
DrawableUpdateCallback()
Definition Callback:300
osg::DrawableUpdateCallback::DrawableUpdateCallback
DrawableUpdateCallback(const DrawableUpdateCallback &org, const CopyOp &copyop)
Definition Callback:302
osg::DrawableUpdateCallback::asDrawableUpdateCallback
virtual const DrawableUpdateCallback * asDrawableUpdateCallback() const
Definition Callback:309
osg::DrawableUpdateCallback::asDrawableUpdateCallback
virtual DrawableUpdateCallback * asDrawableUpdateCallback()
Definition Callback:308
osg::DrawableUpdateCallback::run
virtual bool run(osg::Object *object, osg::Object *data)
osg::DrawableUpdateCallback::update
virtual void update(osg::NodeVisitor *, osg::Drawable *)
Definition Callback:315
osg::Drawable
Definition Drawable:89
osg::NodeCallback
Definition Callback:215
osg::NodeCallback::operator()
virtual void operator()(Node *node, NodeVisitor *nv)
osg::NodeCallback::NodeCallback
NodeCallback(const NodeCallback &nc, const CopyOp &copyop)
Definition Callback:221
osg::NodeCallback::run
virtual bool run(osg::Object *object, osg::Object *data)
osg::NodeCallback::NodeCallback
NodeCallback()
Definition Callback:219
osg::NodeCallback::asNodeCallback
virtual const NodeCallback * asNodeCallback() const
Definition Callback:228
osg::NodeCallback::META_Object
META_Object(osg, NodeCallback)
osg::NodeCallback::asNodeCallback
virtual NodeCallback * asNodeCallback()
Definition Callback:227
osg::NodeCallback::~NodeCallback
virtual ~NodeCallback()
Definition Callback:238
osg::NodeVisitor
Definition NodeVisitor:82
osg::Node
Definition Node:72
osg::Object
Definition Object:61
osg::Object::setName
virtual void setName(const std::string &name)
Definition Object:203
osg::Object::Object
Object()
Definition Object:69
osg::Object::asCallbackObject
virtual CallbackObject * asCallbackObject()
Definition Object:167
osg::Object::getUserDataContainer
osg::UserDataContainer * getUserDataContainer()
Definition Object:243
osg::Object::getName
const std::string & getName() const
Definition Object:213
osg::RenderInfo
Definition RenderInfo:28
osg::StateAttributeCallback
Definition Callback:246
osg::StateAttributeCallback::asStateAttributeCallback
virtual StateAttributeCallback * asStateAttributeCallback()
Definition Callback:256
osg::StateAttributeCallback::run
virtual bool run(osg::Object *object, osg::Object *data)
osg::StateAttributeCallback::StateAttributeCallback
StateAttributeCallback()
Definition Callback:248
osg::StateAttributeCallback::META_Object
META_Object(osg, StateAttributeCallback)
osg::StateAttributeCallback::asStateAttributeCallback
virtual const StateAttributeCallback * asStateAttributeCallback() const
Definition Callback:257
osg::StateAttributeCallback::StateAttributeCallback
StateAttributeCallback(const StateAttributeCallback &org, const CopyOp &copyop)
Definition Callback:250
osg::StateAttribute
Definition StateAttribute:77
osg::State
Definition State:80
osg::UniformCallback
Definition Callback:271
osg::UniformCallback::asUniformCallback
virtual UniformCallback * asUniformCallback()
Definition Callback:281
osg::UniformCallback::META_Object
META_Object(osg, UniformCallback)
osg::UniformCallback::UniformCallback
UniformCallback()
Definition Callback:273
osg::UniformCallback::asUniformCallback
virtual const UniformCallback * asUniformCallback() const
Definition Callback:282
osg::UniformCallback::run
virtual bool run(osg::Object *object, osg::Object *data)
osg::UniformCallback::UniformCallback
UniformCallback(const UniformCallback &org, const CopyOp &copyop)
Definition Callback:275
osg::Uniform
Definition Uniform:414
osg::UserDataContainer
Definition UserDataContainer:26
osg::UserDataContainer::getNumUserObjects
virtual unsigned int getNumUserObjects() const =0
osg::UserDataContainer::getUserObject
virtual Object * getUserObject(unsigned int i)=0
osg::ref_ptr
Definition ref_ptr:32
osgGA
Definition Callback:21
osg
Definition AlphaFunc:19
osg::runNamedCallbackObjects
bool runNamedCallbackObjects(osg::Object *object, const std::string &name, osg::Parameters &inputParameters, osg::Parameters &outputParameters)
Definition Callback:187
osg::getCallbackObject
CallbackObject * getCallbackObject(osg::Object *object, const std::string &name)
Definition Callback:162
osg::Parameters
std::vector< osg::ref_ptr< osg::Object > > Parameters
Definition Callback:130
OSG_EXPORT
#define OSG_EXPORT
Definition osg/Export:39