Futher work on adding event and update callbacks to StateSet, Uniform and StateAttributes

This commit is contained in:
Robert Osfield 2005-04-25 11:05:02 +00:00
parent 193c83cb9c
commit bc83e63bb4
8 changed files with 220 additions and 30 deletions

View File

@ -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.*/

View File

@ -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.*/
@ -484,10 +496,12 @@ class OSG_EXPORT StateSet : public Object
std::string _binName;
ref_ptr<Callback> _updateCallback;
unsigned int _numObjectsRequiringUpdateTraversal;
unsigned int _numChildrenRequiringUpdateTraversal;
void setNumChildrenRequiringUpdateTraversal(unsigned int num);
ref_ptr<Callback> _eventCallback;
unsigned int _numObjectsRequiringEventTraversal;
unsigned int _numChildrenRequiringEventTraversal;
void setNumChildrenRequiringEventTraversal(unsigned int num);
};

View File

@ -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.*/

View File

@ -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());
}
}

View File

@ -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);
}
handle_callbacks(geode.getStateSet());
inline void traverseGeode(osg::Geode& geode)
{
traverse((osg::Node&)geode);
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);
}
};
}

View File

@ -312,14 +312,53 @@ void Drawable::setStateSet(osg::StateSet* stateset)
// do nothing if nothing changed.
if (_stateset==stateset) return;
// track whether we need to account for the need to do a update or event traversal.
int delta_update = 0;
int delta_event = 0;
// remove this node from the current statesets parent list
if (_stateset.valid()) _stateset->removeParent(this);
if (_stateset.valid())
{
_stateset->removeParent(this);
if (_stateset->requiresUpdateTraversal()) --delta_update;
if (_stateset->requiresEventTraversal()) --delta_event;
}
// set the stateset.
_stateset = stateset;
// add this node to the new stateset to the parent list.
if (_stateset.valid()) _stateset->addParent(this);
if (_stateset.valid())
{
_stateset->addParent(this);
if (_stateset->requiresUpdateTraversal()) ++delta_update;
if (_stateset->requiresEventTraversal()) ++delta_event;
}
// only inform parents if change occurs and drawable doesn't already have an update callback
if (delta_update!=0 && !_updateCallback)
{
for(ParentList::iterator itr=_parents.begin();
itr!=_parents.end();
++itr)
{
(*itr)->setNumChildrenRequiringUpdateTraversal((*itr)->getNumChildrenRequiringUpdateTraversal()+delta_update);
}
}
// only inform parents if change occurs and drawable doesn't already have an event callback
if (delta_event!=0 && !_eventCallback)
{
for(ParentList::iterator itr=_parents.begin();
itr!=_parents.end();
++itr)
{
(*itr)->setNumChildrenRequiringEventTraversal((*itr)->getNumChildrenRequiringEventTraversal()+delta_event);
}
}
}
osg::StateSet* Drawable::getOrCreateStateSet()
@ -502,7 +541,7 @@ void Drawable::setUpdateCallback(UpdateCallback* ac)
_updateCallback = ac;
if (delta!=0)
if (delta!=0 && !(_stateset.valid() && _stateset->requiresUpdateTraversal()))
{
for(ParentList::iterator itr=_parents.begin();
itr!=_parents.end();
@ -523,7 +562,7 @@ void Drawable::setEventCallback(EventCallback* ac)
_eventCallback = ac;
if (delta!=0)
if (delta!=0 && !(_stateset.valid() && _stateset->requiresEventTraversal()))
{
for(ParentList::iterator itr=_parents.begin();
itr!=_parents.end();

View File

@ -91,14 +91,38 @@ void Node::setStateSet(osg::StateSet* stateset)
// do nothing if nothing changed.
if (_stateset==stateset) return;
// track whether we need to account for the need to do a update or event traversal.
int delta_update = 0;
int delta_event = 0;
// remove this node from the current statesets parent list
if (_stateset.valid()) _stateset->removeParent(this);
if (_stateset.valid())
{
_stateset->removeParent(this);
if (_stateset->requiresUpdateTraversal()) --delta_update;
if (_stateset->requiresEventTraversal()) --delta_event;
}
// set the stateset.
_stateset = stateset;
// add this node to the new stateset to the parent list.
if (_stateset.valid()) _stateset->addParent(this);
if (_stateset.valid())
{
_stateset->addParent(this);
if (_stateset->requiresUpdateTraversal()) ++delta_update;
if (_stateset->requiresEventTraversal()) ++delta_event;
}
if (delta_update!=0)
{
setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+delta_update);
}
if (delta_event!=0)
{
setNumChildrenRequiringEventTraversal(getNumChildrenRequiringEventTraversal()+delta_event);
}
}
osg::StateSet* Node::getOrCreateStateSet()
@ -113,7 +137,7 @@ void Node::setUpdateCallback(NodeCallback* nc)
// if no changes just return.
if (_updateCallback==nc) return;
// app callback has been changed, will need to update
// updated callback has been changed, will need to update
// both _updateCallback and possibly the numChildrenRequiringAppTraversal
// if the number of callbacks changes.
@ -191,7 +215,7 @@ void Node::setEventCallback(NodeCallback* nc)
// if no changes just return.
if (_eventCallback==nc) return;
// app callback has been changed, will need to Event
// event callback has been changed, will need to Event
// both _EventCallback and possibly the numChildrenRequiringAppTraversal
// if the number of callbacks changes.

View File

@ -73,6 +73,9 @@ StateSet::StateSet()
_renderingHint = DEFAULT_BIN;
_numChildrenRequiringUpdateTraversal = 0;
_numChildrenRequiringEventTraversal = 0;
setRenderBinToInherit();
}
@ -141,6 +144,10 @@ StateSet::StateSet(const StateSet& rhs,const CopyOp& copyop):Object(rhs,copyop)
_binMode = rhs._binMode;
_binNum = rhs._binNum;
_binName = rhs._binName;
_numChildrenRequiringUpdateTraversal = rhs._numChildrenRequiringUpdateTraversal;
_numChildrenRequiringEventTraversal = rhs._numChildrenRequiringEventTraversal;
}
StateSet::~StateSet()
@ -1243,6 +1250,47 @@ void StateSet::setUpdateCallback(Callback* ac)
}
}
void StateSet::runUpdateCallbacks(osg::NodeVisitor* nv)
{
if (_updateCallback.valid()) (*_updateCallback)(this,nv);
if (_numChildrenRequiringUpdateTraversal!=0)
{
// run attribute callbacks
for(AttributeList::iterator itr=_attributeList.begin();
itr!=_attributeList.end();
++itr)
{
StateAttribute::Callback* callback = itr->second.first->getUpdateCallback();
if (callback) (*callback)(itr->second.first.get(),nv);
}
// run texture attribute callbacks.
for(unsigned int i=0;i<_textureAttributeList.size();++i)
{
AttributeList& attributeList = _textureAttributeList[i];
for(AttributeList::iterator itr=attributeList.begin();
itr!=attributeList.end();
++itr)
{
StateAttribute::Callback* callback = itr->second.first->getUpdateCallback();
if (callback) (*callback)(itr->second.first.get(),nv);
}
}
// run uniform callbacks.
for(UniformList::iterator uitr = _uniformList.begin();
uitr != _uniformList.end();
++uitr)
{
Uniform::Callback* callback = uitr->second.first->getUpdateCallback();
if (callback) (*callback)(uitr->second.first.get(),nv);
}
}
}
void StateSet::setEventCallback(Callback* ac)
{
if (_eventCallback==ac) return;
@ -1265,3 +1313,44 @@ void StateSet::setEventCallback(Callback* ac)
#endif
}
}
void StateSet::runEventCallbacks(osg::NodeVisitor* nv)
{
if (_eventCallback.valid()) (*_eventCallback)(this,nv);
if (_numChildrenRequiringEventTraversal!=0)
{
// run attribute callbacks
for(AttributeList::iterator itr=_attributeList.begin();
itr!=_attributeList.end();
++itr)
{
StateAttribute::Callback* callback = itr->second.first->getEventCallback();
if (callback) (*callback)(itr->second.first.get(),nv);
}
// run texture attribute callbacks.
for(unsigned int i=0;i<_textureAttributeList.size();++i)
{
AttributeList& attributeList = _textureAttributeList[i];
for(AttributeList::iterator itr=attributeList.begin();
itr!=attributeList.end();
++itr)
{
StateAttribute::Callback* callback = itr->second.first->getEventCallback();
if (callback) (*callback)(itr->second.first.get(),nv);
}
}
// run uniform callbacks.
for(UniformList::iterator uitr = _uniformList.begin();
uitr != _uniformList.end();
++uitr)
{
Uniform::Callback* callback = uitr->second.first->getEventCallback();
if (callback) (*callback)(uitr->second.first.get(),nv);
}
}
}