|
发表于 2009-10-22 23:26:03
|
显示全部楼层
本帖最后由 harolf 于 2009-10-22 23:50 编辑
我也曾遇到过类似的错误,但具体是怎样已经忘了,现在把早期的代码(可以正常运行)贴出来给您参考一下,希望有所帮助,注:我用的qt,里面还有一些代码牵扯到其它模块,不过可以很容易剔除,里面的itemDragger派生自osgManipulator::CompositeDragger。
class SimHandler : public QObject, public osgGA::GUIEventHandler
{
Q_OBJECT
protected:
osgManipulator:ragger* createDragger()
{
osgManipulator::Dragger* dragger = new ItemDragger();
dragger->setupDefaultGeometry();
return dragger;
}
osg::Node* addDraggerToScene(osgManipulator::Selection* selection)
{
osgManipulator::Dragger* dragger = createDragger();
osg::Group* root = new osg::Group;
root->addChild(dragger);
root->addChild(selection);
float scale = selection->getBound().radius() * 1.6;
dragger->setMatrix(osg::Matrix::scale(scale, scale, scale) *
osg::Matrix::translate(selection->getBound().center()));
SceneEngine::instance()->commandManager()->connect(*dragger, *selection);
return root;
}
public:
SimHandler() : m_mx(0), m_my(0), _activeDragger(0), m_draggerWrap(0), m_object(0)
{
}
~SimHandler()
{
}
//事件处理函数
bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
if (m_object)
return draggerHandle(ea, aa);
else
return pickHandle(ea, aa);
}
//拾取处理
bool pickHandle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
if (!view)
return false;
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter:USH):
{
if(ea.getButtonMask() == osgGA::GUIEventAdapter:EFT_MOUSE_BUTTON)
{
SceneEngine::instance()->pick(ea, aa);
m_object = SceneEngine::instance()->selected();
if (m_object)
{
emit selectObject(m_object);
if (!m_draggerWrap)
{
m_draggerWrap = addDraggerToScene(m_object);
m_parent = m_object->getParent(0);
m_parent->replaceChild(m_object, m_draggerWrap);
}
return true;
}
}
break;
}
}
return false;
} // end of bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
//拖拽处理
bool draggerHandle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
{
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
if (!view) return false;
bool result = false;
switch (ea.getEventType())
{
case osgGA::GUIEventAdapter::PUSH:
{
if(ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
{
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;
result = true;
break;
}
}
}
if (!result)
{
m_parent->replaceChild(m_draggerWrap, m_object);
m_object->setSelectedMark(false);
m_object = 0;
m_draggerWrap = 0;
SceneEngine::instance()->clearSelected();
result = true;
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());
emit transformObject(m_object);
_activeDragger->handle(_pointer, ea, aa);
result = true;
}
break;
}
default:
break;
}
if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE)
{
_activeDragger = 0;
_pointer.reset();
}
return result;
}
private:
osgManipulator::Dragger* _activeDragger;
osgManipulator::PointerInfo _pointer;
float m_mx;
float m_my;
SceneItem *m_object;
osg::Node *m_draggerWrap;
osgManipulator::PointerInfo *m_pointer;
osg::Group *m_parent;
signals:
//!当拾取到对象时发送
void selectObject(SceneItem *object);
//! 对象变换时发送
void transformObject(SceneItem *object);
};
#endif //SIMHANGLER_H |
|