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:
Robert Osfield
2014-06-05 16:26:13 +00:00
parent 35d6cb812f
commit 977ec20751
64 changed files with 590 additions and 471 deletions

View File

@@ -37,6 +37,7 @@ SET(TARGET_H
${HEADER_PATH}/buffered_value
${HEADER_PATH}/BufferIndexBinding
${HEADER_PATH}/BufferObject
${HEADER_PATH}/Callback
${HEADER_PATH}/Camera
${HEADER_PATH}/CameraView
${HEADER_PATH}/ClampColor
@@ -230,6 +231,7 @@ SET(TARGET_SRC
BlendFunc.cpp
BufferIndexBinding.cpp
BufferObject.cpp
Callback.cpp
Camera.cpp
CameraView.cpp
ClampColor.cpp
@@ -296,7 +298,6 @@ SET(TARGET_SRC
# Matrix_implementation.cpp
MatrixTransform.cpp
Multisample.cpp
NodeCallback.cpp
Node.cpp
NodeTrackerCallback.cpp
NodeVisitor.cpp

109
src/osg/Callback.cpp Normal file
View File

@@ -0,0 +1,109 @@
/* -*-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.
*/
#include <osg/Node>
#include <osg/NodeVisitor>
#include <osg/ScriptEngine>
using namespace osg;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Callback
//
bool Callback::traverse(Object* object, Object* data)
{
if (_nestedCallback.valid()) return run(object, data);
else
{
#if 1
osg::Node* node = dynamic_cast<osg::Node*>(object);
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data);
#else
osg::Node* node = object ? data->asNode() : 0;
osg::NodeVisitor* nv = data ? data->asNodeVisitor() : 0;
#endif
if (node && nv)
{
nv->traverse(*node);
return true;
}
else return false;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// CallbackObject
//
bool CallbackObject::run(osg::Object* object, osg::Object* data)
{
osg::Parameters inputParameters, outputParameters;
if (data && data->referenceCount()>=1)
{
inputParameters.push_back(data);
}
return run(object,inputParameters, outputParameters);
}
bool CallbackObject::run(osg::Object* object, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
OSG_NOTICE<<"CallbackObject::run(object="<<object<<")"<<std::endl;
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// NodeCallback
//
bool NodeCallback::run(osg::Object* object, osg::Object* data)
{
osg::Node* node = dynamic_cast<osg::Node*>(object);
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data);
if (node && nv)
{
operator()(node, nv);
return true;
}
else
{
return traverse(object, data);
}
}
void NodeCallback::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);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// StateAttributeCallback
//
bool StateAttributeCallback::run(osg::Object* object, osg::Object* data)
{
osg::StateAttribute* sa = dynamic_cast<osg::StateAttribute*>(object);
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data);
if (sa && nv)
{
operator()(sa, nv);
return true;
}
else
{
return traverse(object, data);
}
}

View File

@@ -19,6 +19,7 @@
#include <osg/PrimitiveSet>
#include <osg/Shape>
#include <osg/StateAttribute>
#include <osg/Callback>
using namespace osg;
@@ -66,19 +67,19 @@ StateAttribute* CopyOp::operator() (const StateAttribute* attr) const
return const_cast<StateAttribute*>(attr);
}
NodeCallback* CopyOp::operator() (const NodeCallback* nc) const
Callback* CopyOp::operator() (const Callback* nc) const
{
if (nc && _flags&DEEP_COPY_CALLBACKS)
{
// deep copy the full chain of callback
osg::NodeCallback* first = osg::clone(nc, *this);
Callback* first = osg::clone(nc, *this);
if (!first) return 0;
first->setNestedCallback(0);
nc = nc->getNestedCallback();
while (nc)
{
osg::NodeCallback* ucb = osg::clone(nc, *this);
Callback* ucb = osg::clone(nc, *this);
if (ucb)
{
ucb->setNestedCallback(0);
@@ -89,5 +90,5 @@ NodeCallback* CopyOp::operator() (const NodeCallback* nc) const
return first;
}
else
return const_cast<NodeCallback*>(nc);
return const_cast<Callback*>(nc);
}

View File

@@ -216,6 +216,36 @@ void Drawable::flushDeletedDisplayLists(unsigned int contextID, double& availabl
#endif
}
bool Drawable::UpdateCallback::run(osg::Object* object, osg::Object* data)
{
osg::Drawable* drawable = dynamic_cast<osg::Drawable*>(object);
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data);
if (drawable && nv)
{
update(nv, drawable);
return true;
}
else
{
return traverse(object, data);
}
}
bool Drawable::EventCallback::run(osg::Object* object, osg::Object* data)
{
osg::Drawable* drawable = dynamic_cast<osg::Drawable*>(object);
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data);
if (drawable && nv)
{
event(nv, drawable);
return true;
}
else
{
return traverse(object, data);
}
}
Drawable::Drawable()
{
_boundingBoxComputed = false;
@@ -468,48 +498,6 @@ void Drawable::dirtyDisplayList()
}
void Drawable::setUpdateCallback(UpdateCallback* ac)
{
if (_drawableUpdateCallback==ac) return;
int delta = 0;
if (_drawableUpdateCallback.valid()) --delta;
if (ac) ++delta;
_drawableUpdateCallback = ac;
if (delta!=0 && !(_stateset.valid() && _stateset->requiresUpdateTraversal()))
{
for(ParentList::iterator itr=_parents.begin();
itr!=_parents.end();
++itr)
{
(*itr)->setNumChildrenRequiringUpdateTraversal((*itr)->getNumChildrenRequiringUpdateTraversal()+delta);
}
}
}
void Drawable::setEventCallback(EventCallback* ac)
{
if (_drawableEventCallback==ac) return;
int delta = 0;
if (_drawableEventCallback.valid()) --delta;
if (ac) ++delta;
_drawableEventCallback = ac;
if (delta!=0 && !(_stateset.valid() && _stateset->requiresEventTraversal()))
{
for(ParentList::iterator itr=_parents.begin();
itr!=_parents.end();
++itr)
{
(*itr)->setNumChildrenRequiringEventTraversal( (*itr)->getNumChildrenRequiringEventTraversal()+delta );
}
}
}
struct ComputeBound : public PrimitiveFunctor
{
ComputeBound()

View File

@@ -203,7 +203,7 @@ MatrixList Node::getWorldMatrices(const osg::Node* haltTraversalAtNode) const
return matrices;
}
void Node::setUpdateCallback(NodeCallback* nc)
void Node::setUpdateCallback(Callback* nc)
{
// if no changes just return.
if (_updateCallback==nc) return;
@@ -281,7 +281,7 @@ void Node::setNumChildrenRequiringUpdateTraversal(unsigned int num)
}
void Node::setEventCallback(NodeCallback* nc)
void Node::setEventCallback(Callback* nc)
{
// if no changes just return.
if (_eventCallback==nc) return;

View File

@@ -1,23 +0,0 @@
/* -*-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.
*/
#include <osg/Node>
#include <osg/NodeCallback>
#include <osg/NodeVisitor>
using namespace osg;
void NodeCallback::traverse(Node* node,NodeVisitor* nv)
{
if (_nestedCallback.valid()) (*_nestedCallback)(node,nv);
else nv->traverse(*node);
}

View File

@@ -16,13 +16,6 @@
using namespace osg;
bool CallbackObject::run(osg::Object* object, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
{
OSG_NOTICE<<"CallbackObject::run(object="<<object<<")"<<std::endl;
return false;
}
ScriptEngine* ScriptNodeCallback::getScriptEngine(osg::NodePath& nodePath)
{
if (!_script) return 0;

View File

@@ -12,6 +12,7 @@
#include <osg/StateAttribute>
#include <osg/StateSet>
#include <osg/State>
#include <osg/NodeVisitor>
#include <osg/Notify>
#include <algorithm>

View File

@@ -84,6 +84,26 @@ bool osg::isTextureMode(StateAttribute::GLMode mode)
return getTextureGLModeSet().isTextureMode(mode);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// StateAttributeCallback
//
bool StateSet::Callback::run(osg::Object* object, osg::Object* data)
{
osg::StateSet* ss = dynamic_cast<osg::StateSet*>(object);
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data);
if (ss && nv)
{
operator()(ss, nv);
return true;
}
else
{
return traverse(object, data);
}
}
StateSet::StateSet():
Object(true),
_nestRenderBins(true)