From f767ab0cc967395aa4fe9573f2fbad4a9b762bae Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 7 Feb 2018 17:23:14 +0000 Subject: [PATCH] Refactored the event handler so that it contains a helper NodeVistor class rather than inherits from inappropriately using multiple inheritance. --- examples/osgsidebyside/osgsidebyside.cpp | 100 +++++++++++++---------- 1 file changed, 55 insertions(+), 45 deletions(-) diff --git a/examples/osgsidebyside/osgsidebyside.cpp b/examples/osgsidebyside/osgsidebyside.cpp index c4acad979..3117f6471 100644 --- a/examples/osgsidebyside/osgsidebyside.cpp +++ b/examples/osgsidebyside/osgsidebyside.cpp @@ -41,48 +41,15 @@ using namespace osg; using namespace osgGA; -class SwitchDOFVisitor : public osg::NodeVisitor, public osgGA::GUIEventHandler +class SwitchDOFHandler : public osgGA::GUIEventHandler { public: - SwitchDOFVisitor(): - osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) + + + SwitchDOFHandler() { } - SwitchDOFVisitor(const SwitchDOFVisitor& sdfv, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): - osg::Object(sdfv, copyop), - osg::Callback(sdfv, copyop), - osg::NodeVisitor(sdfv, copyop), - osgGA::GUIEventHandler(sdfv, copyop) - {} - - META_Object(osg, SwitchDOFVisitor) - - virtual void apply(Group& node) - { - osgSim::MultiSwitch* pMSwitch = dynamic_cast(&node); - - if (pMSwitch) - { - mSwitches.push_back(pMSwitch); - } - - osg::NodeVisitor::apply(node); - } - - virtual void apply(Transform& node) - { - osgSim::DOFTransform* pDof = dynamic_cast(&node); - - if (pDof) - { - mDofs.push_back(pDof); - pDof->setAnimationOn(true); - } - - osg::NodeVisitor::apply(node); - } - void nextSwitch() { for (size_t i=0; i < mSwitches.size(); i++) @@ -156,9 +123,53 @@ public: return false; } + void collectNodesOfInterest(osg::Node* node) + { + CollectNodes cn(this); + node->accept(cn); + } + private: - std::vector mSwitches; - std::vector mDofs; + + friend class CollectNodes; + + class CollectNodes : public osg::NodeVisitor + { + public: + SwitchDOFHandler* _parent; + + CollectNodes(SwitchDOFHandler* parent): + osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), + _parent(parent) + { + } + + virtual void apply(Group& node) + { + osgSim::MultiSwitch* pMSwitch = dynamic_cast(&node); + if (pMSwitch) + { + _parent->mSwitches.push_back(pMSwitch); + } + + traverse(node); + } + + virtual void apply(Transform& node) + { + osgSim::DOFTransform* pDof = dynamic_cast(&node); + if (pDof) + { + _parent->mDofs.push_back(pDof); + pDof->setAnimationOn(true); + } + + traverse(node); + } + }; + + std::vector< osg::ref_ptr > mSwitches; + std::vector< osg::ref_ptr > mDofs; }; void singleWindowSideBySideCameras(osgViewer::Viewer& viewer) @@ -267,8 +278,8 @@ int main( int argc, char **argv ) viewer.addEventHandler(new osgViewer::LODScaleHandler()); viewer.addEventHandler(new osgGA::StateSetManipulator()); - SwitchDOFVisitor* visit = new SwitchDOFVisitor; - viewer.addEventHandler(visit); + SwitchDOFHandler* switchOFHandler = new SwitchDOFHandler; + viewer.addEventHandler(switchOFHandler); // load the scene. osg::ref_ptr loadedModel = osgDB::readRefNodeFiles(arguments); @@ -294,13 +305,12 @@ int main( int argc, char **argv ) group2->addChild(convertedModel.get()); group2->setNodeMask(2); - // Activate DOF animations and collect switches - loadedModel->accept(*visit); - convertedModel->accept(*visit); - group->addChild(group1); group->addChild(group2); + // Activate DOF animations and collect switches + switchOFHandler->collectNodesOfInterest(group); + viewer.setSceneData(group); viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);