mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-02-10 21:40:15 +00:00
factored out mouse picking into a separate function
This commit is contained in:
parent
3844c94975
commit
501ae6372d
@ -347,6 +347,53 @@ bool CSVRender::WorldspaceWidget::storeMappingSetting (const QString& key, const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
osg::ref_ptr<CSVRender::TagBase> CSVRender::WorldspaceWidget::mousePick (QMouseEvent *event)
|
||||||
|
{
|
||||||
|
// (0,0) is considered the lower left corner of an OpenGL window
|
||||||
|
int x = event->x();
|
||||||
|
int y = height() - event->y();
|
||||||
|
|
||||||
|
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector (new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, x, y));
|
||||||
|
|
||||||
|
intersector->setIntersectionLimit(osgUtil::LineSegmentIntersector::NO_LIMIT);
|
||||||
|
osgUtil::IntersectionVisitor visitor(intersector);
|
||||||
|
|
||||||
|
visitor.setTraversalMask(getInteractionMask() << 1);
|
||||||
|
|
||||||
|
mView->getCamera()->accept(visitor);
|
||||||
|
|
||||||
|
for (osgUtil::LineSegmentIntersector::Intersections::iterator it = intersector->getIntersections().begin();
|
||||||
|
it != intersector->getIntersections().end(); ++it)
|
||||||
|
{
|
||||||
|
osgUtil::LineSegmentIntersector::Intersection intersection = *it;
|
||||||
|
|
||||||
|
// reject back-facing polygons
|
||||||
|
osg::Vec3f normal = intersection.getWorldIntersectNormal();
|
||||||
|
normal = osg::Matrix::transform3x3(normal, mView->getCamera()->getViewMatrix());
|
||||||
|
if (normal.z() < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (std::vector<osg::Node*>::iterator it = intersection.nodePath.begin(); it != intersection.nodePath.end(); ++it)
|
||||||
|
{
|
||||||
|
osg::Node* node = *it;
|
||||||
|
if (osg::ref_ptr<CSVRender::TagBase> tag = dynamic_cast<CSVRender::TagBase *>(node->getUserData()))
|
||||||
|
{
|
||||||
|
if (!(tag->getElement() & mInteractionMask))
|
||||||
|
break; // not interested -> continue looking
|
||||||
|
|
||||||
|
return tag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ignoring terrain for now
|
||||||
|
// must be terrain, report coordinates
|
||||||
|
// std::cout << "Terrain hit at " << intersection.getWorldIntersectPoint().x() << " " << intersection.getWorldIntersectPoint().y() << std::endl;
|
||||||
|
// return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return osg::ref_ptr<CSVRender::TagBase>();
|
||||||
|
}
|
||||||
|
|
||||||
void CSVRender::WorldspaceWidget::dropEvent (QDropEvent* event)
|
void CSVRender::WorldspaceWidget::dropEvent (QDropEvent* event)
|
||||||
{
|
{
|
||||||
const CSMWorld::TableMimeData* mime = dynamic_cast<const CSMWorld::TableMimeData*> (event->mimeData());
|
const CSMWorld::TableMimeData* mime = dynamic_cast<const CSMWorld::TableMimeData*> (event->mimeData());
|
||||||
@ -436,54 +483,16 @@ void CSVRender::WorldspaceWidget::mousePressEvent (QMouseEvent *event)
|
|||||||
if (event->button() != Qt::RightButton)
|
if (event->button() != Qt::RightButton)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// (0,0) is considered the lower left corner of an OpenGL window
|
osg::ref_ptr<TagBase> tag = mousePick (event);
|
||||||
int x = event->x();
|
|
||||||
int y = height() - event->y();
|
|
||||||
|
|
||||||
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector (new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, x, y));
|
if (tag)
|
||||||
|
|
||||||
intersector->setIntersectionLimit(osgUtil::LineSegmentIntersector::NO_LIMIT);
|
|
||||||
osgUtil::IntersectionVisitor visitor(intersector);
|
|
||||||
|
|
||||||
visitor.setTraversalMask(getInteractionMask() << 1);
|
|
||||||
|
|
||||||
mView->getCamera()->accept(visitor);
|
|
||||||
|
|
||||||
for (osgUtil::LineSegmentIntersector::Intersections::iterator it = intersector->getIntersections().begin();
|
|
||||||
it != intersector->getIntersections().end(); ++it)
|
|
||||||
{
|
{
|
||||||
osgUtil::LineSegmentIntersector::Intersection intersection = *it;
|
if (CSVRender::ObjectTag *objectTag = dynamic_cast<CSVRender::ObjectTag *> (tag.get()))
|
||||||
|
|
||||||
// reject back-facing polygons
|
|
||||||
osg::Vec3f normal = intersection.getWorldIntersectNormal();
|
|
||||||
normal = osg::Matrix::transform3x3(normal, mView->getCamera()->getViewMatrix());
|
|
||||||
if (normal.z() < 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
for (std::vector<osg::Node*>::iterator it = intersection.nodePath.begin(); it != intersection.nodePath.end(); ++it)
|
|
||||||
{
|
{
|
||||||
osg::Node* node = *it;
|
// hit an Object, toggle its selection state
|
||||||
if (CSVRender::TagBase* tag = dynamic_cast<CSVRender::TagBase *>(node->getUserData()))
|
CSVRender::Object* object = objectTag->mObject;
|
||||||
{
|
object->setSelected (!object->getSelected());
|
||||||
if (!(tag->getElement() & mInteractionMask))
|
|
||||||
break; // not interested -> continue looking
|
|
||||||
|
|
||||||
// hit something marked with a tag
|
|
||||||
if (CSVRender::ObjectTag *objectTag = dynamic_cast<CSVRender::ObjectTag *> (tag))
|
|
||||||
{
|
|
||||||
// hit an Object, toggle its selection state
|
|
||||||
CSVRender::Object* object = objectTag->mObject;
|
|
||||||
object->setSelected (!object->getSelected());
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ignoring terrain for now
|
|
||||||
// must be terrain, report coordinates
|
|
||||||
// std::cout << "Terrain hit at " << intersection.getWorldIntersectPoint().x() << " " << intersection.getWorldIntersectPoint().y() << std::endl;
|
|
||||||
// return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,8 @@ namespace CSVWidget
|
|||||||
|
|
||||||
namespace CSVRender
|
namespace CSVRender
|
||||||
{
|
{
|
||||||
|
class TagBase;
|
||||||
|
|
||||||
class WorldspaceWidget : public SceneWidget
|
class WorldspaceWidget : public SceneWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@ -126,6 +128,8 @@ namespace CSVRender
|
|||||||
/// \return Is \a key a button mapping setting? (ignored otherwise)
|
/// \return Is \a key a button mapping setting? (ignored otherwise)
|
||||||
bool storeMappingSetting (const QString& key, const QString& value);
|
bool storeMappingSetting (const QString& key, const QString& value);
|
||||||
|
|
||||||
|
osg::ref_ptr<TagBase> mousePick (QMouseEvent *event);
|
||||||
|
|
||||||
virtual std::string getStartupInstruction() = 0;
|
virtual std::string getStartupInstruction() = 0;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user