Futher work on adding event and update callbacks to StateSet, Uniform and StateAttributes
This commit is contained in:
@@ -278,7 +278,7 @@ class OSG_EXPORT StateAttribute : public Object
|
||||
META_Object(osg,Callback);
|
||||
|
||||
/** do customized update code.*/
|
||||
virtual void operator () (NodeVisitor*, StateAttribute*) {}
|
||||
virtual void operator () (StateAttribute*, NodeVisitor*) {}
|
||||
};
|
||||
|
||||
/** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal.*/
|
||||
|
||||
@@ -392,7 +392,7 @@ class OSG_EXPORT StateSet : public Object
|
||||
META_Object(osg,Callback);
|
||||
|
||||
/** do customized callback code.*/
|
||||
virtual void operator() (NodeVisitor*, StateSet*) {}
|
||||
virtual void operator() (StateSet*, NodeVisitor*) {}
|
||||
};
|
||||
|
||||
/** Set the Update Callback which allows users to attach customize the updating of an object during the update traversal.*/
|
||||
@@ -404,9 +404,15 @@ class OSG_EXPORT StateSet : public Object
|
||||
/** Get the const Update Callback.*/
|
||||
const Callback* getUpdateCallback() const { return _updateCallback.get(); }
|
||||
|
||||
/** Return whether this StateSet has update callbacks associated with it, and therefore must be traversed.*/
|
||||
bool requiresUpdateTraversal() const { return _updateCallback.valid() || getNumChildrenRequiringUpdateTraversal()!=0; }
|
||||
|
||||
/** Get the number of Objects of this StateSet which require Update traversal,
|
||||
* since they have an Update Callback attached to them or their children.*/
|
||||
inline unsigned int getNumObjectsRequiringUpdateTraversal() const { return _numObjectsRequiringUpdateTraversal; }
|
||||
inline unsigned int getNumChildrenRequiringUpdateTraversal() const { return _numChildrenRequiringUpdateTraversal; }
|
||||
|
||||
/** Run the update callbacks attached directly to this StateSet or to its children.*/
|
||||
void runUpdateCallbacks(osg::NodeVisitor* nv);
|
||||
|
||||
|
||||
/** Set the Event Callback which allows users to attach customize the updating of an object during the event traversal.*/
|
||||
@@ -418,9 +424,15 @@ class OSG_EXPORT StateSet : public Object
|
||||
/** Get the const Event Callback.*/
|
||||
const Callback* getEventCallback() const { return _eventCallback.get(); }
|
||||
|
||||
/** Return whether this StateSet has event callbacks associated with it, and therefore must be traversed.*/
|
||||
bool requiresEventTraversal() const { return _eventCallback.valid() || getNumChildrenRequiringEventTraversal()!=0; }
|
||||
|
||||
/** Get the number of Objects of this StateSet which require Event traversal,
|
||||
* since they have an Eevnt Callback attached to them or their children.*/
|
||||
inline unsigned int getNumObjectsRequiringEventTraversal() const { return _numObjectsRequiringEventTraversal; }
|
||||
inline unsigned int getNumChildrenRequiringEventTraversal() const { return _numChildrenRequiringEventTraversal; }
|
||||
|
||||
/** Run the event callbacks attached directly to this StateSet or to its children.*/
|
||||
void runEventCallbacks(osg::NodeVisitor* nv);
|
||||
|
||||
|
||||
/** call compile on all StateAttributes contained within this StateSet.*/
|
||||
@@ -483,11 +495,13 @@ class OSG_EXPORT StateSet : public Object
|
||||
int _binNum;
|
||||
std::string _binName;
|
||||
|
||||
ref_ptr<Callback> _updateCallback;
|
||||
unsigned int _numObjectsRequiringUpdateTraversal;
|
||||
ref_ptr<Callback> _updateCallback;
|
||||
unsigned int _numChildrenRequiringUpdateTraversal;
|
||||
void setNumChildrenRequiringUpdateTraversal(unsigned int num);
|
||||
|
||||
ref_ptr<Callback> _eventCallback;
|
||||
unsigned int _numObjectsRequiringEventTraversal;
|
||||
ref_ptr<Callback> _eventCallback;
|
||||
unsigned int _numChildrenRequiringEventTraversal;
|
||||
void setNumChildrenRequiringEventTraversal(unsigned int num);
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -276,7 +276,7 @@ class OSG_EXPORT Uniform : public Object
|
||||
META_Object(osg,Callback);
|
||||
|
||||
/** do customized update code.*/
|
||||
virtual void operator () (NodeVisitor*, Uniform*) {}
|
||||
virtual void operator () (Uniform*, NodeVisitor*) {}
|
||||
};
|
||||
|
||||
/** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal.*/
|
||||
|
||||
@@ -93,8 +93,18 @@ class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor
|
||||
/** Prevent unwanted copy operator.*/
|
||||
EventVisitor& operator = (const EventVisitor&) { return *this; }
|
||||
|
||||
inline void handle_callbacks(osg::StateSet* stateset)
|
||||
{
|
||||
if (stateset && stateset->requiresEventTraversal())
|
||||
{
|
||||
stateset->runEventCallbacks(this);
|
||||
}
|
||||
}
|
||||
|
||||
inline void handle_callbacks_and_traverse(osg::Node& node)
|
||||
{
|
||||
handle_callbacks(node.getStateSet());
|
||||
|
||||
osg::NodeCallback* callback = node.getEventCallback();
|
||||
if (callback) (*callback)(&node,this);
|
||||
else if (node.getNumChildrenRequiringEventTraversal()>0) traverse(node);
|
||||
@@ -102,6 +112,8 @@ class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor
|
||||
|
||||
inline void handle_geode_callbacks(osg::Geode& node)
|
||||
{
|
||||
handle_callbacks(node.getStateSet());
|
||||
|
||||
osg::NodeCallback* callback = node.getEventCallback();
|
||||
if (callback) (*callback)(&node,this);
|
||||
/*else if (node.getNumChildrenRequiringEventTraversal()>0)*/
|
||||
@@ -117,6 +129,8 @@ class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor
|
||||
{
|
||||
osg::Drawable::EventCallback* callback = geode.getDrawable(i)->getEventCallback();
|
||||
if (callback) callback->event(this,geode.getDrawable(i));
|
||||
|
||||
handle_callbacks(geode.getDrawable(i)->getStateSet());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,33 +69,43 @@ class OSGUTIL_EXPORT UpdateVisitor : public osg::NodeVisitor
|
||||
/** Prevent unwanted copy operator.*/
|
||||
UpdateVisitor& operator = (const UpdateVisitor&) { return *this; }
|
||||
|
||||
inline void handle_callbacks(osg::StateSet* stateset)
|
||||
{
|
||||
if (stateset && stateset->requiresUpdateTraversal())
|
||||
{
|
||||
stateset->runUpdateCallbacks(this);
|
||||
}
|
||||
}
|
||||
|
||||
inline void handle_callbacks_and_traverse(osg::Node& node)
|
||||
{
|
||||
handle_callbacks(node.getStateSet());
|
||||
|
||||
osg::NodeCallback* callback = node.getUpdateCallback();
|
||||
if (callback) (*callback)(&node,this);
|
||||
else if (node.getNumChildrenRequiringUpdateTraversal()>0) traverse(node);
|
||||
}
|
||||
|
||||
inline void handle_geode_callbacks(osg::Geode& node)
|
||||
inline void handle_geode_callbacks(osg::Geode& geode)
|
||||
{
|
||||
osg::NodeCallback* callback = node.getUpdateCallback();
|
||||
if (callback) (*callback)(&node,this);
|
||||
/*else if (node.getNumChildrenRequiringUpdateTraversal()>0)*/
|
||||
traverseGeode(node);
|
||||
}
|
||||
|
||||
inline void traverseGeode(osg::Geode& geode)
|
||||
{
|
||||
traverse((osg::Node&)geode);
|
||||
|
||||
handle_callbacks(geode.getStateSet());
|
||||
|
||||
osg::NodeCallback* callback = geode.getUpdateCallback();
|
||||
if (callback) (*callback)(&geode,this);
|
||||
|
||||
// Call the app callbacks on the drawables.
|
||||
for(unsigned int i=0;i<geode.getNumDrawables();++i)
|
||||
{
|
||||
osg::Drawable::UpdateCallback* callback = geode.getDrawable(i)->getUpdateCallback();
|
||||
if (callback) callback->update(this,geode.getDrawable(i));
|
||||
|
||||
handle_callbacks(geode.getDrawable(i)->getStateSet());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// should we traverse just in case a subclass of Geode adds children?? Won't for now as
|
||||
// Geode's arn't designed to have children.
|
||||
// traverse(geode);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user