Refactored osgManipulator so that CommandManager is no longer required, instead Dragger directly manages Constaints and associate Selections.
This commit is contained in:
@@ -38,6 +38,8 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// #define USE_COMMAND_MANAGER
|
||||
|
||||
osgManipulator::Dragger* createDragger(const std::string& name)
|
||||
{
|
||||
osgManipulator::Dragger* dragger = 0;
|
||||
@@ -90,43 +92,6 @@ osgManipulator::Dragger* createDragger(const std::string& name)
|
||||
}
|
||||
|
||||
|
||||
osg::Node* createHUD()
|
||||
{
|
||||
osg::Geode* geode = new osg::Geode();
|
||||
|
||||
std::string timesFont("fonts/arial.ttf");
|
||||
|
||||
osg::StateSet* stateset = geode->getOrCreateStateSet();
|
||||
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
||||
|
||||
osgText::Text* text = new osgText::Text;
|
||||
geode->addDrawable( text );
|
||||
|
||||
osg::Vec3 position(50.0f,50.0f,0.0f);
|
||||
text->setPosition(position);
|
||||
text->setText("Use the Tab key to switch between the trackball and pick modes.");
|
||||
text->setFont(timesFont);
|
||||
|
||||
osg::Camera* camera = new osg::Camera;
|
||||
|
||||
// set the projection matrix
|
||||
camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
|
||||
|
||||
// set the view matrix
|
||||
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
|
||||
camera->setViewMatrix(osg::Matrix::identity());
|
||||
|
||||
// only clear the depth buffer
|
||||
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// draw subgraph after main camera view.
|
||||
camera->setRenderOrder(osg::Camera::POST_RENDER);
|
||||
|
||||
camera->addChild(geode);
|
||||
|
||||
return camera;
|
||||
}
|
||||
|
||||
osg::Node* addDraggerToScene(osg::Node* scene, osgManipulator::CommandManager* cmdMgr, const std::string& name)
|
||||
{
|
||||
scene->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
|
||||
@@ -141,13 +106,16 @@ osg::Node* addDraggerToScene(osg::Node* scene, osgManipulator::CommandManager* c
|
||||
osg::Group* root = new osg::Group;
|
||||
root->addChild(dragger);
|
||||
root->addChild(selection);
|
||||
root->addChild(createHUD());
|
||||
|
||||
float scale = scene->getBound().radius() * 1.6;
|
||||
dragger->setMatrix(osg::Matrix::scale(scale, scale, scale) *
|
||||
osg::Matrix::translate(scene->getBound().center()));
|
||||
cmdMgr->connect(*dragger, *selection);
|
||||
|
||||
#ifdef USE_COMMAND_MANAGER
|
||||
cmdMgr->connect(*dragger, *selection);
|
||||
#else
|
||||
dragger->addSelection(selection);
|
||||
#endif
|
||||
return root;
|
||||
}
|
||||
|
||||
@@ -247,104 +215,7 @@ osg::Node* createDemoScene(osgManipulator::CommandManager* cmdMgr) {
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
class PickModeHandler : public osgGA::GUIEventHandler
|
||||
{
|
||||
public:
|
||||
enum Modes
|
||||
{
|
||||
VIEW = 0,
|
||||
PICK
|
||||
};
|
||||
|
||||
PickModeHandler():
|
||||
_mode(VIEW),
|
||||
_activeDragger(0)
|
||||
{
|
||||
}
|
||||
|
||||
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa,
|
||||
osg::Object*, osg::NodeVisitor*)
|
||||
{
|
||||
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
|
||||
if (!view) return false;
|
||||
|
||||
if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Tab &&
|
||||
ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN &&
|
||||
_activeDragger == 0)
|
||||
{
|
||||
_mode = ! _mode;
|
||||
}
|
||||
|
||||
if (VIEW == _mode) return false;
|
||||
|
||||
switch (ea.getEventType())
|
||||
{
|
||||
case osgGA::GUIEventAdapter::PUSH:
|
||||
{
|
||||
osgUtil::LineSegmentIntersector::Intersections intersections;
|
||||
|
||||
_pointer.reset();
|
||||
|
||||
if (view->computeIntersections(ea.getX(),ea.getY(),intersections))
|
||||
{
|
||||
_pointer.setCamera(view->getCamera());
|
||||
_pointer.setMousePosition(ea.getX(), ea.getY());
|
||||
|
||||
for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
|
||||
hitr != intersections.end();
|
||||
++hitr)
|
||||
{
|
||||
_pointer.addIntersection(hitr->nodePath, hitr->getLocalIntersectPoint());
|
||||
}
|
||||
for (osg::NodePath::iterator itr = _pointer._hitList.front().first.begin();
|
||||
itr != _pointer._hitList.front().first.end();
|
||||
++itr)
|
||||
{
|
||||
osgManipulator::Dragger* dragger = dynamic_cast<osgManipulator::Dragger*>(*itr);
|
||||
if (dragger)
|
||||
{
|
||||
|
||||
dragger->handle(_pointer, ea, aa);
|
||||
_activeDragger = dragger;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case osgGA::GUIEventAdapter::DRAG:
|
||||
case osgGA::GUIEventAdapter::RELEASE:
|
||||
{
|
||||
if (_activeDragger)
|
||||
{
|
||||
_pointer._hitIter = _pointer._hitList.begin();
|
||||
_pointer.setCamera(view->getCamera());
|
||||
_pointer.setMousePosition(ea.getX(), ea.getY());
|
||||
|
||||
_activeDragger->handle(_pointer, ea, aa);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE)
|
||||
{
|
||||
_activeDragger = 0;
|
||||
_pointer.reset();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned int _mode;
|
||||
osgManipulator::Dragger* _activeDragger;
|
||||
osgManipulator::PointerInfo _pointer;
|
||||
};
|
||||
|
||||
//
|
||||
int main( int argc, char **argv )
|
||||
{
|
||||
|
||||
@@ -397,7 +268,12 @@ int main( int argc, char **argv )
|
||||
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
|
||||
|
||||
// create a command manager
|
||||
osg::ref_ptr<osgManipulator::CommandManager> cmdMgr = new osgManipulator::CommandManager;
|
||||
osg::ref_ptr<osgManipulator::CommandManager> cmdMgr;
|
||||
|
||||
|
||||
#ifdef USE_COMMAND_MANAGER
|
||||
cmdMgr = new osgManipulator::CommandManager;
|
||||
#endif
|
||||
|
||||
// if no model has been successfully loaded report failure.
|
||||
bool tragger2Scene(true);
|
||||
@@ -434,7 +310,7 @@ int main( int argc, char **argv )
|
||||
} else {
|
||||
viewer.setSceneData(loadedModel.get());
|
||||
}
|
||||
// viewer.addEventHandler(new PickModeHandler());
|
||||
|
||||
|
||||
return viewer.run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user