Refactored Callback system in osg::Node, osg::Drawable, osg::StateSet and osg::StateAttribute to use a new osg::Callback base class.
git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14244 16af8721-9629-0410-8352-f15c8da7e697
This commit is contained in:
@@ -21,7 +21,7 @@
|
||||
#include <osg/Matrixf>
|
||||
#include <osg/Matrixd>
|
||||
#include <osg/Quat>
|
||||
#include <osg/NodeCallback>
|
||||
#include <osg/Callback>
|
||||
|
||||
namespace osg {
|
||||
|
||||
|
||||
202
include/osg/Callback
Normal file
202
include/osg/Callback
Normal file
@@ -0,0 +1,202 @@
|
||||
/* -*-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>
|
||||
|
||||
namespace osg {
|
||||
|
||||
|
||||
class Callback : public virtual Object {
|
||||
|
||||
public :
|
||||
|
||||
Callback(){}
|
||||
|
||||
Callback(const Callback& cb,const CopyOp&):
|
||||
_nestedCallback(cb._nestedCallback) {}
|
||||
|
||||
META_Object(osg, Callback);
|
||||
|
||||
/** Invoke the callback, first parameter is the Object that the callback is attached to,
|
||||
* the second parameter, the data, is typically the NodeVisitor that is invoking the callback.
|
||||
* The run(..) method may be overriden by users directly, or if the user is using one of the old
|
||||
* style callbacks such as NodeCallback or Drawable::UpdateCallback then you can just override
|
||||
* the appropriate callback method on those callback subclasses.
|
||||
* If you are implementing your own callback then one should call traverse() to make sure nested callbacks
|
||||
* and visitor traversal() is completed. */
|
||||
virtual bool run(osg::Object* object, osg::Object* data)
|
||||
{
|
||||
return traverse(object, data);
|
||||
}
|
||||
|
||||
/** traverse the nested callbacks or call NodeVisitor::traverse() if the object is Node, and data is NodeVisitor.*/
|
||||
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;
|
||||
|
||||
/** Callback for attaching a script to a Node's via there UserDataContainer for the purpose of overriding class methods within scripts.*/
|
||||
class OSG_EXPORT CallbackObject : public virtual osg::Callback
|
||||
{
|
||||
public:
|
||||
CallbackObject() {}
|
||||
CallbackObject(const std::string& name) { setName(name); }
|
||||
CallbackObject(const CallbackObject& rhs, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY):osg::Callback(rhs,copyop) {}
|
||||
META_Object(osg, CallbackObject);
|
||||
|
||||
virtual CallbackObject* asCallbackObject() { return this; }
|
||||
virtual const CallbackObject* asCallbackObject() const { return this; }
|
||||
|
||||
/** override Callback::run() entry point to adapt to CallbackObject::run(..) method.*/
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
/** Convinience function for getting the CallbackObject associated with specificed name from an Object's UserDataContainer.*/
|
||||
inline CallbackObject* getCallbackObject(osg::Object* object, const std::string& name)
|
||||
{
|
||||
osg::UserDataContainer* udc = object->getUserDataContainer();
|
||||
return udc ? dynamic_cast<osg::CallbackObject*>(udc->getUserObject(name)) : 0;
|
||||
}
|
||||
|
||||
/** Call run(..) on named CallbackObjects attached to specified Object. Return true if at least one CallbackObject has been successfully invoked.*/
|
||||
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 = dynamic_cast<osg::CallbackObject*>(obj);
|
||||
if (co) result = co->run(object, inputParameters, outputParameters) | result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// forward declare
|
||||
class Node;
|
||||
class NodeVisitor;
|
||||
|
||||
|
||||
/** Deprecated. */
|
||||
class OSG_EXPORT NodeCallback : public virtual Callback {
|
||||
|
||||
public :
|
||||
|
||||
|
||||
NodeCallback(){}
|
||||
|
||||
NodeCallback(const NodeCallback& nc,const CopyOp& copyop):
|
||||
Callback(nc,copyop) {}
|
||||
|
||||
META_Object(osg,NodeCallback);
|
||||
|
||||
/** NodeCallback overrides the Callback::run() method to adapt it the old style NodeCallback::operator()(Node* node, NodeVisitor* nv) method.*/
|
||||
virtual bool run(osg::Object* object, osg::Object* data);
|
||||
|
||||
/** Callback method called by the NodeVisitor when visiting a node.*/
|
||||
virtual void operator()(Node* node, NodeVisitor* nv);
|
||||
|
||||
protected:
|
||||
|
||||
virtual ~NodeCallback() {}
|
||||
};
|
||||
|
||||
// forward declare
|
||||
class StateAttribute;
|
||||
|
||||
/** Deprecated. */
|
||||
class OSG_EXPORT StateAttributeCallback : public virtual osg::Callback
|
||||
{
|
||||
public:
|
||||
StateAttributeCallback() {}
|
||||
|
||||
StateAttributeCallback(const StateAttributeCallback&,const CopyOp&) {}
|
||||
|
||||
META_Object(osg,StateAttributeCallback);
|
||||
|
||||
/** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
|
||||
virtual bool run(osg::Object* object, osg::Object* data);
|
||||
|
||||
/** do customized update code.*/
|
||||
virtual void operator () (StateAttribute*, NodeVisitor*) {}
|
||||
};
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
#define OSG_CLUSTERCULLINGCALLBACK 1
|
||||
|
||||
#include <osg/Drawable>
|
||||
#include <osg/NodeCallback>
|
||||
#include <osg/Callback>
|
||||
|
||||
namespace osg {
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ class Drawable;
|
||||
class Array;
|
||||
class PrimitiveSet;
|
||||
class Shape;
|
||||
class NodeCallback;
|
||||
class Callback;
|
||||
|
||||
|
||||
/** Copy Op(erator) used to control whether shallow or deep copy is used
|
||||
@@ -80,7 +80,7 @@ class OSG_EXPORT CopyOp
|
||||
virtual PrimitiveSet* operator() (const PrimitiveSet* primitives) const;
|
||||
virtual Shape* operator() (const Shape* shape) const;
|
||||
virtual Uniform* operator() (const Uniform* shape) const;
|
||||
virtual NodeCallback* operator() (const NodeCallback* nodecallback) const;
|
||||
virtual Callback* operator() (const Callback* nodecallback) const;
|
||||
virtual StateAttributeCallback* operator() (const StateAttributeCallback* stateattributecallback) const;
|
||||
|
||||
protected:
|
||||
|
||||
@@ -268,7 +268,7 @@ class OSG_EXPORT Drawable : public Node
|
||||
* for all graphics contexts. */
|
||||
virtual void releaseGLObjects(State* state=0) const;
|
||||
|
||||
struct UpdateCallback : public virtual osg::Object
|
||||
struct OSG_EXPORT UpdateCallback : public virtual Callback
|
||||
{
|
||||
UpdateCallback() {}
|
||||
|
||||
@@ -276,24 +276,15 @@ class OSG_EXPORT Drawable : public Node
|
||||
|
||||
META_Object(osg,UpdateCallback);
|
||||
|
||||
/** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
|
||||
virtual bool run(osg::Object* object, osg::Object* data);
|
||||
|
||||
/** do customized update code.*/
|
||||
virtual void update(osg::NodeVisitor*, osg::Drawable*) {}
|
||||
};
|
||||
|
||||
/** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal. */
|
||||
virtual void setUpdateCallback(UpdateCallback* ac);
|
||||
|
||||
/** Get the non const UpdateCallback.*/
|
||||
UpdateCallback* getUpdateCallback() { return _drawableUpdateCallback.get(); }
|
||||
|
||||
/** Get the const UpdateCallback.*/
|
||||
const UpdateCallback* getUpdateCallback() const { return _drawableUpdateCallback.get(); }
|
||||
|
||||
/** Return whether this Drawable has update callbacks associated with it, and therefore must be traversed.*/
|
||||
bool requiresUpdateTraversal() const { return _drawableUpdateCallback.valid() || (_stateset.valid() && _stateset->requiresUpdateTraversal()); }
|
||||
|
||||
|
||||
struct EventCallback : public virtual osg::Object
|
||||
struct OSG_EXPORT EventCallback : public virtual Callback
|
||||
{
|
||||
EventCallback() {}
|
||||
|
||||
@@ -301,24 +292,14 @@ class OSG_EXPORT Drawable : public Node
|
||||
|
||||
META_Object(osg,EventCallback);
|
||||
|
||||
/** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
|
||||
virtual bool run(osg::Object* object, osg::Object* data);
|
||||
|
||||
/** do customized Event code. */
|
||||
virtual void event(osg::NodeVisitor*, osg::Drawable*) {}
|
||||
};
|
||||
|
||||
/** Set the EventCallback which allows users to attach customize the updating of an object during the Event traversal.*/
|
||||
virtual void setEventCallback(EventCallback* ac);
|
||||
|
||||
/** Get the non const EventCallback.*/
|
||||
EventCallback* getEventCallback() { return _drawableEventCallback.get(); }
|
||||
|
||||
/** Get the const EventCallback.*/
|
||||
const EventCallback* getEventCallback() const { return _drawableEventCallback.get(); }
|
||||
|
||||
/** Return whether this Drawable has event callbacks associated with it, and therefore must be traversed.*/
|
||||
bool requiresEventTraversal() const { return _drawableEventCallback.valid() || (_stateset.valid() && _stateset->requiresEventTraversal()); }
|
||||
|
||||
|
||||
struct CullCallback : public virtual osg::Object
|
||||
struct CullCallback : public virtual Callback
|
||||
{
|
||||
CullCallback() {}
|
||||
|
||||
@@ -333,17 +314,6 @@ class OSG_EXPORT Drawable : public Node
|
||||
virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const { return cull(nv, drawable, renderInfo? renderInfo->getState():0); }
|
||||
};
|
||||
|
||||
/** Set the CullCallback which allows users to customize the culling of Drawable during the cull traversal.*/
|
||||
virtual void setCullCallback(CullCallback* cc) { _drawableCullCallback=cc; }
|
||||
|
||||
/** Get the non const CullCallback.*/
|
||||
CullCallback* getCullCallback() { return _drawableCullCallback.get(); }
|
||||
|
||||
/** Get the const CullCallback.*/
|
||||
const CullCallback* getCullCallback() const { return _drawableCullCallback.get(); }
|
||||
|
||||
|
||||
|
||||
|
||||
/** Callback attached to an Drawable which allows the users to customize the drawing of an exist Drawable object.
|
||||
* The draw callback is implement as a replacement to the Drawable's own drawImplementation() method, if the
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
#include <osg/StateSet>
|
||||
#include <osg/BoundingSphere>
|
||||
#include <osg/BoundingBox>
|
||||
#include <osg/NodeCallback>
|
||||
#include <osg/Callback>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -202,16 +202,16 @@ class OSG_EXPORT Node : public Object
|
||||
|
||||
|
||||
/** Set update node callback, called during update traversal. */
|
||||
void setUpdateCallback(NodeCallback* nc);
|
||||
void setUpdateCallback(Callback* nc);
|
||||
|
||||
/** Get update node callback, called during update traversal. */
|
||||
inline NodeCallback* getUpdateCallback() { return _updateCallback.get(); }
|
||||
inline Callback* getUpdateCallback() { return _updateCallback.get(); }
|
||||
|
||||
/** Get const update node callback, called during update traversal. */
|
||||
inline const NodeCallback* getUpdateCallback() const { return _updateCallback.get(); }
|
||||
inline const Callback* getUpdateCallback() const { return _updateCallback.get(); }
|
||||
|
||||
/** Convenience method that sets the update callback of the node if it doesn't exist, or nest it into the existing one. */
|
||||
inline void addUpdateCallback(NodeCallback* nc) {
|
||||
inline void addUpdateCallback(Callback* nc) {
|
||||
if (nc != NULL) {
|
||||
if (_updateCallback.valid()) _updateCallback->addNestedCallback(nc);
|
||||
else setUpdateCallback(nc);
|
||||
@@ -219,7 +219,7 @@ class OSG_EXPORT Node : public Object
|
||||
}
|
||||
|
||||
/** Convenience method that removes a given callback from a node, even if that callback is nested. There is no error return in case the given callback is not found. */
|
||||
inline void removeUpdateCallback(NodeCallback* nc) {
|
||||
inline void removeUpdateCallback(Callback* nc) {
|
||||
if (nc != NULL && _updateCallback.valid()) {
|
||||
if (_updateCallback == nc)
|
||||
{
|
||||
@@ -236,16 +236,16 @@ class OSG_EXPORT Node : public Object
|
||||
|
||||
|
||||
/** Set event node callback, called during event traversal. */
|
||||
void setEventCallback(NodeCallback* nc);
|
||||
void setEventCallback(Callback* nc);
|
||||
|
||||
/** Get event node callback, called during event traversal. */
|
||||
inline NodeCallback* getEventCallback() { return _eventCallback.get(); }
|
||||
inline Callback* getEventCallback() { return _eventCallback.get(); }
|
||||
|
||||
/** Get const event node callback, called during event traversal. */
|
||||
inline const NodeCallback* getEventCallback() const { return _eventCallback.get(); }
|
||||
inline const Callback* getEventCallback() const { return _eventCallback.get(); }
|
||||
|
||||
/** Convenience method that sets the event callback of the node if it doesn't exist, or nest it into the existing one. */
|
||||
inline void addEventCallback(NodeCallback* nc) {
|
||||
inline void addEventCallback(Callback* nc) {
|
||||
if (nc != NULL) {
|
||||
if (_eventCallback.valid()) _eventCallback->addNestedCallback(nc);
|
||||
else setEventCallback(nc);
|
||||
@@ -253,7 +253,7 @@ class OSG_EXPORT Node : public Object
|
||||
}
|
||||
|
||||
/** Convenience method that removes a given callback from a node, even if that callback is nested. There is no error return in case the given callback is not found. */
|
||||
inline void removeEventCallback(NodeCallback* nc) {
|
||||
inline void removeEventCallback(Callback* nc) {
|
||||
if (nc != NULL && _eventCallback.valid()) {
|
||||
if (_eventCallback == nc)
|
||||
{
|
||||
@@ -270,16 +270,16 @@ class OSG_EXPORT Node : public Object
|
||||
|
||||
|
||||
/** Set cull node callback, called during cull traversal. */
|
||||
void setCullCallback(NodeCallback* nc) { _cullCallback = nc; }
|
||||
void setCullCallback(Callback* nc) { _cullCallback = nc; }
|
||||
|
||||
/** Get cull node callback, called during cull traversal. */
|
||||
inline NodeCallback* getCullCallback() { return _cullCallback.get(); }
|
||||
inline Callback* getCullCallback() { return _cullCallback.get(); }
|
||||
|
||||
/** Get const cull node callback, called during cull traversal. */
|
||||
inline const NodeCallback* getCullCallback() const { return _cullCallback.get(); }
|
||||
inline const Callback* getCullCallback() const { return _cullCallback.get(); }
|
||||
|
||||
/** Convenience method that sets the cull callback of the node if it doesn't exist, or nest it into the existing one. */
|
||||
inline void addCullCallback(NodeCallback* nc) {
|
||||
inline void addCullCallback(Callback* nc) {
|
||||
if (nc != NULL) {
|
||||
if (_cullCallback.valid()) _cullCallback->addNestedCallback(nc);
|
||||
else setCullCallback(nc);
|
||||
@@ -287,7 +287,7 @@ class OSG_EXPORT Node : public Object
|
||||
}
|
||||
|
||||
/** Convenience method that removes a given callback from a node, even if that callback is nested. There is no error return in case the given callback is not found. */
|
||||
inline void removeCullCallback(NodeCallback* nc) {
|
||||
inline void removeCullCallback(Callback* nc) {
|
||||
if (nc != NULL && _cullCallback.valid()) {
|
||||
if (_cullCallback == nc)
|
||||
{
|
||||
@@ -477,15 +477,15 @@ class OSG_EXPORT Node : public Object
|
||||
friend class osg::Drawable;
|
||||
friend class osg::StateSet;
|
||||
|
||||
ref_ptr<NodeCallback> _updateCallback;
|
||||
ref_ptr<Callback> _updateCallback;
|
||||
unsigned int _numChildrenRequiringUpdateTraversal;
|
||||
void setNumChildrenRequiringUpdateTraversal(unsigned int num);
|
||||
|
||||
ref_ptr<NodeCallback> _eventCallback;
|
||||
ref_ptr<Callback> _eventCallback;
|
||||
unsigned int _numChildrenRequiringEventTraversal;
|
||||
void setNumChildrenRequiringEventTraversal(unsigned int num);
|
||||
|
||||
ref_ptr<NodeCallback> _cullCallback;
|
||||
ref_ptr<Callback> _cullCallback;
|
||||
|
||||
bool _cullingActive;
|
||||
unsigned int _numChildrenWithCullingDisabled;
|
||||
|
||||
@@ -14,86 +14,7 @@
|
||||
#ifndef OSG_NODECALLBACK
|
||||
#define OSG_NODECALLBACK 1
|
||||
|
||||
#include <osg/Object>
|
||||
#include <osg/ref_ptr>
|
||||
|
||||
namespace osg {
|
||||
|
||||
class Node;
|
||||
class NodeVisitor;
|
||||
|
||||
class OSG_EXPORT NodeCallback : public virtual Object {
|
||||
|
||||
public :
|
||||
|
||||
|
||||
NodeCallback(){}
|
||||
|
||||
NodeCallback(const NodeCallback& nc,const CopyOp&):
|
||||
_nestedCallback(nc._nestedCallback) {}
|
||||
|
||||
|
||||
META_Object(osg,NodeCallback);
|
||||
|
||||
|
||||
/** Callback method called by the NodeVisitor when visiting a node.*/
|
||||
virtual void operator()(Node* node, NodeVisitor* nv)
|
||||
{
|
||||
// note, callback is responsible for scenegraph traversal so
|
||||
// they must call traverse(node,nv) to ensure that the
|
||||
// scene graph subtree (and associated callbacks) are traversed.
|
||||
traverse(node,nv);
|
||||
}
|
||||
|
||||
/** Call any nested callbacks and then traverse the scene graph. */
|
||||
void traverse(Node* node,NodeVisitor* nv);
|
||||
|
||||
void setNestedCallback(NodeCallback* nc) { _nestedCallback = nc; }
|
||||
NodeCallback* getNestedCallback() { return _nestedCallback.get(); }
|
||||
const NodeCallback* getNestedCallback() const { return _nestedCallback.get(); }
|
||||
|
||||
inline void addNestedCallback(NodeCallback* nc)
|
||||
{
|
||||
if (nc)
|
||||
{
|
||||
if (_nestedCallback.valid())
|
||||
{
|
||||
_nestedCallback->addNestedCallback(nc);
|
||||
}
|
||||
else
|
||||
{
|
||||
_nestedCallback = nc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void removeNestedCallback(NodeCallback* nc)
|
||||
{
|
||||
if (nc)
|
||||
{
|
||||
if (_nestedCallback==nc)
|
||||
{
|
||||
ref_ptr<NodeCallback> new_nested_callback = _nestedCallback->getNestedCallback();
|
||||
_nestedCallback->setNestedCallback(0);
|
||||
_nestedCallback = new_nested_callback;
|
||||
}
|
||||
else if (_nestedCallback.valid())
|
||||
{
|
||||
_nestedCallback->removeNestedCallback(nc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
ref_ptr<NodeCallback> _nestedCallback;
|
||||
|
||||
protected:
|
||||
|
||||
virtual ~NodeCallback() {}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
#include <osg/Callback>
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
#include <iterator>
|
||||
|
||||
#include <osg/Node>
|
||||
#include <osg/NodeCallback>
|
||||
#include <osg/ObserverNodePath>
|
||||
|
||||
namespace osg
|
||||
|
||||
@@ -15,20 +15,16 @@
|
||||
#define OSG_SCRIPTENGINE 1
|
||||
|
||||
#include <osg/Object>
|
||||
#include <osg/NodeCallback>
|
||||
#include <osg/Callback>
|
||||
#include <osg/NodeVisitor>
|
||||
#include <osg/UserDataContainer>
|
||||
|
||||
namespace osg
|
||||
{
|
||||
|
||||
typedef std::vector< osg::ref_ptr<osg::Object> > Parameters;
|
||||
|
||||
|
||||
// forward declare
|
||||
class ScriptEngine;
|
||||
|
||||
|
||||
/* Script class for wrapping a script and the language used in the script.*/
|
||||
class Script : public osg::Object
|
||||
{
|
||||
@@ -57,53 +53,6 @@ class Script : public osg::Object
|
||||
unsigned int _modifiedCount;
|
||||
};
|
||||
|
||||
/** Callback for attaching a script to a Node's via there UserDataContainer for the purpose of overriding class methods within scripts.*/
|
||||
class OSG_EXPORT CallbackObject : public osg::Object
|
||||
{
|
||||
public:
|
||||
CallbackObject() {}
|
||||
CallbackObject(const std::string& name) { setName(name); }
|
||||
CallbackObject(const CallbackObject& rhs, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY):osg::Object(rhs,copyop) {}
|
||||
META_Object(osg, CallbackObject);
|
||||
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
/** Convinience function for getting the CallbackObject associated with specificed name from an Object's UserDataContainer.*/
|
||||
inline CallbackObject* getCallbackObject(osg::Object* object, const std::string& name)
|
||||
{
|
||||
osg::UserDataContainer* udc = object->getUserDataContainer();
|
||||
return udc ? dynamic_cast<osg::CallbackObject*>(udc->getUserObject(name)) : 0;
|
||||
}
|
||||
|
||||
/** Call run(..) on named CallbackObjects attached to specified Object. Return true if at least one CallbackObject has been successfully invoked.*/
|
||||
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 = dynamic_cast<osg::CallbackObject*>(obj);
|
||||
if (co) result = co->run(object, inputParameters, outputParameters) | result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/** NodeCallback for attaching a script to a NodeCallback so that it can be called as an update or event callback.*/
|
||||
class OSG_EXPORT ScriptNodeCallback : public osg::NodeCallback
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
#include <osg/Export>
|
||||
#include <osg/Object>
|
||||
#include <osg/StateAttributeCallback>
|
||||
#include <osg/Callback>
|
||||
#include <osg/Shader>
|
||||
#include <osg/GL>
|
||||
|
||||
|
||||
@@ -13,27 +13,6 @@
|
||||
#ifndef OSG_STATEATTRIBUTECALLBACK
|
||||
#define OSG_STATEATTRIBUTECALLBACK 1
|
||||
|
||||
#include <osg/Export>
|
||||
#include <osg/Object>
|
||||
|
||||
namespace osg {
|
||||
|
||||
class StateAttribute;
|
||||
class NodeVisitor;
|
||||
|
||||
class OSG_EXPORT StateAttributeCallback : public virtual osg::Object
|
||||
{
|
||||
public:
|
||||
StateAttributeCallback() {}
|
||||
|
||||
StateAttributeCallback(const StateAttributeCallback&,const CopyOp&) {}
|
||||
|
||||
META_Object(osg,StateAttributeCallback);
|
||||
|
||||
/** do customized update code.*/
|
||||
virtual void operator () (StateAttribute*, NodeVisitor*) {}
|
||||
};
|
||||
|
||||
}
|
||||
#include <osg/Callback>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -378,7 +378,7 @@ class OSG_EXPORT StateSet : public Object
|
||||
inline bool getNestRenderBins() const { return _nestRenderBins; }
|
||||
|
||||
|
||||
struct Callback : public virtual osg::Object
|
||||
struct Callback : public virtual osg::Callback
|
||||
{
|
||||
Callback() {}
|
||||
|
||||
@@ -386,6 +386,9 @@ class OSG_EXPORT StateSet : public Object
|
||||
|
||||
META_Object(osg,Callback);
|
||||
|
||||
/** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
|
||||
virtual bool run(osg::Object* object, osg::Object* data);
|
||||
|
||||
/** do customized callback code.*/
|
||||
virtual void operator() (StateSet*, NodeVisitor*) {}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user