From aee5267677c4876613441e8ec7959c9d85f223aa Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 8 Dec 2021 13:53:53 +0000 Subject: [PATCH] Added 's' write selected nodes to "saved_selected.osgt feature. --- .../osgkeyboardmouse/osgkeyboardmouse.cpp | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/examples/osgkeyboardmouse/osgkeyboardmouse.cpp b/examples/osgkeyboardmouse/osgkeyboardmouse.cpp index 49e28aee7..7b6607a50 100644 --- a/examples/osgkeyboardmouse/osgkeyboardmouse.cpp +++ b/examples/osgkeyboardmouse/osgkeyboardmouse.cpp @@ -71,11 +71,11 @@ public: bool _addToModel; }; -class DeleteSelectedNodesVisitor : public osg::NodeVisitor +class SelectedNodesVisitor : public osg::NodeVisitor { public: - DeleteSelectedNodesVisitor(): + SelectedNodesVisitor(): osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) { } @@ -110,6 +110,23 @@ public: } } + osg::ref_ptr createSelectedNodeSubgraph() + { + if (_selectedNodes.empty()) return osg::ref_ptr(); + + if (_selectedNodes.size()==1) return _selectedNodes[0]; + + // note code doesn't yet handle selected nodes being nested within transforms. + osg::ref_ptr group; + for(SelectedNodes::iterator itr = _selectedNodes.begin(); + itr != _selectedNodes.end(); + ++itr) + { + group->addChild(*itr); + } + return group; + } + typedef std::vector< osg::ref_ptr > SelectedNodes; SelectedNodes _selectedNodes; @@ -150,6 +167,18 @@ public: osg::notify(osg::NOTICE)<<"Saved model to file 'saved_model.osgt'"<getSceneData()), "saved_model.osgt"); } + else if (ea.getKey()=='s') + { + SelectedNodesVisitor snv; + viewer->getSceneData()->accept(snv); + osg::ref_ptr selected = snv.createSelectedNodeSubgraph(); + + if (selected) + { + osg::notify(osg::NOTICE)<<"Saved selected to file 'saved_selected.osgt'"<getSceneData()), "saved_selected.osgt"); + } + } else if (ea.getKey()=='p') { _usePolytopeIntersector = !_usePolytopeIntersector; @@ -177,7 +206,7 @@ public: else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Delete || ea.getKey()==osgGA::GUIEventAdapter::KEY_BackSpace) { osg::notify(osg::NOTICE)<<"Delete"<getSceneData()->accept(dsnv); dsnv.pruneSelectedNodes(); }